001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.activemq.transport;
019
020 import org.activemq.io.WireFormat;
021 import org.activemq.util.FactoryFinder;
022
023 import javax.jms.JMSException;
024 import java.io.IOException;
025 import java.net.URI;
026
027 /**
028 * A TransportChannel is used for tranporting packets between nodes
029 *
030 * @version $Revision: 1.1.1.1 $
031 */
032 public class TransportChannelProvider {
033 private static FactoryFinder finder = new FactoryFinder("META-INF/services/org/activemq/transport/");
034
035 /**
036 * Create a Channel to a remote Node - e.g. a Broker
037 *
038 * @param remoteLocation
039 * @return the TransportChannel bound to the remote node
040 * @throws JMSException
041 */
042 public static TransportChannel create(WireFormat wireFormat, URI remoteLocation) throws JMSException {
043 return getFactory(remoteLocation).create(wireFormat, remoteLocation);
044 }
045
046 /**
047 * Create a Channel to a remote Node - e.g. a Broker
048 *
049 * @param remoteLocation
050 * @param localLocation -
051 * e.g. local InetAddress and local port
052 * @return the TransportChannel bound to the remote node
053 * @throws JMSException
054 */
055 public static TransportChannel create(WireFormat wireFormat, URI remoteLocation, URI localLocation) throws JMSException {
056 return getFactory(remoteLocation).create(wireFormat, remoteLocation, localLocation);
057 }
058
059 public static TransportChannelFactory getFactory(URI remoteLocation) throws JMSException {
060 String protocol = remoteLocation.getScheme();
061 try {
062 Object value = finder.newInstance(protocol);
063 if (value instanceof TransportChannelFactory) {
064 return (TransportChannelFactory) value;
065 }
066 else {
067 throw new JMSException("Factory does not implement TransportChannelFactory: " + value);
068 }
069 }
070 catch (IllegalAccessException e) {
071 throw createJMSexception(protocol, e);
072 }
073 catch (InstantiationException e) {
074 throw createJMSexception(protocol, e);
075 }
076 catch (IOException e) {
077 throw createJMSexception(protocol, e);
078 }
079 catch (ClassNotFoundException e) {
080 throw createJMSexception(protocol, e);
081 }
082
083 }
084
085 protected static JMSException createJMSexception(String protocol, Exception e) {
086 JMSException answer = new JMSException("Could not load protocol: " + protocol + ". Reason: " + e);
087 answer.setLinkedException(e);
088 return answer;
089 }
090 }