OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
149 ilm 18
 
83 ilm 19
import org.openconcerto.utils.cc.ITransformer;
20
import org.openconcerto.utils.i18n.TM;
21
 
22
import java.io.File;
23
import java.net.InterfaceAddress;
24
import java.net.NetworkInterface;
25
import java.net.URI;
26
import java.net.URISyntaxException;
27
import java.util.ArrayList;
28
import java.util.Enumeration;
29
import java.util.Formatter;
30
import java.util.List;
177 ilm 31
import java.util.Locale;
149 ilm 32
import java.util.NavigableMap;
33
import java.util.TreeMap;
83 ilm 34
 
35
import javax.swing.LookAndFeel;
36
import javax.swing.UIManager;
37
 
38
/**
39
 * Various system informations (e.g. VM version, user name, network address).
40
 *
41
 * @author Sylvain CUAZ
42
 */
43
public final class SystemInfo {
44
 
45
    static public enum Info {
46
        JAVA, OS, USER, NETWORK
47
    }
48
 
49
    public static final String CLASS_PROTOCOL = "class";
50
 
177 ilm 51
    static public NavigableMap<Info, String> get(final boolean html, final Locale locale) {
52
        final TM tm = TM.getInstance(locale);
149 ilm 53
        final NavigableMap<Info, String> res = new TreeMap<Info, String>();
83 ilm 54
 
55
        final String lineBreak = getLineBreak(html);
56
 
57
        // * Version 1.6.0_13-b03 de Sun Microsystems Inc. ; dossier d'installation
58
        final String version = getProperty("java.runtime.version") != null ? getProperty("java.runtime.version") : getProperty("java.version");
59
        URI vendorURI = null;
60
        final LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
61
        URI lafURI = null;
62
        try {
63
            vendorURI = new URI(getProperty("java.vendor.url"));
64
            lafURI = new URI(CLASS_PROTOCOL, lookAndFeel.getClass().getName(), null);
65
        } catch (URISyntaxException e1) {
66
            // tant pis pas de lien
67
            e1.printStackTrace();
68
        }
69
        final Runtime rt = Runtime.getRuntime();
177 ilm 70
        final String stats = "<i>" + tm.translate("memory") + " :</i> " + formatBytes(tm, rt.freeMemory()) + " / " + formatBytes(tm, rt.totalMemory()) + " ; "
71
                + tm.translate("processors", rt.availableProcessors());
72
        final String lafDesc = lookAndFeel == null ? tm.translate("no.laf") : getLink(lookAndFeel.getName(), lafURI, html) + ", " + lookAndFeel.getDescription();
83 ilm 73
 
177 ilm 74
        final String p = tm.translate("javaVersion", version, getLink(getProperty("java.vendor"), vendorURI, html)) + " ; "
75
                + getLink(tm.translate("javaHome"), new File(getProperty("java.home")).toURI(), html) + lineBreak + stats + lineBreak + lafDesc;
83 ilm 76
 
77
        res.put(Info.JAVA, p);
78
 
79
        // * Windows XP 5.1 (x86)
80
        res.put(Info.OS, "<b>" + getProperty("os.name") + "</b> " + getProperty("os.version") + " (" + getProperty("os.arch") + ")");
81
 
82
        // * Sylvain ; C:\Documents and Settings\Sylvain ; D:\workspace\CTech
177 ilm 83
        res.put(Info.USER, getProperty("user.name") + " ; " + getLink(tm.translate("home.dir"), new File(getProperty("user.home")).toURI(), html) + " ; "
84
                + getLink(tm.translate("cwd"), new File(getProperty("user.dir")).toURI(), html));
83 ilm 85
 
86
        // * eth0 192.168.28.52/24, état: inactif, nom complet: ""
87
        final List<String> ifs = new ArrayList<String>();
88
        try {
89
            final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
90
            while (en.hasMoreElements()) {
91
                final NetworkInterface ni = en.nextElement();
92
                // on mac both the name and the display name are just "vmnet1"
93
                if (ni.getHardwareAddress() != null && !ni.isLoopback() && !ni.getDisplayName().toLowerCase().contains("vmware") && !ni.getName().toLowerCase().contains("vmnet")) {
94
                    final StringBuilder sb = new StringBuilder();
95
                    // don't wrap each nic in a <p> or <li> since it offset the first line
96
                    sb.append(ni.getName() + " " + join(ni.getInterfaceAddresses(), ", ", new ITransformer<InterfaceAddress, String>() {
97
                        @Override
98
                        public String transformChecked(InterfaceAddress input) {
99
                            return "<b>" + input.getAddress().getHostAddress() + "</b>" + "/" + input.getNetworkPrefixLength();
100
                        }
101
                    }));
177 ilm 102
                    sb.append(" ; <i>" + tm.translate("interfaceState") + " :</i> " + tm.translate(ni.isUp() ? "interfaceStateUp" : "interfaceStateDown"));
83 ilm 103
                    sb.append(lineBreak);
177 ilm 104
                    sb.append(" <i>" + tm.translate("interfaceFullName") + " :</i> " + ni.getDisplayName());
83 ilm 105
 
106
                    sb.append(lineBreak);
177 ilm 107
                    sb.append(" <i>" + tm.translate("hardwareAddress") + " :</i> ");
83 ilm 108
                    final Formatter fmt = new Formatter(sb);
109
                    final byte[] mac = ni.getHardwareAddress();
110
                    for (int i = 0; i < mac.length; i++) {
111
                        fmt.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "");
112
                    }
113
                    ifs.add(sb.toString());
114
                }
115
            }
116
        } catch (Exception e) {
117
            // affiche l'erreur
118
            e.printStackTrace();
119
            ifs.add(e.getLocalizedMessage());
120
        }
121
        res.put(Info.NETWORK, join(ifs, lineBreak));
122
 
123
        return res;
124
    }
125
 
126
    public static final String getLineBreak(final boolean html) {
127
        return html ? "<br>" : "\n";
128
    }
129
 
130
    public static final String getLink(final String name, final URI uri, final boolean html) {
131
        if (uri == null) {
132
            return name;
133
        } else if (html) {
134
            return "<a href=\"" + uri.toString() + "\" >" + name + "</a>";
135
        } else {
136
            return "[" + name + "](" + uri.toString() + ")";
137
        }
138
    }
139
 
177 ilm 140
    private static String formatBytes(final TM tm, final long b) {
141
        return tm.translate("megabytes", b / 1024 / 1024);
83 ilm 142
    }
143
}