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     */
017    package org.apache.activemq.transport.nio;
018    
019    import java.io.IOException;
020    import java.net.InetAddress;
021    import java.net.InetSocketAddress;
022    import java.net.ServerSocket;
023    import java.net.Socket;
024    import java.net.URI;
025    import java.net.URISyntaxException;
026    import java.net.UnknownHostException;
027    import java.nio.channels.ServerSocketChannel;
028    import java.nio.channels.SocketChannel;
029    
030    import javax.net.ServerSocketFactory;
031    import javax.net.SocketFactory;
032    
033    import org.apache.activemq.transport.Transport;
034    import org.apache.activemq.transport.tcp.TcpTransport;
035    import org.apache.activemq.transport.tcp.TcpTransportFactory;
036    import org.apache.activemq.transport.tcp.TcpTransportServer;
037    import org.apache.activemq.wireformat.WireFormat;
038    
039    public class NIOTransportFactory extends TcpTransportFactory {
040    
041        protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
042            return new TcpTransportServer(this, location, serverSocketFactory) {
043                protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
044                    return new NIOTransport(format, socket);
045                }
046            };
047        }
048    
049        protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
050            return new NIOTransport(wf, socketFactory, location, localLocation);
051        }
052    
053        protected ServerSocketFactory createServerSocketFactory() {
054            return new ServerSocketFactory() {
055                public ServerSocket createServerSocket(int port) throws IOException {
056                    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
057                    serverSocketChannel.socket().bind(new InetSocketAddress(port));
058                    return serverSocketChannel.socket();
059                }
060    
061                public ServerSocket createServerSocket(int port, int backlog) throws IOException {
062                    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
063                    serverSocketChannel.socket().bind(new InetSocketAddress(port), backlog);
064                    return serverSocketChannel.socket();
065                }
066    
067                public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
068                    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
069                    serverSocketChannel.socket().bind(new InetSocketAddress(ifAddress, port), backlog);
070                    return serverSocketChannel.socket();
071                }
072            };
073        }
074    
075        protected SocketFactory createSocketFactory() throws IOException {
076            return new SocketFactory() {
077    
078                public Socket createSocket() throws IOException {
079                    SocketChannel channel = SocketChannel.open();
080                    return channel.socket();
081                }
082    
083                public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
084                    SocketChannel channel = SocketChannel.open();
085                    channel.connect(new InetSocketAddress(host, port));
086                    return channel.socket();
087                }
088    
089                public Socket createSocket(InetAddress address, int port) throws IOException {
090                    SocketChannel channel = SocketChannel.open();
091                    channel.connect(new InetSocketAddress(address, port));
092                    return channel.socket();
093                }
094    
095                public Socket createSocket(String address, int port, InetAddress localAddresss, int localPort) throws IOException, UnknownHostException {
096                    SocketChannel channel = SocketChannel.open();
097                    channel.socket().bind(new InetSocketAddress(localAddresss, localPort));
098                    channel.connect(new InetSocketAddress(address, port));
099                    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    }