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:xbird.util.nio.RemoteMemoryMappedFile.java

private static String generateFileIdentifier(final InetSocketAddress sockAddr, final String filePath) {
    final StringBuilder buf = new StringBuilder();
    buf.append(sockAddr.getHostName());
    buf.append(':');
    buf.append(sockAddr.getPort());/*from   w  ww . j  ava 2 s.  com*/
    buf.append('/');
    buf.append(filePath);
    return buf.toString();
}

From source file:co.cask.tigon.test.BasicFlowTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    // Create and start the Netty service.
    service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(new TestHandler())).build();

    service.startAndWait();/* w  w  w .  j a v  a  2  s  .com*/
    InetSocketAddress address = service.getBindAddress();
    baseURL = "http://" + address.getHostName() + ":" + address.getPort();

    httpClient = new HttpClient();
}

From source file:org.apache.hadoop.hbase.HServerAddress.java

private static String getBindAddressInternal(InetSocketAddress address) {
    final InetAddress addr = address.getAddress();
    if (addr != null) {
        return addr.getHostAddress();
    } else {//from  w  w w  . j a v  a  2 s .  co m
        LogFactory.getLog(HServerAddress.class)
                .error("Could not resolve the" + " DNS name of " + address.getHostName());
        return null;
    }
}

From source file:org.apache.tajo.rpc.NettyClientBase.java

private static InetSocketAddress resolveAddress(InetSocketAddress address) {
    if (address.isUnresolved()) {
        return RpcUtils.createSocketAddr(address.getHostName(), address.getPort());
    }//from   w w w  .  j a  va2s  . c  o  m
    return address;
}

From source file:com.pinterest.terrapin.TerrapinUtil.java

/**
* Attempt to load data (already in HDFS on a correct directory) into an already locked fileset.
* The data is assumed to already have been placed in the correct directory on the terrapin
* cluster. This is being called by the Terrapin loader jobs. The @fsInfo object is the same
* as the locked fsInfo object.//w  w w. j  ava2s.c om
*/
public static void loadFileSetData(ZooKeeperManager zkManager, FileSetInfo fsInfo, Options options)
        throws Exception {
    InetSocketAddress controllerSockAddress = zkManager.getControllerLeader();
    LOG.info("Connecting to controller at " + controllerSockAddress.getHostName() + ":"
            + controllerSockAddress.getPort());
    LOG.info("Load timeout " + Constants.LOAD_TIMEOUT_SECONDS + " seconds.");

    Service<ThriftClientRequest, byte[]> service = ClientBuilder.safeBuild(ClientBuilder.get()
            .hosts(controllerSockAddress).codec(new ThriftClientFramedCodecFactory(Option.<ClientId>empty()))
            .retries(1).connectTimeout(Duration.fromMilliseconds(1000))
            .requestTimeout(Duration.fromSeconds(Constants.LOAD_TIMEOUT_SECONDS)).hostConnectionLimit(100)
            .failFast(false));
    TerrapinController.ServiceIface iface = new TerrapinController.ServiceToClient(service,
            new TBinaryProtocol.Factory());
    TerrapinLoadRequest request = new TerrapinLoadRequest();
    request.setHdfsDirectory(fsInfo.servingInfo.hdfsPath);
    request.setOptions(options);
    request.setFileSet(fsInfo.fileSetName);
    request.setExpectedNumPartitions(fsInfo.servingInfo.numPartitions);

    LOG.info("Loading file set " + fsInfo.fileSetName + " at " + fsInfo.servingInfo.hdfsPath);
    long startTimeSeconds = System.currentTimeMillis() / 1000;
    int numTriesLeft = 5;
    boolean done = false;
    Exception e = null;
    while (numTriesLeft > 0) {
        try {
            iface.loadFileSet(request).get();
            done = true;
            break;
        } catch (Throwable t) {
            LOG.error("Swap failed with exception.", t);
            e = new Exception(t);
            numTriesLeft--;
        }
        LOG.info("Retrying in 10 seconds.");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ie) {
            LOG.error("Interrupted.");
            break;
        }
    }
    if (done) {
        LOG.info("Load successful. Swap took " + ((System.currentTimeMillis() / 1000) - startTimeSeconds)
                + " seconds.");
    } else {
        LOG.error("Load failed !!!.");
        throw new Exception(e);
    }
}

