2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package com.sun.servicetag;
44
// This class is a copy of the com.sun.scn.servicetags.SystemEnvironment
45
// class from the Sun Connection source.
47
// The Service Tags team maintains the latest version of the implementation
48
// for system environment data collection. JDK will include a copy of
49
// the most recent released version for a JDK release. We rename
50
// the package to com.sun.servicetag so that the Sun Connection
51
// product always uses the latest version from the com.sun.scn.servicetags
52
// package. JDK and users of the com.sun.servicetag API
53
// (e.g. NetBeans and SunStudio) will use the version in JDK.
56
import java.net.InetAddress;
57
import java.net.UnknownHostException;
60
* SystemEnvironment class collects the environment data with the
61
* best effort from the underlying platform.
63
public class SystemEnvironment {
64
private String hostname;
65
private String hostId;
66
private String osName;
67
private String osVersion;
68
private String osArchitecture;
69
private String systemModel;
70
private String systemManufacturer;
71
private String cpuManufacturer;
72
private String serialNumber;
73
private static SystemEnvironment sysEnv = null;
75
public static synchronized SystemEnvironment getSystemEnvironment() {
77
String os = System.getProperty("os.name");
78
if (os.equals("SunOS")) {
79
sysEnv = new SolarisSystemEnvironment();
80
} else if (os.equals("Linux")) {
81
sysEnv = new LinuxSystemEnvironment();
82
} else if (os.startsWith("Windows")) {
83
sysEnv = new WindowsSystemEnvironment();
85
sysEnv = new SystemEnvironment();
94
this.hostname = InetAddress.getLocalHost().getHostName();
95
} catch (UnknownHostException ex) {
96
this.hostname = "Unknown host";
99
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 = "";
111
* @param hostname The hostname to set.
113
public void setHostname(String hostname) {
114
this.hostname = hostname;
119
* @param osName The osName to set.
121
public void setOsName(String osName) {
122
this.osName = osName;
126
* Sets the OS version.
127
* @param osVersion The osVersion to set.
129
public void setOsVersion(String osVersion) {
130
this.osVersion = osVersion;
134
* Sets the OS architecture.
135
* @param osArchitecture The osArchitecture to set.
137
public void setOsArchitecture(String osArchitecture) {
138
this.osArchitecture = osArchitecture;
142
* Sets the system model.
143
* @param systemModel The systemModel to set.
145
public void setSystemModel(String systemModel) {
146
this.systemModel = systemModel;
150
* Sets the system manufacturer.
151
* @param systemManufacturer The systemManufacturer to set.
153
public void setSystemManufacturer(String systemManufacturer) {
154
this.systemManufacturer = systemManufacturer;
158
* Sets the cpu manufacturer.
159
* @param cpuManufacturer The cpuManufacturer to set.
161
public void setCpuManufacturer(String cpuManufacturer) {
162
this.cpuManufacturer = cpuManufacturer;
166
* Sets the serial number.
167
* @param serialNumber The serialNumber to set.
169
public void setSerialNumber(String serialNumber) {
170
this.serialNumber = serialNumber;
174
* Sets the hostid. Truncates to a max length of 16 chars.
175
* @param hostId The hostid to set.
177
public void setHostId(String hostId) {
178
if (hostId == null || hostId.equals("null")) {
181
if (hostId.length() > 16) {
182
hostId = hostId.substring(0,16);
184
this.hostId = hostId;
188
* Returns the hostname.
189
* @return The hostname.
191
public String getHostname() {
196
* Returns the osName.
197
* @return The osName.
199
public String getOsName() {
204
* Returns the osVersion.
205
* @return The osVersion.
207
public String getOsVersion() {
212
* Returns the osArchitecture.
213
* @return The osArchitecture.
215
public String getOsArchitecture() {
216
return osArchitecture;
220
* Returns the systemModel.
221
* @return The systemModel.
223
public String getSystemModel() {
228
* Returns the systemManufacturer.
229
* @return The systemManufacturer.
231
public String getSystemManufacturer() {
232
return systemManufacturer;
236
* Returns the serialNumber.
237
* @return The serialNumber.
239
public String getSerialNumber() {
244
* Returns the hostId.
245
* @return The hostId.
247
public String getHostId() {
252
* Returns the cpuManufacturer.
253
* @return The cpuManufacturer.
255
public String getCpuManufacturer() {
256
return cpuManufacturer;
259
protected String getCommandOutput(String... command) {
260
StringBuilder sb = new StringBuilder();
261
BufferedReader br = null;
264
ProcessBuilder pb = new ProcessBuilder(command);
268
if (p.exitValue() == 0) {
269
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
271
while ((line = br.readLine()) != null) {
273
if (line.length() > 0) {
274
if (sb.length() > 0) {
281
return sb.toString();
282
} catch (InterruptedException ie) {
283
// in case the command hangs
288
} catch (Exception e) {
294
p.getErrorStream().close();
295
} catch (IOException e) {
299
p.getInputStream().close();
300
} catch (IOException e) {
304
p.getOutputStream().close();
305
} catch (IOException e) {
313
} catch (IOException e) {
320
protected String getFileContent(String filename) {
321
File f = new File(filename);
326
StringBuilder sb = new StringBuilder();
327
BufferedReader br = null;
329
br = new BufferedReader(new FileReader(f));
331
while ((line = br.readLine()) != null) {
333
if (line.length() > 0) {
334
if (sb.length() > 0) {
340
return sb.toString();
341
} catch (Exception e) {
348
} catch (IOException e) {