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.Socket; 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.net.UnknownHostException; 024import java.util.HashMap; 025import java.util.Map; 026 027import javax.net.ServerSocketFactory; 028import javax.net.SocketFactory; 029 030import org.apache.activemq.broker.BrokerService; 031import org.apache.activemq.broker.BrokerServiceAware; 032import org.apache.activemq.transport.MutexTransport; 033import org.apache.activemq.transport.Transport; 034import org.apache.activemq.transport.nio.NIOTransportFactory; 035import org.apache.activemq.transport.tcp.TcpTransport; 036import org.apache.activemq.transport.tcp.TcpTransportServer; 037import org.apache.activemq.util.IntrospectionSupport; 038import org.apache.activemq.wireformat.WireFormat; 039 040/** 041 * A <a href="http://mqtt.org/">MQTT</a> over NIO transport factory 042 */ 043public class MQTTNIOTransportFactory extends NIOTransportFactory implements BrokerServiceAware { 044 045 private BrokerService brokerService = null; 046 047 @Override 048 protected String getDefaultWireFormatType() { 049 return "mqtt"; 050 } 051 052 @Override 053 protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 054 TcpTransportServer result = new TcpTransportServer(this, location, serverSocketFactory) { 055 @Override 056 protected Transport createTransport(Socket socket, WireFormat format) throws IOException { 057 return new MQTTNIOTransport(format, socket); 058 } 059 }; 060 result.setAllowLinkStealing(true); 061 return result; 062 } 063 064 @Override 065 protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException { 066 return new MQTTNIOTransport(wf, socketFactory, location, localLocation); 067 } 068 069 @SuppressWarnings("rawtypes") 070 @Override 071 public Transport serverConfigure(Transport transport, WireFormat format, HashMap options) throws Exception { 072 transport = super.serverConfigure(transport, format, options); 073 074 MutexTransport mutex = transport.narrow(MutexTransport.class); 075 if (mutex != null) { 076 mutex.setSyncOnCommand(true); 077 } 078 079 return transport; 080 } 081 082 @Override 083 @SuppressWarnings("rawtypes") 084 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 085 transport = new MQTTTransportFilter(transport, format, brokerService); 086 IntrospectionSupport.setProperties(transport, options); 087 return super.compositeConfigure(transport, format, options); 088 } 089 090 @Override 091 public void setBrokerService(BrokerService brokerService) { 092 this.brokerService = brokerService; 093 } 094 095 @Override 096 protected Transport createInactivityMonitor(Transport transport, WireFormat format) { 097 MQTTInactivityMonitor monitor = new MQTTInactivityMonitor(transport, format); 098 MQTTTransportFilter filter = transport.narrow(MQTTTransportFilter.class); 099 filter.setInactivityMonitor(monitor); 100 return monitor; 101 } 102} 103