83 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils;
|
|
|
15 |
|
|
|
16 |
import static org.openconcerto.utils.CollectionUtils.join;
|
|
|
17 |
import static java.lang.System.getProperty;
|
|
|
18 |
import org.openconcerto.utils.cc.ITransformer;
|
|
|
19 |
import org.openconcerto.utils.i18n.TM;
|
|
|
20 |
|
|
|
21 |
import java.io.File;
|
|
|
22 |
import java.net.InterfaceAddress;
|
|
|
23 |
import java.net.NetworkInterface;
|
|
|
24 |
import java.net.URI;
|
|
|
25 |
import java.net.URISyntaxException;
|
|
|
26 |
import java.util.ArrayList;
|
|
|
27 |
import java.util.Enumeration;
|
|
|
28 |
import java.util.Formatter;
|
|
|
29 |
import java.util.HashMap;
|
|
|
30 |
import java.util.List;
|
|
|
31 |
import java.util.Map;
|
|
|
32 |
|
|
|
33 |
import javax.swing.LookAndFeel;
|
|
|
34 |
import javax.swing.UIManager;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Various system informations (e.g. VM version, user name, network address).
|
|
|
38 |
*
|
|
|
39 |
* @author Sylvain CUAZ
|
|
|
40 |
*/
|
|
|
41 |
public final class SystemInfo {
|
|
|
42 |
|
|
|
43 |
static public enum Info {
|
|
|
44 |
JAVA, OS, USER, NETWORK
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public static final String CLASS_PROTOCOL = "class";
|
|
|
48 |
|
|
|
49 |
static public Map<Info, String> get(final boolean html) {
|
|
|
50 |
final Map<Info, String> res = new HashMap<Info, String>(Info.values().length);
|
|
|
51 |
|
|
|
52 |
final String lineBreak = getLineBreak(html);
|
|
|
53 |
|
|
|
54 |
// * Version 1.6.0_13-b03 de Sun Microsystems Inc. ; dossier d'installation
|
|
|
55 |
final String version = getProperty("java.runtime.version") != null ? getProperty("java.runtime.version") : getProperty("java.version");
|
|
|
56 |
URI vendorURI = null;
|
|
|
57 |
final LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
|
|
|
58 |
URI lafURI = null;
|
|
|
59 |
try {
|
|
|
60 |
vendorURI = new URI(getProperty("java.vendor.url"));
|
|
|
61 |
lafURI = new URI(CLASS_PROTOCOL, lookAndFeel.getClass().getName(), null);
|
|
|
62 |
} catch (URISyntaxException e1) {
|
|
|
63 |
// tant pis pas de lien
|
|
|
64 |
e1.printStackTrace();
|
|
|
65 |
}
|
|
|
66 |
final Runtime rt = Runtime.getRuntime();
|
|
|
67 |
final String stats = "<i>" + TM.tr("memory") + " :</i> " + formatBytes(rt.freeMemory()) + " / " + formatBytes(rt.totalMemory()) + " ; " + TM.tr("processors", rt.availableProcessors());
|
|
|
68 |
final String lafDesc = lookAndFeel == null ? TM.tr("no.laf") : getLink(lookAndFeel.getName(), lafURI, html) + ", " + lookAndFeel.getDescription();
|
|
|
69 |
|
|
|
70 |
final String p = TM.tr("javaVersion", version, getLink(getProperty("java.vendor"), vendorURI, html)) + " ; " + getLink(TM.tr("javaHome"), new File(getProperty("java.home")).toURI(), html)
|
|
|
71 |
+ lineBreak + stats + lineBreak + lafDesc;
|
|
|
72 |
|
|
|
73 |
res.put(Info.JAVA, p);
|
|
|
74 |
|
|
|
75 |
// * Windows XP 5.1 (x86)
|
|
|
76 |
res.put(Info.OS, "<b>" + getProperty("os.name") + "</b> " + getProperty("os.version") + " (" + getProperty("os.arch") + ")");
|
|
|
77 |
|
|
|
78 |
// * Sylvain ; C:\Documents and Settings\Sylvain ; D:\workspace\CTech
|
|
|
79 |
res.put(Info.USER,
|
|
|
80 |
getProperty("user.name") + " ; " + getLink(TM.tr("home.dir"), new File(getProperty("user.home")).toURI(), html) + " ; "
|
|
|
81 |
+ getLink(TM.tr("cwd"), new File(getProperty("user.dir")).toURI(), html));
|
|
|
82 |
|
|
|
83 |
// * eth0 192.168.28.52/24, état: inactif, nom complet: ""
|
|
|
84 |
final List<String> ifs = new ArrayList<String>();
|
|
|
85 |
try {
|
|
|
86 |
final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
|
|
|
87 |
while (en.hasMoreElements()) {
|
|
|
88 |
final NetworkInterface ni = en.nextElement();
|
|
|
89 |
// on mac both the name and the display name are just "vmnet1"
|
|
|
90 |
if (ni.getHardwareAddress() != null && !ni.isLoopback() && !ni.getDisplayName().toLowerCase().contains("vmware") && !ni.getName().toLowerCase().contains("vmnet")) {
|
|
|
91 |
final StringBuilder sb = new StringBuilder();
|
|
|
92 |
// don't wrap each nic in a <p> or <li> since it offset the first line
|
|
|
93 |
sb.append(ni.getName() + " " + join(ni.getInterfaceAddresses(), ", ", new ITransformer<InterfaceAddress, String>() {
|
|
|
94 |
@Override
|
|
|
95 |
public String transformChecked(InterfaceAddress input) {
|
|
|
96 |
return "<b>" + input.getAddress().getHostAddress() + "</b>" + "/" + input.getNetworkPrefixLength();
|
|
|
97 |
}
|
|
|
98 |
}));
|
|
|
99 |
sb.append(" ; <i>" + TM.tr("interfaceState") + " :</i> " + TM.tr(ni.isUp() ? "interfaceStateUp" : "interfaceStateDown"));
|
|
|
100 |
sb.append(lineBreak);
|
|
|
101 |
sb.append(" <i>" + TM.tr("interfaceFullName") + " :</i> " + ni.getDisplayName());
|
|
|
102 |
|
|
|
103 |
sb.append(lineBreak);
|
|
|
104 |
sb.append(" <i>" + TM.tr("hardwareAddress") + " :</i> ");
|
|
|
105 |
final Formatter fmt = new Formatter(sb);
|
|
|
106 |
final byte[] mac = ni.getHardwareAddress();
|
|
|
107 |
for (int i = 0; i < mac.length; i++) {
|
|
|
108 |
fmt.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "");
|
|
|
109 |
}
|
|
|
110 |
ifs.add(sb.toString());
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
} catch (Exception e) {
|
|
|
114 |
// affiche l'erreur
|
|
|
115 |
e.printStackTrace();
|
|
|
116 |
ifs.add(e.getLocalizedMessage());
|
|
|
117 |
}
|
|
|
118 |
res.put(Info.NETWORK, join(ifs, lineBreak));
|
|
|
119 |
|
|
|
120 |
return res;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public static final String getLineBreak(final boolean html) {
|
|
|
124 |
return html ? "<br>" : "\n";
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public static final String getLink(final String name, final URI uri, final boolean html) {
|
|
|
128 |
if (uri == null) {
|
|
|
129 |
return name;
|
|
|
130 |
} else if (html) {
|
|
|
131 |
return "<a href=\"" + uri.toString() + "\" >" + name + "</a>";
|
|
|
132 |
} else {
|
|
|
133 |
return "[" + name + "](" + uri.toString() + ")";
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
private static String formatBytes(long b) {
|
|
|
138 |
return TM.tr("megabytes", b / 1024 / 1024);
|
|
|
139 |
}
|
|
|
140 |
}
|