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; 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.TransportServer; 033import org.apache.activemq.transport.tcp.TcpTransport; 034import org.apache.activemq.transport.tcp.TcpTransportFactory; 035import org.apache.activemq.util.IOExceptionSupport; 036import org.apache.activemq.util.IntrospectionSupport; 037import org.apache.activemq.util.URISupport; 038import org.apache.activemq.wireformat.WireFormat; 039 040/** 041 * 042 * 043 */ 044public class AutoTcpTransportFactory extends TcpTransportFactory implements BrokerServiceAware { 045 046 protected BrokerService brokerService; 047 /* (non-Javadoc) 048 * @see org.apache.activemq.broker.BrokerServiceAware#setBrokerService(org.apache.activemq.broker.BrokerService) 049 */ 050 @Override 051 public void setBrokerService(BrokerService brokerService) { 052 this.brokerService = brokerService; 053 } 054 055 056 @Override 057 public TransportServer doBind(final URI location) throws IOException { 058 try { 059 Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location)); 060 061 Map<String, Object> autoProperties = IntrospectionSupport.extractProperties(options, "auto."); 062 this.enabledProtocols = AutoTransportUtils.parseProtocols((String) autoProperties.get("protocols")); 063 064 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 065 AutoTcpTransportServer server = createTcpTransportServer(location, serverSocketFactory); 066 //server.setWireFormatFactory(createWireFormatFactory(options)); 067 server.setWireFormatFactory(new OpenWireFormatFactory()); 068 if (options.get("allowLinkStealing") != null){ 069 allowLinkStealingSet = true; 070 } 071 IntrospectionSupport.setProperties(server, options); 072 server.setTransportOption(IntrospectionSupport.extractProperties(options, "transport.")); 073 server.setWireFormatOptions(AutoTransportUtils.extractWireFormatOptions(options)); 074 server.bind(); 075 076 return server; 077 } catch (URISyntaxException e) { 078 throw IOExceptionSupport.create(e); 079 } 080 } 081 082 boolean allowLinkStealingSet = false; 083 private Set<String> enabledProtocols; 084 085 @Override 086 protected AutoTcpTransportServer createTcpTransportServer(final URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 087 AutoTcpTransportServer server = new AutoTcpTransportServer(this, location, serverSocketFactory, brokerService, enabledProtocols) { 088 089 @Override 090 protected TcpTransport createTransport(Socket socket, WireFormat format) 091 throws IOException { 092 if (format.getClass().toString().contains("MQTT") && !allowLinkStealingSet) { 093 this.setAllowLinkStealing(true); 094 } 095 return super.createTransport(socket, format); 096 } 097 098 }; 099 100 return server; 101 } 102}