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.tcp; 018 019import java.io.IOException; 020import java.net.URI; 021import java.net.URISyntaxException; 022import java.net.UnknownHostException; 023import java.util.HashMap; 024import java.util.Map; 025 026import javax.net.ServerSocketFactory; 027import javax.net.SocketFactory; 028 029import org.apache.activemq.TransportLoggerSupport; 030import org.apache.activemq.openwire.OpenWireFormat; 031import org.apache.activemq.transport.InactivityMonitor; 032import org.apache.activemq.transport.Transport; 033import org.apache.activemq.transport.TransportFactory; 034import org.apache.activemq.transport.TransportServer; 035import org.apache.activemq.transport.WireFormatNegotiator; 036import org.apache.activemq.util.IOExceptionSupport; 037import org.apache.activemq.util.IntrospectionSupport; 038import org.apache.activemq.util.URISupport; 039import org.apache.activemq.wireformat.WireFormat; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042 043public class TcpTransportFactory extends TransportFactory { 044 045 private static final Logger LOG = LoggerFactory.getLogger(TcpTransportFactory.class); 046 047 @Override 048 public TransportServer doBind(final URI location) throws IOException { 049 try { 050 Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location)); 051 052 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 053 TcpTransportServer server = createTcpTransportServer(location, serverSocketFactory); 054 server.setWireFormatFactory(createWireFormatFactory(options)); 055 IntrospectionSupport.setProperties(server, options); 056 Map<String, Object> transportOptions = IntrospectionSupport.extractProperties(options, "transport."); 057 server.setTransportOption(transportOptions); 058 server.bind(); 059 060 return server; 061 } catch (URISyntaxException e) { 062 throw IOExceptionSupport.create(e); 063 } 064 } 065 066 /** 067 * Allows subclasses of TcpTransportFactory to create custom instances of 068 * TcpTransportServer. 069 * 070 * @param location 071 * @param serverSocketFactory 072 * @return a new TcpTransportServer instance. 073 * @throws IOException 074 * @throws URISyntaxException 075 */ 076 protected TcpTransportServer createTcpTransportServer(final URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException { 077 return new TcpTransportServer(this, location, serverSocketFactory); 078 } 079 080 @Override 081 @SuppressWarnings("rawtypes") 082 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 083 084 TcpTransport tcpTransport = transport.narrow(TcpTransport.class); 085 IntrospectionSupport.setProperties(tcpTransport, options); 086 087 Map<String, Object> socketOptions = IntrospectionSupport.extractProperties(options, "socket."); 088 tcpTransport.setSocketOptions(socketOptions); 089 090 if (tcpTransport.isTrace()) { 091 try { 092 transport = TransportLoggerSupport.createTransportLogger(transport, tcpTransport.getLogWriterName(), tcpTransport.isDynamicManagement(), tcpTransport.isStartLogging(), tcpTransport.getJmxPort()); 093 } catch (Throwable e) { 094 LOG.error("Could not create TransportLogger object for: " + tcpTransport.getLogWriterName() + ", reason: " + e, e); 095 } 096 } 097 098 boolean useInactivityMonitor = "true".equals(getOption(options, "useInactivityMonitor", "true")); 099 if (useInactivityMonitor && isUseInactivityMonitor(transport)) { 100 transport = createInactivityMonitor(transport, format); 101 IntrospectionSupport.setProperties(transport, options); 102 } 103 104 // Only need the WireFormatNegotiator if using openwire 105 if (format instanceof OpenWireFormat) { 106 transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, tcpTransport.getMinmumWireFormatVersion()); 107 } 108 109 return super.compositeConfigure(transport, format, options); 110 } 111 112 113 /** 114 * @return true if the inactivity monitor should be used on the transport 115 */ 116 protected boolean isUseInactivityMonitor(Transport transport) { 117 return true; 118 } 119 120 @Override 121 protected Transport createTransport(URI location, WireFormat wf) throws UnknownHostException, IOException { 122 URI localLocation = null; 123 String path = location.getPath(); 124 // see if the path is a local URI location 125 if (path != null && path.length() > 0) { 126 int localPortIndex = path.indexOf(':'); 127 try { 128 Integer.parseInt(path.substring(localPortIndex + 1, path.length())); 129 String localString = location.getScheme() + ":/" + path; 130 localLocation = new URI(localString); 131 } catch (Exception e) { 132 LOG.warn("path isn't a valid local location for TcpTransport to use", e.getMessage()); 133 if(LOG.isDebugEnabled()) { 134 LOG.debug("Failure detail", e); 135 } 136 } 137 } 138 SocketFactory socketFactory = createSocketFactory(); 139 return createTcpTransport(wf, socketFactory, location, localLocation); 140 } 141 142 /** 143 * Allows subclasses of TcpTransportFactory to provide a create custom 144 * TcpTransport instances. 145 * 146 * @param wf 147 * @param socketFactory 148 * @param location 149 * @param localLocation 150 * 151 * @return a new TcpTransport instance connected to the given location. 152 * 153 * @throws UnknownHostException 154 * @throws IOException 155 */ 156 protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException { 157 return new TcpTransport(wf, socketFactory, location, localLocation); 158 } 159 160 protected ServerSocketFactory createServerSocketFactory() throws IOException { 161 return ServerSocketFactory.getDefault(); 162 } 163 164 protected SocketFactory createSocketFactory() throws IOException { 165 return SocketFactory.getDefault(); 166 } 167 168 protected Transport createInactivityMonitor(Transport transport, WireFormat format) { 169 return new InactivityMonitor(transport, format); 170 } 171}