Example usage for java.net InetSocketAddress getPort

List of usage examples for java.net InetSocketAddress getPort

Introduction

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

Prototype

public final int getPort() 

Source Link

Document

Gets the port number.

Usage

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 va2  s  .  c o m
 * @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;
}

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);// w  w  w  .java2s  .  co  m
    }
    return nameNodeConfs;
}

From source file:edu.umass.cs.nio.MessageExtractor.java

/**
 * @param sndrAddress/*from   w  w  w  .  j ava  2 s.c  om*/
 * @param rcvrAddress
 * @param json
 * @return JSONObject with addresses stamped.
 */
@SuppressWarnings("deprecation")
// for backwards compatibility
public static JSONObject stampAddressIntoJSONObject(InetSocketAddress sndrAddress,
        InetSocketAddress rcvrAddress, JSONObject json) {
    // only put the IP field in if it doesn't exist already
    try {
        // put sender address
        if (!json.has(MessageNIOTransport.SNDR_ADDRESS_FIELD))
            json.put(MessageNIOTransport.SNDR_ADDRESS_FIELD,
                    sndrAddress.getAddress().getHostAddress() + ":" + sndrAddress.getPort());

        // TODO: remove the deprecated lines bel
        if (!json.has(JSONNIOTransport.SNDR_IP_FIELD))
            json.put(JSONNIOTransport.SNDR_IP_FIELD, sndrAddress.getAddress().getHostAddress());
        if (!json.has(JSONNIOTransport.SNDR_PORT_FIELD))
            json.put(JSONNIOTransport.SNDR_PORT_FIELD, sndrAddress.getPort());

        // put receiver address
        if (!json.has(MessageNIOTransport.RCVR_ADDRESS_FIELD))
            json.put(MessageNIOTransport.RCVR_ADDRESS_FIELD,
                    rcvrAddress.getAddress().getHostAddress() + ":" + rcvrAddress.getPort());

    } catch (JSONException e) {
        log.severe("Encountered JSONException while stamping sender address and port at receiver: ");
        e.printStackTrace();
    }
    return json;
}

From source file:edu.umass.cs.utils.Util.java

public static InetSocketAddress offsetPort(InetSocketAddress isa, int offset) {
    return new InetSocketAddress(isa.getAddress(), isa.getPort() + offset);
}

From source file:com.datatorrent.stram.LaunchContainerRunnable.java

public static ByteBuffer getTokens(StramDelegationTokenManager delegationTokenManager,
        InetSocketAddress heartbeatAddress) throws IOException {
    if (UserGroupInformation.isSecurityEnabled()) {
        UserGroupInformation ugi = UserGroupInformation.getLoginUser();
        StramDelegationTokenIdentifier identifier = new StramDelegationTokenIdentifier(
                new Text(ugi.getUserName()), new Text(""), new Text(""));
        String service = heartbeatAddress.getAddress().getHostAddress() + ":" + heartbeatAddress.getPort();
        Token<StramDelegationTokenIdentifier> stramToken = new Token<StramDelegationTokenIdentifier>(identifier,
                delegationTokenManager);
        stramToken.setService(new Text(service));
        return getTokens(ugi, stramToken);
    }//from w  w  w  . j a v  a2  s  .  c om
    return null;
}

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

/***
 * Connect to an EC2 instance./*from   w  w w  . j ava  2  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.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);
}

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 w  w  . ja v a  2 s. c  o  m*/
}

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

private static void configureHttpClientProxy(AbstractHttpClient client, HttpContext context,
        AbstractWebLocation location) {/*from  w ww.jav  a 2 s.com*/
    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);
    }
}

From source file:edu.umass.cs.msocket.gns.GnsIntegration.java

/**
 * Remove a specific IP address from the list of IPs associated with the
 * MServerSocket that has the given Human Readable Name
 * /*from   w  ww  .  j a va2s  . co  m*/
 * @param name Human readable name of the MServerSocket
 * @param saddr address to remove from the GNS
 * @param gnsCredentials GNS credentials to use
 * @throws Exception
 */
public static void unregisterWithGNS(String name, InetSocketAddress saddr, GnsCredentials gnsCredentials)
        throws Exception {
    if (gnsCredentials == null)
        gnsCredentials = GnsCredentials.getDefaultCredentials();

    final UniversalGnsClient gnsClient = gnsCredentials.getGnsClient();
    GuidEntry socketGuid = KeyPairUtils
            .getGuidEntryFromPreferences(gnsCredentials.getGnsHost() + ":" + gnsCredentials.getGnsPort(), name);
    String ipPort = saddr.getAddress().getHostAddress() + ":" + saddr.getPort();

    // If all GNS accesses are synchronized on this object, we shouldn't have a
    // concurrency issue while updating
    synchronized (gnsClient) {
        JSONArray currentIPs = gnsClient.fieldRead(socketGuid.getGuid(), GnsConstants.SERVER_REG_ADDR,
                gnsCredentials.getGuidEntry());
        JSONArray newIPs = new JSONArray();
        int idx = -1;
        for (int i = 0; i < currentIPs.length(); i++) {
            if (ipPort.equals(currentIPs.getString(i))) {
                idx = i;
                //break;
            } else {
                newIPs.put(currentIPs.getString(i));
            }
        }
        if (idx != -1) {
            //currentIPs.remove(idx);
            gnsClient.fieldReplace(socketGuid.getGuid(), GnsConstants.SERVER_REG_ADDR, newIPs,
                    gnsCredentials.getGuidEntry());
        }
    }

}