KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > ra > ManagedConnectionFactoryImpl


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.ra;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51
52 import java.io.PrintWriter JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Set JavaDoc;
55
56 import javax.jms.JMSException JavaDoc;
57 import javax.resource.ResourceException JavaDoc;
58 import javax.resource.spi.ConnectionManager JavaDoc;
59 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
60 import javax.resource.spi.ManagedConnection JavaDoc;
61 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
62 import javax.resource.spi.ResourceAdapter JavaDoc;
63 import javax.resource.spi.ResourceAdapterAssociation JavaDoc;
64 import javax.security.auth.Subject JavaDoc;
65
66 /**
67  * @version $Revision: 1.1.1.1 $
68  *
69  * @todo Must override equals and hashCode (JCA spec 16.4)
70  */

71 public class ManagedConnectionFactoryImpl implements
72         ManagedConnectionFactory JavaDoc, ResourceAdapterAssociation JavaDoc {
73
74     private static final long serialVersionUID = 3545800978927334450L;
75     private static final Log log = LogFactory.getLog(ManagedConnectionFactoryImpl.class);
76     
77     private ResourceAdapterImpl adapter = null;
78     private PrintWriter JavaDoc logWriter = null;
79     private ConnectionRequestInfoImpl info;
80     
81     
82     /**
83      * JavaBean requirement for null constructor
84      */

85     public ManagedConnectionFactoryImpl() {
86         info = new ConnectionRequestInfoImpl();
87     }
88     
89     /////////////////////////////////////////////////////////////////////
90
//
91
// Implement javax.resource.spi.ManagedConnectionFactory interface
92
//
93
/////////////////////////////////////////////////////////////////////
94

95     /**
96      * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
97      */

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

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

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

135     public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connections,
136                                                      Subject JavaDoc subject,
137                                                      ConnectionRequestInfo JavaDoc info)
138                                                      throws ResourceException JavaDoc
139     {
140         Iterator JavaDoc iterator = connections.iterator();
141         while (iterator.hasNext()) {
142             ManagedConnectionImpl mc = (ManagedConnectionImpl) iterator.next();
143             // in our case all the connections suppose to match
144
if (mc.matches(subject, info)) {
145                 //try {
146
// mc.associate(subject, (ConnectionRequestInfoImpl) info);
147
return mc;
148                 //} catch (JMSException e) {
149
// throw new ResourceException(e);
150
//}
151
}
152         }
153         return null;
154     }
155
156     /**
157      * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter)
158      */

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

166     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
167         return logWriter;
168     }
169     
170     
171     /////////////////////////////////////////////////////////////////////
172
//
173
// Implement javax.resource.spi.ResourceAdapterAssociation interface
174
//
175
/////////////////////////////////////////////////////////////////////
176

177     /**
178      * @see javax.resource.spi.ResourceAdapterAssociation#setResourceAdapter(javax.resource.spi.ResourceAdapter)
179      */

180     public void setResourceAdapter(ResourceAdapter JavaDoc adapter) throws ResourceException JavaDoc {
181         this.adapter = (ResourceAdapterImpl) adapter;
182         ConnectionRequestInfoImpl baseInfo = this.adapter.getInfo().copy();
183         if (info.getPassword() == null)
184             info.setPassword(baseInfo.getPassword());
185         if (info.getUserName() == null)
186             info.setUserName(baseInfo.getUserName());
187     }
188     
189     /**
190      * @see javax.resource.spi.ResourceAdapterAssociation#getResourceAdapter()
191      */

192     public ResourceAdapter JavaDoc getResourceAdapter() {
193         return adapter;
194     }
195     
196     
197     /**
198      * Need to override hashCode(). See Spec section 6.5.3.2.
199      */

200     public int hashCode() {
201         return info.hashCode();
202     }
203     
204     /**
205      * Need to override equals(). See Spec section 6.5.3.2.
206      */

207     public boolean equals(Object JavaDoc o) {
208         if (o == null) {
209             return false;
210         }
211         if (!this.getClass().equals(o.getClass())) {
212             return false;
213         }
214         ManagedConnectionFactoryImpl otherFactory = (ManagedConnectionFactoryImpl)o;
215         return this.info.equals(otherFactory.info);
216     }
217
218     
219     //////////////////////////////////////////
220
//
221
// Implement JavaBean setters and getters
222
//
223
//////////////////////////////////////////
224

225     public String JavaDoc getPassword() {
226         return info.getPassword();
227     }
228     
229     public void setPassword(String JavaDoc password) {
230         info.setPassword(password);
231     }
232
233     public String JavaDoc getUserName() {
234         return info.getUserName();
235     }
236     
237     public void setUserName(String JavaDoc userid) {
238         info.setUserName(userid);
239     }
240
241 }
242
Popular Tags