1 18 package org.apache.activemq.transport.nio; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.InetSocketAddress ; 23 import java.net.ServerSocket ; 24 import java.net.Socket ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.net.UnknownHostException ; 28 import java.nio.channels.ServerSocketChannel ; 29 import java.nio.channels.SocketChannel ; 30 31 import javax.net.ServerSocketFactory; 32 import javax.net.SocketFactory; 33 34 import org.apache.activemq.wireformat.WireFormat; 35 import org.apache.activemq.transport.Transport; 36 import org.apache.activemq.transport.tcp.TcpTransport; 37 import org.apache.activemq.transport.tcp.TcpTransportFactory; 38 import org.apache.activemq.transport.tcp.TcpTransportServer; 39 40 public class NIOTransportFactory extends TcpTransportFactory { 41 42 protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException , URISyntaxException { 43 return new TcpTransportServer(this, location, serverSocketFactory) { 44 protected Transport createTransport(Socket socket, WireFormat format) throws IOException { 45 return new NIOTransport(format,socket); 46 } 47 }; 48 } 49 50 protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException , IOException { 51 return new NIOTransport(wf, socketFactory, location, localLocation); 52 } 53 54 55 protected ServerSocketFactory createServerSocketFactory() { 56 return new ServerSocketFactory() { 57 public ServerSocket createServerSocket(int port) throws IOException { 58 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 59 serverSocketChannel.socket().bind(new InetSocketAddress (port)); 60 return serverSocketChannel.socket(); 61 } 62 public ServerSocket createServerSocket(int port, int backlog) throws IOException { 63 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 64 serverSocketChannel.socket().bind(new InetSocketAddress (port), backlog); 65 return serverSocketChannel.socket(); 66 } 67 public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException { 68 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); 69 serverSocketChannel.socket().bind(new InetSocketAddress (ifAddress, port), backlog); 70 return serverSocketChannel.socket(); 71 } 72 }; 73 } 74 75 protected SocketFactory createSocketFactory() { 76 return new SocketFactory() { 77 78 public Socket createSocket() throws IOException { 79 SocketChannel channel = SocketChannel.open(); 80 return channel.socket(); 81 } 82 83 public Socket createSocket(String host, int port) throws IOException , UnknownHostException { 84 SocketChannel channel = SocketChannel.open(); 85 channel.connect(new InetSocketAddress (host, port)); 86 return channel.socket(); 87 } 88 89 public Socket createSocket(InetAddress address, int port) throws IOException { 90 SocketChannel channel = SocketChannel.open(); 91 channel.connect(new InetSocketAddress (address, port)); 92 return channel.socket(); 93 } 94 95 public Socket createSocket(String address, int port, InetAddress localAddresss, int localPort) throws IOException , UnknownHostException { 96 SocketChannel channel = SocketChannel.open(); 97 channel.socket().bind(new InetSocketAddress (localAddresss, localPort)); 98 channel.connect(new InetSocketAddress (address, port)); 99 return channel.socket(); 100 } 101 102 public Socket createSocket(InetAddress address, int port, InetAddress localAddresss, int localPort) throws IOException { 103 SocketChannel channel = SocketChannel.open(); 104 channel.socket().bind(new InetSocketAddress (localAddresss, localPort)); 105 channel.connect(new InetSocketAddress (address, port)); 106 return channel.socket(); 107 } 108 }; 109 } 110 } 111 | Popular Tags |