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.SolarisSystemEnvironment
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: * Solaris implementation of the SystemEnvironment class.
061: */
062: class SolarisSystemEnvironment extends SystemEnvironment {
063: SolarisSystemEnvironment() {
064: setHostId(getCommandOutput("/usr/bin/hostid"));
065: setSystemModel(getCommandOutput("/usr/bin/uname", "-i"));
066: setSystemManufacturer(getSolarisSystemManufacturer());
067: setCpuManufacturer(getSolarisCpuManufacturer());
068: setSerialNumber(getSolarisSN());
069: }
070:
071: /**
072: * Tries to obtain the cpu manufacturer.
073: * @return The cpu manufacturer (an empty string if not found or an error occurred)
074: */
075: private String getSolarisCpuManufacturer() {
076: // not fully accurate, this could be another manufacturer (fujitsu for example)
077: if ("sparc".equalsIgnoreCase(System.getProperty("os.arch"))) {
078: return "Sun Microsystems, Inc";
079: }
080:
081: // if we're here, then we'll try smbios (type 3)
082: return getSmbiosData("3", "Manufacturer: ");
083: }
084:
085: /**
086: * Tries to obtain the system manufacturer.
087: * @return The system manufacturer (an empty string if not found or an error occurred)
088: */
089: private String getSolarisSystemManufacturer() {
090: // not fully accurate, this could be another manufacturer (fujitsu for example)
091: if ("sparc".equalsIgnoreCase(System.getProperty("os.arch"))) {
092: return "Sun Microsystems, Inc";
093: }
094:
095: // if we're here, then we'll try smbios (type 1)
096: return getSmbiosData("1", "Manufacturer: ");
097: }
098:
099: /**
100: * Tries to obtain the serial number.
101: * @return The serial number (empty string if not found or an error occurred)
102: */
103: private String getSolarisSN() {
104: // try to read from the psn file if it exists
105: String tmp = getFileContent("/var/run/psn");
106: if (tmp.length() > 0) {
107: return tmp.trim();
108: }
109:
110: // if we're here, then we'll try sneep
111: String tmpSN = getSneepSN();
112: if (tmpSN.length() > 0) {
113: return tmpSN;
114: }
115:
116: // if we're here, then we'll try smbios (type 1)
117: tmpSN = getSmbiosData("1", "Serial Number: ");
118: if (tmpSN.length() > 0) {
119: return tmpSN;
120: }
121:
122: // if we're here, then we'll try smbios (type 3)
123: tmpSN = getSmbiosData("3", "Serial Number: ");
124: if (tmpSN.length() > 0) {
125: return tmpSN;
126: }
127:
128: // give up and return
129: return "";
130: }
131:
132: // Sample smbios output segment:
133: // ID SIZE TYPE
134: // 1 150 SMB_TYPE_SYSTEM (system information)
135: //
136: // Manufacturer: Sun Microsystems
137: // Product: Sun Fire X4600
138: // Version: To Be Filled By O.E.M.
139: // Serial Number: 00:14:4F:45:0C:2A
140: private String getSmbiosData(String type, String target) {
141: String output = getCommandOutput("/usr/sbin/smbios", "-t", type);
142: for (String s : output.split("\n")) {
143: if (s.contains(target)) {
144: int indx = s.indexOf(target) + target.length();
145: if (indx < s.length()) {
146: String tmp = s.substring(indx).trim();
147: String lowerCaseStr = tmp.toLowerCase();
148: if (!lowerCaseStr.startsWith("not available")
149: && !lowerCaseStr
150: .startsWith("to be filled by o.e.m")) {
151: return tmp;
152: }
153: }
154: }
155: }
156:
157: return "";
158: }
159:
160: private String getSneepSN() {
161: String basedir = getCommandOutput("pkgparam", "SUNWsneep",
162: "BASEDIR");
163: File f = new File(basedir + "/bin/sneep");
164: if (f.exists()) {
165: String sneepSN = getCommandOutput(basedir + "/bin/sneep");
166: if (sneepSN.equalsIgnoreCase("unknown")) {
167: return "";
168: } else {
169: return sneepSN;
170: }
171: } else {
172: return "";
173: }
174: }
175:
176: }
|