KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > MuleManagedConnectionFactory


1 /*
2  * $Id: MuleManagedConnectionFactory.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.ra;
12
13 import java.beans.PropertyChangeListener JavaDoc;
14 import java.beans.PropertyChangeSupport JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.resource.ResourceException JavaDoc;
22 import javax.resource.spi.ConnectionManager JavaDoc;
23 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
24 import javax.resource.spi.ManagedConnection JavaDoc;
25 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
26 import javax.resource.spi.security.PasswordCredential JavaDoc;
27 import javax.security.auth.Subject JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * <code>MuleManagedConnectionFactory</code> TODO
34  */

35
36 public class MuleManagedConnectionFactory implements ManagedConnectionFactory JavaDoc
37 {
38     /**
39      * Serial version
40      */

41     private static final long serialVersionUID = -1460847590293644271L;
42
43     private transient PrintWriter JavaDoc out;
44     private transient PropertyChangeSupport JavaDoc changes = new PropertyChangeSupport JavaDoc(this);
45
46     // userName property value
47
private String JavaDoc username = null;
48
49     // password property value
50
private String JavaDoc password = null;
51
52     /**
53      * logger used by this class
54      */

55     protected transient Log logger = LogFactory.getLog(this.getClass());
56
57     public MuleManagedConnectionFactory()
58     {
59         super();
60     }
61
62     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
63     {
64         in.defaultReadObject();
65         this.logger = LogFactory.getLog(this.getClass());
66         this.changes = new PropertyChangeSupport JavaDoc(this);
67         this.out = null;
68     }
69
70     public int hashCode()
71     {
72         final int PRIME = 31;
73         int result = 1;
74         result = PRIME * result + ((password == null) ? 0 : password.hashCode());
75         return PRIME * result + ((username == null) ? 0 : username.hashCode());
76     }
77
78     public boolean equals(Object JavaDoc obj)
79     {
80         if (this == obj)
81         {
82             return true;
83         }
84
85         if (obj == null)
86         {
87             return false;
88         }
89
90         if (this.getClass() != obj.getClass())
91         {
92             return false;
93         }
94
95         final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory)obj;
96
97         if (password == null)
98         {
99             if (other.password != null)
100             {
101                 return false;
102             }
103         }
104         else if (!password.equals(other.password))
105         {
106             return false;
107         }
108
109         if (username == null)
110         {
111             if (other.username != null)
112             {
113                 return false;
114             }
115         }
116         else if (!username.equals(other.username))
117         {
118             return false;
119         }
120
121         return true;
122     }
123
124     /**
125      * Creates a Connection Factory instance. The ConnectionFactory instance is
126      * initialized with the passed ConnectionManager. In the managed scenario,
127      * ConnectionManager is provided by the application server.
128      *
129      * @param cxManager ConnectionManager to be associated with created EIS
130      * connection factory instance
131      * @return EIS-specific Connection Factory instance
132      * @throws javax.resource.ResourceException if the attempt to create a connection
133      * factory fails
134      */

135
136     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager) throws ResourceException JavaDoc
137     {
138         try
139         {
140             return new DefaultMuleConnectionFactory(this, cxManager, null);
141         }
142         catch (Exception JavaDoc e)
143         {
144             throw new ResourceException JavaDoc(e);
145         }
146     }
147
148     /**
149      * Creates a Connection Factory instance. The Connection Factory instance is
150      * initialized with a default ConnectionManager. In the non-managed scenario, the
151      * ConnectionManager is provided by the resource adapter.
152      *
153      * @return EIS-specific Connection Factory instance
154      * @throws ResourceException if the attempt to create a connection factory fails
155      */

156
157     public Object JavaDoc createConnectionFactory() throws ResourceException JavaDoc
158     {
159         return new DefaultMuleConnectionFactory(this, null, null);
160     }
161
162     /**
163      * ManagedConnectionFactory uses the security information (passed as Subject) and
164      * additional ConnectionRequestInfo (which is specific to ResourceAdapter and
165      * opaque to application server) to create this new connection.
166      *
167      * @param subject caller's security information
168      * @param cxRequestInfo additional resource adapter specific connection request
169      * information
170      * @return ManagedConnection instance
171      * @throws ResourceException if the attempt to create a connection fails
172      */

