001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.servicetag;
043:
044: // This class is a copy of the com.sun.scn.servicetags.SystemEnvironment
045: // class from the Sun Connection source.
046: //
047: // The Service Tags team maintains the latest version of the implementation
048: // for system environment data collection. JDK will include a copy of
049: // the most recent released version for a JDK release. We rename
050: // the package to com.sun.servicetag so that the Sun Connection
051: // product always uses the latest version from the com.sun.scn.servicetags
052: // package. JDK and users of the com.sun.servicetag API
053: // (e.g. NetBeans and SunStudio) will use the version in JDK.
054:
055: import java.io.*;
056: import java.net.InetAddress;
057: import java.net.UnknownHostException;
058:
059: /**
060: * SystemEnvironment class collects the environment data with the
061: * best effort from the underlying platform.
062: */
063: public class SystemEnvironment {
064: private String hostname;
065: private String hostId;
066: private String osName;
067: private String osVersion;
068: private String osArchitecture;
069: private String systemModel;
070: private String systemManufacturer;
071: private String cpuManufacturer;
072: private String serialNumber;
073: private static SystemEnvironment sysEnv = null;
074:
075: public static synchronized SystemEnvironment getSystemEnvironment() {
076: if (sysEnv == null) {
077: String os = System.getProperty("os.name");
078: if (os.equals("SunOS")) {
079: sysEnv = new SolarisSystemEnvironment();
080: } else if (os.equals("Linux")) {
081: sysEnv = new LinuxSystemEnvironment();
082: } else if (os.startsWith("Windows")) {
083: sysEnv = new WindowsSystemEnvironment();
084: } else {
085: sysEnv = new SystemEnvironment();
086: }
087: }
088: return sysEnv;
089: }
090:
091: // package-private
092: SystemEnvironment() {
093: try {
094: this .hostname = InetAddress.getLocalHost().getHostName();
095: } catch (UnknownHostException ex) {
096: this .hostname = "Unknown host";
097: }
098: this .hostId = "";
099: this .osName = System.getProperty("os.name");
100: this .osVersion = System.getProperty("os.version");
101: this .osArchitecture = System.getProperty("os.arch");
102: this .systemModel = "";
103: this .systemManufacturer = "";
104: this .cpuManufacturer = "";
105: this .serialNumber = "";
106: }
107:
108: /**
109: * Sets the hostname.
110: * @param hostname The hostname to set.
111: */
112: public void setHostname(String hostname) {
113: this .hostname = hostname;
114: }
115:
116: /**
117: * Sets the OS name.
118: * @param osName The osName to set.
119: */
120: public void setOsName(String osName) {
121: this .osName = osName;
122: }
123:
124: /**
125: * Sets the OS version.
126: * @param osVersion The osVersion to set.
127: */
128: public void setOsVersion(String osVersion) {
129: this .osVersion = osVersion;
130: }
131:
132: /**
133: * Sets the OS architecture.
134: * @param osArchitecture The osArchitecture to set.
135: */
136: public void setOsArchitecture(String osArchitecture) {
137: this .osArchitecture = osArchitecture;
138: }
139:
140: /**
141: * Sets the system model.
142: * @param systemModel The systemModel to set.
143: */
144: public void setSystemModel(String systemModel) {
145: this .systemModel = systemModel;
146: }
147:
148: /**
149: * Sets the system manufacturer.
150: * @param systemManufacturer The systemManufacturer to set.
151: */
152: public void setSystemManufacturer(String systemManufacturer) {
153: this .systemManufacturer = systemManufacturer;
154: }
155:
156: /**
157: * Sets the cpu manufacturer.
158: * @param cpuManufacturer The cpuManufacturer to set.
159: */
160: public void setCpuManufacturer(String cpuManufacturer) {
161: this .cpuManufacturer = cpuManufacturer;
162: }
163:
164: /**
165: * Sets the serial number.
166: * @param serialNumber The serialNumber to set.
167: */
168: public void setSerialNumber(String serialNumber) {
169: this .serialNumber = serialNumber;
170: }
171:
172: /**
173: * Sets the hostid. Truncates to a max length of 16 chars.
174: * @param hostId The hostid to set.
175: */
176: public void setHostId(String hostId) {
177: if (hostId == null || hostId.equals("null")) {
178: hostId = "";
179: }
180: if (hostId.length() > 16) {
181: hostId = hostId.substring(0, 16);
182: }
183: this .hostId = hostId;
184: }
185:
186: /**
187: * Returns the hostname.
188: * @return The hostname.
189: */
190: public String getHostname() {
191: return hostname;
192: }
193:
194: /**
195: * Returns the osName.
196: * @return The osName.
197: */
198: public String getOsName() {
199: return osName;
200: }
201:
202: /**
203: * Returns the osVersion.
204: * @return The osVersion.
205: */
206: public String getOsVersion() {
207: return osVersion;
208: }
209:
210: /**
211: * Returns the osArchitecture.
212: * @return The osArchitecture.
213: */
214: public String getOsArchitecture() {
215: return osArchitecture;
216: }
217:
218: /**
219: * Returns the systemModel.
220: * @return The systemModel.
221: */
222: public String getSystemModel() {
223: return systemModel;
224: }
225:
226: /**
227: * Returns the systemManufacturer.
228: * @return The systemManufacturer.
229: */
230: public String getSystemManufacturer() {
231: return systemManufacturer;
232: }
233:
234: /**
235: * Returns the serialNumber.
236: * @return The serialNumber.
237: */
238: public String getSerialNumber() {
239: return serialNumber;
240: }
241:
242: /**
243: * Returns the hostId.
244: * @return The hostId.
245: */
246: public String getHostId() {
247: return hostId;
248: }
249:
250: /**
251: * Returns the cpuManufacturer.
252: * @return The cpuManufacturer.
253: */
254: public String getCpuManufacturer() {
255: return cpuManufacturer;
256: }
257:
258: protected String getCommandOutput(String... command) {
259: StringBuilder sb = new StringBuilder();
260: BufferedReader br = null;
261: Process p = null;
262: try {
263: ProcessBuilder pb = new ProcessBuilder(command);
264: p = pb.start();
265: p.waitFor();
266:
267: if (p.exitValue() == 0) {
268: br = new BufferedReader(new InputStreamReader(p
269: .getInputStream()));
270: String line = null;
271: while ((line = br.readLine()) != null) {
272: line = line.trim();
273: if (line.length() > 0) {
274: if (sb.length() > 0) {
275: sb.append("\n");
276: }
277: sb.append(line);
278: }
279: }
280: }
281: return sb.toString();
282: } catch (InterruptedException ie) {
283: // in case the command hangs
284: if (p != null) {
285: p.destroy();
286: }
287: return "";
288: } catch (Exception e) {
289: // ignore exception
290: return "";
291: } finally {
292: if (p != null) {
293: try {
294: p.getErrorStream().close();
295: } catch (IOException e) {
296: // ignore
297: }
298: try {
299: p.getInputStream().close();
300: } catch (IOException e) {
301: // ignore
302: }
303: try {
304: p.getOutputStream().close();
305: } catch (IOException e) {
306: // ignore
307: }
308: p = null;
309: }
310: if (br != null) {
311: try {
312: br.close();
313: } catch (IOException e) {
314: // ignore
315: }
316: }
317: }
318: }
319:
320: protected String getFileContent(String filename) {
321: File f = new File(filename);
322: if (!f.exists()) {
323: return "";
324: }
325:
326: StringBuilder sb = new StringBuilder();
327: BufferedReader br = null;
328: try {
329: br = new BufferedReader(new FileReader(f));
330: String line = null;
331: while ((line = br.readLine()) != null) {
332: line = line.trim();
333: if (line.length() > 0) {
334: if (sb.length() > 0) {
335: sb.append("\n");
336: }
337: sb.append(line);
338: }
339: }
340: return sb.toString();
341: } catch (Exception e) {
342: // ignore exception
343: return "";
344: } finally {
345: if (br != null) {
346: try {
347: br.close();
348: } catch (IOException e) {
349: // ignore
350: }
351: }
352: }
353: }
354: }
|