OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | 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
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
83 ilm 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";
182 ilm 50
    public static final String PROPS_PROTOCOL = "props";
51
    public static final String ENV_PROTOCOL = "env";
83 ilm 52
 
177 ilm 53
    static public NavigableMap<Info, String> get(final boolean html, final Locale locale) {
54
        final TM tm = TM.getInstance(locale);
149 ilm 55
        final NavigableMap<Info, String> res = new TreeMap<Info, String>();
83 ilm 56
 
57
        final String lineBreak = getLineBreak(html);
58
 
59
        // * Version 1.6.0_13-b03 de Sun Microsystems Inc. ; dossier d'installation
60
        final String version = getProperty("java.runtime.version") != null ? getProperty("java.runtime.version") : getProperty("java.version");
61
        URI vendorURI = null;
62
        final LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
63
        URI lafURI = null;
64
        try {
65
            vendorURI = new URI(getProperty("java.vendor.url"));
66
            lafURI = new URI(CLASS_PROTOCOL, lookAndFeel.getClass().getName(), null);
67
        } catch (URISyntaxException e1) {
68
            // tant pis pas de lien
69
            e1.printStackTrace();
70
        }
71
        final Runtime rt = Runtime.getRuntime();
182 ilm 72
        final long totalMemory = rt.totalMemory();
73
        final String stats = "<i>" + tm.translate("memory.used") + " :</i> " + formatBytes(tm, totalMemory - rt.freeMemory()) + " / " + formatBytes(tm, totalMemory) + " ; "
177 ilm 74
                + tm.translate("processors", rt.availableProcessors());
75
        final String lafDesc = lookAndFeel == null ? tm.translate("no.laf") : getLink(lookAndFeel.getName(), lafURI, html) + ", " + lookAndFeel.getDescription();
182 ilm 76
        URI propsURI = null;
77
        URI envURI = null;
78
        try {
79
            propsURI = new URI(PROPS_PROTOCOL, "/", null);
80
            envURI = new URI(ENV_PROTOCOL, "/", null);
81
        } catch (URISyntaxException e1) {
82
            e1.printStackTrace();
83
        }
84
        final String propsDesc = envURI == null ? "" : lineBreak + getLink(tm.translate("properties.all"), propsURI, html) + ", " + getLink(tm.translate("env.all"), envURI, html);
83 ilm 85
 
177 ilm 86
        final String p = tm.translate("javaVersion", version, getLink(getProperty("java.vendor"), vendorURI, html)) + " ; "
182 ilm 87
                + getLink(tm.translate("javaHome"), new File(getProperty("java.home")).toURI(), html) + lineBreak + stats + lineBreak + lafDesc + propsDesc;
83 ilm 88
 
89
        res.put(Info.JAVA, p);
90
 
91
        // * Windows XP 5.1 (x86)
92
        res.put(Info.OS, "<b>" + getProperty("os.name") + "</b> " + getProperty("os.version") + " (" + getProperty("os.arch") + ")");
93
 
94
        // * Sylvain ; C:\Documents and Settings\Sylvain ; D:\workspace\CTech
177 ilm 95
        res.put(Info.USER, getProperty("user.name") + " ; " + getLink(tm.translate("home.dir"), new File(getProperty("user.home")).toURI(), html) + " ; "
96
                + getLink(tm.translate("cwd"), new File(getProperty("user.dir")).toURI(), html));
83 ilm 97
 
98
        // * eth0 192.168.28.52/24, état: inactif, nom complet: ""
99
        final List<String> ifs = new ArrayList<String>();
100
        try {
101
            final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
102
            while (en.hasMoreElements()) {
103
                final NetworkInterface ni = en.nextElement();
104
                // on mac both the name and the display name are just "vmnet1"
105
                if (ni.getHardwareAddress() != null && !ni.isLoopback() && !ni.getDisplayName().toLowerCase().contains("vmware") && !ni.getName().toLowerCase().contains("vmnet")) {
106
                    final StringBuilder sb = new StringBuilder();
107
                    // don't wrap each nic in a <p> or <li> since it offset the first line
108
                    sb.append(ni.getName() + " " + join(ni.getInterfaceAddresses(), ", ", new ITransformer<InterfaceAddress, String>() {
109
                        @Override
110
                        public String transformChecked(InterfaceAddress input) {
111
                            return "<b>" + input.getAddress().getHostAddress() + "</b>" + "/" + input.getNetworkPrefixLength();
112
                        }
113
                    }));
177 ilm 114
                    sb.append(" ; <i>" + tm.translate("interfaceState") + " :</i> " + tm.translate(ni.isUp() ? "interfaceStateUp" : "interfaceStateDown"));
83 ilm 115
                    sb.append(lineBreak);
177 ilm 116
                    sb.append(" <i>" + tm.translate("interfaceFullName") + " :</i> " + ni.getDisplayName());
83 ilm 117
 
118
                    sb.append(lineBreak);
177 ilm 119
                    sb.append(" <i>" + tm.translate("hardwareAddress") + " :</i> ");
83 ilm 120
                    final Formatter fmt = new Formatter(sb);
121
                    final byte[] mac = ni.getHardwareAddress();
122
                    for (int i = 0; i < mac.length; i++) {
123
                        fmt.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : "");
124
                    }
125
                    ifs.add(sb.toString());
126
                }
127
            }
128
        } catch (Exception e) {
129
            // affiche l'erreur
130
            e.printStackTrace();
131
            ifs.add(e.getLocalizedMessage());
132
        }
133
        res.put(Info.NETWORK, join(ifs, lineBreak));
134
 
135
        return res;
136
    }
137
 
138
    public static final String getLineBreak(final boolean html) {
139
        return html ? "<br>" : "\n";
140
    }
141
 
142
    public static final String getLink(final String name, final URI uri, final boolean html) {
143
        if (uri == null) {
144
            return name;
145
        } else if (html) {
146
            return "<a href=\"" + uri.toString() + "\" >" + name + "</a>";
147
        } else {
148
            return "[" + name + "](" + uri.toString() + ")";
149
        }
150
    }
151
 
177 ilm 152
    private static String formatBytes(final TM tm, final long b) {
153
        return tm.translate("megabytes", b / 1024 / 1024);
83 ilm 154
    }
155
}