View Javadoc

1   /*
2    * $Id: MuleManagedConnectionFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.jca;
12  
13  import java.beans.PropertyChangeListener;
14  import java.beans.PropertyChangeSupport;
15  import java.io.IOException;
16  import java.io.ObjectInputStream;
17  import java.io.PrintWriter;
18  import java.util.Iterator;
19  import java.util.Set;
20  
21  import javax.resource.ResourceException;
22  import javax.resource.spi.ConnectionManager;
23  import javax.resource.spi.ConnectionRequestInfo;
24  import javax.resource.spi.ManagedConnection;
25  import javax.resource.spi.ManagedConnectionFactory;
26  import javax.resource.spi.security.PasswordCredential;
27  import javax.security.auth.Subject;
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
37  {
38      /**
39       * Serial version
40       */
41      private static final long serialVersionUID = -1460847590293644271L;
42  
43      private transient PrintWriter out;
44      private transient PropertyChangeSupport changes = new PropertyChangeSupport(this);
45  
46      // userName property value
47      private String username = null;
48  
49      // password property value
50      private String 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 in) throws IOException, ClassNotFoundException
63      {
64          in.defaultReadObject();
65          this.logger = LogFactory.getLog(this.getClass());
66          this.changes = new PropertyChangeSupport(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 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 createConnectionFactory(ConnectionManager cxManager) throws ResourceException
137     {
138         try
139         {
140             return new DefaultMuleConnectionFactory(this, cxManager, null);
141         }
142         catch (Exception e)
143         {
144             throw new ResourceException(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 createConnectionFactory() throws ResourceException
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 createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo)
175         throws ResourceException
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 matchManagedConnections(Set connectionSet,
197                                                      Subject subject,
198                                                      ConnectionRequestInfo cxRequestInfo)
199         throws ResourceException
200     {
201         PasswordCredential pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo);
202 
203         Iterator it = connectionSet.iterator();
204         while (it.hasNext())
205         {
206             Object obj = it.next();
207             if (obj instanceof MuleManagedConnection)
208             {
209                 MuleManagedConnection mc = (MuleManagedConnection)obj;
210                 PasswordCredential 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 out) throws ResourceException
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 getLogWriter() throws ResourceException
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 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 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 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 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 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 password)
311     {
312         this.password = password;
313     }
314 }