001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 */
012
013package org.nuxeo.ecm.core.management.statuses;
014
015import java.io.IOException;
016import java.lang.reflect.Method;
017import java.math.BigInteger;
018import java.net.InetAddress;
019import java.net.NetworkInterface;
020import java.security.MessageDigest;
021import java.security.NoSuchAlgorithmException;
022import java.util.Enumeration;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.utils.Base64;
028import org.nuxeo.common.utils.ExceptionUtils;
029import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Instance identifier (mainly imported from connect client : TechnicalInstanceIdentifier)
034 *
035 * @author matic
036 */
037public class NuxeoInstanceIdentifierHelper {
038
039    private static final String HASH_METHOD = "MD5";
040
041    protected static final Log log = LogFactory.getLog(NuxeoInstanceIdentifierHelper.class);
042
043    protected static String serverInstanceName;
044
045    public static String generateHardwareUID() throws IOException {
046        String hwUID = "";
047
048        String javaVersion = System.getProperty("java.version");
049
050        Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
051
052        while (ifs.hasMoreElements()) {
053            NetworkInterface ni = ifs.nextElement();
054
055            if (javaVersion.contains("1.6")) {
056                // ni.getHardwareAddress() only in jdk 1.6
057                Method[] methods = ni.getClass().getMethods();
058                for (Method method : methods) {
059                    if (method.getName().equalsIgnoreCase("getHardwareAddress")) {
060                        try {
061                            byte[] hwAddr = (byte[]) method.invoke(ni);
062                            if (hwAddr != null) {
063                                hwUID = hwUID + "-" + Base64.encodeBytes(hwAddr);
064                            }
065                            break;
066                        } catch (ReflectiveOperationException e) {
067                            throw ExceptionUtils.runtimeException(e);
068                        }
069                    }
070                }
071            } else {
072                Enumeration<InetAddress> addrs = ni.getInetAddresses();
073                while (addrs.hasMoreElements()) {
074                    hwUID = hwUID + "-" + Base64.encodeBytes(addrs.nextElement().getAddress());
075                }
076            }
077        }
078        return hwUID;
079    }
080
081    public static String summarize(String value) throws NoSuchAlgorithmException {
082        byte[] digest = MessageDigest.getInstance(HASH_METHOD).digest(value.getBytes());
083        BigInteger sum = new BigInteger(digest);
084        return sum.toString(16);
085    }
086
087    public static String newServerInstanceName() {
088
089        String osName = System.getProperty("os.name");
090
091        String hwInfo;
092        try {
093            hwInfo = generateHardwareUID();
094            hwInfo = summarize(hwInfo);
095        } catch (IOException | NoSuchAlgorithmException e) {
096            hwInfo = "***";
097        }
098
099        String instancePath;
100        try {
101            instancePath = Framework.getRuntime().getHome().toString();
102            instancePath = summarize(instancePath);
103        } catch (NoSuchAlgorithmException e) {
104            instancePath = "***";
105        }
106
107        return osName + "-" + instancePath + "-" + hwInfo;
108
109    }
110
111    public static String getServerInstanceName() {
112        if (serverInstanceName == null) {
113            serverInstanceName = Framework.getProperty(AdministrativeStatusManager.ADMINISTRATIVE_INSTANCE_ID);
114            if (StringUtils.isEmpty(serverInstanceName)) {
115                serverInstanceName = newServerInstanceName();
116            }
117        }
118
119        return serverInstanceName;
120    }
121}