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.mqtt;
018
019import java.io.IOException;
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.net.ssl.SSLServerSocketFactory;
026
027import org.apache.activemq.broker.BrokerService;
028import org.apache.activemq.broker.BrokerServiceAware;
029import org.apache.activemq.transport.MutexTransport;
030import org.apache.activemq.transport.Transport;
031import org.apache.activemq.transport.tcp.SslTransportFactory;
032import org.apache.activemq.transport.tcp.SslTransportServer;
033import org.apache.activemq.util.IntrospectionSupport;
034import org.apache.activemq.wireformat.WireFormat;
035
036/**
037 * A <a href="http://mqtt.org/">MQTT</a> over SSL transport factory
038 */
039public class MQTTSslTransportFactory extends SslTransportFactory implements BrokerServiceAware {
040
041    private BrokerService brokerService = null;
042
043    protected String getDefaultWireFormatType() {
044        return "mqtt";
045    }
046
047    @SuppressWarnings("rawtypes")
048
049    public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
050        transport = new MQTTTransportFilter(transport, format, brokerService);
051        IntrospectionSupport.setProperties(transport, options);
052        return super.compositeConfigure(transport, format, options);
053    }
054
055    @Override
056    protected SslTransportServer createSslTransportServer(URI location, SSLServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
057        final SslTransportServer server = super.createSslTransportServer(location, serverSocketFactory);
058        server.setAllowLinkStealing(true);
059        return server;
060    }
061
062    @SuppressWarnings("rawtypes")
063    @Override
064    public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception {
065        transport = super.serverConfigure(transport, format, options);
066
067        MutexTransport mutex = transport.narrow(MutexTransport.class);
068        if (mutex != null) {
069            mutex.setSyncOnCommand(true);
070        }
071        return transport;
072    }
073
074    public void setBrokerService(BrokerService brokerService) {
075        this.brokerService = brokerService;
076    }
077
078    protected Transport createInactivityMonitor(Transport transport, WireFormat format) {
079        MQTTInactivityMonitor monitor = new MQTTInactivityMonitor(transport, format);
080
081        MQTTTransportFilter filter = transport.narrow(MQTTTransportFilter.class);
082        filter.setInactivityMonitor(monitor);
083
084        return monitor;
085    }
086
087}