View Javadoc

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