01: /*
02: * Created on February 12, 2004
03: *
04: * Utility.java uses Sun logging
05: *
06: */
07:
08: package ersatz.resourceadapter;
09:
10: import javax.resource.ResourceException;
11: import javax.resource.spi.ConnectionRequestInfo;
12: import javax.resource.spi.SecurityException;
13: import javax.resource.spi.security.PasswordCredential;
14: import javax.security.auth.Subject;
15: import java.util.Set;
16: import java.util.Iterator;
17:
18: import org.objectweb.jonas.common.Log;
19: import org.objectweb.util.monolog.api.Logger;
20: import org.objectweb.util.monolog.api.BasicLevel;
21:
22: /**
23: *
24: *
25: **/
26: public class Utility {
27: private static Logger logger = null;
28:
29: /**
30: * Write a JOnAS log record
31: *
32: * @param msg display this message in log
33: *
34: */
35: public static synchronized void log(String msg) {
36: if (logger == null) {
37: logger = Log.getLogger("ersatz.resourceadapter");
38: }
39: logger.log(BasicLevel.DEBUG, msg);
40: }
41:
42: /** Returns the Password credential
43: *
44: * @param mcf ManagedConnectionFactory currently being used
45: * @param subject Subject associated with this call
46: * @param info ConnectionRequestInfo
47: *
48: * @return PasswordCredential for this user
49: */
50: static synchronized PasswordCredential getPasswordCredential(
51: ManagedConnectionFactoryImpl mcf, Subject subject,
52: ConnectionRequestInfo info) throws ResourceException {
53: String mName = "Utility.getPasswordCredential";
54: if (subject == null) {
55: if (info == null)
56: return null;
57: ConnectionRequestInfoImpl crii = (ConnectionRequestInfoImpl) info;
58: PasswordCredential pc = new PasswordCredential(crii
59: .getUserName(), crii.getPassword().toCharArray());
60: pc.setManagedConnectionFactory(mcf);
61: return pc;
62: }
63: Set cred = subject
64: .getPrivateCredentials(PasswordCredential.class);
65: PasswordCredential pc = null;
66: for (Iterator iter = cred.iterator(); iter.hasNext();) {
67: PasswordCredential tmpPc = (PasswordCredential) iter.next();
68: if (mcf.equals(tmpPc.getManagedConnectionFactory())) {
69: log(mName + " PasswordCredential found mcf=" + mcf);
70: pc = tmpPc;
71: break;
72: }
73: }
74: if (pc == null) {
75: SecurityException se = new SecurityException(
76: "No PasswordCredential found");
77: log(mName + " No PasswordCredential found.");
78: throw se;
79: }
80: return pc;
81: }
82:
83: }
|