Example usage for java.net InetSocketAddress getAddress

List of usage examples for java.net InetSocketAddress getAddress

Introduction

In this page you can find the example usage for java.net InetSocketAddress getAddress.

Prototype

public final InetAddress getAddress() 

Source Link

Document

Gets the InetAddress .

Usage

From source file:org.apache.hadoop.hdfs.server.namenode.AvatarNodeZkUtil.java

public static String toIpPortString(InetSocketAddress addr) {
    return addr.getAddress().getHostAddress() + ":" + addr.getPort();
}

From source file:org.apache.atlas.ha.AtlasServerIdSelector.java

/**
 * Return the ID corresponding to this Atlas instance.
 *
 * The match is done by looking for an ID configured in {@link HAConfiguration#ATLAS_SERVER_IDS} key
 * that has a host:port entry for the key {@link HAConfiguration#ATLAS_SERVER_ADDRESS_PREFIX}+ID where
 * the host is a local IP address and port is set in the system property
 * {@link AtlasConstants#SYSTEM_PROPERTY_APP_PORT}.
 *
 * @param configuration/* w  w w .  j a v a  2 s  .co  m*/
 * @return
 * @throws AtlasException if no ID is found that maps to a local IP Address or port
 */
public static String selectServerId(Configuration configuration) throws AtlasException {
    // ids are already trimmed by this method
    String[] ids = configuration.getStringArray(HAConfiguration.ATLAS_SERVER_IDS);
    String matchingServerId = null;
    int appPort = Integer.parseInt(System.getProperty(AtlasConstants.SYSTEM_PROPERTY_APP_PORT));
    for (String id : ids) {
        String hostPort = configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX + id);
        if (!StringUtils.isEmpty(hostPort)) {
            InetSocketAddress socketAddress;
            try {
                socketAddress = NetUtils.createSocketAddr(hostPort);
            } catch (Exception e) {
                LOG.warn("Exception while trying to get socket address for {}", hostPort, e);
                continue;
            }
            if (!socketAddress.isUnresolved() && NetUtils.isLocalAddress(socketAddress.getAddress())
                    && appPort == socketAddress.getPort()) {
                LOG.info("Found matched server id {} with host port: {}", id, hostPort);
                matchingServerId = id;
                break;
            }
        } else {
            LOG.info("Could not find matching address entry for id: {}", id);
        }
    }
    if (matchingServerId == null) {
        String msg = String.format(
                "Could not find server id for this instance. "
                        + "Unable to find IDs matching any local host and port binding among %s",
                StringUtils.join(ids, ","));
        throw new AtlasException(msg);
    }
    return matchingServerId;
}

From source file:org.apache.phoenix.hive.util.PhoenixStorageHandlerUtil.java

public static String getRegionLocation(HRegionLocation location, Log log) throws IOException {
    InetSocketAddress isa = new InetSocketAddress(location.getHostname(), location.getPort());
    if (isa.isUnresolved()) {
        log.warn("Failed resolve " + isa);
    }/*from w  ww .j a  va  2 s. c om*/
    InetAddress regionAddress = isa.getAddress();
    String regionLocation = null;
    try {
        regionLocation = reverseDNS(regionAddress);
    } catch (NamingException e) {
        log.warn("Cannot resolve the host name for " + regionAddress + " because of " + e);
        regionLocation = location.getHostname();
    }

    return regionLocation;
}

From source file:ezbake.thrift.ThriftUtils.java

public static TServerSocket getSslServerSocket(InetSocketAddress addr, Properties properties)
        throws TTransportException {
    return EzSSLTransportFactory.getServerSocket(addr.getPort(), 0, addr.getAddress(),
            new EzSSLTransportFactory.EzSSLTransportParameters(properties));
}

From source file:org.pepstock.jem.node.NodeInfoUtility.java

/**
  * Factory creates a NodeInfo copying a set of information from Member
  * object of Hazelcast framework. NodeInfo will use Uuid of Member as the
  * key./* ww  w.ja  v a  2  s  .c o m*/
  * 
  * @see org.pepstock.jem.node.NodeInfo
  * @param member member object of Hazelcast framework
  * @param info node info to load
  * @throws NodeException if any exception occurs
  */
public static final void loadNodeInfo(Member member, NodeInfo info) throws NodeException {
    String jemVersion = getManifestAttribute(ConfigKeys.JEM_MANIFEST_VERSION);
    // sets the version
    if (jemVersion != null) {
        info.setJemVersion(jemVersion);
    }
    // set uuid of member of hazelcast as key
    info.setKey(member.getUuid());
    // set status starting at the beginning
    info.setStatus(Status.STARTING);
    // sets boolean if has affinity loader

    // for net info of member, loads all info inside of nodeinfo
    // port of RMI will be set later
    InetSocketAddress address = member.getInetSocketAddress();
    info.setPort(address.getPort());
    info.setIpaddress(address.getAddress().getHostAddress());

    // sets label to be displayed by GRS
    info.setLabel(info.getIpaddress() + ":" + info.getPort());

    // sets execution environment
    info.setExecutionEnvironment(Main.EXECUTION_ENVIRONMENT);
    // use JMX to extract current process id
    info.setProcessId(ManagementFactory.getRuntimeMXBean().getName());

    // extracts the name using the MXBean result
    String hostname = StringUtils.substringAfter(info.getProcessId(), "@");
    info.setHostname(hostname);

    // extracts from operating ssytem MXbean all info about the ssytem
    OperatingSystemMXBean bean = ManagementFactory.getOperatingSystemMXBean();
    info.getNodeInfoBean().setSystemArchitecture(bean.getArch());
    info.getNodeInfoBean().setAvailableProcessors(bean.getAvailableProcessors());
    info.getNodeInfoBean().setSystemName(bean.getName());

    // uses SIGAR to get total memory and the user used by JEM to start
    try {
        info.getNodeInfoBean().setTotalMemory(SIGAR.getMem().getTotal());
        ProcCredName cred = SIGAR.getProcCredName(SIGAR.getPid());
        info.setUser(cred.getUser());
    } catch (SigarException e) {
        throw new NodeException(e.getMessage(), e);
    }
    // informs the node itself that it has been loaded
    info.loaded();
}

