KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > Utility


1 /*
2  * Created on February 12, 2004
3  *
4  * Utility.java uses Sun logging
5  *
6  */

7
8 package ersatz.resourceadapter;
9
10
11 import javax.resource.ResourceException;
12 import javax.resource.spi.ConnectionRequestInfo;
13 import javax.resource.spi.SecurityException;
14 import javax.resource.spi.security.PasswordCredential;
15 import javax.security.auth.Subject;
16 import java.util.Set;
17 import java.util.Iterator;
18
19 import org.objectweb.jonas.common.Log;
20 import org.objectweb.util.monolog.api.Logger;
21 import org.objectweb.util.monolog.api.BasicLevel;
22
23 /**
24  *
25  *
26 **/

27 public class Utility
28 {
29   private static Logger logger = null;
30   /**
31    * Write a JOnAS log record
32    *
33    * @param msg display this message in log
34    *
35    */

36   public static synchronized void log(String msg) {
37     if (logger == null) {
38         logger = Log.getLogger("ersatz.resourceadapter");
39     }
40     logger.log(BasicLevel.DEBUG, msg);
41   }
42
43
44   /** Returns the Password credential
45   *
46   * @param mcf ManagedConnectionFactory currently being used
47   * @param subject Subject associated with this call
48   * @param info ConnectionRequestInfo
49   *
50   * @return PasswordCredential for this user
51   */

52   static synchronized PasswordCredential getPasswordCredential
53          (ManagedConnectionFactoryImpl mcf, Subject subject,
54             ConnectionRequestInfo info)
55          throws ResourceException
56   {
57     String mName = "Utility.getPasswordCredential";
58     if (subject == null)
59     {
60       if (info == null) return null;
61       ConnectionRequestInfoImpl crii = (ConnectionRequestInfoImpl) info;
62       PasswordCredential pc =
63            new PasswordCredential(crii.getUserName(),
64                                   crii.getPassword().toCharArray());
65       pc.setManagedConnectionFactory(mcf);
66       return pc;
67     }
68     Set cred = subject.getPrivateCredentials(PasswordCredential.class);
69     PasswordCredential pc = null;
70     for (Iterator iter = cred.iterator(); iter.hasNext();)
71     {
72       PasswordCredential tmpPc = (PasswordCredential) iter.next();
73       if (mcf.equals(tmpPc.getManagedConnectionFactory()))
74       {
75         log(mName+" PasswordCredential found mcf="+mcf);
76         pc = tmpPc;
77         break;
78       }
79     }
80     if (pc == null)
81     {
82       SecurityException se = new SecurityException("No PasswordCredential found");
83       log(mName+" No PasswordCredential found.");
84       throw se;
85     }
86     return pc;
87   }
88  
89
90 }
91
Popular Tags