173
174     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxRequestInfo)
175         throws ResourceException JavaDoc
176     {
177         return new MuleManagedConnection(this, subject, cxRequestInfo);
178     }
179
180     /**
181      * Returns a matched managed connection from the candidate set of connections.
182      * ManagedConnectionFactory uses the security info (as in Subject) and
183      * information provided through ConnectionRequestInfo and additional Resource
184      * Adapter specific criteria to do matching. A MC that has the requested store is
185      * returned as a match
186      *
187      * @param connectionSet candidate connection set
188      * @param subject caller's security information
189      * @param cxRequestInfo additional resource adapter specific connection request
190      * information
191      * @return ManagedConnection if resource adapter finds an acceptable match,
192      * otherwise null
193      * @throws ResourceException if the match fails
194      */

195
196     public ManagedConnection JavaDoc matchManagedConnections(Set JavaDoc connectionSet,
197                                                      Subject JavaDoc subject,
198                                                      ConnectionRequestInfo JavaDoc cxRequestInfo)
199         throws ResourceException JavaDoc
200     {
201         PasswordCredential JavaDoc pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo);
202
203         Iterator JavaDoc it = connectionSet.iterator();
204         while (it.hasNext())
205         {
206             Object JavaDoc obj = it.next();
207             if (obj instanceof MuleManagedConnection)
208             {
209                 MuleManagedConnection mc = (MuleManagedConnection)obj;
210                 PasswordCredential JavaDoc mcpc = mc.getPasswordCredential();
211                 if (mcpc != null && pc != null && mcpc.equals(pc))
212                 {
213                     return mc;
214                 }
215             }
216         }
217         return null;
218     }
219
220     /**
221      * Sets the log writer for this ManagedConnectionFactory instance. The log writer
222      * is a character output stream to which all logging and tracing messages for
223      * this ManagedConnectionfactory instance will be printed.
224      *
225      * @param out an output stream for error logging and tracing
226      * @throws ResourceException if the method fails
227      */

228
229     public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
230     {
231         this.out = out;
232     }
233
234     /**
235      * Gets the log writer for this ManagedConnectionFactory instance.
236      *
237      * @return PrintWriter an output stream for error logging and tracing
238      * @throws ResourceException if the method fails
239      */

240
241     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
242     {
243         return this.out;
244     }
245
246     /**
247      * Associate PropertyChangeListener with the ManagedConnectionFactory, in order
248      * to notify about properties changes.
249      *
250      * @param lis the PropertyChangeListener to be associated with the
251      * ManagedConnectionFactory
252      */

253
254     public void addPropertyChangeListener(PropertyChangeListener JavaDoc lis)
255     {
256         changes.addPropertyChangeListener(lis);
257     }
258
259     /**
260      * Delete association of PropertyChangeListener with the
261      * ManagedConnectionFactory.
262      *
263      * @param lis the PropertyChangeListener to be removed
264      */

265
266     public void removePropertyChangeListener(PropertyChangeListener JavaDoc lis)
267     {
268         changes.removePropertyChangeListener(lis);
269     }
270
271     /**
272      * Returns the value of the userName property.
273      *
274      * @return the value of the userName property
275      */

276
277     public String JavaDoc getUsername()
278     {
279         return this.username;
280     }
281
282     /**
283      * Sets the value of the userName property.
284      *
285      * @param username String containing the value to be assigned to userName
286      */

287
288     public void setUsername(String JavaDoc username)
289     {
290         this.username = username;
291     }
292
293     /**
294      * Returns the value of the password property.
295      *
296      * @return the value of the password property
297      */

298
299     public String JavaDoc getPassword()
300     {
301         return this.password;
302     }
303
304     /**
305      * Sets the value of the password property.
306      *
307      * @param password String containing the value to be assigned to password
308      */

309
310     public void setPassword(String JavaDoc password)
311     {
312         this.password = password;
313     }
314 }
315
Popular Tags