Example usage for java.net InetSocketAddress getHostName

List of usage examples for java.net InetSocketAddress getHostName

Introduction

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

Prototype

public final String getHostName() 

Source Link

Document

Gets the hostname .

Usage

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

/**
 * Creates a Thrift name node client.//ww w. java2 s .com
 *
 * @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.hama.bsp.message.TestHamaAsyncMessageManager.java

private static void messagingInternal(HamaConfiguration conf) throws Exception {
    conf.set(MessageManagerFactory.MESSAGE_MANAGER_CLASS,
            "org.apache.hama.bsp.message.HamaAsyncMessageManagerImpl");
    MessageManager<IntWritable> messageManager = MessageManagerFactory.getMessageManager(conf);

    assertTrue(messageManager instanceof HamaAsyncMessageManagerImpl);

    InetSocketAddress peer = new InetSocketAddress(BSPNetUtils.getCanonicalHostname(),
            BSPNetUtils.getFreePort() + (increment++));
    conf.set(Constants.PEER_HOST, Constants.DEFAULT_PEER_HOST);
    conf.setInt(Constants.PEER_PORT, Constants.DEFAULT_PEER_PORT);

    BSPPeer<?, ?, ?, ?, IntWritable> dummyPeer = new BSPPeerImpl<NullWritable, NullWritable, NullWritable, NullWritable, IntWritable>(
            conf, FileSystem.get(conf), new Counters());
    TaskAttemptID id = new TaskAttemptID("1", 1, 1, 1);
    messageManager.init(id, dummyPeer, conf, peer);
    peer = messageManager.getListenerAddress();
    String peerName = peer.getHostName() + ":" + peer.getPort();
    System.out.println("Peer is " + peerName);
    messageManager.send(peerName, new IntWritable(1337));

    Iterator<Entry<InetSocketAddress, BSPMessageBundle<IntWritable>>> messageIterator = messageManager
            .getOutgoingBundles();/*  www. j  a  v  a 2  s .  c o  m*/

    Entry<InetSocketAddress, BSPMessageBundle<IntWritable>> entry = messageIterator.next();

    assertEquals(entry.getKey(), peer);

    assertTrue(entry.getValue().size() == 1);

    BSPMessageBundle<IntWritable> bundle = new BSPMessageBundle<IntWritable>();
    Iterator<IntWritable> it = entry.getValue().iterator();
    while (it.hasNext()) {
        bundle.addMessage(it.next());
    }

    messageManager.transfer(peer, bundle);

    messageManager.clearOutgoingMessages();

    assertTrue(messageManager.getNumCurrentMessages() == 1);
    IntWritable currentMessage = messageManager.getCurrentMessage();

    assertEquals(currentMessage.get(), 1337);
    messageManager.close();
}

From source file:org.apache.hadoop.mapred.ProxyJobTracker.java

private static SessionInfo getRunningSessionInfo(String sessionHandle) throws Exception {
    // Connect to cluster manager thrift service
    String target = CoronaConf.getClusterManagerAddress(conf);
    LOG.info("Connecting to Cluster Manager at " + target);
    InetSocketAddress address = NetUtils.createSocketAddr(target);
    TTransport transport = new TFramedTransport(new TSocket(address.getHostName(), address.getPort()));
    TProtocol protocol = new TBinaryProtocol(transport);
    ClusterManagerService.Client client = new ClusterManagerService.Client(protocol);
    transport.open();/*from w  w  w  . jav  a  2 s  .  c o m*/
    LOG.info("Requesting running session info for handle: " + sessionHandle);
    SessionInfo info = client.getSessionInfo(sessionHandle);
    transport.close();
    return info;
}

From source file:com.buaa.cfs.utils.SecurityUtil.java

/**
 * Construct the service key for a token
 *
 * @param addr InetSocketAddress of remote connection with a token
 *
 * @return "ip:port" or "host:port" depending on the value of hadoop.security.token.service.use_ip
 *///from  www.  j  a  v  a  2s. co m
