KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.resource.ResourceException JavaDoc;
26 import javax.resource.spi.ConnectionManager JavaDoc;
27 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
28 import javax.resource.spi.ManagedConnection JavaDoc;
29 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
30 import javax.resource.spi.ResourceAdapter JavaDoc;
31 import javax.resource.spi.ResourceAdapterAssociation JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33
34 /**
35  * @version $Revisio n$
36  *
37  * TODO: Must override equals and hashCode (JCA spec 16.4)
38  *
39  * @org.apache.xbean.XBean element="managedConnectionFactory"
40  */

41 public class ActiveMQManagedConnectionFactory implements ManagedConnectionFactory JavaDoc, ResourceAdapterAssociation JavaDoc {
42
43     private static final long serialVersionUID = 6196921962230582875L;
44
45     private MessageResourceAdapter adapter;
46     private PrintWriter JavaDoc logWriter;
47     private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo();
48
49     /**
50      * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
51      */

52     public void setResourceAdapter(ResourceAdapter JavaDoc adapter) throws ResourceException JavaDoc {
53         if (!(adapter instanceof MessageResourceAdapter)) {
54             throw new ResourceException JavaDoc("ResourceAdapter is not of type: " + MessageResourceAdapter.class.getName());
55         }
56         this.adapter = (MessageResourceAdapter) adapter;
57         ActiveMQConnectionRequestInfo baseInfo = this.adapter.getInfo().copy();
58         if (info.getClientid() == null)
59             info.setClientid(baseInfo.getClientid());
60         if (info.getPassword() == null)
61             info.setPassword(baseInfo.getPassword());
62         if (info.getServerUrl() == null)
63             info.setServerUrl(baseInfo.getServerUrl());
64         if (info.getUseInboundSession() == null)
65             info.setUseInboundSession(baseInfo.getUseInboundSession());
66         if (info.getUserName() == null)
67             info.setUserName(baseInfo.getUserName());
68     }
69     
70     /**
71      * @see java.lang.Object#equals(java.lang.Object)
72      */

73     @Override JavaDoc
74     public boolean equals(Object JavaDoc object) {
75         if( object == null || object.getClass()!=ActiveMQManagedConnectionFactory.class ) {
76             return false;
77         }
78         return ((ActiveMQManagedConnectionFactory)object).info.equals(info);
79     }
80     
81     /**
82      * @see java.lang.Object#hashCode()
83      */

84     @Override JavaDoc
85     public int hashCode() {
86         return info.hashCode();
87     }
88     
89
90     /**
91      * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
92      */

93     public ResourceAdapter JavaDoc getResourceAdapter() {
94         return adapter;
95     }
96
97     /**
98      * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
99      */

100     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc manager) throws ResourceException JavaDoc {
101         return new ActiveMQConnectionFactory(this, manager, info);
102     }
103
104     /**
105      * This is used when not running in an app server. For now we are creating a
106      * ConnectionFactory that has our SimpleConnectionManager implementation but
107      * it may be a better idea to not support this. The JMS api will have many
108      * quirks the user may not expect when running through the resource adapter.
109      *
110      * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
111      */

112     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc {
113         return new ActiveMQConnectionFactory(this, new SimpleConnectionManager(), info);
114     }
115
116     /**
117      * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject,
118      * javax.resource.spi.ConnectionRequestInfo)
119      */

120     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
121         try {
122             if( info == null ) {
123                 info = this.info;
124             }
125             ActiveMQConnectionRequestInfo amqInfo = (ActiveMQConnectionRequestInfo) info;
126             return new ActiveMQManagedConnection(subject, adapter.makeConnection(amqInfo), amqInfo);
127         }
128         catch (JMSException JavaDoc e) {
129             throw new ResourceException JavaDoc("Could not create connection.", e);
130         }
131     }
132
133     /**
134      * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set,
135      * javax.security.auth.Subject,
136      * javax.resource.spi.ConnectionRequestInfo)
137      */

138     public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connections, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
139         Iterator JavaDoc iterator = connections.iterator();
140         while (iterator.hasNext()) {
141             ActiveMQManagedConnection c = (ActiveMQManagedConnection) iterator.next();
142             if (c.matches(subject, info)) {
143                 try {
144                     c.associate(subject, (ActiveMQConnectionRequestInfo) info);
145                     return c;
146                 }
147                 catch (JMSException JavaDoc e) {
148                     throw new ResourceException JavaDoc(e);
149                 }
150             }
151         }
152         return null;
153     }
154
155     /**
156      * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter)
157      */

158     public void setLogWriter(PrintWriter JavaDoc logWriter) throws ResourceException JavaDoc {
159         this.logWriter = logWriter;
160     }
161
162     /**
163      * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter()
164      */

165     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
166         return logWriter;
167     }
168
169     // /////////////////////////////////////////////////////////////////////////
170
//
171
// Bean setters and getters.
172
//
173
// /////////////////////////////////////////////////////////////////////////
174

