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.LinuxSystemEnvironment
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: // So we keep this class in src/share/classes instead of src/<os>/classes.
056:
057: import java.io.*;
058:
059: /**
060: * Linux implementation of the SystemEnvironment class.
061: */
062: class LinuxSystemEnvironment extends SystemEnvironment {
063: LinuxSystemEnvironment() {
064: setHostId(getLinuxHostId());
065: setSystemModel(getCommandOutput("/bin/uname", "-i"));
066: setSystemManufacturer(getLinuxSystemManufacturer());
067: setCpuManufacturer(getLinuxCpuManufacturer());
068: setSerialNumber(getLinuxSN());
069: }
070:
071: private String dmiInfo = null;
072:
073: private static final int SN = 1;
074: private static final int SYS = 2;
075: private static final int CPU = 3;
076:
077: private String getLinuxHostId() {
078: String output = getCommandOutput("/usr/bin/hostid");
079: // trim off the leading 0x
080: if (output.startsWith("0x")) {
081: output = output.substring(2);
082: }
083: return output;
084: }
085:
086: /**
087: * Tries to obtain and return the cpu manufacturer.
088: * @return The cpu manufacturer (an empty string if not found or an error occurred)
089: */
090: private String getLinuxCpuManufacturer() {
091: String tmp = getLinuxPSNInfo(CPU);
092: if (tmp.length() > 0) {
093: return tmp;
094: }
095:
096: String contents = getFileContent("/proc/cpuinfo");
097: for (String line : contents.split("\n")) {
098: if (line.contains("vendor_id")) {
099: String[] ss = line.split(":", 2);
100: if (ss.length > 1) {
101: return ss[1].trim();
102: }
103: }
104: }
105:
106: // returns an empty string if it can't be found or an error happened
107: return getLinuxDMIInfo("dmi type 4", "manufacturer");
108: }
109:
110: /**
111: * Tries to obtain and return the system manufacturer.
112: * @return The system manufacturer (an empty string if not found or an error occurred)
113: */
114: private String getLinuxSystemManufacturer() {
115: String tmp = getLinuxPSNInfo(SYS);
116: if (tmp.length() > 0) {
117: return tmp;
118: }
119:
120: // returns an empty string if it can't be found or an error happened
121: return getLinuxDMIInfo("dmi type 1", "manufacturer");
122: }
123:
124: /**
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)
127: */
128: private String getLinuxSN() {
129: String tmp = getLinuxPSNInfo(SN);
130: if (tmp.length() > 0) {
131: return tmp;
132: }
133:
134: // returns an empty string if it can't be found or an error happened
135: return getLinuxDMIInfo("dmi type 1", "serial number");
136: }
137:
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) {
143: return ss[target - 1];
144: }
145:
146: // default case is to return ""
147: return "";
148: }
149:
150: // reads from dmidecode with the given type and target
151: // returns an empty string if nothing was found or an error occurred
152: //
153: // Sample output segment:
154: // Handle 0x0001
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
163:
164: private synchronized String getLinuxDMIInfo(String dmiType,
165: String target) {
166: // only try to get dmidecode information once, after that, we can
167: // reuse the output
168: if (dmiInfo == null) {
169: Thread dmidecodeThread = new Thread() {
170: public void run() {
171: dmiInfo = getCommandOutput("/usr/sbin/dmidecode");
172: }
173: };
174: dmidecodeThread.start();
175:
176: try {
177: dmidecodeThread.join(2000);
178: if (dmidecodeThread.isAlive()) {
179: dmidecodeThread.interrupt();
180: dmiInfo = "";
181: }
182: } catch (InterruptedException ie) {
183: dmidecodeThread.interrupt();
184: }
185: }
186:
187: if (dmiInfo.length() == 0) {
188: return "";
189: }
190: boolean dmiFlag = false;
191: for (String s : dmiInfo.split("\n")) {
192: String line = s.toLowerCase();
193: if (dmiFlag) {
194: if (line.contains(target)) {
195: String key = target + ":";
196: int indx = line.indexOf(key) + key.length();
197: if (line.contains(key) && indx < line.length()) {
198: return line.substring(indx).trim();
199: }
200: String[] ss = line.split(":");
201: return ss[ss.length - 1];
202: }
203: } else if (line.contains(dmiType)) {
204: dmiFlag = true;
205: }
206: }
207: return "";
208: }
209:
210: }
|