1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.glxdesktop;
18
19 import java.io.BufferedReader;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.nio.ByteBuffer;
26 import java.nio.CharBuffer;
27 import java.nio.channels.FileChannel;
28 import java.nio.charset.Charset;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 public class Utils {
34
35 private static final Log LOG = LogFactory.getLog(Utils.class);
36
37 public static class RunType {
38 public static final int ONLY_ERROR = 0;
39
40 public static final int ONLY_INTPUT = 1;
41
42 public static final int ALL = 2;
43 }
44
45 /***
46 * Run a system command
47 * @param cmd to execute.
48 * @param runType see RunType class members.
49 * @return the command input.
50 */
51 public static String runCmd(String cmd, int runType) {
52 int c;
53 LOG.debug(Messages.getString("Utils.run_command") + cmd);
54 StringBuffer stringBuffer = new StringBuffer();
55 try {
56
57 Process child = Runtime.getRuntime().exec(cmd);
58
59 System.out.println(cmd);
60 InputStream err = child.getErrorStream();
61 System.out.println(cmd);
62 if (runType == RunType.ONLY_ERROR) {
63 while ((c = err.read()) != -1)
64 stringBuffer.append((char) c);
65 System.out.print(c);
66 err.close();
67 }
68 if (runType == RunType.ONLY_INTPUT || runType == RunType.ALL) {
69
70 InputStream in = child.getInputStream();
71 while ((c = in.read()) != -1)
72 stringBuffer.append((char) c);
73 in.close();
74 }
75 } catch (IOException e) {
76 if (!LOG.isDebugEnabled()) {
77 LOG.warn("TODO ADD WARN MSG");
78 } else {
79 LOG.warn(e.getMessage(), e);
80 }
81 }
82
83 if (stringBuffer.toString().length() > 0) {
84 return stringBuffer.toString();
85 } else {
86 return new String("");
87 }
88
89 }
90
91 /***
92 * Return a string from a file, if the file
93 * is not found this method find the file in the current directory.
94 * @param fileName the path to the file
95 * @return a string
96 */
97 public static String fileToString(String fileName, String charset) {
98 FileInputStream fileInputStream;
99 try {
100 fileInputStream = new FileInputStream(fileName);
101 return fileToString(fileInputStream, charset);
102
103 } catch (FileNotFoundException e) {
104 fileInputStream = (FileInputStream) ClassLoader.getSystemResourceAsStream(fileName);
105 return fileToString(fileInputStream, charset);
106 }
107 }
108
109 public static InputStream fileToInputStream(String fileName) throws UtilsException {
110 try {
111 return new FileInputStream(fileName);
112 } catch (FileNotFoundException e) {
113 InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(fileName);
114 if (inputStream == null) {
115 throw new UtilsException("Cannot open Stream", e );
116 }
117 return inputStream;
118 }
119 }
120
121 public static String inputStreamToString(InputStream inputStream) throws UtilsException {
122 String lines = "", line ;
123 InputStreamReader inputStreamReader = new InputStreamReader(
124 inputStream);
125 BufferedReader inputReader = new BufferedReader(
126 inputStreamReader);
127 try {
128 while ((line = inputReader.readLine()) != null) {
129 lines += line + "\n";
130 }
131 } catch (IOException e) {
132 throw new UtilsException("Error when reading the Stream", e);
133 }
134 return lines;
135 }
136
137 /***
138 * Return a string from a file, if the file
139 * is not found this method find the file in the current directory.
140 * @param fileName the path to the file
141 * @return a string
142 * @throws IOException
143 * @throws UtilsException
144 */
145 public static String fileToString(String fileName) throws UtilsException {
146 return inputStreamToString(fileToInputStream(fileName));
147 }
148
149 public static String fileToString(FileInputStream inputStream,
150 String charset) {
151 FileChannel fc = inputStream.getChannel();
152 ByteBuffer bbuf;
153 try {
154 bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
155
156 fc.close();
157 CharBuffer cbuf = Charset.forName(charset)
158 .decode(bbuf);
159 return cbuf.toString();
160 } catch (IOException e) {
161 if (!LOG.isDebugEnabled()) {
162 LOG.error(e.getMessage());
163 } else {
164 LOG.error(e.getMessage(), e);
165 }
166 }
167 return new String();
168 }
169
170 /***
171 * This method retrun true, if the given process is running.
172 * @param processName to check
173 * @return the process status
174 */
175 public static boolean getProcessRunningStatus(String processName) {
176 try {
177 if (Integer
178 .valueOf(
179 Utils
180 .runCmd(
181 "pgrep " + processName, RunType.ONLY_INTPUT).trim())
182 .intValue() > 0) {
183 return true;
184 } else {
185 return false;
186 }
187 } catch (NumberFormatException exception) {
188 return false;
189 }
190 }
191
192
193
194
195 }