public static Text buildTokenService(InetSocketAddress addr) {
    String host = null;
    if (useIpForTokenService) {
        if (addr.isUnresolved()) { // host has no ip address
            throw new IllegalArgumentException(new UnknownHostException(addr.getHostName()));
        }
        host = addr.getAddress().getHostAddress();
    } else {
        host = StringUtils.toLowerCase(addr.getHostName());
    }
    return new Text(host + ":" + addr.getPort());
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Compose a "host:port" string from the address.
 */// w w w .j  a v a2 s.  c om
public static String getHostPortString(InetSocketAddress addr) {
    return addr.getHostName() + ":" + addr.getPort();
}

From source file:au.com.jwatmuff.genericp2p.rmi.RMIPeerManager.java

@SuppressWarnings("unchecked")
private static <T> T getService(InetSocketAddress address, String serviceName, Class<T> serviceClass)
        throws NoSuchServiceException {
    String url = "rmi://" + address.getHostName() + ":" + address.getPort() + "/" + serviceName;
    try {// w w w . ja va2 s. com
        /* maybe should use address.getAddress().getHostAddress() */
        RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
        factory.setServiceInterface(serviceClass);
        factory.setServiceUrl(url);
        factory.afterPropertiesSet();
        return (T) factory.getObject();
    } catch (RemoteLookupFailureException rlfe) {
        throw new NoSuchServiceException("Unable to resolve service " + serviceName + " at url: " + url);
    } catch (Exception e) {
        throw new RuntimeException("getService failed", e);
    }
}

From source file:org.apache.hadoop.hbase.mob.mapreduce.SweepJob.java

static ServerName getCurrentServerName(Configuration conf) throws IOException {
    String hostname = conf.get("hbase.regionserver.ipc.address",
            Strings.domainNamePointerToHostName(
                    DNS.getDefaultHost(conf.get("hbase.regionserver.dns.interface", "default"),
                            conf.get("hbase.regionserver.dns.nameserver", "default"))));
    int port = conf.getInt(HConstants.REGIONSERVER_PORT, HConstants.DEFAULT_REGIONSERVER_PORT);
    // Creation of a HSA will force a resolve.
    InetSocketAddress initialIsa = new InetSocketAddress(hostname, port);
    if (initialIsa.getAddress() == null) {
        throw new IllegalArgumentException("Failed resolve of " + initialIsa);
    }/*from  www. j a v a2 s . c  o  m*/
    return ServerName.valueOf(initialIsa.getHostName(), initialIsa.getPort(),
            EnvironmentEdgeManager.currentTime());
}

From source file:com.intellij.util.net.HttpConfigurable.java

public static List<KeyValue<String, String>> getJvmPropertiesList(final boolean withAutodetection,
        @Nullable final URI uri) {
    final HttpConfigurable me = getInstance();
    if (!me.USE_HTTP_PROXY && !me.USE_PROXY_PAC) {
        return Collections.emptyList();
    }//  www .  j av  a 2  s .  c o m
    final List<KeyValue<String, String>> result = new ArrayList<KeyValue<String, String>>();
    if (me.USE_HTTP_PROXY) {
        final boolean putCredentials = me.KEEP_PROXY_PASSWORD && StringUtil.isNotEmpty(me.PROXY_LOGIN);
        if (me.PROXY_TYPE_IS_SOCKS) {
            result.add(KeyValue.create(JavaProxyProperty.SOCKS_HOST, me.PROXY_HOST));
            result.add(KeyValue.create(JavaProxyProperty.SOCKS_PORT, String.valueOf(me.PROXY_PORT)));
            if (putCredentials) {
                result.add(KeyValue.create(JavaProxyProperty.SOCKS_USERNAME, me.PROXY_LOGIN));
                result.add(KeyValue.create(JavaProxyProperty.SOCKS_PASSWORD, me.getPlainProxyPassword()));
            }
        } else {
            result.add(KeyValue.create(JavaProxyProperty.HTTP_HOST, me.PROXY_HOST));
            result.add(KeyValue.create(JavaProxyProperty.HTTP_PORT, String.valueOf(me.PROXY_PORT)));
            result.add(KeyValue.create(JavaProxyProperty.HTTPS_HOST, me.PROXY_HOST));
            result.add(KeyValue.create(JavaProxyProperty.HTTPS_PORT, String.valueOf(me.PROXY_PORT)));
            if (putCredentials) {
                result.add(KeyValue.create(JavaProxyProperty.HTTP_USERNAME, me.PROXY_LOGIN));
                result.add(KeyValue.create(JavaProxyProperty.HTTP_PASSWORD, me.getPlainProxyPassword()));
            }
        }
    } else if (me.USE_PROXY_PAC && withAutodetection && uri != null) {
        final List<Proxy> proxies = CommonProxy.getInstance().select(uri);
        // we will just take the first returned proxy, but we have an option to test connection through each of them,
        // for instance, by calling prepareUrl()
        if (proxies != null && !proxies.isEmpty()) {
            for (Proxy proxy : proxies) {
                if (isRealProxy(proxy)) {
                    final SocketAddress address = proxy.address();
                    if (address instanceof InetSocketAddress) {
                        final InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
                        if (Proxy.Type.SOCKS.equals(proxy.type())) {
                            result.add(KeyValue.create(JavaProxyProperty.SOCKS_HOST,
                                    inetSocketAddress.getHostName()));
                            result.add(KeyValue.create(JavaProxyProperty.SOCKS_PORT,
                                    String.valueOf(inetSocketAddress.getPort())));
                        } else {
                            result.add(KeyValue.create(JavaProxyProperty.HTTP_HOST,
                                    inetSocketAddress.getHostName()));
                            result.add(KeyValue.create(JavaProxyProperty.HTTP_PORT,
                                    String.valueOf(inetSocketAddress.getPort())));
                            result.add(KeyValue.create(JavaProxyProperty.HTTPS_HOST,
                                    inetSocketAddress.getHostName()));
                            result.add(KeyValue.create(JavaProxyProperty.HTTPS_PORT,
                                    String.valueOf(inetSocketAddress.getPort())));
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:gov.nih.nci.nbia.StandaloneDMDispatcher.java

static String getProxyInfo() {
    StringBuffer sb = new StringBuffer("Get proxy info: ");
    try {/*from  www.  jav a 2  s .  co  m*/
        System.setProperty("java.net.useSystemProxies", "true");
        List<Proxy> l = ProxySelector.getDefault().select(new URI("https://www.google.com/"));

        for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
            Proxy proxy = iter.next();
            sb.append("proxy type : " + proxy.type() + "\n");
            InetSocketAddress addr = (InetSocketAddress) proxy.address();

            if (addr == null) {
                sb.append("No Proxy\n");
            } else {
                sb.append("proxy hostname : " + addr.getHostName() + "\n");
                sb.append("proxy port : " + addr.getPort() + "\n");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        sb.append(e.getMessage());
    }
    return sb.toString();
}

From source file:org.apache.hadoop.dfs.NameNode.java

static URI getUri(InetSocketAddress namenode) {
    int port = namenode.getPort();
    String portString = port == DEFAULT_PORT ? "" : (":" + port);
    return URI.create("hdfs://" + namenode.getHostName() + portString);
}