jdk/src/share/classes/com/sun/servicetag/WindowsSystemEnvironment.java
changeset 16802 ea3325542aa8
parent 16801 e2de240b437f
parent 16575 d7ad0dfaa411
child 16803 3bdc22a32b0e
equal deleted inserted replaced
16801:e2de240b437f 16802:ea3325542aa8
     1 /*
       
     2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.servicetag;
       
    27 
       
    28 // This class is a copy of the com.sun.scn.servicetags.WindowsSystemEnvironment
       
    29 // class from the Sun Connection source.
       
    30 //
       
    31 // The Service Tags team maintains the latest version of the implementation
       
    32 // for system environment data collection.  JDK will include a copy of
       
    33 // the most recent released version for a JDK release. We rename
       
    34 // the package to com.sun.servicetag so that the Sun Connection
       
    35 // product always uses the latest version from the com.sun.scn.servicetags
       
    36 // package. JDK and users of the com.sun.servicetag API
       
    37 // (e.g. NetBeans and SunStudio) will use the version in JDK.
       
    38 //
       
    39 // So we keep this class in src/share/classes instead of src/<os>/classes.
       
    40 
       
    41 import java.io.*;
       
    42 import java.util.ArrayList;
       
    43 import java.util.List;
       
    44 
       
    45 /**
       
    46  * Windows implementation of the SystemEnvironment class.
       
    47  */
       
    48 class WindowsSystemEnvironment extends SystemEnvironment {
       
    49     WindowsSystemEnvironment() {
       
    50         super();
       
    51 
       
    52         // run a call to make sure things are initialized
       
    53         // ignore the first call result as the system may
       
    54         // give inconsistent data on the first invocation ever
       
    55         getWmicResult("computersystem", "get", "model");
       
    56 
       
    57         setSystemModel(getWmicResult("computersystem", "get", "model"));
       
    58         setSystemManufacturer(getWmicResult("computersystem", "get", "manufacturer"));
       
    59         setSerialNumber(getWmicResult("bios", "get", "serialnumber"));
       
    60 
       
    61         String cpuMfr = getWmicResult("cpu", "get", "manufacturer");
       
    62         // this isn't as good an option, but if we couldn't get anything
       
    63         // from wmic, try the processor_identifier
       
    64         if (cpuMfr.length() == 0) {
       
    65             String procId = System.getenv("processor_identifer");
       
    66             if (procId != null) {
       
    67                 String[] s = procId.split(",");
       
    68                 cpuMfr = s[s.length - 1].trim();
       
    69             }
       
    70         }
       
    71         setCpuManufacturer(cpuMfr);
       
    72 
       
    73         // try to remove the temp file that gets created from running wmic cmds
       
    74         try {
       
    75             // look in the current working directory
       
    76             File f = new File("TempWmicBatchFile.bat");
       
    77             if (f.exists()) {
       
    78                 f.delete();
       
    79             }
       
    80         } catch (Exception e) {
       
    81             // ignore the exception
       
    82         }
       
    83     }
       
    84 
       
    85 
       
    86     /**
       
    87      * This method invokes wmic outside of the normal environment
       
    88      * collection routines.
       
    89      *
       
    90      * An initial call to wmic can be costly in terms of time.
       
    91      *
       
    92      * <code>
       
    93      * Details of why the first call is costly can be found at:
       
    94      *
       
    95      * http://support.microsoft.com/kb/290216/en-us
       
    96      *
       
    97      * "When you run the Wmic.exe utility for the first time, the utility
       
    98      * compiles its .mof files into the repository. To save time during
       
    99      * Windows installation, this operation takes place as necessary."
       
   100      * </code>
       
   101      */
       
   102     private String getWmicResult(String alias, String verb, String property) {
       
   103         String res = "";
       
   104         BufferedReader in = null;
       
   105         try {
       
   106             ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "WMIC", alias, verb, property);
       
   107             Process p = pb.start();
       
   108             // need this for executing windows commands (at least
       
   109             // needed for executing wmic command)
       
   110             BufferedWriter bw = null;
       
   111             try {
       
   112                 bw = new BufferedWriter(
       
   113                          new OutputStreamWriter(p.getOutputStream()));
       
   114                 bw.write(13);
       
   115                 bw.flush();
       
   116             } finally {
       
   117                 if (bw != null) {
       
   118                     bw.close();
       
   119                 }
       
   120             }
       
   121 
       
   122             p.waitFor();
       
   123             if (p.exitValue() == 0) {
       
   124                 in = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
   125                 String line = null;
       
   126                 while ((line = in.readLine()) != null) {
       
   127                     line = line.trim();
       
   128                     if (line.length() == 0) {
       
   129                         continue;
       
   130                     }
       
   131                     res = line;
       
   132                 }
       
   133                 // return the *last* line read
       
   134                 return res;
       
   135             }
       
   136 
       
   137         } catch (Exception e) {
       
   138             // ignore the exception
       
   139         } finally {
       
   140             if (in != null) {
       
   141                 try {
       
   142                     in.close();
       
   143                 } catch (IOException e) {
       
   144                     // ignore
       
   145                 }
       
   146             }
       
   147         }
       
   148         return res.trim();
       
   149     }
       
   150 }