1 45 package org.exolab.jms.client; 46 47 import java.io.Externalizable ; 48 import java.io.IOException ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectOutput ; 51 import java.lang.reflect.Constructor ; 52 import java.lang.reflect.InvocationTargetException ; 53 import java.util.ArrayList ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.util.Map ; 57 import javax.jms.Connection ; 58 import javax.jms.ConnectionFactory ; 59 import javax.jms.ExceptionListener ; 60 import javax.jms.JMSException ; 61 import javax.jms.JMSSecurityException ; 62 import javax.jms.QueueConnection ; 63 import javax.jms.QueueConnectionFactory ; 64 import javax.jms.TopicConnectionFactory ; 65 import javax.jms.TopicConnection ; 66 import javax.naming.Reference ; 67 import javax.naming.Referenceable ; 68 import javax.naming.StringRefAddr ; 69 70 71 79 public class JmsConnectionFactory 80 implements ConnectionFactory , QueueConnectionFactory , 81 TopicConnectionFactory , ExceptionListener , Externalizable , 82 Referenceable { 83 84 87 private String _className; 88 89 92 private Map _properties; 93 94 98 private Map _environment; 99 100 103 private JmsServerStubIfc _proxy; 104 105 108 private List _connections = new ArrayList (); 109 110 113 private static final long serialVersionUID = 3; 114 115 116 119 public JmsConnectionFactory() { 120 } 121 122 130 public JmsConnectionFactory(String className, Map properties, 131 Map environment) { 132 if (className == null) { 133 throw new IllegalArgumentException ("Argument 'className' is null"); 134 } 135 if (properties == null) { 136 throw new IllegalArgumentException ("Argument 'properties' is null"); 137 } 138 _className = className; 139 _properties = properties; 140 _environment = environment; 141 } 142 143 149 public synchronized JmsServerStubIfc getProxy() throws JMSException { 150 if (_proxy == null) { 151 try { 152 Class [] argTypes = {Map .class, Map .class}; 153 Object [] args = {_properties, _environment}; 154 155 Class factoryClass = Class.forName(_className); 156 Constructor constructor = 157 factoryClass.getDeclaredConstructor(argTypes); 158 _proxy = (JmsServerStubIfc) constructor.newInstance(args); 159 _proxy.setExceptionListener(this); 160 } catch (InvocationTargetException exception) { 161 if (exception.getTargetException() != null) { 162 throw new JMSException ("Failed to create proxy: " 163 + exception.getTargetException()); 164 } else { 165 throw new JMSException ("Failed to create proxy: " 166 + exception); 167 } 168 } catch (Exception exception) { 169 throw new JMSException ("Failed to create proxy: " 170 + exception); 171 } 172 } 173 174 return _proxy; 175 } 176 177 182 public void onException(JMSException exception) { 183 JmsConnection[] connections = getConnections(); 186 for (int i = 0; i < connections.length; ++i) { 187 JmsConnection connection = connections[i]; 188 connection.notifyExceptionListener(exception); 189 } 190 191 synchronized (this) { 192 _connections.clear(); 193 _proxy = null; 194 } 195 } 196 197 202 public Reference getReference() { 203 Reference reference = new Reference (getClass().getName(), 204 new StringRefAddr ("serverClass", 205 _className), 206 JmsConnectionFactoryBuilder.class.getName(), 207 null); 208 209 Iterator iterator = _properties.entrySet().iterator(); 211 while (iterator.hasNext()) { 212 Map.Entry entry = (Map.Entry ) iterator.next(); 213 String key = (String ) entry.getKey(); 214 String value = (String ) entry.getValue(); 215 reference.add(new StringRefAddr (key, value)); 216 } 217 218 return reference; 219 } 220 221 227 public void writeExternal(ObjectOutput stream) throws IOException { 228 stream.writeLong(serialVersionUID); 229 stream.writeObject(_className); 230 stream.writeObject(_properties); 231 } 232 233 241 public void readExternal(ObjectInput stream) 242 throws IOException , ClassNotFoundException { 243 long version = stream.readLong(); 244 if (version == serialVersionUID) { 245 _className = (String ) stream.readObject(); 246 _properties = (Map ) stream.readObject(); 247 } else { 248 throw new IOException (JmsConnectionFactory.class.getName() 249 + " with version " + version 250 + " is not supported."); 251 } 252 } 253 254 265 public Connection createConnection() throws JMSException { 266 return createConnection(null, null); 267 } 268 269 282 public Connection createConnection(String userName, String password) 283 throws JMSException { 284 JmsConnection connection = new JmsConnection(this, null, userName, 285 password); 286 addConnection(connection); 287 return connection; 288 } 289 290 299 public QueueConnection createQueueConnection() throws JMSException { 300 return createQueueConnection(null, null); 301 } 302 303 314 public QueueConnection createQueueConnection(String userName, 315 String password) 316 throws JMSException { 317 318 JmsQueueConnection connection = new JmsQueueConnection(this, null, 319 userName, 320 password); 321 addConnection(connection); 322 return connection; 323 } 324 325 334 public TopicConnection createTopicConnection() throws JMSException { 335 return createTopicConnection(null, null); 336 } 337 338 349 public TopicConnection createTopicConnection(String userName, 350 String password) 351 throws JMSException { 352 353 JmsTopicConnection connection = new JmsTopicConnection( 354 this, null, userName, password); 355 addConnection(connection); 356 return connection; 357 } 358 359 364 protected synchronized void addConnection(JmsConnection connection) { 365 _connections.add(connection); 366 } 367 368 373 protected synchronized void removeConnection(JmsConnection connection) { 374 _connections.remove(connection); 375 } 376 377 382 protected synchronized JmsConnection[] getConnections() { 383 return (JmsConnection[]) _connections.toArray(new JmsConnection[0]); 384 } 385 386 } 387 | Popular Tags |