KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > ra > RaHelper


1 /*
2  * $Id: RaHelper.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 java.security.AccessController JavaDoc;
14 import java.security.PrivilegedAction JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import javax.resource.ResourceException JavaDoc;
19 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
20 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
21 import javax.resource.spi.security.PasswordCredential JavaDoc;
22 import javax.security.auth.Subject JavaDoc;
23
24 import org.mule.config.i18n.Message;
25 import org.mule.config.i18n.Messages;
26
27 /**
28  * <code>RaHelper</code> is a collection of helper methods used by this RA
29  * implementation
30  */

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