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 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 [] FACTORY_SINGLE_SIGNATURE = new Class [] {URI.class}; 57 private static final Class [] FACTORY_MULTI_SIGNATURE = new Class [] {URI[].class}; 58 59 63 public static final String URL_REGISTRY_NAME = "/url-registry.properties"; 64 65 71 public static final String DEFAULT_UBERMQ_SCHEME = "ubermq"; 72 73 79 public static final String DEFAULT_UBERMQ_MULTICAST_SCHEME = "ubermq-lrmp"; 80 81 84 public static final String 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 Properties p = new Properties(); 94 try 95 { 96 InputStream is = URLConnectionFactory.class.getResourceAsStream(URL_REGISTRY_NAME); 97 p.load(is); 98 } 99 catch (NullPointerException npe) { 100 } 102 catch (Exception e) { 103 log.error("", e);; 104 } 105 106 registerFactory(DEFAULT_UBERMQ_SCHEME, UnicastConnectionFactory.class); 108 registerFactory(DEFAULT_UBERMQ_MULTICAST_SCHEME, MulticastTopicConnectionFactory.class); 109 registerFactory(DEFAULT_UBERMQ_SECURE_SCHEME, SSLConnectionFactory.class); 110 111 Iterator iter = p.keySet().iterator(); 113 while (iter.hasNext()) 114 { 115 String scheme = (String )iter.next(); 116 try 117 { 118 registerFactory(scheme, 119 Class.forName(p.getProperty(scheme))); 120 } 121 catch (ClassNotFoundException e) { 122 log.fatal("Factory class " + p.getProperty(scheme) + " not found for URL scheme " + scheme, e); 123 } 124 catch (Exception e) { 125 log.fatal("", e); 126 } 127 } 128 129 } 130 131 145 public static void registerFactory(String scheme, 146 Class clazz) 147 { 148 try 150 { 151 clazz.getConstructor(FACTORY_SINGLE_SIGNATURE); 152 153 Class [] 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 ("class must implement TopicConnectionFactory or ConnectionFactory"); 167 } 168 catch (NoSuchMethodException e) { 169 throw new IllegalArgumentException (e.getMessage()); 170 } 171 catch (SecurityException e) { 172 throw new IllegalArgumentException (e.getMessage()); 173 } 174 175 factories.put(scheme, clazz); 177 log.debug("Registered URL scheme " + scheme + " with factory class " + clazz); 178 } 179 180 184 public URLConnectionFactory(String 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 factory = (Class )factories.get(uri.getScheme()); 195 if (factory == null) 196 throw new IllegalArgumentException ("unrecognized scheme"); 197 try 198 { 199 Constructor singleCons = factory.getConstructor(FACTORY_SINGLE_SIGNATURE); 200 ConnectionFactory delegate = 201 (ConnectionFactory)singleCons.newInstance( new Object [] {uri} ); 202 203 log.debug("URL delegating to " + delegate.getClass()); 204 return delegate; 205 } 206 catch (Exception e) { 207 log.error("", e);; 208 209 throw new java.lang.IllegalStateException (e.getMessage()); 212 } 213 } 214 215 private void readObject(ObjectInputStream ois) 216 throws IOException, ClassNotFoundException 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 p0, String p1) throws JMSException 228 { 229 return delegate.createConnection(p0, p1); 230 } 231 232 235 public TopicConnection createTopicConnection() throws JMSException 236 { 237 return ((TopicConnectionFactory)delegate).createTopicConnection(); 238 } 239 240 243 public QueueConnection createQueueConnection() throws JMSException 244 { 245 return ((QueueConnectionFactory)delegate).createQueueConnection(); 246 } 247 248 251 public TopicConnection createTopicConnection(String userName, String password) throws JMSException 252 { 253 return ((TopicConnectionFactory)delegate).createTopicConnection(userName, password); 254 } 255 256 259 public QueueConnection createQueueConnection(String userName, String password) throws JMSException 260 { 261 return ((QueueConnectionFactory)delegate).createQueueConnection(userName, password); 262 } 263 264 } 265 | Popular Tags |