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