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.WindowsSystemEnvironment
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: import java.util.ArrayList;
059: import java.util.List;
060:
061: /**
062: * Windows implementation of the SystemEnvironment class.
063: */
064: class WindowsSystemEnvironment extends SystemEnvironment {
065: WindowsSystemEnvironment() {
066: super ();
067:
068: // run a call to make sure things are initialized
069: // ignore the first call result as the system may
070: // give inconsistent data on the first invocation ever
071: getWmicResult("computersystem", "get", "model");
072:
073: setSystemModel(getWmicResult("computersystem", "get", "model"));
074: setSystemManufacturer(getWmicResult("computersystem", "get",
075: "manufacturer"));
076: setSerialNumber(getWmicResult("bios", "get", "serialnumber"));
077:
078: String cpuMfr = getWmicResult("cpu", "get", "manufacturer");
079: // this isn't as good an option, but if we couldn't get anything
080: // from wmic, try the processor_identifier
081: if (cpuMfr.length() == 0) {
082: String procId = System.getenv("processor_identifer");
083: if (procId != null) {
084: String[] s = procId.split(",");
085: cpuMfr = s[s.length - 1].trim();
086: }
087: }
088: setCpuManufacturer(cpuMfr);
089:
090: // try to remove the temp file that gets created from running wmic cmds
091: try {
092: // look in the current working directory
093: File f = new File("TempWmicBatchFile.bat");
094: if (f.exists()) {
095: f.delete();
096: }
097: } catch (Exception e) {
098: // ignore the exception
099: }
100: }
101:
102: /**
103: * This method invokes wmic outside of the normal environment
104: * collection routines.
105: *
106: * An initial call to wmic can be costly in terms of time.
107: *
108: * <code>
109: * Details of why the first call is costly can be found at:
110: *
111: * http://support.microsoft.com/kb/290216/en-us
112: *
113: * "When you run the Wmic.exe utility for the first time, the utility
114: * compiles its .mof files into the repository. To save time during
115: * Windows installation, this operation takes place as necessary."
116: * </code>
117: */
118: private String getWmicResult(String alias, String verb,
119: String property) {
120: String res = "";
121: BufferedReader in = null;
122: try {
123: ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "WMIC",
124: alias, verb, property);
125: Process p = pb.start();
126: // need this for executing windows commands (at least
127: // needed for executing wmic command)
128: BufferedWriter bw = new BufferedWriter(
129: new OutputStreamWriter(p.getOutputStream()));
130: bw.write(13);
131: bw.flush();
132: bw.close();
133:
134: p.waitFor();
135: if (p.exitValue() == 0) {
136: in = new BufferedReader(new InputStreamReader(p
137: .getInputStream()));
138: String line = null;
139: while ((line = in.readLine()) != null) {
140: line = line.trim();
141: if (line.length() == 0) {
142: continue;
143: }
144: res = line;
145: }
146: // return the *last* line read
147: return res;
148: }
149:
150: } catch (Exception e) {
151: // ignore the exception
152: } finally {
153: if (in != null) {
154: try {
155: in.close();
156: } catch (IOException e) {
157: // ignore
158: }
159: }
160: }
161: return res.trim();
162: }
163: }
|