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

« back to all changes in this revision

Viewing changes to ide/reglib/src/com/sun/servicetag/SystemEnvironment.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.SystemEnvironment
 
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
import java.io.*;
 
56
import java.net.InetAddress;
 
57
import java.net.UnknownHostException;
 
58
 
 
59
/**
 
60
 * SystemEnvironment class collects the environment data with the
 
61
 * best effort from the underlying platform.
 
62
 */
 
63
public class SystemEnvironment {
 
64
    private String hostname;
 
65
    private String hostId;
 
66
    private String osName;
 
67
    private String osVersion;
 
68
    private String osArchitecture;
 
69
    private String systemModel;
 
70
    private String systemManufacturer;
 
71
    private String cpuManufacturer;
 
72
    private String serialNumber;
 
73
    private static SystemEnvironment sysEnv = null;
 
74
 
 
75
    public static synchronized SystemEnvironment getSystemEnvironment() {
 
76
        if (sysEnv == null) {
 
77
            String os = System.getProperty("os.name");
 
78
            if (os.equals("SunOS")) {
 
79
                sysEnv = new SolarisSystemEnvironment();
 
80
            } else if (os.equals("Linux")) {
 
81
                sysEnv = new LinuxSystemEnvironment();
 
82
            } else if (os.startsWith("Windows")) {
 
83
                sysEnv = new WindowsSystemEnvironment();
 
84
            } else {
 
85
                sysEnv = new SystemEnvironment();
 
86
            }
 
87
        }
 
88
        return sysEnv;
 
89
    }
 
90
 
 
91
    // package-private
 
92
    SystemEnvironment() {
 
93
        try {
 
94
            this.hostname = InetAddress.getLocalHost().getHostName();
 
95
        } catch (UnknownHostException ex) {
 
96
            this.hostname = "Unknown host";
 
97
        }
 
98
        this.hostId = "";
 
99
        this.osName = System.getProperty("os.name");
 
100
        this.osVersion = System.getProperty("os.version");
 
101
        this.osArchitecture = System.getProperty("os.arch");
 
102
        this.systemModel = "";
 
103
        this.systemManufacturer = "";
 
104
        this.cpuManufacturer = "";
 
105
        this.serialNumber = "";
 
106
    }
 
107
 
 
108
 
 
109
    /**
 
110
     * Sets the hostname.
 
111
     * @param hostname The hostname to set.
 
112
     */
 
113
    public void setHostname(String hostname) {
 
114
        this.hostname = hostname;
 
115
    }
 
116
 
 
117
    /**
 
118
     * Sets the OS name.
 
119
     * @param osName The osName to set.
 
120
     */
 
121
    public void setOsName(String osName) {
 
122
        this.osName = osName;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Sets the OS version.
 
127
     * @param osVersion The osVersion to set.
 
128
     */
 
129
    public void setOsVersion(String osVersion) {
 
130
        this.osVersion = osVersion;
 
131
    }
 
132
 
 
133
    /**
 
134
     * Sets the OS architecture.
 
135
     * @param osArchitecture The osArchitecture to set.
 
136
     */
 
137
    public void setOsArchitecture(String osArchitecture) {
 
138
        this.osArchitecture = osArchitecture;
 
139
    }
 
140
 
 
141
    /**
 
142
     * Sets the system model.
 
143
     * @param systemModel The systemModel to set.
 
144
     */
 
145
    public void setSystemModel(String systemModel) {
 
146
        this.systemModel = systemModel;
 
147
    }
 
148
 
 
149
    /**
 
150
     * Sets the system manufacturer.
 
151
     * @param systemManufacturer The systemManufacturer to set.
 
152
     */
 
153
    public void setSystemManufacturer(String systemManufacturer) {
 
154
        this.systemManufacturer = systemManufacturer;
 
155
    }
 
156
 
 
157
    /**
 
158
     * Sets the cpu manufacturer.
 
159
     * @param cpuManufacturer The cpuManufacturer to set.
 
160
     */
 
161
    public void setCpuManufacturer(String cpuManufacturer) {
 
162
        this.cpuManufacturer = cpuManufacturer;
 
163
    }
 
164
 
 
165
    /**
 
166
     * Sets the serial number.
 
167
     * @param serialNumber The serialNumber to set.
 
168
     */
 
169
    public void setSerialNumber(String serialNumber) {
 
170
        this.serialNumber = serialNumber;
 
171
    }
 
172
 
 
173
    /**
 
174
     * Sets the hostid.  Truncates to a max length of 16 chars.
 
175
     * @param hostId The hostid to set.
 
176
     */
 
177
    public void setHostId(String hostId) {
 
178
        if (hostId == null || hostId.equals("null")) {
 
179
            hostId = "";
 
180
        }
 
181
        if (hostId.length() > 16) {
 
182
            hostId = hostId.substring(0,16);
 
183
        }
 
184
        this.hostId = hostId;
 
185
    }
 
186
 
 
187
    /**
 
188
     * Returns the hostname.
 
189
     * @return The hostname.
 
190
     */
 
191
    public String getHostname() {
 
192
        return hostname;
 
193
    }
 
194
 
 
195
    /**
 
196
     * Returns the osName.
 
197
     * @return The osName.
 
198
     */
 
199
    public String getOsName() {
 
200
        return osName;
 
201
    }
 
202
 
 
203
    /**
 
204
     * Returns the osVersion.
 
205
     * @return The osVersion.
 
206
     */
 
207
    public String getOsVersion() {
 
208
        return osVersion;
 
209
    }
 
210
 
 
211
    /**
 
212
     * Returns the osArchitecture.
 
213
     * @return The osArchitecture.
 
214
     */
 
215
    public String getOsArchitecture() {
 
216
        return osArchitecture;
 
217
    }
 
218
 
 
219
    /**
 
220
     * Returns the systemModel.
 
221
     * @return The systemModel.
 
222
     */
 
223
    public String getSystemModel() {
 
224
        return systemModel;
 
225
    }
 
226
 
 
227
    /**
 
228
     * Returns the systemManufacturer.
 
229
     * @return The systemManufacturer.
 
230
     */
 
231
    public String getSystemManufacturer() {
 
232
        return systemManufacturer;
 
233
    }
 
234
 
 
235
    /**
 
236
     * Returns the serialNumber.
 
237
     * @return The serialNumber.
 
238
     */
 
239
    public String getSerialNumber() {
 
240
        return serialNumber;
 
241
    }
 
242
 
 
243
    /**
 
244
     * Returns the hostId.
 
245
     * @return The hostId.
 
246
     */
 
247
    public String getHostId() {
 
248
        return hostId;
 
249
    }
 
250
 
 
251
    /**
 
252
     * Returns the cpuManufacturer.
 
253
     * @return The cpuManufacturer.
 
254
     */
 
255
    public String getCpuManufacturer() {
 
256
        return cpuManufacturer;
 
257
    }
 
258
 
 
259
    protected String getCommandOutput(String... command) {
 
260
        StringBuilder sb = new StringBuilder();
 
261
        BufferedReader br = null;
 
262
        Process p = null;
 
263
        try {
 
264
            ProcessBuilder pb = new ProcessBuilder(command);
 
265
            p = pb.start();
 
266
            p.waitFor();
 
267
 
 
268
            if (p.exitValue() == 0) {
 
269
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));
 
270
                String line = null;
 
271
                while ((line = br.readLine()) != null) {
 
272
                    line = line.trim();
 
273
                    if (line.length() > 0) {
 
274
                        if (sb.length() > 0) {
 
275
                            sb.append("\n");
 
276
                        }
 
277
                        sb.append(line);
 
278
                    }
 
279
                }
 
280
            }
 
281
            return sb.toString();
 
282
        } catch (InterruptedException ie) {
 
283
            // in case the command hangs
 
284
            if (p != null) {
 
285
                p.destroy();
 
286
            }
 
287
            return "";
 
288
        } catch (Exception e) {
 
289
            // ignore exception
 
290
            return "";
 
291
        } finally {
 
292
            if (p != null) {
 
293
                try {
 
294
                    p.getErrorStream().close();
 
295
                } catch (IOException e) {
 
296
                    // ignore
 
297
                }
 
298
                try {
 
299
                    p.getInputStream().close();
 
300
                } catch (IOException e) {
 
301
                    // ignore
 
302
                }
 
303
                try {
 
304
                    p.getOutputStream().close();
 
305
                } catch (IOException e) {
 
306
                    // ignore
 
307
                }
 
308
                p = null;
 
309
            }
 
310
            if (br != null) {
 
311
                try {
 
312
                    br.close();
 
313
                } catch (IOException e) {
 
314
                    // ignore
 
315
                } 
 
316
            }
 
317
        }
 
318
    }
 
319
 
 
320
    protected String getFileContent(String filename) {
 
321
        File f = new File(filename);
 
322
        if (!f.exists()) {
 
323
            return "";
 
324
        }
 
325
 
 
326
        StringBuilder sb = new StringBuilder();
 
327
        BufferedReader br = null;
 
328
        try {
 
329
            br = new BufferedReader(new FileReader(f));
 
330
            String line = null;
 
331
            while ((line = br.readLine()) != null) {
 
332
                line = line.trim();
 
333
                if (line.length() > 0) {
 
334
                    if (sb.length() > 0) {
 
335
                        sb.append("\n");
 
336
                    }
 
337
                    sb.append(line);
 
338
                }
 
339
            }
 
340
            return sb.toString();
 
341
        } catch (Exception e) {
 
342
            // ignore exception
 
343
            return "";
 
344
        } finally {
 
345
            if (br != null) {
 
346
                try {
 
347
                    br.close();
 
348
                } catch (IOException e) {
 
349
                    // ignore
 
350
                } 
 
351
            }
 
352
        }
 
353
    }
 
354
}