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.eclipse.jetty.Starter.java

private static void startServer(String hostname, int port) {
    InetSocketAddress inetAddress = new InetSocketAddress(hostname, port);
    Server server = new Server(inetAddress);

    WebAppContext context = new WebAppContext();
    context.setServer(server);//from   w  w  w.java2s. c  o  m
    context.setContextPath("/");

    ProtectionDomain protectionDomain = Starter.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    context.setWar(location.toExternalForm());

    server.setHandler(context);
    try {
        server.start();
        System.out.println("Visit http://" + inetAddress.getHostName() + ":" + port);
        System.out.println("Press any key to stop server!");
        System.in.read();
        server.stop();
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(100);
    }
}

From source file:org.owasp.proxy.Main.java

private static HttpRequestHandler configureAJP(HttpRequestHandler rh, Configuration config) {
    if (config.ajpServer != null) {
        final DefaultAJPRequestHandler arh = new DefaultAJPRequestHandler();
        arh.setTarget(config.ajpServer);
        AJPProperties ajpProperties = new AJPProperties();
        ajpProperties.setRemoteAddress(config.ajpClientAddress);
        if (config.ajpClientCert != null && config.ajpClientCert.endsWith(".pem")) {
            try {
                BufferedReader in = new BufferedReader(new FileReader(config.ajpClientCert));
                StringBuffer buff = new StringBuffer();
                String line;/*  ww w  . j  a  v a 2  s .  c  om*/
                while ((line = in.readLine()) != null) {
                    buff.append(line);
                }
                in.close();
                ajpProperties.setSslCert(buff.toString());
                ajpProperties.setSslCipher("ECDHE-RSA-AES256-SHA");
                ajpProperties.setSslSession("RANDOMID");
                ajpProperties.setSslKeySize("256");
            } catch (IOException ioe) {
                ioe.printStackTrace();
                System.exit(1);
            }
        }
        ajpProperties.setRemoteUser(config.ajpUser);
        ajpProperties.setAuthType("BASIC");
        ajpProperties.setContext("/manager");
        arh.setProperties(ajpProperties);
        final Set<String> ajpHosts = new HashSet<String>();
        if (config.ajpHosts != null)
            ajpHosts.addAll(Arrays.asList(config.ajpHosts));
        final HttpRequestHandler hrh = rh;
        return new HttpRequestHandler() {

            @Override
            public StreamingResponse handleRequest(InetAddress source, StreamingRequest request,
                    boolean isContinue) throws IOException, MessageFormatException {
                InetSocketAddress target = request.getTarget();
                if (ajpHosts.isEmpty() || ajpHosts.contains(target.getHostName())
                        || ajpHosts.contains(target.getAddress().getHostAddress())) {
                    return arh.handleRequest(source, request, isContinue);
                } else {
                    return hrh.handleRequest(source, request, isContinue);
                }
            }

            @Override
            public void dispose() throws IOException {
                arh.dispose();
                hrh.dispose();
            }

        };
    } else {
        return rh;
    }
}

From source file:org.apache.hadoop.thriftfs.ThriftUtils.java

/**
 * Creates a Thrift name node client./* w w  w. j a  v  a2s.  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());
    t.open();
    TProtocol p = new TBinaryProtocol(t);
    return new Namenode.Client(p);
}

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

public static List<JobConf> getNameNodeConfs(JobConf conf) throws IOException {
    List<InetSocketAddress> nameNodeAddrs = DFSUtil.getClientRpcAddresses(conf, null);
    List<JobConf> nameNodeConfs = new ArrayList<JobConf>(nameNodeAddrs.size());
    for (InetSocketAddress nnAddr : nameNodeAddrs) {
        JobConf newConf = new JobConf(conf);
        newConf.set(NameNode.DFS_NAMENODE_RPC_ADDRESS_KEY, nnAddr.getHostName() + ":" + nnAddr.getPort());
        NameNode.setupDefaultURI(newConf);
        nameNodeConfs.add(newConf);//from  w w w  . j  av a  2  s.c  om
    }
    return nameNodeConfs;
}

From source file:hudson.plugins.ec2.EC2Cloud.java

/***
 * Connect to an EC2 instance.//from w ww .  ja va2 s  .  co  m
 * @return {@link AmazonEC2} client
 */
