1 /* |
|
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.servicetag; |
|
27 |
|
28 // This class is a copy of the com.sun.scn.servicetags.SystemEnvironment |
|
29 // class from the Sun Connection source. |
|
30 // |
|
31 // The Service Tags team maintains the latest version of the implementation |
|
32 // for system environment data collection. JDK will include a copy of |
|
33 // the most recent released version for a JDK release. We rename |
|
34 // the package to com.sun.servicetag so that the Sun Connection |
|
35 // product always uses the latest version from the com.sun.scn.servicetags |
|
36 // package. JDK and users of the com.sun.servicetag API |
|
37 // (e.g. NetBeans and SunStudio) will use the version in JDK. |
|
38 |
|
39 import java.io.*; |
|
40 import java.net.InetAddress; |
|
41 import java.net.UnknownHostException; |
|
42 |
|
43 /** |
|
44 * SystemEnvironment class collects the environment data with the |
|
45 * best effort from the underlying platform. |
|
46 */ |
|
47 public class SystemEnvironment { |
|
48 private String hostname; |
|
49 private String hostId; |
|
50 private String osName; |
|
51 private String osVersion; |
|
52 private String osArchitecture; |
|
53 private String systemModel; |
|
54 private String systemManufacturer; |
|
55 private String cpuManufacturer; |
|
56 private String serialNumber; |
|
57 private static SystemEnvironment sysEnv = null; |
|
58 |
|
59 public static synchronized SystemEnvironment getSystemEnvironment() { |
|
60 if (sysEnv == null) { |
|
61 String os = System.getProperty("os.name"); |
|
62 if (os.equals("SunOS")) { |
|
63 sysEnv = new SolarisSystemEnvironment(); |
|
64 } else if (os.equals("Linux")) { |
|
65 sysEnv = new LinuxSystemEnvironment(); |
|
66 } else if (os.startsWith("Windows")) { |
|
67 sysEnv = new WindowsSystemEnvironment(); |
|
68 } else { |
|
69 sysEnv = new SystemEnvironment(); |
|
70 } |
|
71 } |
|
72 return sysEnv; |
|
73 } |
|
74 |
|
75 // package-private |
|
76 SystemEnvironment() { |
|
77 try { |
|
78 this.hostname = InetAddress.getLocalHost().getHostName(); |
|
79 } catch (UnknownHostException ex) { |
|
80 this.hostname = "Unknown host"; |
|
81 } |
|
82 this.hostId = ""; |
|
83 this.osName = System.getProperty("os.name"); |
|
84 this.osVersion = System.getProperty("os.version"); |
|
85 this.osArchitecture = System.getProperty("os.arch"); |
|
86 this.systemModel = ""; |
|
87 this.systemManufacturer = ""; |
|
88 this.cpuManufacturer = ""; |
|
89 this.serialNumber = ""; |
|
90 } |
|
91 |
|
92 |
|
93 /** |
|
94 * Sets the hostname. |
|
95 * @param hostname The hostname to set. |
|
96 */ |
|
97 public void setHostname(String hostname) { |
|
98 this.hostname = hostname; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Sets the OS name. |
|
103 * @param osName The osName to set. |
|
104 */ |
|
105 public void setOsName(String osName) { |
|
106 this.osName = osName; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Sets the OS version. |
|
111 * @param osVersion The osVersion to set. |
|
112 */ |
|
113 public void setOsVersion(String osVersion) { |
|
114 this.osVersion = osVersion; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Sets the OS architecture. |
|
119 * @param osArchitecture The osArchitecture to set. |
|
120 */ |
|
121 public void setOsArchitecture(String osArchitecture) { |
|
122 this.osArchitecture = osArchitecture; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Sets the system model. |
|
127 * @param systemModel The systemModel to set. |
|
128 */ |
|
129 public void setSystemModel(String systemModel) { |
|
130 this.systemModel = systemModel; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Sets the system manufacturer. |
|
135 * @param systemManufacturer The systemManufacturer to set. |
|
136 */ |
|
137 public void setSystemManufacturer(String systemManufacturer) { |
|
138 this.systemManufacturer = systemManufacturer; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Sets the cpu manufacturer. |
|
143 * @param cpuManufacturer The cpuManufacturer to set. |
|
144 */ |
|
145 public void setCpuManufacturer(String cpuManufacturer) { |
|
146 this.cpuManufacturer = cpuManufacturer; |
|
147 } |
|
148 |
|
149 /** |
|
150 * Sets the serial number. |
|
151 * @param serialNumber The serialNumber to set. |
|
152 */ |
|
153 public void setSerialNumber(String serialNumber) { |
|
154 this.serialNumber = serialNumber; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Sets the hostid. Truncates to a max length of 16 chars. |
|
159 * @param hostId The hostid to set. |
|
160 */ |
|
161 public void setHostId(String hostId) { |
|
162 if (hostId == null || hostId.equals("null")) { |
|
163 hostId = ""; |
|
164 } |
|
165 if (hostId.length() > 16) { |
|
166 hostId = hostId.substring(0,16); |
|
167 } |
|
168 this.hostId = hostId; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Returns the hostname. |
|
173 * @return The hostname. |
|
174 */ |
|
175 public String getHostname() { |
|
176 return hostname; |
|
177 } |
|
178 |
|
179 /** |
|
180 * Returns the osName. |
|
181 * @return The osName. |
|
182 */ |
|
183 public String getOsName() { |
|
184 return osName; |
|
185 } |
|
186 |
|
187 /** |
|
188 * Returns the osVersion. |
|
189 * @return The osVersion. |
|
190 */ |
|
191 public String getOsVersion() { |
|
192 return osVersion; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Returns the osArchitecture. |
|
197 * @return The osArchitecture. |
|
198 */ |
|
199 public String getOsArchitecture() { |
|
200 return osArchitecture; |
|
201 } |
|
202 |
|
203 /** |
|
204 * Returns the systemModel. |
|
205 * @return The systemModel. |
|
206 */ |
|
207 public String getSystemModel() { |
|
208 return systemModel; |
|
209 } |
|
210 |
|
211 /** |
|
212 * Returns the systemManufacturer. |
|
213 * @return The systemManufacturer. |
|
214 */ |
|
215 public String getSystemManufacturer() { |
|
216 return systemManufacturer; |
|
217 } |
|
218 |
|
219 /** |
|
220 * Returns the serialNumber. |
|
221 * @return The serialNumber. |
|
222 */ |
|
223 public String getSerialNumber() { |
|
224 return serialNumber; |
|
225 } |
|
226 |
|
227 /** |
|
228 * Returns the hostId. |
|
229 * @return The hostId. |
|
230 */ |
|
231 public String getHostId() { |
|
232 return hostId; |
|
233 } |
|
234 |
|
235 /** |
|
236 * Returns the cpuManufacturer. |
|
237 * @return The cpuManufacturer. |
|
238 */ |
|
239 public String getCpuManufacturer() { |
|
240 return cpuManufacturer; |
|
241 } |
|
242 |
|
243 protected String getCommandOutput(String... command) { |
|
244 StringBuilder sb = new StringBuilder(); |
|
245 BufferedReader br = null; |
|
246 Process p = null; |
|
247 try { |
|
248 ProcessBuilder pb = new ProcessBuilder(command); |
|
249 p = pb.start(); |
|
250 p.waitFor(); |
|
251 |
|
252 if (p.exitValue() == 0) { |
|
253 br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
|
254 String line = null; |
|
255 while ((line = br.readLine()) != null) { |
|
256 line = line.trim(); |
|
257 if (line.length() > 0) { |
|
258 if (sb.length() > 0) { |
|
259 sb.append("\n"); |
|
260 } |
|
261 sb.append(line); |
|
262 } |
|
263 } |
|
264 } |
|
265 return sb.toString(); |
|
266 } catch (InterruptedException ie) { |
|
267 // in case the command hangs |
|
268 if (p != null) { |
|
269 p.destroy(); |
|
270 } |
|
271 return ""; |
|
272 } catch (Exception e) { |
|
273 // ignore exception |
|
274 return ""; |
|
275 } finally { |
|
276 if (p != null) { |
|
277 try { |
|
278 p.getErrorStream().close(); |
|
279 } catch (IOException e) { |
|
280 // ignore |
|
281 } |
|
282 try { |
|
283 p.getInputStream().close(); |
|
284 } catch (IOException e) { |
|
285 // ignore |
|
286 } |
|
287 try { |
|
288 p.getOutputStream().close(); |
|
289 } catch (IOException e) { |
|
290 // ignore |
|
291 } |
|
292 p = null; |
|
293 } |
|
294 if (br != null) { |
|
295 try { |
|
296 br.close(); |
|
297 } catch (IOException e) { |
|
298 // ignore |
|
299 } |
|
300 } |
|
301 } |
|
302 } |
|
303 |
|
304 protected String getFileContent(String filename) { |
|
305 File f = new File(filename); |
|
306 if (!f.exists()) { |
|
307 return ""; |
|
308 } |
|
309 |
|
310 StringBuilder sb = new StringBuilder(); |
|
311 BufferedReader br = null; |
|
312 try { |
|
313 br = new BufferedReader(new FileReader(f)); |
|
314 String line = null; |
|
315 while ((line = br.readLine()) != null) { |
|
316 line = line.trim(); |
|
317 if (line.length() > 0) { |
|
318 if (sb.length() > 0) { |
|
319 sb.append("\n"); |
|
320 } |
|
321 sb.append(line); |
|
322 } |
|
323 } |
|
324 return sb.toString(); |
|
325 } catch (Exception e) { |
|
326 // ignore exception |
|
327 return ""; |
|
328 } finally { |
|
329 if (br != null) { |
|
330 try { |
|
331 br.close(); |
|
332 } catch (IOException e) { |
|
333 // ignore |
|
334 } |
|
335 } |
|
336 } |
|
337 } |
|
338 } |
|