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 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  public class RaHelper
28  {
29      public static PasswordCredential getPasswordCredential(final ManagedConnectionFactory mcf,
30                                                             final Subject subject,
31                                                             ConnectionRequestInfo info)
32          throws ResourceException
33      {
34          if (subject == null)
35          {
36              if (info == null)
37              {
38                  return null;
39              }
40              else
41              {
42                  MuleConnectionRequestInfo muleInfo = (MuleConnectionRequestInfo)info;
43  
44                  // Can't create a PC with null values
45                  if (muleInfo.getUserName() == null || muleInfo.getPassword() == null)
46                  {
47                      // logger.info("\tUtil::GetPasswordCred: User or password is
48                      // null");
49                      return null;
50                  }
51  
52                  char[] password = muleInfo.getPassword().toCharArray();
53                  PasswordCredential pc = new PasswordCredential(muleInfo.getUserName(), password);
54                  pc.setManagedConnectionFactory(mcf);
55                  return pc;
56              }
57          }
58          else
59          {
60              PasswordCredential pc = (PasswordCredential)AccessController.doPrivileged(new PrivilegedAction()
61              {
62                  public Object run()
63                  {
64                      Set creds = subject.getPrivateCredentials(PasswordCredential.class);
65                      Iterator iter = creds.iterator();
66                      while (iter.hasNext())
67                      {
68                          PasswordCredential candidate = (PasswordCredential)iter.next();
69                          if (candidate != null)
70                          {
71                              ManagedConnectionFactory candidatemcf = candidate.getManagedConnectionFactory();
72                              if (candidatemcf != null && candidatemcf.equals(mcf))
73                              {
74                                  return candidate;
75                              }
76                          }
77                      }
78                      return null;
79                  }
80              });
81              if (pc == null)
82              {
83                  throw new java.lang.SecurityException(CoreMessages.authNoCredentials().getMessage());
84              }
85              else
86              {
87                  return pc;
88              }
89          }
90      }
91  }