Search in sources :

Example 6 with ServerSocketFactory

use of javax.net.ServerSocketFactory in project robovm by robovm.

the class SSLContextTest method test_SSLContext_getServerSocketFactory.

public void test_SSLContext_getServerSocketFactory() throws Exception {
    for (String protocol : StandardNames.SSL_CONTEXT_PROTOCOLS) {
        if (protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
            SSLContext.getInstance(protocol).getServerSocketFactory();
        } else {
            try {
                SSLContext.getInstance(protocol).getServerSocketFactory();
                fail();
            } catch (IllegalStateException expected) {
            }
        }
        SSLContext sslContext = SSLContext.getInstance(protocol);
        if (!protocol.equals(StandardNames.SSL_CONTEXT_PROTOCOLS_DEFAULT)) {
            sslContext.init(null, null, null);
        }
        ServerSocketFactory ssf = sslContext.getServerSocketFactory();
        assertNotNull(ssf);
        assertTrue(SSLServerSocketFactory.class.isAssignableFrom(ssf.getClass()));
    }
}
Also used : ServerSocketFactory(javax.net.ServerSocketFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLContext(javax.net.ssl.SSLContext)

Example 7 with ServerSocketFactory

use of javax.net.ServerSocketFactory in project robovm by robovm.

the class MyServerSocketFactory method test_createServerSocket.

public final void test_createServerSocket() throws Exception {
    ServerSocketFactory sf = ServerSocketFactory.getDefault();
    ServerSocket ss = sf.createServerSocket();
    assertNotNull(ss);
    ss.close();
}
Also used : ServerSocketFactory(javax.net.ServerSocketFactory) ServerSocket(java.net.ServerSocket)

Example 8 with ServerSocketFactory

use of javax.net.ServerSocketFactory in project geode by apache.

the class SecurityTestUtils method clearStaticSSLContext.

/**
   * This is a hack using reflection to clear the static objects in JSSE since otherwise changing
   * the javax.* store related properties has no effect during the course of running dunit suite
   * unless the VMs are restarted.
   */
protected static void clearStaticSSLContext() {
    ServerSocketFactory defaultServerFact = SSLServerSocketFactory.getDefault();
    // Get the class of this and use reflection to blank out any static SSLContext objects inside
    Map<Field, Object> contextMap = getSSLFields(defaultServerFact, new Class[] { SSLContext.class, SSLContextSpi.class });
    makeNullSSLFields(defaultServerFact, contextMap);
    for (Iterator contextObjsIter = contextMap.values().iterator(); contextObjsIter.hasNext(); ) {
        Object contextObj = contextObjsIter.next();
        Map<Field, Object> contextObjsMap = getSSLFields(contextObj, new Class[] { TrustManager.class, KeyManager.class, TrustManager[].class, KeyManager[].class });
        makeNullSSLFields(contextObj, contextObjsMap);
    }
    makeNullStaticField(SSLServerSocketFactory.class);
    // Do the same for normal SSL socket factory
    SocketFactory defaultFact = SSLSocketFactory.getDefault();
    contextMap = getSSLFields(defaultFact, new Class[] { SSLContext.class, SSLContextSpi.class });
    makeNullSSLFields(defaultFact, contextMap);
    for (Iterator contextObjsIter = contextMap.values().iterator(); contextObjsIter.hasNext(); ) {
        Object contextObj = contextObjsIter.next();
        Map<Field, Object> contextObjsMap = getSSLFields(contextObj, new Class[] { TrustManager.class, KeyManager.class, TrustManager[].class, KeyManager[].class });
        makeNullSSLFields(contextObj, contextObjsMap);
    }
    makeNullStaticField(SSLSocketFactory.class);
    makeNullStaticField(SSLContext.class);
}
Also used : Field(java.lang.reflect.Field) SSLContextSpi(javax.net.ssl.SSLContextSpi) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) Iterator(java.util.Iterator) SSLContext(javax.net.ssl.SSLContext) KeyManager(javax.net.ssl.KeyManager) TrustManager(javax.net.ssl.TrustManager)

Example 9 with ServerSocketFactory

use of javax.net.ServerSocketFactory in project tomee by apache.

the class ServiceDaemon method start.

@Override
public void start() throws ServiceException {
    synchronized (this) {
        // Don't bother if we are already started/starting
        if (this.socketListener != null) {
            return;
        }
        this.next.start();
        final ServerSocket serverSocket;
        try {
            if (this.secure) {
                final ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
                serverSocket = factory.createServerSocket(this.port, this.backlog, this.inetAddress);
                ((SSLServerSocket) serverSocket).setEnabledCipherSuites(this.enabledCipherSuites);
            } else {
                serverSocket = new ServerSocket();
                serverSocket.setReuseAddress(true);
                try {
                    serverSocket.bind(new InetSocketAddress(this.inetAddress, this.port), this.backlog);
                } catch (final BindException e) {
                    //One retry - Port may be closing
                    Thread.sleep(1000);
                    serverSocket.bind(new InetSocketAddress(this.inetAddress, this.port), this.backlog);
                }
            }
            serverSocket.setSoTimeout(this.timeout);
            int serverPort = serverSocket.getLocalPort();
            if (this.port == 0 && next.getName() != null) {
                SystemInstance.get().getProperties().put(next.getName() + ".port", Integer.toString(serverPort));
                this.port = serverPort;
            }
        } catch (Exception e) {
            throw new ServiceException("Service failed to open socket", e);
        }
        this.socketListener = new SocketListener(this.next, serverSocket);
        final Thread thread = new Thread(this.socketListener);
        thread.setName("Service." + this.getName() + "@" + this.socketListener.hashCode());
        thread.setDaemon(true);
        thread.start();
        final DiscoveryAgent agent = SystemInstance.get().getComponent(DiscoveryAgent.class);
        if (agent != null && this.discoveryUriFormat != null) {
            final Map<String, String> map = new HashMap<String, String>();
            // add all the properties that were used to construct this service
            for (final Map.Entry<Object, Object> entry : this.props.entrySet()) {
                map.put(entry.getKey().toString(), entry.getValue().toString());
            }
            map.put("port", Integer.toString(this.port));
            String address = this.ip;
            if ("0.0.0.0".equals(address)) {
                try {
                    address = InetAddress.getLocalHost().getHostAddress();
                } catch (UnknownHostException e) {
                    log.error("Failed to resolve 0.0.0.0 to a routable address", e);
                }
            }
            map.put("host", address);
            map.put("bind", address);
            final String uriString = this.discoveryUriFormat.apply(map);
            try {
                this.serviceUri = new URI(uriString);
                agent.registerService(this.serviceUri);
            } catch (Exception e) {
                log.error("Cannot register service '" + this.getName() + "' with DiscoveryAgent.", e);
            }
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) SSLServerSocketFactory(javax.net.ssl.SSLServerSocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) InetSocketAddress(java.net.InetSocketAddress) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URI(java.net.URI) BindException(java.net.BindException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ServerSocketFactory (javax.net.ServerSocketFactory)9 ServerSocket (java.net.ServerSocket)6 IOException (java.io.IOException)4 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)4 SSLServerSocket (javax.net.ssl.SSLServerSocket)3 BindException (java.net.BindException)2 InetSocketAddress (java.net.InetSocketAddress)2 SSLContext (javax.net.ssl.SSLContext)2 Field (java.lang.reflect.Field)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 SocketFactory (javax.net.SocketFactory)1 KeyManager (javax.net.ssl.KeyManager)1 SSLContextSpi (javax.net.ssl.SSLContextSpi)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1