001/*
002 * (C) Copyright 2006-2018 Nuxeo (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 */
019package org.nuxeo.ecm.core.management.statuses;
020
021import java.io.IOException;
022import java.lang.reflect.Method;
023import java.math.BigInteger;
024import java.net.InetAddress;
025import java.net.NetworkInterface;
026import java.security.MessageDigest;
027import java.security.NoSuchAlgorithmException;
028import java.util.Enumeration;
029
030import org.apache.commons.codec.binary.Base64;
031import org.apache.commons.lang3.StringUtils;
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.common.utils.ExceptionUtils;
035import org.nuxeo.ecm.core.management.api.AdministrativeStatusManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Instance identifier (mainly imported from connect client : TechnicalInstanceIdentifier)
040 *
041 * @author matic
042 */
043public class NuxeoInstanceIdentifierHelper {
044
045    private static final String HASH_METHOD = "MD5";
046
047    protected static final Log log = LogFactory.getLog(NuxeoInstanceIdentifierHelper.class);
048
049    protected static String serverInstanceName;
050
051    public static String generateHardwareUID() throws IOException {
052        String hwUID = "";
053
054        String javaVersion = System.getProperty("java.version");
055
056        Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
057
058        while (ifs.hasMoreElements()) {
059            NetworkInterface ni = ifs.nextElement();
060
061            if (javaVersion.contains("1.6")) {
062                // ni.getHardwareAddress() only in jdk 1.6
063                Method[] methods = ni.getClass().getMethods();
064                for (Method method : methods) {
065                    if (method.getName().equalsIgnoreCase("getHardwareAddress")) {
066                        try {
067                            byte[] hwAddr = (byte[]) method.invoke(ni);
068                            if (hwAddr != null) {
069                                hwUID = hwUID + "-" + Base64.encodeBase64String(hwAddr);
070                            }
071                            break;
072                        } catch (ReflectiveOperationException e) {
073                            throw ExceptionUtils.runtimeException(e);
074                        }
075                    }
076                }
077            } else {
078                Enumeration<InetAddress> addrs = ni.getInetAddresses();
079                while (addrs.hasMoreElements()) {
080                    hwUID = hwUID + "-" + Base64.encodeBase64String(addrs.nextElement().getAddress());
081                }
082            }
083        }
084        return hwUID;
085    }
086
087    public static String summarize(String value) throws NoSuchAlgorithmException {
088        byte[] digest = MessageDigest.getInstance(HASH_METHOD).digest(value.getBytes());
089        BigInteger sum = new BigInteger(digest);
090        return sum.toString(16);
091    }
092
093    public static String newServerInstanceName() {
094
095        String osName = System.getProperty("os.name");
096
097        String hwInfo;
098        try {
099            hwInfo = generateHardwareUID();
100            hwInfo = summarize(hwInfo);
101        } catch (IOException | NoSuchAlgorithmException e) {
102            hwInfo = "***";
103        }
104
105        String instancePath;
106        try {
107            instancePath = Framework.getRuntime().getHome().toString();
108            instancePath = summarize(instancePath);
109        } catch (NoSuchAlgorithmException e) {
110            instancePath = "***";
111        }
112
113        return osName + "-" + instancePath + "-" + hwInfo;
114
115    }
116
117    public static String getServerInstanceName() {
118        if (serverInstanceName == null) {
119            serverInstanceName = Framework.getProperty(AdministrativeStatusManager.ADMINISTRATIVE_INSTANCE_ID);
120            if (StringUtils.isEmpty(serverInstanceName)) {
121                serverInstanceName = newServerInstanceName();
122            }
123        }
124
125        return serverInstanceName;
126    }
127}