~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ide/reglib/src/com/sun/servicetag/LinuxSystemEnvironment.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package com.sun.servicetag;
 
43
 
 
44
// This class is a copy of the com.sun.scn.servicetags.LinuxSystemEnvironment
 
45
// class from the Sun Connection source.
 
46
//
 
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.
 
54
//
 
55
// So we keep this class in src/share/classes instead of src/<os>/classes.
 
56
 
 
57
import java.io.*;
 
58
 
 
59
/**
 
60
 * Linux implementation of the SystemEnvironment class.
 
61
 */
 
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());
 
69
    }
 
70
    private String dmiInfo = null;
 
71
 
 
72
    private static final int SN  = 1;
 
73
    private static final int SYS = 2;
 
74
    private static final int CPU = 3;
 
75
 
 
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);
 
81
        }
 
82
        return output;
 
83
    }
 
84
 
 
85
    /**
 
86
     * Tries to obtain and return the cpu manufacturer.
 
87
     * @return The cpu manufacturer (an empty string if not found or an error occurred)
 
88
     */
 
89
    private String getLinuxCpuManufacturer() {
 
90
        String tmp = getLinuxPSNInfo(CPU);
 
91
        if (tmp.length() > 0) {
 
92
            return tmp;
 
93
        }
 
94
 
 
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);
 
99
                if (ss.length > 1) {
 
100
                    return ss[1].trim();
 
101
                }
 
102
            }
 
103
        }
 
104
 
 
105
        // returns an empty string if it can't be found or an error happened
 
106
        return getLinuxDMIInfo("dmi type 4", "manufacturer");
 
107
    }
 
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, String target) {
 
165
        // only try to get dmidecode information once, after that, we can
 
166
        // reuse the output
 
167
        if (dmiInfo == null) {
 
168
            Thread dmidecodeThread = new Thread() {
 
169
                public void run() {
 
170
                    dmiInfo = getCommandOutput("/usr/sbin/dmidecode");
 
171
                }
 
172
            };
 
173
            dmidecodeThread.start();
 
174
 
 
175
            try {
 
176
                dmidecodeThread.join(2000);
 
177
                if (dmidecodeThread.isAlive()) {
 
178
                    dmidecodeThread.interrupt();
 
179
                    dmiInfo = "";
 
180
                }
 
181
            } catch (InterruptedException ie) {
 
182
                dmidecodeThread.interrupt();
 
183
            }
 
184
        }
 
185
 
 
186
        if (dmiInfo.length() == 0) {
 
187
            return "";
 
188
        }
 
189
        boolean dmiFlag = false;
 
190
        for (String s : dmiInfo.split("\n")) {
 
191
            String line = s.toLowerCase();
 
192
            if (dmiFlag) {
 
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();
 
198
                    }
 
199
                    String[] ss = line.split(":"); 
 
200
                    return ss[ss.length-1];
 
201
                }
 
202
            } else if (line.contains(dmiType)) {
 
203
                dmiFlag = true;
 
204
            }
 
205
        }
 
206
        return "";
 
207
    }
 
208
 
 
209
}