View Javadoc

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  import java.net.URISyntaxException;
27  
28  /***
29   * locates a protocol specific TransportServerChannelFactory
30   *
31   * @version $Revision: 1.13 $
32   */
33  public class TransportServerChannelProvider {
34  
35      private static FactoryFinder finder = new FactoryFinder("META-INF/services/org/codehaus/activemq/transport/server/");
36  
37      /***
38       * Create a Channel used for the Broker to listen to remove connections
39       *
40       * @param bindAddress
41       * @return the TransportServerChannel bound to the address
42       * @throws JMSException
43       */
44      public static TransportServerChannel create(WireFormat wireFormat, URI bindAddress) throws JMSException {
45          return getFactory(bindAddress.getScheme()).create(wireFormat, bindAddress);
46      }
47  
48      /***
49       * Create a Channel used for the Broker to listen to remove connections
50       *
51       * @param bindAddress
52       * @return the TransportServerChannel bound to the address
53       * @throws JMSException
54       */
55      public static TransportServerChannel newInstance(WireFormat wireFormat, String bindAddress) throws JMSException, URISyntaxException {
56          return create(wireFormat, new URI(bindAddress));
57      }
58  
59      protected static TransportServerChannelFactory getFactory(String protocol) throws JMSException {
60          try {
61              Object value = finder.newInstance(protocol);
62              if (value instanceof TransportServerChannelFactory) {
63                  return (TransportServerChannelFactory) value;
64              }
65              else {
66                  throw new JMSException("Factory does not implement TransportServerChannelFactory: " + value);
67              }
68          }
69          catch (IllegalAccessException e) {
70              throw createJMSexception(protocol, e);
71          }
72          catch (InstantiationException e) {
73              throw createJMSexception(protocol, e);
74          }
75          catch (IOException e) {
76              throw createJMSexception(protocol, e);
77          }
78          catch (ClassNotFoundException e) {
79              throw createJMSexception(protocol, e);
80          }
81  
82      }
83  
84      protected static JMSException createJMSexception(String protocol, Exception e) {
85          JMSException answer = new JMSException("Could not load protocol: " + protocol + ". Reason: " + e);
86          answer.setLinkedException(e);
87          return answer;
88      }
89  }