1 17 package org.apache.activemq.transport.tcp; 18 19 import org.apache.activemq.wireformat.WireFormat; 20 import org.apache.activemq.openwire.OpenWireFormat; 21 import org.apache.activemq.transport.InactivityMonitor; 22 import org.apache.activemq.transport.Transport; 23 import org.apache.activemq.transport.TransportLogger; 24 import org.apache.activemq.transport.TransportServer; 25 import org.apache.activemq.transport.WireFormatNegotiator; 26 import org.apache.activemq.util.IOExceptionSupport; 27 import org.apache.activemq.util.IntrospectionSupport; 28 import org.apache.activemq.util.URISupport; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import java.io.IOException ; 33 import java.net.URI ; 34 import java.net.URISyntaxException ; 35 import java.net.UnknownHostException ; 36 import java.security.KeyManagementException ; 37 import java.security.NoSuchAlgorithmException ; 38 import java.security.SecureRandom ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 import javax.net.ServerSocketFactory; 44 import javax.net.SocketFactory; 45 import javax.net.ssl.SSLContext; 46 import javax.net.ssl.KeyManager; 47 import javax.net.ssl.SSLServerSocketFactory; 48 import javax.net.ssl.SSLSocketFactory; 49 import javax.net.ssl.TrustManager; 50 51 60 public class SslTransportFactory extends TcpTransportFactory { 61 private SSLContext sslContext = null; 63 64 private static final Log log = LogFactory.getLog(SslTransportFactory.class); 66 67 71 public SslTransportFactory() { 72 } 73 74 77 public TransportServer doBind(String brokerId, final URI location) throws IOException { 78 try { 79 Map options = new HashMap (URISupport.parseParamters(location)); 80 81 ServerSocketFactory serverSocketFactory = createServerSocketFactory(); 82 SslTransportServer server = 83 new SslTransportServer(this, location, (SSLServerSocketFactory)serverSocketFactory); 84 server.setWireFormatFactory(createWireFormatFactory(options)); 85 IntrospectionSupport.setProperties(server, options); 86 Map transportOptions = IntrospectionSupport.extractProperties(options, "transport."); 87 server.setTransportOption(transportOptions); 88 server.bind(); 89 90 return server; 91 } 92 catch (URISyntaxException e) { 93 throw IOExceptionSupport.create(e); 94 } 95 } 96 97 100 public Transport compositeConfigure(Transport transport, WireFormat format, Map options) { 101 102 SslTransport sslTransport = (SslTransport) transport.narrow(SslTransport.class); 103 IntrospectionSupport.setProperties(sslTransport, options); 104 105 Map socketOptions = IntrospectionSupport.extractProperties(options, "socket."); 106 107 sslTransport.setSocketOptions(socketOptions); 108 109 if (sslTransport.isTrace()) { 110 transport = new TransportLogger(transport); 111 } 112 113 transport = new InactivityMonitor(transport); 114 115 if (format instanceof OpenWireFormat) { 117 transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, sslTransport.getMinmumWireFormatVersion()); 118 } 119 120 return transport; 121 } 122 123 126 protected Transport createTransport(URI location,WireFormat wf) throws UnknownHostException ,IOException { 127 URI localLocation = null; 128 String path = location.getPath(); 129 if (path != null && path.length() > 0) { 131 int localPortIndex = path.indexOf(':'); 132 try { 133 Integer.parseInt(path.substring((localPortIndex + 1), path.length())); 134 String localString = location.getScheme() + ":/" + path; 135 localLocation = new URI (localString); 136 } 137 catch (Exception e) { 138 log.warn("path isn't a valid local location for SslTransport to use", e); 139 } 140 } 141 SocketFactory socketFactory = createSocketFactory(); 142 return new SslTransport(wf, (SSLSocketFactory)socketFactory, location, localLocation, false); 143 } 144 145 154 public void setKeyAndTrustManagers(KeyManager[] km, TrustManager[] tm, SecureRandom random) throws KeyManagementException { 155 try { 157 sslContext = SSLContext.getInstance("TLS"); 158 } catch (NoSuchAlgorithmException e) { 159 throw new RuntimeException ("Unknown SSL algorithm encountered.", e); 161 } 162 sslContext.init(km, tm, random); 163 } 164 165 172 protected ServerSocketFactory createServerSocketFactory() { 173 if (sslContext == null) { 174 return SSLServerSocketFactory.getDefault(); 175 } 176 else 177 return sslContext.getServerSocketFactory(); 178 } 179 180 187 protected SocketFactory createSocketFactory() { 188 if ( sslContext == null ) { 189 return SSLSocketFactory.getDefault(); 190 } 191 else 192 return sslContext.getSocketFactory(); 193 } 194 195 196 } 197 | Popular Tags |