Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » nio » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.transport.nio;
   18   
   19   import java.io.IOException;
   20   import java.net.InetAddress;
   21   import java.net.InetSocketAddress;
   22   import java.net.ServerSocket;
   23   import java.net.Socket;
   24   import java.net.URI;
   25   import java.net.URISyntaxException;
   26   import java.net.UnknownHostException;
   27   import java.nio.channels.ServerSocketChannel;
   28   import java.nio.channels.SocketChannel;
   29   
   30   import javax.net.ServerSocketFactory;
   31   import javax.net.SocketFactory;
   32   
   33   import org.apache.activemq.transport.Transport;
   34   import org.apache.activemq.transport.tcp.TcpTransport;
   35   import org.apache.activemq.transport.tcp.TcpTransportFactory;
   36   import org.apache.activemq.transport.tcp.TcpTransportServer;
   37   import org.apache.activemq.wireformat.WireFormat;
   38   
   39   public class NIOTransportFactory extends TcpTransportFactory {
   40   
   41       protected TcpTransportServer createTcpTransportServer(URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
   42           return new TcpTransportServer(this, location, serverSocketFactory) {
   43               protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
   44                   return new NIOTransport(format, socket);
   45               }
   46           };
   47       }
   48   
   49       protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
   50           return new NIOTransport(wf, socketFactory, location, localLocation);
   51       }
   52   
   53       protected ServerSocketFactory createServerSocketFactory() {
   54           return new ServerSocketFactory() {
   55               public ServerSocket createServerSocket(int port) throws IOException {
   56                   ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
   57                   serverSocketChannel.socket().bind(new InetSocketAddress(port));
   58                   return serverSocketChannel.socket();
   59               }
   60   
   61               public ServerSocket createServerSocket(int port, int backlog) throws IOException {
   62                   ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
   63                   serverSocketChannel.socket().bind(new InetSocketAddress(port), backlog);
   64                   return serverSocketChannel.socket();
   65               }
   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   }

Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » nio » [javadoc | source]