~ubuntu-branches/ubuntu/utopic/gridengine/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<%
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/
%>
   String getKeyAttributeValueFromString(final PrintWriter err, final String type, final String fileName, final String inputText) {
      String keyAttrValue = null;
      //Get the key attribute value from the file
      String keyAttr = EditorUtil.unifyAttrWithClientNames(type, "name");
      StringReader sr = new StringReader(inputText);
      LineNumberReader lnr = new LineNumberReader(sr);
      String keyAttrLine = null;
      try {
         while (lnr.ready()) {
            keyAttrLine = lnr.readLine().trim();
            if (keyAttrLine.startsWith(keyAttr)) {
               keyAttrValue = keyAttrLine.substring(keyAttr.length()).trim();
               break;
            }
         }
         //Exit if the key attribute is missing
         if (keyAttrValue == null) {
            err.println("error: required attribute \"" + keyAttr + "\" is missing");
            err.println(type + " file \"" + fileName + "\" is not correct");
            err.flush();
            //TODO LP: Set correct exit code
         }
      } catch (IOException ex) {
         err.println(ex.getMessage());
         err.flush();
         setExitCode(1);
      }
      return keyAttrValue;
   }

    String readFile(final OptionInfo oi) throws JGDIException {
        String msg = "Error messages file not found!";
        int exitCode = 0;
        String option = oi.getOd().getOption();
        if (oi.getArgs().size() == 0) {
            msg = getErrorMessage("NoArgument", option);
            //err.println(msg);
            setExitCode(getCustomExitCode("NoArgument", option));
            throw new JGDIException(msg, exitCode);
        }
        File f = new File(oi.getFirstArg());
        long fileSize = f.length();
        FileReader fr;
        StringBuilder sb = new StringBuilder();
        try {
            fr = new FileReader(f);
            char[] buff = new char[2048];
            int r;
            while (fr.ready()) {
                r = fr.read(buff);
                if (r > 0) {
                    sb.append(buff, 0, r);
                }
            }
        } catch (IOException ex) {
            msg = getErrorMessage("InvalidFile", option);
            exitCode = getCustomExitCode("InvalidFile", option);
            throw new JGDIException(msg, exitCode);
        }
        //Check we have whole content
        if (sb.length() != fileSize) {
            throw new JGDIException("Unable to read whole file content. Filesize is " + fileSize + " got only " + sb.length());
        }
        return sb.toString();
    }

   private String runJavaEditor(final String text) {
      TextEditor ted = new TextEditor(text);
      while (!ted.isDone()) {
         try {
            Thread.currentThread().sleep(1000);
         } catch (InterruptedException ex) {
            ex.printStackTrace();
         }
      }
      return ted.getText();
   }

   String runEditor(final String text) {
      String editor;
      String version = System.getProperty("java.specification.version");
      //TODO LP <1.5 doesn't list properties like EDITOR...
      if (Double.parseDouble(version) < 1.5) {
         editor = System.getProperty("EDITOR");
      } else {
         editor = System.getenv("EDITOR");
      }
      if (editor == null) {
         return runJavaEditor(text);
      }
      StringBuilder sb = new StringBuilder();
      try {
         File f = File.createTempFile("edit", null, new File("/tmp"));
         FileWriter fw = new FileWriter(f);
         fw.write(text);
         fw.flush();
         fw.close();

         int exitCode = EditorUtil.sgeEdit(f);
         if (exitCode != 0) {
            return null;
         }
         char[] buff = new char[1024];
         FileReader fr = new FileReader(f);
         int readChars = 0;
         while (fr.ready() && readChars != -1) {
            readChars = fr.read(buff);
            if (readChars > 0) {
               sb.append(buff, 0, readChars);
            }
         }
      } catch (IOException ioe) {
         ioe.printStackTrace();
/*
      } catch (InterruptedException ie) {
         ie.printStackTrace();
*/
      }
      return sb.toString();
   }