Search in sources :

Example 61 with SSLServerSocket

use of javax.net.ssl.SSLServerSocket 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

SSLServerSocket (javax.net.ssl.SSLServerSocket)61 SSLContext (javax.net.ssl.SSLContext)23 SSLSocket (javax.net.ssl.SSLSocket)19 InetSocketAddress (java.net.InetSocketAddress)14 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)13 IOException (java.io.IOException)12 ServerSocket (java.net.ServerSocket)10 URL (java.net.URL)10 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)10 SSLEngine (javax.net.ssl.SSLEngine)9 UnknownHostException (java.net.UnknownHostException)7 Proxy (java.net.Proxy)6 Test (org.junit.Test)6 InetAddress (java.net.InetAddress)5 Method (java.lang.reflect.Method)3 KeyManagementException (java.security.KeyManagementException)3 KeyStore (java.security.KeyStore)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ServerSocketFactory (javax.net.ServerSocketFactory)3 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)3