KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > JMSConnectionFactory


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms;
8
9 import java.io.Serializable JavaDoc;
10 import java.lang.reflect.Proxy JavaDoc;
11 import java.rmi.server.UnicastRemoteObject JavaDoc;
12 import javax.jms.Connection JavaDoc;
13 import javax.jms.ConnectionFactory JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15 import javax.jms.QueueConnection JavaDoc;
16 import javax.jms.QueueConnectionFactory JavaDoc;
17 import javax.jms.TopicConnection JavaDoc;
18 import javax.jms.TopicConnectionFactory JavaDoc;
19 import javax.jms.XAConnection JavaDoc;
20 import javax.jms.XAConnectionFactory JavaDoc;
21 import javax.jms.XAQueueConnection JavaDoc;
22 import javax.jms.XAQueueConnectionFactory JavaDoc;
23 import javax.jms.XATopicConnection JavaDoc;
24 import javax.jms.XATopicConnectionFactory JavaDoc;
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 /**
36  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
37  */

38
39 public class JMSConnectionFactory implements ConnectionFactory JavaDoc,
40         QueueConnectionFactory JavaDoc,
41         TopicConnectionFactory JavaDoc,
42         XAConnectionFactory JavaDoc,
43         XAQueueConnectionFactory JavaDoc,
44         XATopicConnectionFactory JavaDoc,
45         Serializable JavaDoc {
46
47     private transient JMSContainer container = null;
48
49     public Connection JavaDoc createConnection() throws JMSException JavaDoc {
50         return createConnection(null, null);
51     }
52
53     public Connection JavaDoc createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
54         return createConnection(userName, password, false);
55     }
56
57     public QueueConnection JavaDoc createQueueConnection() throws JMSException JavaDoc {
58         return (QueueConnection JavaDoc)createConnection();
59     }
60
61     public QueueConnection JavaDoc createQueueConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
62         return (QueueConnection JavaDoc) createConnection(userName,password);
63     }
64
65     public TopicConnection JavaDoc createTopicConnection() throws JMSException JavaDoc {
66         return (TopicConnection JavaDoc) createConnection();
67     }
68
69     public TopicConnection JavaDoc createTopicConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
70         return (TopicConnection JavaDoc) createConnection(userName,password);
71     }
72
73     public XAConnection JavaDoc createXAConnection() throws JMSException JavaDoc {
74         return createXAConnection(null, null);
75     }
76
77     public XAConnection JavaDoc createXAConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
78         return createConnection(userName, password, true);
79     }
80
81     public XAQueueConnection JavaDoc createXAQueueConnection() throws JMSException JavaDoc {
82         return (XAQueueConnection JavaDoc)createXAConnection();
83     }
84
85     public XAQueueConnection JavaDoc createXAQueueConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
86         return (XAQueueConnection JavaDoc) createXAConnection(userName,password);
87     }
88
89     public XATopicConnection JavaDoc createXATopicConnection() throws JMSException JavaDoc {
90         return (XATopicConnection JavaDoc) createXAConnection();
91     }
92
93     public XATopicConnection JavaDoc createXATopicConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
94         return (XATopicConnection JavaDoc) createXAConnection(userName,password);
95     }
96
97     /**
98      * @return
99      * @throws JMSException
100      */

101     protected synchronized JMSConnection createConnection(String JavaDoc userName, String JavaDoc password, boolean isXA) throws JMSException JavaDoc {
102
103 // String clientId = UUID.randomUUID().toString();
104
ObjectId clientId = new ObjectId(ObjectUUID.randomUUID());
105         if (container == null) {
106             // lookup ConnectorService from jndi, 然后使用动态代理封装成 JMSContainer 对象
107
ConnectorRemote remote = ConnectorHelper.lookupConnector(JNDIProperties.getProviderURL());
108             container = (JMSContainer) Proxy.newProxyInstance(this.getClass().getClassLoader(),
109                     new Class JavaDoc[]{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 JavaDoc e) {
120             e.printStackTrace();
121         }
122         return conn;
123     }
124
125     public static void main(String JavaDoc[] args) {
126
127     }
128 }
Popular Tags