KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > URLConnectionFactory


1 package com.ubermq.jms.client;
2
3 import com.ubermq.*;
4 import com.ubermq.util.*;
5
6 import java.io.*;
7 import java.lang.reflect.*;
8 import java.net.*;
9 import java.util.*;
10 import javax.jms.*;
11
12 /**
13  * Processes uniform resource identifiers (URI's) as a way of
14  * describing a connection to a messaging infrastructure via
15  * a delegate ConnectionFactory.<P>
16  *
17  * This implementation also allows custom factories to register
18  * themselves with this centralized registry. Clients of this
19  * unified connection factory will have all ranges of
20  * factories available to them at runtime. <P>
21  *
22  * URI's are formatted as follows: <BR>
23  * <code>scheme://host:port</code>
24  * <P>
25  *
26  * Some URI handlers, like the default UberMQ <code>UnicastTopicConnectionFactory</code>
27  * can accept URI's that have failover instructions embedded in them, as follows:<br>
28  * <code>scheme://host:port,host2:port2...</code>
29  *
30  * The two default connection schemes are registered at initialization
31  * time. These are given by <code>DEFAULT_UBERMQ_SCHEME</code>
32  * and <code>DEFAULT_UBERMQ_MULTICAST_SCHEME</code>.<P>
33  *
34  * The factory class will also look for a properties file named
35  * <code>URL_REGISTRY_NAME</code> and use that as a set of key-value pairs
36  * specifying custom connection schemes and implementations at runtime.
37  * The <code>ClassLoader.findResource</code> method is used to locate this
38  * file. For example, the line: <br>
39
40  * <code>my-scheme=com.my-company.MySchemeConnectionFactory</code><br>
41  *
42  * will delegate URI's for <code>my-scheme</code> to the named factory
43  * class. The factory class must provide a constructor that accepts a
44  * single <code>java.net.URI</code> object.<P>
45  *
46  * @see java.net.URI
47  * @since 2.1
48  */