public synchronized static AmazonEC2 connect(String accessId, Secret secretKey, URL endpoint) {
    awsCredentials = new BasicAWSCredentials(accessId, Secret.toString(secretKey));
    ClientConfiguration config = new ClientConfiguration();
    ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy;
    Proxy proxy = proxyConfig == null ? Proxy.NO_PROXY : proxyConfig.createProxy(endpoint.getHost());
    if (!proxy.equals(Proxy.NO_PROXY) && proxy.address() instanceof InetSocketAddress) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();
        config.setProxyHost(address.getHostName());
        config.setProxyPort(address.getPort());
        if (null != proxyConfig.getUserName()) {
            config.setProxyUsername(proxyConfig.getUserName());
            config.setProxyPassword(proxyConfig.getPassword());
        }
    }
    AmazonEC2 client = new AmazonEC2Client(awsCredentials, config);
    client.setEndpoint(endpoint.toString());
    return client;
}

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 {//  w w 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.apache.hadoop.security.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
 *//*  ww w .ja va2 s. c om*/
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 = addr.getHostName().toLowerCase();
    }
    return new Text(host + ":" + addr.getPort());
}

From source file:org.apache.sshd.common.util.net.SshdSocketAddress.java

public static SshdSocketAddress toSshdSocketAddress(SocketAddress addr) {
    if (addr == null) {
        return null;
    } else if (addr instanceof SshdSocketAddress) {
        return (SshdSocketAddress) addr;
    } else if (addr instanceof InetSocketAddress) {
        InetSocketAddress isockAddress = (InetSocketAddress) addr;
        return new SshdSocketAddress(isockAddress.getHostName(), isockAddress.getPort());
    } else {/*from ww w . j  a va 2 s.  c  o m*/
        throw new UnsupportedOperationException("Cannot convert " + addr.getClass().getSimpleName() + "=" + addr
                + " to " + SshdSocketAddress.class.getSimpleName());
    }
}

From source file:org.apache.hadoop.hdfs.server.journalservice.JournalService.java

/** Create an RPC server. */
private static RPC.Server createRpcServer(Configuration conf, InetSocketAddress address, JournalProtocol impl)
        throws IOException {
    RPC.setProtocolEngine(conf, JournalProtocolPB.class, ProtobufRpcEngine.class);
    JournalProtocolServerSideTranslatorPB xlator = new JournalProtocolServerSideTranslatorPB(impl);
    BlockingService service = JournalProtocolService.newReflectiveBlockingService(xlator);
    return RPC.getServer(JournalProtocolPB.class, service, address.getHostName(), address.getPort(), 1, false,
            conf, null);//  w  ww  .  java 2  s  . c om
}

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

private static void configureHttpClientProxy(AbstractHttpClient client, HttpContext context,
        AbstractWebLocation location) {//from www.ja va  2s  .  c  om
    String host = getHost(location.getUrl());

    Proxy proxy;
    if (isRepositoryHttps(location.getUrl())) {
        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());
            if (credentials instanceof NTCredentials) {
                List<String> authpref = new ArrayList<String>();
                authpref.add(AuthPolicy.NTLM);
                authpref.add(AuthPolicy.BASIC);
                authpref.add(AuthPolicy.DIGEST);
                client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
            } else {
                List<String> authpref = new ArrayList<String>();
                authpref.add(AuthPolicy.BASIC);
                authpref.add(AuthPolicy.DIGEST);
                authpref.add(AuthPolicy.NTLM);
                client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
            }
            AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(),
                    AuthScope.ANY_REALM);
            client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials);
        }
    } else {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}