summaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/network/Identity.java
blob: 45887b072ab0b318c790a9320946508ce2238b77 (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.network;

import com.yahoo.log.LogLevel;
import com.yahoo.net.HostName;
import com.yahoo.net.LinuxInetAddress;

import java.net.Inet6Address;
import java.net.InetAddress;

/**
 * This class encapsulates the identity of the application that uses this instance of message bus. This identity
 * contains a servicePrefix identifier, which is the configuration id of the current servicePrefix, and the canonical
 * host name of the host running this.
 *
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class Identity {

    private final String hostname;
    private final String servicePrefix;

    /**
     * The default constructor requires a configuration identifier that it will use to subscribe to this message bus'
     * identity. This identity is necessary so that the network layer is able to identify self for registration with
     * Slobrok.
     *
     * @param configId The config identifier for the application.
     */
    public Identity(String configId) {
        InetAddress addr = LinuxInetAddress.getLocalHost(); // try hard to get a resolvable address
        if (addr instanceof Inet6Address) // 
            hostname = HostName.getLocalhost(); // ... but fallback to hostname if we get an IPv6 address
        else
            hostname = addr.getCanonicalHostName();
        servicePrefix = configId;
    }

    /**
     * Implements the copy constructor.
     *
     * @param identity The object to copy.
     */
    public Identity(Identity identity) {
        hostname = identity.hostname;
        servicePrefix = identity.servicePrefix;
    }

    /**
     * Returns the hostname for this. This is the network name of the host on which this identity exists. It is
     * retrieved on creation by running shell command "hostname".
     *
     * @return The canonical host name.
     */
    public String getHostname() {
        return hostname;
    }

    /**
     * Returns the service prefix for this. This is what is prefixed to every session that is created on this identity's
     * message bus before registered in the naming service.
     *
     * @return The service prefix.
     */
    public String getServicePrefix() {
        return servicePrefix;
    }

}