01: /*
02: * $Id: RaHelper.java 10789 2008-02-12 20:04:43Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.module.jca;
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: public static PasswordCredential getPasswordCredential(
33: final ManagedConnectionFactory mcf, final Subject subject,
34: ConnectionRequestInfo info) throws ResourceException {
35: if (subject == null) {
36: if (info == null) {
37: return null;
38: } else {
39: MuleConnectionRequestInfo muleInfo = (MuleConnectionRequestInfo) info;
40:
41: // Can't create a PC with null values
42: if (muleInfo.getUserName() == null
43: || muleInfo.getPassword() == null) {
44: // logger.info("\tUtil::GetPasswordCred: User or password is
45: // null");
46: return null;
47: }
48:
49: char[] password = muleInfo.getPassword().toCharArray();
50: PasswordCredential pc = new PasswordCredential(muleInfo
51: .getUserName(), password);
52: pc.setManagedConnectionFactory(mcf);
53: return pc;
54: }
55: } else {
56: PasswordCredential pc = (PasswordCredential) AccessController
57: .doPrivileged(new PrivilegedAction() {
58: public Object run() {
59: Set creds = subject
60: .getPrivateCredentials(PasswordCredential.class);
61: Iterator iter = creds.iterator();
62: while (iter.hasNext()) {
63: PasswordCredential candidate = (PasswordCredential) iter
64: .next();
65: if (candidate != null) {
66: ManagedConnectionFactory candidatemcf = candidate
67: .getManagedConnectionFactory();
68: if (candidatemcf != null
69: && candidatemcf.equals(mcf)) {
70: return candidate;
71: }
72: }
73: }
74: return null;
75: }
76: });
77: if (pc == null) {
78: throw new java.lang.SecurityException(CoreMessages
79: .authNoCredentials().getMessage());
80: } else {
81: return pc;
82: }
83: }
84: }
85: }
|