Coverage Report - org.mule.module.jca.MuleManagedConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleManagedConnectionFactory
0%
0/62
0%
0/32
2.438
 
 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  0
     private transient PropertyChangeSupport changes = new PropertyChangeSupport(this);
 41  
 
 42  
     // userName property value
 43  0
     private String username = null;
 44  
 
 45  
     // password property value
 46  0
     private String password = null;
 47  
 
 48  
     /**
 49  
      * logger used by this class
 50  
      */
 51  0
     protected transient Log logger = LogFactory.getLog(this.getClass());
 52  
 
 53  
     public MuleManagedConnectionFactory()
 54  
     {
 55  0
         super();
 56  0
     }
 57  
 
 58  
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
 59  
     {
 60  0
         in.defaultReadObject();
 61  0
         this.logger = LogFactory.getLog(this.getClass());
 62  0
         this.changes = new PropertyChangeSupport(this);
 63  0
         this.out = null;
 64  0
     }
 65  
 
 66  
     public int hashCode()
 67  
     {
 68  0
         final int PRIME = 31;
 69  0
         int result = 1;
 70  0
         result = PRIME * result + ((password == null) ? 0 : password.hashCode());
 71  0
         return PRIME * result + ((username == null) ? 0 : username.hashCode());
 72  
     }
 73  
 
 74  
     public boolean equals(Object obj)
 75  
     {
 76  0
         if (this == obj)
 77  
         {
 78  0
             return true;
 79  
         }
 80  
 
 81  0
         if (obj == null)
 82  
         {
 83  0
             return false;
 84  
         }
 85  
 
 86  0
         if (this.getClass() != obj.getClass())
 87  
         {
 88  0
             return false;
 89  
         }
 90  
 
 91  0
         final MuleManagedConnectionFactory other = (MuleManagedConnectionFactory)obj;
 92  
 
 93  0
         if (password == null)
 94  
         {
 95  0
             if (other.password != null)
 96  
             {
 97  0
                 return false;
 98  
             }
 99  
         }
 100  0
         else if (!password.equals(other.password))
 101  
         {
 102  0
             return false;
 103  
         }
 104  
 
 105  0
         if (username == null)
 106  
         {
 107  0
             if (other.username != null)
 108  
             {
 109  0
                 return false;
 110  
             }
 111  
         }
 112  0
         else if (!username.equals(other.username))
 113  
         {
 114  0
             return false;
 115  
         }
 116  
 
 117  0
         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  0
             return new DefaultMuleConnectionFactory(this, cxManager, null);
 137  
         }
 138  0
         catch (Exception e)
 139  
         {
 140  0
             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  0
         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  0
         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  0
         PasswordCredential pc = RaHelper.getPasswordCredential(this, subject, cxRequestInfo);
 198  
 
 199  0
         Iterator it = connectionSet.iterator();
 200  0
         while (it.hasNext())
 201  
         {
 202  0
             Object obj = it.next();
 203  0
             if (obj instanceof MuleManagedConnection)
 204  
             {
 205  0
                 MuleManagedConnection mc = (MuleManagedConnection)obj;
 206  0
                 PasswordCredential mcpc = mc.getPasswordCredential();
 207  0
                 if (mcpc != null && pc != null && mcpc.equals(pc))
 208  
                 {
 209  0
                     return mc;
 210  
                 }
 211  
             }
 212  0
         }
 213  0
         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  0
         this.out = out;
 228  0
     }
 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  0
         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  0
         changes.addPropertyChangeListener(lis);
 253  0
     }
 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  0
         changes.removePropertyChangeListener(lis);
 265  0
     }
 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  0
         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  0
         this.username = username;
 287  0
     }
 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  0
         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  0
         this.password = password;
 309  0
     }
 310  
 }