1 18 package org.apache.activemq.transport.tcp; 19 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.net.UnknownHostException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import javax.net.ServerSocketFactory; 28 import javax.net.SocketFactory; 29 30 import org.apache.activemq.openwire.OpenWireFormat; 31 import org.apache.activemq.transport.InactivityMonitor; 32 import org.apache.activemq.transport.Transport; 33 import org.apache.activemq.transport.TransportFactory; 34 import org.apache.activemq.transport.TransportLogger; 35 import org.apache.activemq.transport.TransportServer; 36 import org.apache.activemq.transport.WireFormatNegotiator; 37 import org.apache.activemq.util.IOExceptionSupport; 38 import org.apache.activemq.util.IntrospectionSupport; 39 import org.apache.activemq.util.URISupport; 40 import org.apache.activemq.wireformat.WireFormat; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 public class TcpTransportFactory extends TransportFactory { 45 private static final Log log = LogFactory.getLog(TcpTransportFactory.class); 46 public TransportServer doBind(String brokerId, final URI location) throws IOException { 47 try { 48 Map options = new HashMap (URISupport.parseParamters(location)); 49 50 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 51 TcpTransportServer server = createTcpTransportServer(location, serverSocketFactory); 52 server.setWireFormatFactory(createWireFormatFactory(options)); 53 IntrospectionSupport.setProperties(server, options); 54 Map transportOptions = IntrospectionSupport.extractProperties(options, "transport."); 55 server.setTransportOption(transportOptions); 56 server.bind(); 57 58 return server; 59 } 60 catch (URISyntaxException e) { 61 throw IOExceptionSupport.create(e); 62 } 63 } 64 65 74 protected TcpTransportServer createTcpTransportServer(final URI location, ServerSocketFactory serverSocketFactory) throws IOException , URISyntaxException { 75 return new TcpTransportServer(this, location, serverSocketFactory); 76 } 77 78 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 79 80 TcpTransport tcpTransport = (TcpTransport) transport.narrow(TcpTransport.class); 81 IntrospectionSupport.setProperties(tcpTransport, options); 82 83 Map socketOptions = IntrospectionSupport.extractProperties(options, "socket."); 84 tcpTransport.setSocketOptions(socketOptions); 85 86 if (tcpTransport.isTrace()) { 87 transport = new TransportLogger(transport); 88 } 89 90 if (isUseInactivityMonitor(transport)) { 91 transport = new InactivityMonitor(transport); 92 } 93 94 if( format instanceof OpenWireFormat ) { 96 transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, tcpTransport.getMinmumWireFormatVersion()); 97 } 98 99 return transport; 100 } 101 102 105 protected boolean isUseInactivityMonitor(Transport transport) { 106 return true; 107 } 108 109 protected Transport createTransport(URI location,WireFormat wf) throws UnknownHostException ,IOException { 110 URI localLocation=null; 111 String path=location.getPath(); 112 if (path != null && path.length() > 0) { 114 int localPortIndex = path.indexOf(':'); 115 try { 116 Integer.parseInt(path.substring((localPortIndex + 1), path.length())); 117 String localString = location.getScheme() + ":/" + path; 118 localLocation = new URI (localString); 119 } 120 catch (Exception e) { 121 log.warn("path isn't a valid local location for TcpTransport to use", e); 122 } 123 } 124 SocketFactory socketFactory = createSocketFactory(); 125 return createTcpTransport(wf, socketFactory, location, localLocation); 126 } 127 128 139 protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException , IOException { 140 return new TcpTransport(wf, socketFactory, location, localLocation); 141 } 142 143 protected ServerSocketFactory createServerSocketFactory() { 144 return ServerSocketFactory.getDefault(); 145 } 146 147 protected SocketFactory createSocketFactory() { 148 return SocketFactory.getDefault(); 149 } 150 } 151 | Popular Tags |