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