001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019
020package org.nuxeo.ecm.core.management.statuses;
021
022import java.io.IOException;
023import java.lang.reflect.Method;
024import java.math.BigInteger;
025import java.net.InetAddress;
026import java.net.NetworkInterface;
027import java.security.MessageDigest;
028import java.security.NoSuchAlgorithmException;
029import java.util.Enumeration;
030
031import org.apache.commons.lang.StringUtils;
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.common.utils.Base64;
035import org.nuxeo.common.utils.ExceptionUtils;
036import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * Instance identifier (mainly imported from connect client : TechnicalInstanceIdentifier)
041 *
042 * @author matic
043 */
044public class NuxeoInstanceIdentifierHelper {
045
046    private static final String HASH_METHOD = "MD5";
047
048    protected static final Log log = LogFactory.getLog(NuxeoInstanceIdentifierHelper.class);
049
050    protected static String serverInstanceName;
051
052    public static String generateHardwareUID() throws IOException {
053        String hwUID = "";
054
055        String javaVersion = System.getProperty("java.version");
056
057        Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
058
059        while (ifs.hasMoreElements()) {
060            NetworkInterface ni = ifs.nextElement();
061
062            if (javaVersion.contains("1.6")) {
063                // ni.getHardwareAddress() only in jdk 1.6
064                Method[] methods = ni.getClass().getMethods();
065                for (Method method : methods) {
066                    if (method.getName().equalsIgnoreCase("getHardwareAddress")) {
067                        try {
068                            byte[] hwAddr = (byte[]) method.invoke(ni);
069                            if (hwAddr != null) {
070                                hwUID = hwUID + "-" + Base64.encodeBytes(hwAddr);
071                            }
072                            break;
073                        } catch (ReflectiveOperationException e) {
074                            throw ExceptionUtils.runtimeException(e);
075                        }
076                    }
077                }
078            } else {
079                Enumeration<InetAddress> addrs = ni.getInetAddresses();
080                while (addrs.hasMoreElements()) {
081                    hwUID = hwUID + "-" + Base64.encodeBytes(addrs.nextElement().getAddress());
082                }
083            }
084        }
085        return hwUID;
086    }
087
088    public static String summarize(String value) throws NoSuchAlgorithmException {
089        byte[] digest = MessageDigest.getInstance(HASH_METHOD).digest(value.getBytes());
090        BigInteger sum = new BigInteger(digest);
091        return sum.toString(16);
092    }
093
094    public static String newServerInstanceName() {
095
096        String osName = System.getProperty("os.name");
097
098        String hwInfo;
099        try {
100            hwInfo = generateHardwareUID();
101            hwInfo = summarize(hwInfo);
102        } catch (IOException | NoSuchAlgorithmException e) {
103            hwInfo = "***";
104        }
105
106        String instancePath;
107        try {
108            instancePath = Framework.getRuntime().getHome().toString();
109            instancePath = summarize(instancePath);
110        } catch (NoSuchAlgorithmException e) {
111            instancePath = "***";
112        }
113
114        return osName + "-" + instancePath + "-" + hwInfo;
115
116    }
117
118    public static String getServerInstanceName() {
119        if (serverInstanceName == null) {
120            serverInstanceName = Framework.getProperty(AdministrativeStatusManager.ADMINISTRATIVE_INSTANCE_ID);
121            if (StringUtils.isEmpty(serverInstanceName)) {
122                serverInstanceName = newServerInstanceName();
123            }
124        }
125
126        return serverInstanceName;
127    }
128}