KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jms > JmsCred


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jms;
23
24 import java.util.Set JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28
29 import javax.security.auth.Subject JavaDoc;
30
31 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
32 import javax.resource.spi.SecurityException JavaDoc;
33 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
34
35 import javax.resource.spi.security.PasswordCredential JavaDoc;
36
37 /**
38  * Credential information
39  *
40  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman </a>.
41  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 38315 $
43  */

44 public class JmsCred
45 {
46     public String JavaDoc name;
47
48     public String JavaDoc pwd;
49
50     public JmsCred()
51     {
52         // empty
53
}
54
55     /**
56      * Get our own simple cred
57      */

58     public static JmsCred getJmsCred(ManagedConnectionFactory JavaDoc mcf, Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
59             throws SecurityException JavaDoc
60     {
61         JmsCred jc = new JmsCred();
62         if (subject == null && info != null)
63         {
64             // Credentials specifyed on connection request
65
jc.name = ((JmsConnectionRequestInfo) info).getUserName();
66             jc.pwd = ((JmsConnectionRequestInfo) info).getPassword();
67         }
68         else if (subject != null)
69         {
70             // Credentials from appserver
71
PasswordCredential JavaDoc pwdc = GetCredentialAction.getCredential(subject, mcf);
72             if (pwdc == null)
73             {
74                 // No hit - we do need creds
75
throw new SecurityException JavaDoc("No Password credentials found");
76             }
77             jc.name = pwdc.getUserName();
78             jc.pwd = new String JavaDoc(pwdc.getPassword());
79         }
80         else
81         {
82             throw new SecurityException JavaDoc("No Subject or ConnectionRequestInfo set, could not get credentials");
83         }
84         return jc;
85     }
86
87     public String JavaDoc toString()
88     {
89         return super.toString() + "{ username=" + name + ", password=**** }";
90     }
91
92    private static class GetCredentialAction implements PrivilegedAction JavaDoc
93    {
94       Subject JavaDoc subject;
95       ManagedConnectionFactory JavaDoc mcf;
96       GetCredentialAction(Subject JavaDoc subject, ManagedConnectionFactory JavaDoc mcf)
97       {
98          this.subject = subject;
99          this.mcf = mcf;
100       }
101       public Object JavaDoc run()
102       {
103          Set JavaDoc creds = subject.getPrivateCredentials(PasswordCredential JavaDoc.class);
104          PasswordCredential JavaDoc pwdc = null;
105          Iterator JavaDoc credentials = creds.iterator();
106          while (credentials.hasNext())
107          {
108             PasswordCredential JavaDoc curCred = (PasswordCredential JavaDoc) credentials.next();
109             if (curCred.getManagedConnectionFactory().equals(mcf))
110             {
111                pwdc = curCred;
112                break;
113             }
114          }
115          return pwdc;
116       }
117       static PasswordCredential JavaDoc getCredential(Subject JavaDoc subject, ManagedConnectionFactory JavaDoc mcf)
118       {
119          GetCredentialAction action = new GetCredentialAction(subject, mcf);
120          PasswordCredential JavaDoc pc = (PasswordCredential JavaDoc) AccessController.doPrivileged(action);
121          return pc;
122       }
123     }
124 }
125
Popular Tags