001    /**
002     * Copyright (C) 2010-2011, FuseSource Corp.  All rights reserved.
003     *
004     *     http://fusesource.com
005     *
006     * The software in this package is published under the terms of the
007     * CDDL license a copy of which has been included with this distribution
008     * in the license.txt file.
009     */
010    package org.fusesource.hawtdispatch.transport;
011    
012    import javax.net.ssl.KeyManager;
013    import javax.net.ssl.SSLContext;
014    import javax.net.ssl.TrustManager;
015    import java.net.URI;
016    import java.net.UnknownHostException;
017    import java.util.concurrent.Executor;
018    import java.security.NoSuchAlgorithmException;
019    
020    /**
021     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
022     */
023    
024    public class SslTransportServer extends TcpTransportServer {
025    
026        public static SslTransportServer createTransportServer(URI uri) throws Exception {
027            SslTransportServer rc = new SslTransportServer(uri);
028            rc.setSSLContext(SSLContext.getInstance(SslTransport.protocol(uri.getScheme())));
029            return rc;
030        }
031    
032        protected KeyManager[] keyManagers;
033        private TrustManager[] trustManagers;
034        protected String protocol = "TLS";
035        protected SSLContext sslContext;
036        protected Executor blockingExecutor;
037    
038        public SslTransportServer(URI location) throws UnknownHostException {
039            super(location);
040        }
041    
042        public void setKeyManagers(KeyManager[] keyManagers) {
043            this.keyManagers = keyManagers;
044        }
045        public void setTrustManagers(TrustManager[] trustManagers) {
046            this.trustManagers = trustManagers;
047        }
048    
049        public void start(Runnable onCompleted) throws Exception {
050            if( keyManagers!=null ) {
051                sslContext.init(keyManagers, trustManagers, null);
052            } else {
053                sslContext = SSLContext.getDefault();
054            }
055            super.start(onCompleted);
056        }
057    
058        protected TcpTransport createTransport() {
059            SslTransport rc = new SslTransport();
060            rc.setSSLContext(sslContext);
061            rc.setBlockingExecutor(blockingExecutor);
062            return rc;
063        }
064    
065        public SslTransportServer protocol(String value) throws NoSuchAlgorithmException {
066            this.protocol = value;
067            sslContext = SSLContext.getInstance(protocol);
068            return this;
069        }
070    
071        public SSLContext getSSLContext() {
072            return sslContext;
073        }
074    
075        public void setSSLContext(SSLContext sslContext) {
076            this.sslContext = sslContext;
077        }
078    
079        public Executor getBlockingExecutor() {
080            return blockingExecutor;
081        }
082    
083        public void setBlockingExecutor(Executor blockingExecutor) {
084            this.blockingExecutor = blockingExecutor;
085        }
086    
087    }