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.LinuxSystemEnvironment
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.
55
// So we keep this class in src/share/classes instead of src/<os>/classes.
60
* Linux implementation of the SystemEnvironment class.
62
class LinuxSystemEnvironment extends SystemEnvironment {
63
LinuxSystemEnvironment() {
64
setHostId(getLinuxHostId());
65
setSystemModel(getCommandOutput("/bin/uname", "-i"));
66
setSystemManufacturer(getLinuxSystemManufacturer());
67
setCpuManufacturer(getLinuxCpuManufacturer());
68
setSerialNumber(getLinuxSN());
70
private String dmiInfo = null;
72
private static final int SN = 1;
73
private static final int SYS = 2;
74
private static final int CPU = 3;
76
private String getLinuxHostId() {
77
String output = getCommandOutput("/usr/bin/hostid");
78
// trim off the leading 0x
79
if (output.startsWith("0x")) {
80
output = output.substring(2);
86
* Tries to obtain and return the cpu manufacturer.
87
* @return The cpu manufacturer (an empty string if not found or an error occurred)
89
private String getLinuxCpuManufacturer() {
90
String tmp = getLinuxPSNInfo(CPU);
91
if (tmp.length() > 0) {
95
String contents = getFileContent("/proc/cpuinfo");
96
for (String line : contents.split("\n")) {
97
if (line.contains("vendor_id")) {
98
String[] ss = line.split(":", 2);
105
// returns an empty string if it can't be found or an error happened
106
return getLinuxDMIInfo("dmi type 4", "manufacturer");
111
* Tries to obtain and return the system manufacturer.
112
* @return The system manufacturer (an empty string if not found or an error occurred)
114
private String getLinuxSystemManufacturer() {
115
String tmp = getLinuxPSNInfo(SYS);
116
if (tmp.length() > 0) {
120
// returns an empty string if it can't be found or an error happened
121
return getLinuxDMIInfo("dmi type 1", "manufacturer");
125
* Tries to obtain and return the serial number of the system.
126
* @return The serial number (an empty string if not found or an error occurred)
128
private String getLinuxSN() {
129
String tmp = getLinuxPSNInfo(SN);
130
if (tmp.length() > 0) {
134
// returns an empty string if it can't be found or an error happened
135
return getLinuxDMIInfo("dmi type 1", "serial number");
138
private String getLinuxPSNInfo(int target) {
139
// try to read from the psn file if it exists
140
String contents = getFileContent("/var/run/psn");
141
String[] ss = contents.split("\n");
142
if (target <= ss.length) {
146
// default case is to return ""
150
// reads from dmidecode with the given type and target
151
// returns an empty string if nothing was found or an error occurred
153
// Sample output segment:
155
// DMI type 1, 25 bytes.
156
// System Information
157
// Manufacturer: System manufacturer
158
// Product Name: System Product Name
159
// Version: System Version
160
// Serial Number: System Serial Number
161
// UUID: 3091D719-B25B-D911-959D-6D1B12C7686E
162
// Wake-up Type: Power Switch
164
private synchronized String getLinuxDMIInfo(String dmiType, String target) {
165
// only try to get dmidecode information once, after that, we can
167
if (dmiInfo == null) {
168
Thread dmidecodeThread = new Thread() {
170
dmiInfo = getCommandOutput("/usr/sbin/dmidecode");
173
dmidecodeThread.start();
176
dmidecodeThread.join(2000);
177
if (dmidecodeThread.isAlive()) {
178
dmidecodeThread.interrupt();
181
} catch (InterruptedException ie) {
182
dmidecodeThread.interrupt();
186
if (dmiInfo.length() == 0) {
189
boolean dmiFlag = false;
190
for (String s : dmiInfo.split("\n")) {
191
String line = s.toLowerCase();
193
if (line.contains(target)) {
194
String key = target + ":";
195
int indx = line.indexOf(key) + key.length();
196
if (line.contains(key) && indx < line.length()) {
197
return line.substring(indx).trim();
199
String[] ss = line.split(":");
200
return ss[ss.length-1];
202
} else if (line.contains(dmiType)) {