From source file:com.alibaba.rocketmq.common.MixAll.java

public static long createBrokerId(final String ip, final int port) {
    InetSocketAddress isa = new InetSocketAddress(ip, port);
    byte[] ipArray = isa.getAddress().getAddress();
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put(ipArray);/*from w  w  w.  ja v  a 2s .  c om*/
    bb.putInt(port);
    long value = bb.getLong(0);
    return Math.abs(value);
}

From source file:org.openflamingo.remote.thrift.thriftfs.ThriftUtils.java

/**
 * Creates a Thrift name node client.//  ww  w .  ja va  2s  . c  o m
 *
 * @param conf the HDFS instance
 * @return a Thrift name node client.
 */
public static Namenode.Client createNamenodeClient(Configuration conf) throws Exception {
    String s = conf.get(NamenodePlugin.THRIFT_ADDRESS_PROPERTY, NamenodePlugin.DEFAULT_THRIFT_ADDRESS);
    // TODO(todd) use fs.default.name here if set to 0.0.0.0 - but share this with the code in
    // SecondaryNameNode that does the same
    InetSocketAddress addr = NetUtils.createSocketAddr(s);

    // If the NN thrift server is listening on the wildcard address (0.0.0.0),
    // use the external IP from the NN configuration, but with the port listed
    // in the thrift config.
    if (addr.getAddress().isAnyLocalAddress()) {
        InetSocketAddress nnAddr = NameNode.getAddress(conf);
        addr = new InetSocketAddress(nnAddr.getAddress(), addr.getPort());
    }

    TTransport t = new TSocket(addr.getHostName(), addr.getPort());
    if (UserGroupInformation.isSecurityEnabled()) {
        t = new HadoopThriftAuthBridge.Client().createClientTransport(
                conf.get(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY), addr.getHostName(), "KERBEROS", t);
    }

    t.open();
    TProtocol p = new TBinaryProtocol(t);
    return new Namenode.Client(p);
}

From source file:org.apache.hadoop.yarn.webapp.util.WebAppUtils.java

private static String getResolvedAddress(InetSocketAddress address) {
    address = NetUtils.getConnectAddress(address);
    StringBuilder sb = new StringBuilder();
    InetAddress resolved = address.getAddress();
    if (resolved == null || resolved.isAnyLocalAddress() || resolved.isLoopbackAddress()) {
        String lh = address.getHostName();
        try {/*  ww  w .  j a v  a  2 s.  co m*/
            lh = InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e) {
            //Ignore and fallback.
        }
        sb.append(lh);
    } else {
        sb.append(address.getHostName());
    }
    sb.append(":").append(address.getPort());
    return sb.toString();
}

From source file:org.sipfoundry.sipxbridge.symmitron.DataShuffler.java

/**
 * //w  ww  .  ja v  a2 s .c  om
 * Implements the following search algorithm to retrieve a datagram channel that is associated
 * with the far end:
 * 
 * <pre>
 * getSelfRoutedDatagramChannel(farEnd)
 * For each selectable key do:
 *   let ipAddress be the local ip address
 *   let p be the local port
 *   let d be the datagramChannel associated with the key
 *   If  farEnd.ipAddress == ipAddress  &amp;&amp; port == localPort return d
 * return null
 * </pre>
 * 
 * @param farEnd
 * @return
 */

public static DatagramChannel getSelfRoutedDatagramChannel(InetSocketAddress farEnd) {
    // Iterate over the set of keys for which events are
    // available
    InetAddress ipAddress = farEnd.getAddress();
    int port = farEnd.getPort();
    for (Iterator<SelectionKey> selectedKeys = selector.keys().iterator(); selectedKeys.hasNext();) {
        SelectionKey key = selectedKeys.next();
        if (!key.isValid()) {
            continue;
        }
        DatagramChannel datagramChannel = (DatagramChannel) key.channel();
        if (datagramChannel.socket().getLocalAddress().equals(ipAddress)
                && datagramChannel.socket().getLocalPort() == port) {
            return datagramChannel;
        }

    }
    return null;

}

From source file:org.apache.hadoop.net.NetUtils.java

/**
 * Returns InetSocketAddress that a client can use to 
 * connect to the server. Server.getListenerAddress() is not correct when
 * the server binds to "0.0.0.0". This returns "127.0.0.1:port" when
 * the getListenerAddress() returns "0.0.0.0:port".
 * //w  w  w. j  a v a 2s . c  om
 * @param server
 * @return socket address that a client can use to connect to the server.
 */
public static InetSocketAddress getConnectAddress(Server server) {
    InetSocketAddress addr = server.getListenerAddress();
    if (addr.getAddress().isAnyLocalAddress()) {
        addr = makeSocketAddr("127.0.0.1", addr.getPort());
    }
    return addr;
}