KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > nio > NIOTransportFactory


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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