KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > net > TransportProvider


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.net;
47
48 import java.io.IOException JavaDoc;
49 import java.net.SocketAddress JavaDoc;
50 import java.net.InetAddress JavaDoc;
51 import java.nio.channels.SocketChannel JavaDoc;
52
53 import javax.net.ssl.SSLSocket;
54
55 import org.mr.MantaAgent;
56 import org.mr.indexing.WBManager;
57 import org.apache.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59
60 /**
61  * TransportProvider.java
62  *
63  * A factory class for TransportImpl objects.
64  *
65  * Created: Wed Feb 04 15:33:22 2004
66  *
67  * @author Uri Schneider
68  * @version 1.0
69  */

70 public class TransportProvider {
71     private static Log log = LogFactory.getLog("TransportProvider");
72
73     public TransportProvider() {
74     } // TransportProvider constructor
75

76     /**
77      * Create a TransportImpl of the given type, connecting the local and
78      * remote addresses provided.
79      *
80      * @param type - the transport type (currently supported: TCP, HTTP, CDP)
81      * @param local - the local address
82      * @param remote - the remote address
83      * @return the impl created
84      * @throws IOException
85      */

86     public static TransportImpl createImpl(TransportType type,
87                                            SocketAddress JavaDoc local,
88                                            SocketAddress JavaDoc remote,
89                                            String JavaDoc remoteAgent,
90                                            Transport owner)
91         throws IOException JavaDoc
92     {
93         if(log.isInfoEnabled()){
94             log.info("Creating " + type + " impl " +
95                      (local != null ? local.toString() : "0.0.0.0:0") +
96                      "--" +remote.toString()+".");
97         }
98
99         if (type.equals(TransportType.TCP)) {
100             return new TCPTransportImpl(local, remote);
101         } else if(type.equals(TransportType.HTTP)) {
102             return new HTTPTransportImpl(local, remote);
103         } else if (type.equals(TransportType.SSL)) {
104             return new SSLTransportImpl(local, remote, owner);
105         } else if (type.equals(TransportType.MWB)) {
106             WBManager wblink = MantaAgent.getInstance().
107                 getSingletonRepository().getWBManager();
108             if (wblink != null) {
109                 return wblink.getIRSTransportImpl(local, remote);
110             }
111         }
112
113         // not found
114
return null;
115     }
116
117     /**
118      * Create a TransportImpl of the given type, with an already connected
119      * channel
120      * @param type - the impl's type (currently supported: TCP)
121      * @param channel
122      * @return the created impl
123      */

124     public static TransportImpl createImpl(TransportType type,
125                                            SocketChannel JavaDoc channel)
126         throws IOException JavaDoc
127     {
128         if(log.isInfoEnabled()){
129             log.info("Creating " + type + " impl from channel " +channel.socket().getLocalSocketAddress().toString() +"--" + channel.socket().getRemoteSocketAddress().toString()+".");
130         }
131         
132         if (type.equals(TransportType.TCP)) {
133             return new TCPTransportImpl(channel);
134         } else if(type.equals(TransportType.HTTP)) {
135             return new HTTPTransportImpl(channel);
136         } else if (type.equals(TransportType.MWB)) {
137             return new MWBTransportImpl(channel);
138         } else {
139             // not found
140
return null;
141         }
142     }
143
144     /**
145      * @param type
146      * @param socket
147      * @return
148      * @throws IOException
149      */

150     public static TransportImpl createImpl(TransportType type, SSLSocket socket)
151         throws IOException JavaDoc
152     {
153         if (type == TransportType.SSL) {
154             return new SSLTransportImpl(socket);
155         }
156
157         return null;
158     }
159
160     public static LocalTransport createLocalTransport(TransportType type,
161                                                       InetAddress JavaDoc addr,
162                                                       int portMin,
163                                                       int portMax,
164                                                       NetworkListener listener,
165                                                       NetworkSelector selector)
166         throws IOException JavaDoc
167     {
168         if (type.equals(TransportType.SSL)) {
169             return new LocalSSLTransport(addr, portMin, portMax, listener);
170         } else if (type.equals(TransportType.HTTP) ||
171                    type.equals(TransportType.TCP) ||
172                    type.equals(TransportType.MWB)) {
173             return new LocalTCPTransport(type, addr, portMin, portMax,
174                                          selector);
175         } else {
176             throw new IllegalArgumentException JavaDoc("Unsupported transport type: " +
177                                                type.toString());
178         }
179     }
180 } // TransportProvider
181
Popular Tags