OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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