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.auto.nio; 018 019import java.io.IOException; 020import java.net.Socket; 021import java.net.URI; 022import java.net.URISyntaxException; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Set; 026 027import javax.net.ServerSocketFactory; 028 029import org.apache.activemq.broker.BrokerService; 030import org.apache.activemq.broker.BrokerServiceAware; 031import org.apache.activemq.openwire.OpenWireFormatFactory; 032import org.apache.activemq.transport.Transport; 033import org.apache.activemq.transport.TransportServer; 034import org.apache.activemq.transport.auto.AutoTcpTransportServer; 035import org.apache.activemq.transport.auto.AutoTransportUtils; 036import org.apache.activemq.transport.nio.NIOTransport; 037import org.apache.activemq.transport.nio.NIOTransportFactory; 038import org.apache.activemq.transport.tcp.TcpTransport; 039import org.apache.activemq.transport.tcp.TcpTransportFactory; 040import org.apache.activemq.util.IOExceptionSupport; 041import org.apache.activemq.util.IntrospectionSupport; 042import org.apache.activemq.util.URISupport; 043import org.apache.activemq.wireformat.WireFormat; 044 045/** 046 * 047 * 048 */ 049public class AutoNioTransportFactory extends NIOTransportFactory implements BrokerServiceAware { 050 protected BrokerService brokerService; 051 /* (non-Javadoc) 052 * @see org.apache.activemq.broker.BrokerServiceAware#setBrokerService(org.apache.activemq.broker.BrokerService) 053 */ 054 @Override 055 public void setBrokerService(BrokerService brokerService) { 056 this.brokerService = brokerService; 057 } 058 059 @Override 060 protected AutoTcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 061 return new AutoTcpTransportServer(this, location, serverSocketFactory, brokerService, enabledProtocols) { 062 @Override 063 protected TcpTransport createTransport(Socket socket, WireFormat format, TcpTransportFactory detectedTransportFactory) throws IOException { 064 TcpTransport nioTransport = null; 065 if (detectedTransportFactory.getClass().equals(NIOTransportFactory.class)) { 066 nioTransport = new AutoNIOTransport(format, socket,this.initBuffer); 067 } else { 068 nioTransport = detectedTransportFactory.createTransport( 069 format, socket, this.initBuffer); 070 } 071 072 if (format.getClass().toString().contains("MQTT")) { 073 if (!allowLinkStealingSet) { 074 this.setAllowLinkStealing(true); 075 } 076 } 077 078 return nioTransport; 079 } 080 }; 081 082 } 083 084 boolean allowLinkStealingSet = false; 085 private Set<String> enabledProtocols; 086 087 @Override 088 public TransportServer doBind(final URI location) throws IOException { 089 try { 090 Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location)); 091 092 Map<String, Object> autoProperties = IntrospectionSupport.extractProperties(options, "auto."); 093 this.enabledProtocols = AutoTransportUtils.parseProtocols((String) autoProperties.get("protocols")); 094 095 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 096 AutoTcpTransportServer server = createTcpTransportServer(location, serverSocketFactory); 097 //server.setWireFormatFactory(createWireFormatFactory(options)); 098 server.setWireFormatFactory(new OpenWireFormatFactory()); 099 if (options.get("allowLinkStealing") != null){ 100 allowLinkStealingSet = true; 101 } 102 IntrospectionSupport.setProperties(server, options); 103 server.setTransportOption(IntrospectionSupport.extractProperties(options, "transport.")); 104 server.setWireFormatOptions(AutoTransportUtils.extractWireFormatOptions(options)); 105 server.bind(); 106 107 return server; 108 } catch (URISyntaxException e) { 109 throw IOExceptionSupport.create(e); 110 } 111 } 112 113 114}