Source code: org/activemq/ra/ActiveMQManagedConnectionFactory.java
1 /**
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.ra;
19
20 import java.io.PrintWriter;
21 import java.util.Iterator;
22 import java.util.Set;
23
24 import javax.jms.JMSException;
25 import javax.resource.ResourceException;
26 import javax.resource.spi.ConnectionManager;
27 import javax.resource.spi.ConnectionRequestInfo;
28 import javax.resource.spi.ManagedConnection;
29 import javax.resource.spi.ManagedConnectionFactory;
30 import javax.resource.spi.ResourceAdapter;
31 import javax.resource.spi.ResourceAdapterAssociation;
32 import javax.security.auth.Subject;
33
34 /**
35 * @version $Revision: 1.1.1.1 $
36 *
37 * @todo Must override equals and hashCode (JCA spec 16.4)
38 */
39 public class ActiveMQManagedConnectionFactory implements
40 ManagedConnectionFactory, ResourceAdapterAssociation {
41
42 private ActiveMQResourceAdapter adapter;
43 private PrintWriter logWriter;
44 private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo();
45
46 public void setResourceAdapter(ResourceAdapter adapter) throws ResourceException {
47 this.adapter = (ActiveMQResourceAdapter) adapter;
48 ActiveMQConnectionRequestInfo baseInfo = this.adapter.getInfo().copy();
49 if( info.getClientid()==null )
50 info.setClientid(baseInfo.getClientid());
51 if( info.getPassword()==null )
52 info.setPassword(baseInfo.getPassword());
53 if( info.getServerUrl()==null )
54 info.setServerUrl(baseInfo.getServerUrl());
55 if( info.getUseInboundSession()==null )
56 info.setUseInboundSession(baseInfo.getUseInboundSession());
57 if( info.getUserName()==null )
58 info.setUserName(baseInfo.getUserName());
59 }
60
61 public ResourceAdapter getResourceAdapter() {
62 return adapter;
63 }
64
65 /**
66 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
67 */
68 public Object createConnectionFactory(ConnectionManager manager) throws ResourceException {
69 return new ActiveMQConnectionFactory(this, manager, info);
70 }
71
72 /**
73 * This is used when not running in an app server. For now we are creating a
74 * ConnectionFactory that has our SimpleConnectionManager implementation but
75 * it may be a better idea to not support this. The JMS api will have many quirks
76 * the user may not expect when running through the resource adapter.
77 *
78 * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
79 */
80 public Object createConnectionFactory() throws ResourceException {
81 return new ActiveMQConnectionFactory(this, new SimpleConnectionManager(), info);
82 }
83
84 /**
85 * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject,
86 * javax.resource.spi.ConnectionRequestInfo)
87 */
88 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo info) throws ResourceException {
89 try {
90 ActiveMQConnectionRequestInfo amqInfo = (ActiveMQConnectionRequestInfo)info;
91 return new ActiveMQManagedConnection(subject, adapter.makeConnection(amqInfo), amqInfo);
92 } catch (JMSException e) {
93 throw new ResourceException("Could not create connection.", e);
94 }
95 }
96
97 /**
98 * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set,
99 * javax.security.auth.Subject,
100 * javax.resource.spi.ConnectionRequestInfo)
101 */
102 public ManagedConnection matchManagedConnections(Set connections, Subject subject, ConnectionRequestInfo info) throws ResourceException {
103 Iterator iterator = connections.iterator();
104 while (iterator.hasNext()) {
105 ActiveMQManagedConnection c = (ActiveMQManagedConnection) iterator.next();
106 if (c.matches(subject, info)) {
107 try {
108 c.associate(subject, (ActiveMQConnectionRequestInfo) info);
109 return c;
110 } catch (JMSException e) {
111 throw new ResourceException(e);
112 }
113 }
114 }
115 return null;
116 }
117
118 /**
119 * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter)
120 */
121 public void setLogWriter(PrintWriter logWriter) throws ResourceException {
122 this.logWriter = logWriter;
123 }
124
125 /**
126 * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter()
127 */
128 public PrintWriter getLogWriter() throws ResourceException {
129 return logWriter;
130 }
131
132 ///////////////////////////////////////////////////////////////////////////
133 //
134 // Bean setters and getters.
135 //
136 ///////////////////////////////////////////////////////////////////////////
137
138 public String getClientid() {
139 return info.getClientid();
140 }
141
142 public String getPassword() {
143 return info.getPassword();
144 }
145
146 public String getServerUrl() {
147 return info.getServerUrl();
148 }
149
150 public String getUserName() {
151 return info.getUserName();
152 }
153
154 public void setClientid(String clientid) {
155 info.setClientid(clientid);
156 }
157
158 public void setPassword(String password) {
159 info.setPassword(password);
160 }
161
162 public void setServerUrl(String url) {
163 info.setServerUrl(url);
164 }
165
166 public void setUserName(String userid) {
167 info.setUserName(userid);
168 }
169
170 public Boolean getUseInboundSession() {
171 return info.getUseInboundSession();
172 }
173
174 public void setUseInboundSession(Boolean useInboundSession) {
175 info.setUseInboundSession(useInboundSession);
176 }
177 }