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