1 6 7 package org.jfox.jms; 8 9 import java.io.Serializable ; 10 import java.lang.reflect.Proxy ; 11 import java.rmi.server.UnicastRemoteObject ; 12 import javax.jms.Connection ; 13 import javax.jms.ConnectionFactory ; 14 import javax.jms.JMSException ; 15 import javax.jms.QueueConnection ; 16 import javax.jms.QueueConnectionFactory ; 17 import javax.jms.TopicConnection ; 18 import javax.jms.TopicConnectionFactory ; 19 import javax.jms.XAConnection ; 20 import javax.jms.XAConnectionFactory ; 21 import javax.jms.XAQueueConnection ; 22 import javax.jms.XAQueueConnectionFactory ; 23 import javax.jms.XATopicConnection ; 24 import javax.jms.XATopicConnectionFactory ; 25 26 import org.jfox.ioc.connector.ConnectorHelper; 27 import org.jfox.ioc.connector.ConnectorRemote; 28 import org.jfox.ioc.connector.ObjectId; 29 import org.jfox.ioc.util.Marshaller; 30 import org.jfox.ioc.util.ObjectUUID; 31 import org.jfox.jms.connector.JMSConnectorInvoker; 32 import org.jfox.jms.connector.JMSContainer; 33 import org.jfox.jndi.JNDIProperties; 34 35 38 39 public class JMSConnectionFactory implements ConnectionFactory , 40 QueueConnectionFactory , 41 TopicConnectionFactory , 42 XAConnectionFactory , 43 XAQueueConnectionFactory , 44 XATopicConnectionFactory , 45 Serializable { 46 47 private transient JMSContainer container = null; 48 49 public Connection createConnection() throws JMSException { 50 return createConnection(null, null); 51 } 52 53 public Connection createConnection(String userName, String password) throws JMSException { 54 return createConnection(userName, password, false); 55 } 56 57 public QueueConnection createQueueConnection() throws JMSException { 58 return (QueueConnection )createConnection(); 59 } 60 61 public QueueConnection createQueueConnection(String userName, String password) throws JMSException { 62 return (QueueConnection ) createConnection(userName,password); 63 } 64 65 public TopicConnection createTopicConnection() throws JMSException { 66 return (TopicConnection ) createConnection(); 67 } 68 69 public TopicConnection createTopicConnection(String userName, String password) throws JMSException { 70 return (TopicConnection ) createConnection(userName,password); 71 } 72 73 public XAConnection createXAConnection() throws JMSException { 74 return createXAConnection(null, null); 75 } 76 77 public XAConnection createXAConnection(String userName, String password) throws JMSException { 78 return createConnection(userName, password, true); 79 } 80 81 public XAQueueConnection createXAQueueConnection() throws JMSException { 82 return (XAQueueConnection )createXAConnection(); 83 } 84 85 public XAQueueConnection createXAQueueConnection(String userName, String password) throws JMSException { 86 return (XAQueueConnection ) createXAConnection(userName,password); 87 } 88 89 public XATopicConnection createXATopicConnection() throws JMSException { 90 return (XATopicConnection ) createXAConnection(); 91 } 92 93 public XATopicConnection createXATopicConnection(String userName, String password) throws JMSException { 94 return (XATopicConnection ) createXAConnection(userName,password); 95 } 96 97 101 protected synchronized JMSConnection createConnection(String userName, String password, boolean isXA) throws JMSException { 102 103 ObjectId clientId = new ObjectId(ObjectUUID.randomUUID()); 105 if (container == null) { 106 ConnectorRemote remote = ConnectorHelper.lookupConnector(JNDIProperties.getProviderURL()); 108 container = (JMSContainer) Proxy.newProxyInstance(this.getClass().getClassLoader(), 109 new Class []{JMSContainer.class}, 110 new JMSConnectorInvoker(clientId, remote)); 111 } 112 113 container.auth(userName, password); 114 115 JMSConnection conn = new JMSConnection(clientId.toString(), container, isXA); 116 try { 117 UnicastRemoteObject.exportObject(conn); 118 container.registerConnection(clientId.toString(), Marshaller.marshall(conn)); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 } 122 return conn; 123 } 124 125 public static void main(String [] args) { 126 127 } 128 } | Popular Tags |