Coverage Report - org.mule.module.jca.RaHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
RaHelper
0%
0/15
0%
0/10
7
RaHelper$1
0%
0/11
0%
0/8
7
 
 1  
 /*
 2  
  * $Id: RaHelper.java 10789 2008-02-12 20:04:43Z dfeist $
 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 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 org.mule.config.i18n.CoreMessages;
 14  
 
 15  
 import java.security.AccessController;
 16  
 import java.security.PrivilegedAction;
 17  
 import java.util.Iterator;
 18  
 import java.util.Set;
 19  
 
 20  
 import javax.resource.ResourceException;
 21  
 import javax.resource.spi.ConnectionRequestInfo;
 22  
 import javax.resource.spi.ManagedConnectionFactory;
 23  
 import javax.resource.spi.security.PasswordCredential;
 24  
 import javax.security.auth.Subject;
 25  
 
 26  
 /**
 27  
  * <code>RaHelper</code> is a collection of helper methods used by this RA
 28  
  * implementation
 29  
  */
 30  
 // @ThreadSafe
 31  0
 public class RaHelper
 32  
 {
 33  
     public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf,
 34  
                                                            final Subject subject,
 35  
                                                            ConnectionRequestInfo info)
 36  
         throws ResourceException
 37  
     {
 38  0
         if (subject == null)
 39  
         {
 40  0
             if (info == null)
 41  
             {
 42  0
                 return null;
 43  
             }
 44  
             else
 45  
             {
 46  0
                 MuleConnectionRequestInfo muleInfo = (MuleConnectionRequestInfo)info;
 47  
 
 48  
                 // Can't create a PC with null values
 49  0
                 if (muleInfo.getUserName() == null || muleInfo.getPassword() == null)
 50  
                 {
 51  
                     // logger.info("\tUtil::GetPasswordCred: User or password is
 52  
                     // null");
 53  0
                     return null;
 54  
                 }
 55  
 
 56  0
                 char[] password = muleInfo.getPassword().toCharArray();
 57  0
                 PasswordCredential pc = new PasswordCredential(muleInfo.getUserName(), password);
 58  0
                 pc.setManagedConnectionFactory(mcf);
 59  0
                 return pc;
 60  
             }
 61  
         }
 62  
         else
 63  
         {
 64  0
             PasswordCredential pc = (PasswordCredential)AccessController.doPrivileged(new PrivilegedAction()
 65  
             {
 66  0
                 public Object run()
 67  
                 {
 68  0
                     Set creds = subject.getPrivateCredentials(PasswordCredential.class);
 69  0
                     Iterator iter = creds.iterator();
 70  0
                     while (iter.hasNext())
 71  
                     {
 72  0
                         PasswordCredential candidate = (PasswordCredential)iter.next();
 73  0
                         if (candidate != null)
 74  
                         {
 75  0
                             ManagedConnectionFactory candidatemcf = candidate.getManagedConnectionFactory();
 76  0
                             if (candidatemcf != null && candidatemcf.equals(mcf))
 77  
                             {
 78  0
                                 return candidate;
 79  
                             }
 80  
                         }
 81  0
                     }
 82  0
                     return null;
 83  
                 }
 84  
             });
 85  0
             if (pc == null)
 86  
             {
 87  0
                 throw new java.lang.SecurityException(CoreMessages.authNoCredentials().getMessage());
 88  
             }
 89  
             else
 90  
             {
 91  0
                 return pc;
 92  
             }
 93  
         }
 94  
     }
 95  
 }