49 public class URLConnectionFactory
50     implements ConnectionFactory, TopicConnectionFactory, QueueConnectionFactory, Serializable
51 {
52     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(URLConnectionFactory.class);
53     
54     private static final Map factories = new HashMap();
55
56     private static final Class JavaDoc[] FACTORY_SINGLE_SIGNATURE = new Class JavaDoc[] {URI.class};
57     private static final Class JavaDoc[] FACTORY_MULTI_SIGNATURE = new Class JavaDoc[] {URI[].class};
58
59     /**
60      * The name of the file, made available via the class loader, that
61      * specifies custom schemes, or overrides standard schemes.
62      */

63     public static final String JavaDoc URL_REGISTRY_NAME = "/url-registry.properties";
64
65     /**
66      * The standard UberMQ TCP/IP unicast scheme, that delegates
67      * to UnicastTopicConnectionFactory.
68      *
69      * @see com.ubermq.jms.client.UnicastTopicConnectionFactory
70      */

71     public static final String JavaDoc DEFAULT_UBERMQ_SCHEME = "ubermq";
72
73     /**
74      * The standard UberMQ LRMP multicast scheme, that delegates
75      * to MulticastTopicConnectionFactory.
76      *
77      * @see com.ubermq.jms.client.MulticastTopicConnectionFactory
78      */

79     public static final String JavaDoc DEFAULT_UBERMQ_MULTICAST_SCHEME = "ubermq-lrmp";
80
81     /**
82      * The standard uberMQ over SSL scheme.
83      */

84     public static final String JavaDoc DEFAULT_UBERMQ_SECURE_SCHEME = "ubermqs";
85
86     public static final long serialVersionUID = 1L;
87
88     private final URI uri;
89     private transient ConnectionFactory delegate;
90
91     static {
92         // find a url-registry.properties file, if it exists.
93
Properties p = new Properties();
94         try
95         {
96             InputStream is = URLConnectionFactory.class.getResourceAsStream(URL_REGISTRY_NAME);
97             p.load(is);
98         }
99         catch (NullPointerException JavaDoc npe) {
100             // it's ok if there is no url-registry.properties
101
}
102         catch (Exception JavaDoc e) {
103             log.error("", e);;
104         }
105
106         // register the default factories
107
registerFactory(DEFAULT_UBERMQ_SCHEME, UnicastConnectionFactory.class);
108         registerFactory(DEFAULT_UBERMQ_MULTICAST_SCHEME, MulticastTopicConnectionFactory.class);
109         registerFactory(DEFAULT_UBERMQ_SECURE_SCHEME, SSLConnectionFactory.class);
110
111         // register the factories
112
Iterator iter = p.keySet().iterator();
113         while (iter.hasNext())
114         {
115             String JavaDoc scheme = (String JavaDoc)iter.next();
116             try
117             {
118                 registerFactory(scheme,
119                                 Class.forName(p.getProperty(scheme)));
120             }
121             catch (ClassNotFoundException JavaDoc e) {
122                 log.fatal("Factory class " + p.getProperty(scheme) + " not found for URL scheme " + scheme, e);
123             }
124             catch (Exception JavaDoc e) {
125                 log.fatal("", e);
126             }
127         }
128
129     }
130
131     /**
132      * Registers a scheme with this factory. The specified
133      * class must have a constructor that accepts a URI object.
134      *
135      * @param scheme the scheme associated with the factory class.
136      * @param clazz a class that implements TopicConnectionFactory
137      * and provides a constructor taking a single URI object. This
138      * class will be instantiated once as a delegate of this class,
139      * and creation calls will be passed through.
140      *
141      * @throws IllegalArgumentException if the class does not conform
142      * to a valid delegate class.
143      *
144      */

145     public static void registerFactory(String JavaDoc scheme,
146                                        Class JavaDoc clazz)
147     {
148         // ensure the class is valid
149
try
150         {
151             clazz.getConstructor(FACTORY_SINGLE_SIGNATURE);
152
153             Class JavaDoc[] interfaces = clazz.getInterfaces();
154             boolean impls = false;
155             for (int i = 0; i < interfaces.length; i++)
156             {
157                 if (interfaces[i] == TopicConnectionFactory.class ||
158                     interfaces[i] == ConnectionFactory.class)
159                 {
160                     impls = true;
161                     break;
162                 }
163             }
164
165             if (!impls)
166                 throw new IllegalArgumentException JavaDoc("class must implement TopicConnectionFactory or ConnectionFactory");
167         }
168         catch (NoSuchMethodException JavaDoc e) {
169             throw new IllegalArgumentException JavaDoc(e.getMessage());
170         }
171         catch (SecurityException JavaDoc e) {
172             throw new IllegalArgumentException JavaDoc(e.getMessage());
173         }
174
175         // it seems valid, put in the factory
176
factories.put(scheme, clazz);
177         log.debug("Registered URL scheme " + scheme + " with factory class " + clazz);
178     }
179
180     /**
181      * Creates a URL topic connection factory with the given string.
182      * @param sz a URI
183      */

184     public URLConnectionFactory(String JavaDoc sz)
185     {
186         this.uri = URI.create(sz);
187         log.debug("URL connection factory using " + uri);
188
189         this.delegate = getDelegate(uri);
190     }
191
192     private static ConnectionFactory getDelegate(URI uri)
193     {
194         Class JavaDoc factory = (Class JavaDoc)factories.get(uri.getScheme());
195         if (factory == null)
196             throw new IllegalArgumentException JavaDoc("unrecognized scheme");
197         try
198         {
199             Constructor singleCons = factory.getConstructor(FACTORY_SINGLE_SIGNATURE);
200             ConnectionFactory delegate =
201                 (ConnectionFactory)singleCons.newInstance( new Object JavaDoc[] {uri} );
202
203             log.debug("URL delegating to " + delegate.getClass());
204             return delegate;
205         }
206         catch (Exception JavaDoc e) {
207             log.error("", e);;
208
209             // this shouldn't happen - we should have verified
210
// everything beforehand.
211
throw new java.lang.IllegalStateException JavaDoc(e.getMessage());
212         }
213     }
214
215     private void readObject(ObjectInputStream ois)
216         throws IOException, ClassNotFoundException JavaDoc
217     {
218         ois.defaultReadObject();
219         this.delegate = getDelegate(uri);
220     }
221
222     public Connection createConnection() throws JMSException
223     {
224         return delegate.createConnection();
225     }
226
227     public Connection createConnection(String JavaDoc p0, String JavaDoc p1) throws JMSException
228     {
229         return delegate.createConnection(p0, p1);
230     }
231
232     /**
233      * @deprecated
234      */

235     public TopicConnection createTopicConnection() throws JMSException
236     {
237         return ((TopicConnectionFactory)delegate).createTopicConnection();
238     }
239
240     /**
241      * @deprecated
242      */

243     public QueueConnection createQueueConnection() throws JMSException
244     {
245         return ((QueueConnectionFactory)delegate).createQueueConnection();
246     }
247
248     /**
249      * @deprecated
250      */

251     public TopicConnection createTopicConnection(String JavaDoc userName, String JavaDoc password) throws JMSException
252     {
253         return ((TopicConnectionFactory)delegate).createTopicConnection(userName, password);
254     }
255
256     /**
257      * @deprecated
258      */

259     public QueueConnection createQueueConnection(String JavaDoc userName, String JavaDoc password) throws JMSException
260     {
261         return ((QueueConnectionFactory)delegate).createQueueConnection(userName, password);
262     }
263
264 }
265
Popular Tags