KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > TransportFactory


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;
19
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.activemq.util.FactoryFinder;
29 import org.apache.activemq.util.IOExceptionSupport;
30 import org.apache.activemq.util.IntrospectionSupport;
31 import org.apache.activemq.util.URISupport;
32 import org.apache.activemq.wireformat.WireFormat;
33 import org.apache.activemq.wireformat.WireFormatFactory;
34
35 import java.util.concurrent.ConcurrentHashMap JavaDoc;
36 import java.util.concurrent.Executor JavaDoc;
37
38 public abstract class TransportFactory {
39
40     public abstract TransportServer doBind(String JavaDoc brokerId, URI JavaDoc location) throws IOException JavaDoc;
41
42     public Transport doConnect(URI JavaDoc location, Executor JavaDoc ex) throws Exception JavaDoc {
43         return doConnect(location);
44     }
45
46     public Transport doCompositeConnect(URI JavaDoc location, Executor JavaDoc ex) throws Exception JavaDoc {
47         return doCompositeConnect(location);
48     }
49
50     static final private FactoryFinder transportFactoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/transport/");
51     static final private FactoryFinder wireFormatFactoryFinder = new FactoryFinder("META-INF/services/org/apache/activemq/wireformat/");
52
53     static final private ConcurrentHashMap JavaDoc transportFactorys = new ConcurrentHashMap JavaDoc();
54
55     /**
56      * Creates a normal transport.
57      *
58      * @param location
59      * @return the transport
60      * @throws Exception
61      */

62     public static Transport connect(URI JavaDoc location) throws Exception JavaDoc {
63         TransportFactory tf = findTransportFactory(location);
64         return tf.doConnect(location);
65     }
66
67     /**
68      * Creates a normal transport.
69      *
70      * @param location
71      * @param ex
72      * @return the transport
73      * @throws Exception
74      */

75     public static Transport connect(URI JavaDoc location, Executor JavaDoc ex) throws Exception JavaDoc {
76         TransportFactory tf = findTransportFactory(location);
77         return tf.doConnect(location, ex);
78     }
79
80     /**
81      * Creates a slimmed down transport that is more efficient so that it can be
82      * used by composite transports like reliable and HA.
83      *
84      * @param location
85      * @return the Transport
86      * @throws Exception
87      */

88     public static Transport compositeConnect(URI JavaDoc location) throws Exception JavaDoc {
89         TransportFactory tf = findTransportFactory(location);
90         return tf.doCompositeConnect(location);
91     }
92
93     /**
94      * Creates a slimmed down transport that is more efficient so that it can be
95      * used by composite transports like reliable and HA.
96      *
97      * @param location
98      * @param ex
99      * @return the Transport
100      * @throws Exception
101      */

102     public static Transport compositeConnect(URI JavaDoc location, Executor JavaDoc ex) throws Exception JavaDoc {
103         TransportFactory tf = findTransportFactory(location);
104         return tf.doCompositeConnect(location, ex);
105     }
106
107     public static TransportServer bind(String JavaDoc brokerId, URI JavaDoc location) throws IOException JavaDoc {
108         TransportFactory tf = findTransportFactory(location);
109         return tf.doBind(brokerId, location);
110     }
111
112     public Transport doConnect(URI JavaDoc location) throws Exception JavaDoc {
113         try {
114             Map JavaDoc options = new HashMap JavaDoc(URISupport.parseParamters(location));
115             WireFormat wf = createWireFormat(options);
116             Transport transport = createTransport(location, wf);
117             Transport rc = configure(transport, wf, options);
118             if (!options.isEmpty()) {
119                 throw new IllegalArgumentException JavaDoc("Invalid connect parameters: " + options);
120             }
121             return rc;
122         }
123         catch (URISyntaxException JavaDoc e) {
124             throw IOExceptionSupport.create(e);
125         }
126     }
127
128     public Transport doCompositeConnect(URI JavaDoc location) throws Exception JavaDoc {
129         try {
130             Map JavaDoc options = new HashMap JavaDoc(URISupport.parseParamters(location));
131             WireFormat wf = createWireFormat(options);
132             Transport transport = createTransport(location, wf);
133             Transport rc = compositeConfigure(transport, wf, options);
134             if (!options.isEmpty()) {
135                 throw new IllegalArgumentException JavaDoc("Invalid connect parameters: " + options);
136             }
137             return rc;
138
139         }
140         catch (URISyntaxException JavaDoc e) {
141             throw IOExceptionSupport.create(e);
142         }
143     }
144
145     /**
146      * Factory method to create a new transport
147      * @throws IOException
148      * @throws UnknownHostException
149      */

150     protected Transport createTransport(URI JavaDoc location, WireFormat wf) throws MalformedURLException JavaDoc, UnknownHostException JavaDoc, IOException JavaDoc {
151         throw new IOException JavaDoc("createTransport() method not implemented!");
152     }
153
154     /**
155      * @param location
156      * @return
157      * @throws IOException
158      */

159     private static TransportFactory findTransportFactory(URI JavaDoc location) throws IOException JavaDoc {
160         String JavaDoc scheme = location.getScheme();
161         if( scheme == null )
162             throw new IOException JavaDoc("Transport not scheme specified: [" + location + "]");
163         TransportFactory tf = (TransportFactory) transportFactorys.get(scheme);
164         if (tf == null) {
165             // Try to load if from a META-INF property.
166
try {
167                 tf = (TransportFactory) transportFactoryFinder.newInstance(scheme);
168                 transportFactorys.put(scheme, tf);
169             }
170             catch (Throwable JavaDoc e) {
171                 throw IOExceptionSupport.create("Transport scheme NOT recognized: [" + scheme + "]", e);
172             }
173         }
174         return tf;
175     }
176
177     protected WireFormat createWireFormat(Map JavaDoc options) throws IOException JavaDoc {
178         WireFormatFactory factory = createWireFormatFactory(options);
179         WireFormat format = factory.createWireFormat();
180         return format;
181     }
182
183     protected WireFormatFactory createWireFormatFactory(Map JavaDoc options) throws IOException JavaDoc {
184         String JavaDoc wireFormat = (String JavaDoc) options.get("wireFormat");
185         if (wireFormat == null)
186             wireFormat = getDefaultWireFormatType();
187
188         try {
189             WireFormatFactory wff = (WireFormatFactory) wireFormatFactoryFinder.newInstance(wireFormat);
190             IntrospectionSupport.setProperties(wff, options, "wireFormat.");
191             return wff;
192         }
193         catch (Throwable JavaDoc e) {
194             throw IOExceptionSupport.create("Could not create wire format factory for: " + wireFormat + ", reason: " + e, e);
195         }
196     }
197
198     protected String JavaDoc getDefaultWireFormatType() {
199         return "default";
200     }
201
202     /**
203      * Fully configures and adds all need transport filters so that the transport
204      * can be used by the JMS client.
205      *
206      * @param transport
207      * @param wf
208      * @param options
209      * @return
210      * @throws Exception
211      */

212     public Transport configure(Transport transport, WireFormat wf, Map JavaDoc options) throws Exception JavaDoc {
213         transport = compositeConfigure(transport, wf, options);
214         
215         transport = new MutexTransport(transport);
216         transport = new ResponseCorrelator(transport);
217         
218         return transport;
219     }
220
221     /**
222      * Fully configures and adds all need transport filters so that the transport
223      * can be used by the ActiveMQ message broker. The main difference between this and the
224      * configure() method is that the broker does not issue requests to the client so the
225      * ResponseCorrelator is not needed.
226      *
227      * @param transport
228      * @param format
229      * @param options
230      * @return
231      * @throws Exception
232      */

233     public Transport serverConfigure(Transport transport, WireFormat format, HashMap JavaDoc options) throws Exception JavaDoc {
234         transport = compositeConfigure(transport, format, options);
235         transport = new MutexTransport(transport);
236         return transport;
237     }
238     
239     /**
240      * Similar to configure(...) but this avoid adding in the MutexTransport and ResponseCorrelator transport layers
241      * so that the resulting transport can more efficiently be used as part of a composite transport.
242      *
243      * @param transport
244      * @param format
245      * @param options
246      * @return
247      */

248     public Transport compositeConfigure(Transport transport, WireFormat format, Map JavaDoc options) {
249         IntrospectionSupport.setProperties(transport, options);
250         return transport;
251     }
252
253 }
254
Popular Tags