Example usage for com.google.common.net HostAndPort fromString

List of usage examples for com.google.common.net HostAndPort fromString

Introduction

In this page you can find the example usage for com.google.common.net HostAndPort fromString.

Prototype

public static HostAndPort fromString(String hostPortString) 

Source Link

Document

Split a freeform string into a host and port, without strict validation.

Usage

From source file:com.torodb.di.MongoLayerModule.java

public MongoLayerModule(Config config) {
    if (config.getProtocol().getMongo().getReplication() != null
            && !config.getProtocol().getMongo().getReplication().isEmpty()
            && config.getProtocol().getMongo().getReplication().get(0).getSyncSource() != null) {
        this.syncSource = HostAndPort
                .fromString(config.getProtocol().getMongo().getReplication().get(0).getSyncSource());
    } else {//from  w w w  . j  a v  a  2s . c o m
        this.syncSource = null;
    }
}

From source file:io.druid.server.listener.announcer.ListenerDiscoverer.java

Map<HostAndPort, Long> getCurrentNodes(final String listener_key) throws IOException {
    final HashMap<HostAndPort, Long> retVal = new HashMap<>();
    final String zkPath = listeningAnnouncerConfig.getAnnouncementPath(listener_key);
    final Collection<String> children;
    try {//from   ww  w .  j a  va  2 s.c o m
        children = cf.getChildren().forPath(zkPath);
    } catch (org.apache.zookeeper.KeeperException.NoNodeException e) {
        LOG.debug(e, "No path found at [%s]", zkPath);
        return ImmutableMap.of();
    } catch (Exception e) {
        throw new IOException("Error getting children for " + zkPath, e);
    }
    for (String child : children) {
        final String childPath = ZKPaths.makePath(zkPath, child);
        try {
            final byte[] data;
            try {
                data = cf.getData().decompressed().forPath(childPath);
            } catch (Exception e) {
                throw new IOException("Error getting data for " + childPath, e);
            }
            if (data == null) {
                LOG.debug("Lost data at path [%s]", childPath);
                continue;
            }
            final HostAndPort hostAndPort = HostAndPort.fromString(child);
            final Long l = ByteBuffer.wrap(data).getLong();
            retVal.put(hostAndPort, l);
        } catch (IllegalArgumentException iae) {
            LOG.warn(iae, "Error parsing [%s]", childPath);
        }
    }
    return ImmutableMap.copyOf(retVal);
}

From source file:com.facebook.presto.cli.ClientOptions.java