From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java

public static void configureProxy(AbstractHttpClient client, RepositoryLocation location) {
    Assert.isNotNull(client);//from w w  w .  java 2s .c  o  m
    Assert.isNotNull(location);
    String url = location.getUrl();
    Assert.isNotNull(url, "The location url must not be null"); //$NON-NLS-1$

    String host = NetUtil.getHost(url);
    Proxy proxy;
    if (NetUtil.isUrlHttps(url)) {
        proxy = location.getProxyForHost(host, IProxyData.HTTPS_PROXY_TYPE);
    } else {
        proxy = location.getProxyForHost(host, IProxyData.HTTP_PROXY_TYPE);
    }

    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();

        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(address.getHostName(), address.getPort()));

        if (proxy instanceof AuthenticatedProxy) {
            AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy;
            Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(),
                    address.getAddress(), false);
            if (credentials instanceof NTCredentials) {
                AuthScope proxyAuthScopeNTLM = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM, AuthPolicy.NTLM);
                client.getCredentialsProvider().setCredentials(proxyAuthScopeNTLM, credentials);

                AuthScope proxyAuthScopeAny = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM);
                Credentials usernamePasswordCredentials = getCredentials(authProxy.getUserName(),
                        authProxy.getPassword(), address.getAddress(), true);
                client.getCredentialsProvider().setCredentials(proxyAuthScopeAny, usernamePasswordCredentials);

            } else {
                AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM);
                client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials);
            }
        }
    } else {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

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

public static void printZookeeperEntries(Configuration originalConf, Configuration conf, String serviceName,
        PrintStream outputStream) throws IOException, KeeperException, InterruptedException {
    String connection = conf.get(FSConstants.FS_HA_ZOOKEEPER_QUORUM);
    if (connection == null)
        return;/*www  .j  av a  2  s .c  om*/
    AvatarZooKeeperClient zk = new AvatarZooKeeperClient(conf, null);
    outputStream.println("ZooKeeper entries:");

    // client protocol
    InetSocketAddress defaultAddr = NameNode.getClientProtocolAddress(originalConf);
    String defaultName = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
    outputStream.println("Default name is " + defaultName);
    String registration = zk.getPrimaryAvatarAddress(defaultName, new Stat(), false);
    outputStream.println("Primary node according to ZooKeeper: " + registration);

    // datanode protocol
    defaultAddr = NameNode.getDNProtocolAddress(originalConf);
    defaultName = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
    registration = zk.getPrimaryAvatarAddress(defaultName, new Stat(), false);
    outputStream.println("Primary node DN protocol     : " + registration);

    // http address
    defaultAddr = NetUtils.createSocketAddr(originalConf.get("dfs.http.address"));
    defaultName = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
    registration = zk.getPrimaryAvatarAddress(defaultName, new Stat(), false);
    outputStream.println("Primary node http address    : " + registration);

    for (Avatar anAvatar : Avatar.avatars) {
        outputStream.println(anAvatar + " entries: ");
        for (ZookeeperKey key : ZookeeperKey.values()) {
            String keyInZookeeper = getZnodeName(conf, serviceName, anAvatar, key);
            outputStream.println(
                    keyInZookeeper + " : " + zk.getPrimaryAvatarAddress(keyInZookeeper, new Stat(), false));
        }
    }
}

From source file:org.springframework.cloud.aws.core.env.ec2.AmazonEc2InstanceDataPropertySourceAwsTest.java

@BeforeClass
public static void setupHttpServer() throws Exception {
    InetSocketAddress address = new InetSocketAddress(HTTP_SERVER_TEST_PORT);
    httpServer = HttpServer.create(address, -1);
    httpServer.createContext("/latest/user-data", new StringWritingHttpHandler(
            Base64.encodeBase64("key1:value1;key2:value2;key3:value3".getBytes())));
    httpServer.createContext("/latest/meta-data/instance-id",
            new StringWritingHttpHandler("i123456".getBytes()));
    httpServer.start();//  w w  w.jav  a 2 s .  c om
    overwriteMetadataEndpointUrl("http://" + address.getHostName() + ":" + address.getPort());
}

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

public static void clearZookeeper(Configuration originalConf, Configuration conf, String serviceName)
        throws IOException {
    String connection = conf.get(FSConstants.FS_HA_ZOOKEEPER_QUORUM);
    if (connection == null) {
        return;//from   w ww .j a  v a 2 s  . c  o m
    }
    AvatarZooKeeperClient zk = new AvatarZooKeeperClient(conf, null);

    // Clear NameNode address in ZK
    InetSocketAddress defaultAddr;
    String[] aliases;

    defaultAddr = NameNode.getClientProtocolAddress(originalConf);
    String defaultName = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
    LOG.info("Clear Client Address information in ZooKeeper: " + defaultName);
    zk.clearPrimary(defaultName);

    aliases = conf.getStrings(FSConstants.FS_NAMENODE_ALIASES);
    if (aliases != null) {
        for (String alias : aliases) {
            zk.clearPrimary(alias);
        }
    }

    LOG.info("Clear Service Address information in ZooKeeper");
    defaultAddr = NameNode.getDNProtocolAddress(originalConf);
    if (defaultAddr != null) {
        String defaultServiceName = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
        zk.clearPrimary(defaultServiceName);
    }

    aliases = conf.getStrings(FSConstants.DFS_NAMENODE_DN_ALIASES);
    if (aliases != null) {
        for (String alias : aliases) {
            zk.clearPrimary(alias);
        }
    }

    LOG.info("Clear Http Address information in ZooKeeper");
    // Clear http address in ZK
    // Stolen from NameNode so we have the same code in both places
    defaultAddr = NetUtils.createSocketAddr(originalConf.get(FSConstants.DFS_NAMENODE_HTTP_ADDRESS_KEY));
    String defaultHttpAddress = defaultAddr.getHostName() + ":" + defaultAddr.getPort();
    zk.clearPrimary(defaultHttpAddress);

    aliases = conf.getStrings(FSConstants.DFS_HTTP_ALIASES);
    if (aliases != null) {
        for (String alias : aliases) {
            zk.clearPrimary(alias);
        }
    }

    for (Avatar avatar : Avatar.avatars) {
        for (ZookeeperKey key : ZookeeperKey.values()) {
            zk.clearPrimary(getZnodeName(conf, serviceName, avatar, key));
        }
    }
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.ContainerLocalizer.java

/**
 * Adds the ContainerLocalizer arguments for a @{link ShellCommandExecutor},
 * as expected by ContainerLocalizer.main
 * @param command the current ShellCommandExecutor command line
 * @param user localization user/*from   w  ww  .  ja v  a  2s . co  m*/
 * @param appId localized app id
 * @param locId localizer id
 * @param nmAddr nodemanager address
 * @param localDirs list of local dirs
 */
public static void buildMainArgs(List<String> command, String user, String appId, String locId,
        InetSocketAddress nmAddr, List<String> localDirs, String userFolder) {

    command.add(ContainerLocalizer.class.getName());
    command.add(user);
    command.add(userFolder);
    command.add(appId);
    command.add(locId);
    command.add(nmAddr.getHostName());
    command.add(Integer.toString(nmAddr.getPort()));
    for (String dir : localDirs) {
        command.add(dir);
    }
}