summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/GlobalLoggingContext.java
blob: a708ed6715666cfa3ab086276bd5e5b5f17c8382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openecomp.sdc.logging;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * Collect information the is required for logging, but should not concern the business code of an application. For
 * example, host name and IP address.
 *
 * @author evitaliy
 * @since 04 Mar 2018
 */
@SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace", "squid:S106", "squid:S1148"})
public class GlobalLoggingContext {

    // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
    private static final HostAddressCache HOST_ADDRESS_CACHE = new HostAddressCache();

    @SuppressWarnings("squid:S1075")
    private static final String INSTANCE_UUID_PREFERENCES_PATH = "/logging/instance/uuid";

    private static final String INSTANCE_ID;

    static {
        INSTANCE_ID = readInstanceId();
    }

    private GlobalLoggingContext() {
        // prevent instantiation
    }

    /**
     * A unique ID of the logging entity. Is useful to distinguish between different nodes of the same application. It
     * is assumed, that the node can be re-started, in which case the unique ID must be retained.
     *
     * @return unique logging entity ID
     */
    public static String getInstanceId() {
        return INSTANCE_ID;
    }

    /**
     * Local host address as returned by Java runtime. A value of host address will be cached for the interval specified
     * in {@link HostAddressCache#REFRESH_TIME}
     *
     * @return local host address, may be null if could not be read for some reason
     */
    public static InetAddress getHostAddress() {
        return HOST_ADDRESS_CACHE.get();
    }

    private static String readInstanceId() {

        try {

            // On Linux, by default this will be ~/.java/.userPrefs/prefs.xml
            final Preferences preferences = Preferences.userRoot();
            String existingId = preferences.get(INSTANCE_UUID_PREFERENCES_PATH, null);
            if (existingId != null) {
                return existingId;
            }

            String newId = UUID.randomUUID().toString();
            preferences.put(INSTANCE_UUID_PREFERENCES_PATH, newId);
            preferences.flush();
            return newId;

        } catch (BackingStoreException e) {
            e.printStackTrace();
            // don't fail if there's a problem to use the store for some unexpected reason
            return UUID.randomUUID().toString();
        }
    }

    private static class HostAddressCache {

        private static final long REFRESH_TIME = 60000L;

        private final AtomicLong lastUpdated = new AtomicLong(0L);
        private InetAddress hostAddress;

        InetAddress get() {

            long current = System.currentTimeMillis();
            if (current - lastUpdated.get() > REFRESH_TIME) {

                synchronized (this) {

                    try {
                        // set now to register the attempt even if failed
                        lastUpdated.set(current);
                        hostAddress = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace(); // can't really use logging
                        hostAddress = null;
                    }
                }
            }

            return hostAddress;
        }
    }
}