View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.jca;
8   
9   import java.beans.PropertyChangeListener;
10  import java.beans.PropertyChangeSupport;
11  import java.io.IOException;
12  import java.io.ObjectInputStream;
13  import java.io.PrintWriter;
14  import java.util.Iterator;
15  import java.util.Set;
16  
17  import javax.resource.ResourceException;
18  import javax.resource.spi.ConnectionManager;
19  import javax.resource.spi.ConnectionRequestInfo;
20  import javax.resource.spi.ManagedConnection;
21  import javax.resource.spi.ManagedConnectionFactory;
22  import javax.resource.spi.security.PasswordCredential;
23  import javax.security.auth.Subject;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * <code>MuleManagedConnectionFactory</code> TODO
30   */
31  
32  public class MuleManagedConnectionFactory implements ManagedConnectionFactory
33  {
34      /**
35       * Serial version
36       */
37      private static final long serialVersionUID = -1460847590293644271L;
38  
39      private transient PrintWriter out;
40      private transient PropertyChangeSupport changes = new PropertyChangeSupport(this);
41  
42      // userName property value
43      private String username = null;
44  
45      // password property value
46      private String password = null;
47  
48      /**
49       * logger used by this class
50       */
51      protected transient Log logger = LogFactory.getLog(this.getClass());
52  
53      public MuleManagedConnectionFactory()
54      {
55          super();
56      }
57  
58      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
59      {
60          in.defaultReadObject();
61          this.logger = LogFactory.getLog(this.getClass());
62          this.changes = new PropertyChangeSupport(this);
63          this.out = null;
64      }
65  
66      public int hashCode()
67      {
68          final int PRIME = 31;
69          int result = 1;
70          result = PRIME * result + ((password == null) ? 0 : password.hashCode());
71          return PRIME * result + ((username == null) ? 0 : username.hashCode());
72      }
73  
74      public boolean equals(Object obj)
75      {
76          if (this == obj)
77          {
78              return true;
79          }
80  
81          if (obj == null)
82          {
83              return false;
84          }
85  
86          if (this.getClass() != obj.getClass())
87          {
88              return false;
89          }
90  
91          final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory)obj;
92  
93          if (password == null)
94          {
95              if (other.password != null)
96              {
97                  return false;
98              }
99          }
100         else if (!password.equals(other.password))
101         {
102             return false;
103         }
104 
105         if (username == null)
106         {
107             if (other.username != null)
108             {
109                 return false;
110             }
111         }
112         else if (!username.equals(other.username))
113         {
114             return false;
115         }
116 
117         return true;
118     }
119 
120     /**
121      * Creates a Connection Factory instance. The ConnectionFactory instance is
122      * initialized with the passed ConnectionManager. In the managed scenario,
123      * ConnectionManager is provided by the application server.
124      * 
125      * @param cxManager ConnectionManager to be associated with created EIS
126      *            connection factory instance
127      * @return EIS-specific Connection Factory instance
128      * @throws javax.resource.ResourceException if the attempt to create a connection
129      *             factory fails
130      */
131 
132     public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException
133     {
134         try
135         {
136             return new DefaultMuleConnectionFactory(this, cxManager, null);
137         }
138         catch (Exception e)
139         {
140             throw new ResourceException(e);
141         }
142     }
143 
144     /**
145      * Creates a Connection Factory instance. The Connection Factory instance is
146      * initialized with a default ConnectionManager. In the non-managed scenario, the
147      * ConnectionManager is provided by the resource adapter.
148      * 
149      * @return EIS-specific Connection Factory instance
150      * @throws ResourceException if the attempt to create a connection factory fails
151      */
152 
153     public Object createConnectionFactory() throws ResourceException
154     {
155         return new DefaultMuleConnectionFactory(this, null, null);
156     }
157 
158     /**
159      * ManagedConnectionFactory uses the security information (passed as Subject) and
160      * additional ConnectionRequestInfo (which is specific to ResourceAdapter and
161      * opaque to application server) to create this new connection.
162      * 
163      * @param subject caller's security information
164      * @param cxRequestInfo additional resource adapter specific connection request
165      *            information
166      * @return ManagedConnection instance
167      * @throws ResourceException if the attempt to create a connection fails
168      */
169 
170     public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo cxRequestInfo)
171         throws ResourceException
172     {
173         return new MuleManagedConnection(this, subject, cxRequestInfo);
174     }
175 
176     /**
177      * Returns a matched managed connection from the candidate set of connections.
178      * ManagedConnectionFactory uses the security info (as in Subject) and
179      * information provided through ConnectionRequestInfo and additional Resource
180      * Adapter specific criteria to do matching. A MC that has the requested store is
181      * returned as a match
182      * 
183      * @param connectionSet candidate connection set
184      * @param subject caller's security information
185      * @param cxRequestInfo additional resource adapter specific connection request
186      *            information
187      * @return ManagedConnection if resource adapter finds an acceptable match,
188      *         otherwise null
189      * @throws ResourceException if the match fails
190      */
191 
192     public ManagedConnection matchManagedConnections(Set connectionSet,
193                                                      Subject subject,
194                                                      ConnectionRequestInfo cxRequestInfo)
195         throws ResourceException
196     {
197         PasswordCredential pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo);
198 
199         Iterator it = connectionSet.iterator();
200         while (it.hasNext())
201         {
202             Object obj = it.next();
203             if (obj instanceof MuleManagedConnection)
204             {
205                 MuleManagedConnection mc = (MuleManagedConnection)obj;
206                 PasswordCredential mcpc = mc.getPasswordCredential();
207                 if (mcpc != null && pc != null && mcpc.equals(pc))
208                 {
209                     return mc;
210                 }
211             }
212         }
213         return null;
214     }
215 
216     /**
217      * Sets the log writer for this ManagedConnectionFactory instance. The log writer
218      * is a character output stream to which all logging and tracing messages for
219      * this ManagedConnectionfactory instance will be printed.
220      * 
221      * @param out an output stream for error logging and tracing
222      * @throws ResourceException if the method fails
223      */
224 
225     public void setLogWriter(PrintWriter out) throws ResourceException
226     {
227         this.out = out;
228     }
229 
230     /**
231      * Gets the log writer for this ManagedConnectionFactory instance.
232      * 
233      * @return PrintWriter an output stream for error logging and tracing
234      * @throws ResourceException if the method fails
235      */
236 
237     public PrintWriter getLogWriter() throws ResourceException
238     {
239         return this.out;
240     }
241 
242     /**
243      * Associate PropertyChangeListener with the ManagedConnectionFactory, in order
244      * to notify about properties changes.
245      * 
246      * @param lis the PropertyChangeListener to be associated with the
247      *            ManagedConnectionFactory
248      */
249 
250     public void addPropertyChangeListener(PropertyChangeListener lis)
251     {
252         changes.addPropertyChangeListener(lis);
253     }
254 
255     /**
256      * Delete association of PropertyChangeListener with the
257      * ManagedConnectionFactory.
258      * 
259      * @param lis the PropertyChangeListener to be removed
260      */
261 
262     public void removePropertyChangeListener(PropertyChangeListener lis)
263     {
264         changes.removePropertyChangeListener(lis);
265     }
266 
267     /**
268      * Returns the value of the userName property.
269      * 
270      * @return the value of the userName property
271      */
272 
273     public String getUsername()
274     {
275         return this.username;
276     }
277 
278     /**
279      * Sets the value of the userName property.
280      * 
281      * @param username String containing the value to be assigned to userName
282      */
283 
284     public void setUsername(String username)
285     {
286         this.username = username;
287     }
288 
289     /**
290      * Returns the value of the password property.
291      * 
292      * @return the value of the password property
293      */
294 
295     public String getPassword()
296     {
297         return this.password;
298     }
299 
300     /**
301      * Sets the value of the password property.
302      * 
303      * @param password String containing the value to be assigned to password
304      */
305 
306     public void setPassword(String password)
307     {
308         this.password = password;
309     }
310 }