Search in sources :

Example 1 with SocketChannel

use of com.datastax.shaded.netty.channel.socket.SocketChannel in project cassandra by apache.

the class BulkLoader method buildSSLOptions.

private static SSLOptions buildSSLOptions(EncryptionOptions clientEncryptionOptions) {
    if (!clientEncryptionOptions.isEnabled()) {
        return null;
    }
    SSLContext sslContext;
    try {
        sslContext = SSLFactory.createSSLContext(clientEncryptionOptions, true);
    } catch (IOException e) {
        throw new RuntimeException("Could not create SSL Context.", e);
    }
    // Temporarily override newSSLEngine to set accepted protocols until it is added to
    // RemoteEndpointAwareJdkSSLOptions.  See CASSANDRA-13325 and CASSANDRA-16362.
    RemoteEndpointAwareJdkSSLOptions sslOptions = new RemoteEndpointAwareJdkSSLOptions(sslContext, null) {

        protected SSLEngine newSSLEngine(SocketChannel channel, InetSocketAddress remoteEndpoint) {
            SSLEngine engine = super.newSSLEngine(channel, remoteEndpoint);
            String[] acceptedProtocols = clientEncryptionOptions.acceptedProtocolsArray();
            if (acceptedProtocols != null && acceptedProtocols.length > 0)
                engine.setEnabledProtocols(acceptedProtocols);
            return engine;
        }
    };
    return sslOptions;
}
Also used : RemoteEndpointAwareJdkSSLOptions(com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions) SocketChannel(com.datastax.shaded.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException)

Example 2 with SocketChannel

use of com.datastax.shaded.netty.channel.socket.SocketChannel in project cassandra by apache.

the class JavaDriverClient method connect.

public void connect(ProtocolOptions.Compression compression) throws Exception {
    PoolingOptions poolingOpts = new PoolingOptions().setConnectionsPerHost(HostDistance.LOCAL, connectionsPerHost, connectionsPerHost).setMaxRequestsPerConnection(HostDistance.LOCAL, maxPendingPerConnection).setNewConnectionThreshold(HostDistance.LOCAL, 100);
    HostAndPort hap = HostAndPort.fromString(host).withDefaultPort(port);
    InetSocketAddress contact = new InetSocketAddress(InetAddress.getByName(hap.getHost()), hap.getPort());
    Cluster.Builder clusterBuilder = Cluster.builder().addContactPointsWithPorts(contact).withPoolingOptions(poolingOpts).withoutJMXReporting().withProtocolVersion(protocolVersion).withoutMetrics();
    if (loadBalancingPolicy != null)
        clusterBuilder.withLoadBalancingPolicy(loadBalancingPolicy);
    clusterBuilder.withCompression(compression);
    if (encryptionOptions.isEnabled()) {
        SSLContext sslContext;
        sslContext = SSLFactory.createSSLContext(encryptionOptions, true);
        // Temporarily override newSSLEngine to set accepted protocols until it is added to
        // RemoteEndpointAwareJdkSSLOptions.  See CASSANDRA-13325 and CASSANDRA-16362.
        RemoteEndpointAwareJdkSSLOptions sslOptions = new RemoteEndpointAwareJdkSSLOptions(sslContext, null) {

            protected SSLEngine newSSLEngine(SocketChannel channel, InetSocketAddress remoteEndpoint) {
                SSLEngine engine = super.newSSLEngine(channel, remoteEndpoint);
                String[] acceptedProtocols = encryptionOptions.acceptedProtocolsArray();
                if (acceptedProtocols != null && acceptedProtocols.length > 0)
                    engine.setEnabledProtocols(acceptedProtocols);
                return engine;
            }
        };
        clusterBuilder.withSSL(sslOptions);
    }
    if (authProvider != null) {
        clusterBuilder.withAuthProvider(authProvider);
    } else if (username != null) {
        clusterBuilder.withCredentials(username, password);
    }
    cluster = clusterBuilder.build();
    Metadata metadata = cluster.getMetadata();
    System.out.printf("Connected to cluster: %s, max pending requests per connection %d, max connections per host %d%n", metadata.getClusterName(), maxPendingPerConnection, connectionsPerHost);
    for (Host host : metadata.getAllHosts()) {
        System.out.printf("Datacenter: %s; Host: %s; Rack: %s%n", host.getDatacenter(), host.getAddress() + ":" + host.getSocketAddress().getPort(), host.getRack());
    }
    session = cluster.connect();
}
Also used : SocketChannel(com.datastax.shaded.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) SSLContext(javax.net.ssl.SSLContext) HostAndPort(com.google.common.net.HostAndPort)

Aggregations

SocketChannel (com.datastax.shaded.netty.channel.socket.SocketChannel)2 InetSocketAddress (java.net.InetSocketAddress)2 SSLContext (javax.net.ssl.SSLContext)2 SSLEngine (javax.net.ssl.SSLEngine)2 RemoteEndpointAwareJdkSSLOptions (com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions)1 HostAndPort (com.google.common.net.HostAndPort)1 IOException (java.io.IOException)1