public static URI parseServer(String server) {
    server = server.toLowerCase(ENGLISH);
    if (server.startsWith("http://") || server.startsWith("https://")) {
        return URI.create(server);
    }/*w w w. jav  a2s  .  c o  m*/

    HostAndPort host = HostAndPort.fromString(server);
    try {
        return new URI("http", null, host.getHostText(), host.getPortOrDefault(80), null, null, null);
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.mpush.client.gateway.connection.GatewayTCPConnectionFactory.java

private void reconnect(Connection connection, String hostAndPort) {
    HostAndPort h_p = HostAndPort.fromString(hostAndPort);
    connections.get(hostAndPort).remove(connection);
    connection.close();//ww w  . j a  va 2s .c  o  m
    addConnection(h_p.getHost(), h_p.getPort(), false);
}

From source file:org.graylog2.configuration.MongoDbConfiguration.java

@Deprecated
public List<ServerAddress> getReplicaSet() {
    if (replicaSet == null || replicaSet.isEmpty()) {
        return null;
    }/*from  w  w w.j a va 2s .  c  o m*/

    final List<ServerAddress> replicaServers = new ArrayList<>(replicaSet.size());
    for (String host : replicaSet) {
        try {
            final HostAndPort hostAndPort = HostAndPort.fromString(host).withDefaultPort(27017);
            replicaServers.add(
                    new ServerAddress(InetAddress.getByName(hostAndPort.getHostText()), hostAndPort.getPort()));
        } catch (IllegalArgumentException e) {
            LOG.error("Malformed mongodb_replica_set configuration.", e);
            return null;
        } catch (UnknownHostException e) {
            LOG.error("Unknown host in mongodb_replica_set", e);
            return null;
        }
    }

    return replicaServers;
}

From source file:de.qaware.playground.zwitscher.util.servicediscovery.impl.ConsulFabioServiceDiscovery.java

public static HostAndPort getConsulHostAndPort() {
    String consulEnv = System.getenv(CONSUL_ENVVAR);
    if (consulEnv == null)
        return HostAndPort.fromString(CONSUL_DEFAULT_HOSTANDPORT);
    else/*from ww  w.  j  a va 2 s.  c  om*/
        return HostAndPort.fromString(consulEnv);
}

From source file:org.apache.brooklyn.util.javalang.coerce.CommonAdaptorTypeCoercions.java

@SuppressWarnings("rawtypes")
public void registerStandardAdapters() {
    registerAdapter(CharSequence.class, String.class, new Function<CharSequence, String>() {
        @Override//from  w ww. ja va 2s  .c o  m
        public String apply(CharSequence input) {
            return input.toString();
        }
    });
    registerAdapter(byte[].class, String.class, new Function<byte[], String>() {
        @Override
        public String apply(byte[] input) {
            return new String(input);
        }
    });
    registerAdapter(Collection.class, Set.class, new Function<Collection, Set>() {
        @SuppressWarnings("unchecked")
        @Override
        public Set apply(Collection input) {
            return Sets.newLinkedHashSet(input);
        }
    });
    registerAdapter(Collection.class, List.class, new Function<Collection, List>() {
        @SuppressWarnings("unchecked")
        @Override
        public List apply(Collection input) {
            return Lists.newArrayList(input);
        }
    });
    registerAdapter(String.class, InetAddress.class, new Function<String, InetAddress>() {
        @Override
        public InetAddress apply(String input) {
            return Networking.getInetAddressWithFixedName(input);
        }
    });
    registerAdapter(String.class, HostAndPort.class, new Function<String, HostAndPort>() {
        @Override
        public HostAndPort apply(String input) {
            return HostAndPort.fromString(input);
        }
    });
    registerAdapter(String.class, UserAndHostAndPort.class, new Function<String, UserAndHostAndPort>() {
        @Override
        public UserAndHostAndPort apply(String input) {
            return UserAndHostAndPort.fromString(input);
        }
    });
    registerAdapter(String.class, Cidr.class, new Function<String, Cidr>() {
        @Override
        public Cidr apply(String input) {
            return new Cidr(input);
        }
    });
    registerAdapter(String.class, URL.class, new Function<String, URL>() {
        @Override
        public URL apply(String input) {
            try {
                return new URL(input);
            } catch (Exception e) {
                throw Exceptions.propagate(e);
            }
        }
    });
    registerAdapter(URL.class, String.class, new Function<URL, String>() {
        @Override
        public String apply(URL input) {
            return input.toString();
        }
    });
    registerAdapter(String.class, URI.class, new Function<String, URI>() {
        @Override
        public URI apply(String input) {
            return URI.create(input);
        }
    });
    registerAdapter(URI.class, String.class, new Function<URI, String>() {
        @Override
        public String apply(URI input) {
            return input.toString();
        }
    });
    registerAdapter(Object.class, Duration.class, new Function<Object, Duration>() {
        @Override
        public Duration apply(final Object input) {
            return org.apache.brooklyn.util.time.Duration.of(input);
        }
    });

    registerAdapter(Integer.class, AtomicLong.class, new Function<Integer, AtomicLong>() {
        @Override
        public AtomicLong apply(final Integer input) {
            return new AtomicLong(input);
        }
    });
    registerAdapter(Long.class, AtomicLong.class, new Function<Long, AtomicLong>() {
        @Override
        public AtomicLong apply(final Long input) {
            return new AtomicLong(input);
        }
    });
    registerAdapter(String.class, AtomicLong.class, new Function<String, AtomicLong>() {
        @Override
        public AtomicLong apply(final String input) {
            return new AtomicLong(Long.parseLong(input.trim()));
        }
    });
    registerAdapter(Integer.class, AtomicInteger.class, new Function<Integer, AtomicInteger>() {
        @Override
        public AtomicInteger apply(final Integer input) {
            return new AtomicInteger(input);
        }
    });
    registerAdapter(String.class, AtomicInteger.class, new Function<String, AtomicInteger>() {
        @Override
        public AtomicInteger apply(final String input) {
            return new AtomicInteger(Integer.parseInt(input.trim()));
        }
    });
    /** This always returns a {@link Double}, cast as a {@link Number}; 
     * however primitives and boxers get exact typing due to call in #stringToPrimitive */
    registerAdapter(String.class, Number.class, new Function<String, Number>() {
        @Override
        public Number apply(String input) {
            return Double.valueOf(input);
        }
    });
    registerAdapter(BigDecimal.class, Double.class, new Function<BigDecimal, Double>() {
        @Override
        public Double apply(BigDecimal input) {
            return input.doubleValue();
        }
    });
    registerAdapter(BigInteger.class, Long.class, new Function<BigInteger, Long>() {
        @Override
        public Long apply(BigInteger input) {
            return input.longValue();
        }
    });
    registerAdapter(BigInteger.class, Integer.class, new Function<BigInteger, Integer>() {
        @Override
        public Integer apply(BigInteger input) {
            return input.intValue();
        }
    });
    registerAdapter(String.class, BigDecimal.class, new Function<String, BigDecimal>() {
        @Override
        public BigDecimal apply(String input) {
            return new BigDecimal(input);
        }
    });
    registerAdapter(Double.class, BigDecimal.class, new Function<Double, BigDecimal>() {
        @Override
        public BigDecimal apply(Double input) {
            return BigDecimal.valueOf(input);
        }
    });
    registerAdapter(String.class, BigInteger.class, new Function<String, BigInteger>() {
        @Override
        public BigInteger apply(String input) {
            return new BigInteger(input);
        }
    });
    registerAdapter(Long.class, BigInteger.class, new Function<Long, BigInteger>() {
        @Override
        public BigInteger apply(Long input) {
            return BigInteger.valueOf(input);
        }
    });
    registerAdapter(Integer.class, BigInteger.class, new Function<Integer, BigInteger>() {
        @Override
        public BigInteger apply(Integer input) {
            return BigInteger.valueOf(input);
        }
    });
    registerAdapter(String.class, Date.class, new Function<String, Date>() {
        @Override
        public Date apply(final String input) {
            return Time.parseDate(input);
        }
    });
    registerAdapter(String.class, QuorumCheck.class, new Function<String, QuorumCheck>() {
        @Override
        public QuorumCheck apply(final String input) {
            return QuorumChecks.of(input);
        }
    });
}

From source file:zipkin.storage.cassandra3.DefaultSessionFactory.java

static List<InetSocketAddress> parseContactPoints(Cassandra3Storage cassandra) {
    List<InetSocketAddress> result = new LinkedList<>();
    for (String contactPoint : cassandra.contactPoints.split(",")) {
        HostAndPort parsed = HostAndPort.fromString(contactPoint);
        result.add(new InetSocketAddress(parsed.getHostText(), parsed.getPortOrDefault(9042)));
    }/*from   ww w.ja va2s .  c  o m*/
    return result;
}

From source file:ratpack.server.internal.DefaultPublicAddress.java

private HostAndPort getForwardedHostData(Context context) {
    Headers headers = context.getRequest().getHeaders();
    String forwardedHostHeader = Strings.emptyToNull(headers.get(X_FORWARDED_HOST.toString()));
    String hostPortString = forwardedHostHeader != null
            ? Iterables.getFirst(FORWARDED_HOST_SPLITTER.split(forwardedHostHeader), null)
            : null;//  w  w w  .ja v a2 s . c o  m
    return hostPortString != null ? HostAndPort.fromString(hostPortString) : null;
}

From source file:com.pingcap.tikv.RegionStoreClient.java

public static RegionStoreClient create(Region region, Store store, TiSession session) {
    RegionStoreClient client = null;// ww  w  . j  a v  a2s  . com
    try {
        HostAndPort address = HostAndPort.fromString(store.getAddress());
        ManagedChannel channel = ManagedChannelBuilder.forAddress(address.getHostText(), address.getPort())
                .usePlaintext(true).build();
        TiKVBlockingStub blockingStub = TiKVGrpc.newBlockingStub(channel);
        TiKVStub asyncStub = TiKVGrpc.newStub(channel);
        client = new RegionStoreClient(region, session, channel, blockingStub, asyncStub);
    } catch (Exception e) {
        if (client != null) {
            try {
                client.close();
            } catch (Exception ignore) {
            }
        }
    }
    return client;
}