1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 **/
18 package org.codehaus.activemq.transport;
19
20 import org.codehaus.activemq.message.WireFormat;
21 import org.codehaus.activemq.util.FactoryFinder;
22
23 import javax.jms.JMSException;
24 import java.io.IOException;
25 import java.net.URI;
26
27 /***
28 * A TransportChannel is used for tranporting packets between nodes
29 *
30 * @version $Revision: 1.12 $
31 */
32 public class TransportChannelProvider {
33 private static FactoryFinder finder = new FactoryFinder("META-INF/services/org/codehaus/activemq/transport/");
34
35 /***
36 * Create a Channel to a remote Node - e.g. a Broker
37 *
38 * @param remoteLocation
39 * @return the TransportChannel bound to the remote node
40 * @throws JMSException
41 */
42 public static TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
43 return getFactory(remoteLocation).create(wireFormat, remoteLocation);
44 }
45
46 /***
47 * Create a Channel to a remote Node - e.g. a Broker
48 *
49 * @param remoteLocation
50 * @param localLocation -
51 * e.g. local InetAddress and local port
52 * @return the TransportChannel bound to the remote node
53 * @throws JMSException
54 */
55 public static TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
56 return getFactory(remoteLocation).create(wireFormat, remoteLocation, localLocation);
57 }
58
59 public static TransportChannelFactory getFactory(URI remoteLocation) throws JMSException {
60 String protocol = remoteLocation.getScheme();
61 try {
62 Object value = finder.newInstance(protocol);
63 if (value instanceof TransportChannelFactory) {
64 return (TransportChannelFactory) value;
65 }
66 else {
67 throw new JMSException("Factory does not implement TransportChannelFactory: " + value);
68 }
69 }
70 catch (IllegalAccessException e) {
71 throw createJMSexception(protocol, e);
72 }
73 catch (InstantiationException e) {
74 throw createJMSexception(protocol, e);
75 }
76 catch (IOException e) {
77 throw createJMSexception(protocol, e);
78 }
79 catch (ClassNotFoundException e) {
80 throw createJMSexception(protocol, e);
81 }
82
83 }
84
85 protected static JMSException createJMSexception(String protocol, Exception e) {
86 JMSException answer = new JMSException("Could not load protocol: " + protocol + ". Reason: " + e);
87 answer.setLinkedException(e);
88 return answer;
89 }
90 }