175     /**
176      *
177      */

178     public String JavaDoc getClientid() {
179         return info.getClientid();
180     }
181
182     /**
183      *
184      */

185     public String JavaDoc getPassword() {
186         return info.getPassword();
187     }
188
189     /**
190      *
191      */

192     public String JavaDoc getUserName() {
193         return info.getUserName();
194     }
195
196     /**
197      *
198      */

199     public void setClientid(String JavaDoc clientid) {
200         info.setClientid(clientid);
201     }
202
203     /**
204      *
205      */

206     public void setPassword(String JavaDoc password) {
207         info.setPassword(password);
208     }
209
210     /**
211      *
212      */

213     public void setUserName(String JavaDoc userid) {
214         info.setUserName(userid);
215     }
216
217     /**
218      *
219      */

220     /**
221      *
222      */

223     public Boolean JavaDoc getUseInboundSession() {
224         return info.getUseInboundSession();
225     }
226
227     /**
228      *
229      */

230     public void setUseInboundSession(Boolean JavaDoc useInboundSession) {
231         info.setUseInboundSession(useInboundSession);
232     }
233
234     /**
235      *
236      */

237     public boolean isUseInboundSessionEnabled() {
238         return info.isUseInboundSessionEnabled();
239     }
240
241     // Redelivery policy configuration
242
/**
243      *
244      */

245     public Long JavaDoc getInitialRedeliveryDelay() {
246         return info.getInitialRedeliveryDelay();
247     }
248
249     /**
250      *
251      */

252     public Integer JavaDoc getMaximumRedeliveries() {
253         return info.getMaximumRedeliveries();
254     }
255
256     /**
257      *
258      */

259     public Short JavaDoc getRedeliveryBackOffMultiplier() {
260         return info.getRedeliveryBackOffMultiplier();
261     }
262
263     /**
264      *
265      */

266     public Boolean JavaDoc getRedeliveryUseExponentialBackOff() {
267         return info.getRedeliveryUseExponentialBackOff();
268     }
269
270     /**
271      *
272      */

273     public void setInitialRedeliveryDelay(Long JavaDoc value) {
274         info.setInitialRedeliveryDelay(value);
275     }
276
277     /**
278      *
279      */

280     public void setMaximumRedeliveries(Integer JavaDoc value) {
281         info.setMaximumRedeliveries(value);
282     }
283
284     /**
285      *
286      */

287     public void setRedeliveryBackOffMultiplier(Short JavaDoc value) {
288         info.setRedeliveryBackOffMultiplier(value);
289     }
290
291     /**
292      *
293      */

294     public void setRedeliveryUseExponentialBackOff(Boolean JavaDoc value) {
295         info.setRedeliveryUseExponentialBackOff(value);
296     }
297
298
299     // Prefetch policy configuration
300
/**
301      *
302      */

303     public Integer JavaDoc getDurableTopicPrefetch() {
304         return info.getDurableTopicPrefetch();
305     }
306
307     /**
308      *
309      */

310     public Integer JavaDoc getInputStreamPrefetch() {
311         return info.getInputStreamPrefetch();
312     }
313
314     /**
315      *
316      */

317     public Integer JavaDoc getQueueBrowserPrefetch() {
318         return info.getQueueBrowserPrefetch();
319     }
320
321     /**
322      *
323      */

324     public Integer JavaDoc getQueuePrefetch() {
325         return info.getQueuePrefetch();
326     }
327
328     /**
329      *
330      */

331     public Integer JavaDoc getTopicPrefetch() {
332         return info.getTopicPrefetch();
333     }
334
335     /**
336      *
337      */

338     public void setAllPrefetchValues(Integer JavaDoc i) {
339         info.setAllPrefetchValues(i);
340     }
341
342     /**
343      *
344      */

345     public void setDurableTopicPrefetch(Integer JavaDoc durableTopicPrefetch) {
346         info.setDurableTopicPrefetch(durableTopicPrefetch);
347     }
348
349     /**
350      *
351      */

352     public void setInputStreamPrefetch(Integer JavaDoc inputStreamPrefetch) {
353         info.setInputStreamPrefetch(inputStreamPrefetch);
354     }
355
356     /**
357      *
358      */

359     public void setQueueBrowserPrefetch(Integer JavaDoc queueBrowserPrefetch) {
360         info.setQueueBrowserPrefetch(queueBrowserPrefetch);
361     }
362
363     /**
364      *
365      */

366     public void setQueuePrefetch(Integer JavaDoc queuePrefetch) {
367         info.setQueuePrefetch(queuePrefetch);
368     }
369
370     /**
371      * @param topicPrefetch
372      */

373     public void setTopicPrefetch(Integer JavaDoc topicPrefetch) {
374         info.setTopicPrefetch(topicPrefetch);
375     }
376 }
377
Popular Tags