001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.stomp;
018
019import org.apache.activemq.broker.SslContext;
020import org.apache.activemq.transport.Transport;
021import org.apache.activemq.transport.TransportServer;
022import org.apache.activemq.transport.tcp.TcpTransport;
023import org.apache.activemq.transport.tcp.TcpTransportServer;
024import org.apache.activemq.wireformat.WireFormat;
025
026import javax.net.ServerSocketFactory;
027import javax.net.SocketFactory;
028import javax.net.ssl.SSLContext;
029import java.io.IOException;
030import java.net.Socket;
031import java.net.URI;
032import java.net.URISyntaxException;
033import java.net.UnknownHostException;
034
035public class StompNIOSSLTransportFactory extends StompNIOTransportFactory {
036
037    SSLContext context;
038
039    @Override
040    protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
041        return new TcpTransportServer(this, location, serverSocketFactory) {
042            protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
043                StompNIOSSLTransport transport = new StompNIOSSLTransport(format, socket);
044                if (context != null) {
045                    transport.setSslContext(context);
046                }
047                return transport;
048            }
049        };
050    }
051
052    @Override
053    protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
054        return new StompNIOSSLTransport(wf, socketFactory, location, localLocation);
055    }
056
057    @Override
058    public TransportServer doBind(URI location) throws IOException {
059       if (SslContext.getCurrentSslContext() != null) {
060            try {
061                context = SslContext.getCurrentSslContext().getSSLContext();
062            } catch (Exception e) {
063                throw new IOException(e);
064            }
065        }
066        return super.doBind(location);
067    }
068
069}