KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ActiveMQConnectionFactory


1     /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.ConnectionFactory JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.QueueConnectionFactory JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.TopicConnectionFactory JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.resource.Referenceable JavaDoc;
32 import javax.resource.ResourceException JavaDoc;
33 import javax.resource.spi.ConnectionManager JavaDoc;
34 import java.io.Serializable JavaDoc;
35
36
37 /**
38  * @version $Revision$
39  */

40 public class ActiveMQConnectionFactory implements ConnectionFactory JavaDoc, QueueConnectionFactory JavaDoc, TopicConnectionFactory JavaDoc, Referenceable JavaDoc, Serializable JavaDoc {
41
42     private static final long serialVersionUID = -5754338187296859149L;
43
44     private static final Log log = LogFactory.getLog(ActiveMQConnectionFactory.class);
45     private ConnectionManager JavaDoc manager;
46     transient private ActiveMQManagedConnectionFactory factory;
47     private Reference JavaDoc reference;
48     private final ActiveMQConnectionRequestInfo info;
49
50
51     /**
52      * @param factory
53      * @param manager
54      * @param info
55      */

56     public ActiveMQConnectionFactory(ActiveMQManagedConnectionFactory factory, ConnectionManager JavaDoc manager, ActiveMQConnectionRequestInfo info) {
57         this.factory = factory;
58         this.manager = manager;
59         this.info = info;
60     }
61
62     /**
63      * @see javax.jms.ConnectionFactory#createConnection()
64      */

65     public Connection JavaDoc createConnection() throws JMSException JavaDoc {
66         return createConnection(info.copy());
67     }
68
69     /**
70      * @see javax.jms.ConnectionFactory#createConnection(java.lang.String, java.lang.String)
71      */

72     public Connection JavaDoc createConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
73         ActiveMQConnectionRequestInfo i = info.copy();
74         i.setUserName(userName);
75         i.setPassword(password);
76         return createConnection(i);
77     }
78
79     /**
80      * @param info
81      * @return
82      * @throws JMSException
83      */

84     private Connection JavaDoc createConnection(ActiveMQConnectionRequestInfo info) throws JMSException JavaDoc {
85         try {
86             if( info.isUseInboundSessionEnabled() ) {
87                 return new InboundConnectionProxy();
88             }
89             if (manager == null) {
90                 throw new JMSException JavaDoc("No JCA ConnectionManager configured! Either enable UseInboundSessionEnabled or get your JCA container to configure one.");
91             }
92             return (Connection JavaDoc) manager.allocateConnection(factory, info);
93         }
94         catch (ResourceException JavaDoc e) {
95             // Throw the root cause if it was a JMSException..
96
if (e.getCause() instanceof JMSException JavaDoc) {
97                 throw (JMSException JavaDoc) e.getCause();
98             }
99             log.debug("Connection could not be created:", e);
100             throw new JMSException JavaDoc(e.getMessage());
101         }
102     }
103
104     /**
105      * @see javax.naming.Referenceable#getReference()
106      */

107     public Reference JavaDoc getReference() {
108         return reference;
109     }
110
111     /**
112      * @see javax.resource.Referenceable#setReference(javax.naming.Reference)
113      */

114     public void setReference(Reference JavaDoc reference) {
115         this.reference = reference;
116     }
117
118     public QueueConnection JavaDoc createQueueConnection() throws JMSException JavaDoc {
119         return (QueueConnection JavaDoc) createConnection();
120     }
121
122     public QueueConnection JavaDoc createQueueConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
123         return (QueueConnection JavaDoc) createConnection(userName, password);
124     }
125
126     public TopicConnection JavaDoc createTopicConnection() throws JMSException JavaDoc {
127         return (TopicConnection JavaDoc) createConnection();
128     }
129
130     public TopicConnection JavaDoc createTopicConnection(String JavaDoc userName, String JavaDoc password) throws JMSException JavaDoc {
131         return (TopicConnection JavaDoc) createConnection(userName, password);
132     }
133 }
134
Popular Tags