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 import java.net.URISyntaxException;
027
028 /**
029 * locates a protocol specific TransportServerChannelFactory
030 *
031 * @version $Revision: 1.1.1.1 $
032 */
033 public class TransportServerChannelProvider {
034
035 private static FactoryFinder finder = new FactoryFinder("META-INF/services/org/activemq/transport/server/");
036
037 /**
038 * Create a Channel used for the Broker to listen to remove connections
039 *
040 * @param bindAddress
041 * @return the TransportServerChannel bound to the address
042 * @throws JMSException
043 */
044 public static TransportServerChannel create(WireFormat wireFormat, URI bindAddress) throws JMSException {
045 return getFactory(bindAddress.getScheme()).create(wireFormat, bindAddress);
046 }
047
048 /**
049 * Create a Channel used for the Broker to listen to remove connections
050 *
051 * @param bindAddress
052 * @return the TransportServerChannel bound to the address
053 * @throws JMSException
054 */
055 public static TransportServerChannel newInstance(WireFormat wireFormat, String bindAddress) throws JMSException, URISyntaxException {
056 return create(wireFormat, new URI(bindAddress));
057 }
058
059 protected static TransportServerChannelFactory getFactory(String protocol) throws JMSException {
060 try {
061 Object value = finder.newInstance(protocol);
062 if (value instanceof TransportServerChannelFactory) {
063 return (TransportServerChannelFactory) value;
064 }
065 else {
066 throw new JMSException("Factory does not implement TransportServerChannelFactory: " + value);
067 }
068 }
069 catch (IllegalAccessException e) {
070 throw createJMSexception(protocol, e);
071 }
072 catch (InstantiationException e) {
073 throw createJMSexception(protocol, e);
074 }
075 catch (IOException e) {
076 throw createJMSexception(protocol, e);
077 }
078 catch (ClassNotFoundException e) {
079 throw createJMSexception(protocol, e);
080 }
081
082 }
083
084 protected static JMSException createJMSexception(String protocol, Exception e) {
085 JMSException answer = new JMSException("Could not load protocol: " + protocol + ". Reason: " + e);
086 answer.setLinkedException(e);
087 return answer;
088 }
089 }