KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > security > SubjectActions


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.security;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.security.acl.Group JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
31 import javax.resource.spi.security.PasswordCredential JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33
34 import org.jboss.security.SimpleGroup;
35
36 /**
37  * Common package privileged actions.
38  * @author Scott.Stark@jboss.org
39  * @version $Revision: 37459 $
40  */

41 class SubjectActions
42 {
43    interface AddRolesActions
44    {
45       AddRolesActions PRIVILEGED = new AddRolesActions()
46       {
47          public void addRoles(final Subject JavaDoc subject, final Set JavaDoc roles)
48          {
49             AccessController.doPrivileged(new PrivilegedAction JavaDoc()
50             {
51                public Object JavaDoc run()
52                {
53                   addSubjectRoles(subject, roles);
54                   return null;
55                }
56             });
57          }
58       };
59
60       AddRolesActions NON_PRIVILEGED = new AddRolesActions()
61       {
62          public void addRoles(final Subject JavaDoc subject, final Set JavaDoc roles)
63          {
64             addSubjectRoles(subject, roles);
65          }
66       };
67
68       void addRoles(Subject JavaDoc subject, Set JavaDoc roles);
69    }
70
71    static class AddCredentialsAction implements PrivilegedAction JavaDoc
72    {
73       Subject JavaDoc subject;
74       PasswordCredential JavaDoc cred;
75
76       AddCredentialsAction(Subject JavaDoc subject, PasswordCredential JavaDoc cred)
77       {
78          this.subject = subject;
79          this.cred = cred;
80       }
81
82       public Object JavaDoc run()
83       {
84          subject.getPrivateCredentials().add(cred);
85          return null;
86       }
87    }
88
89    static class AddPrincipalsAction implements PrivilegedAction JavaDoc
90    {
91       Subject JavaDoc subject;
92       Principal JavaDoc p;
93
94       AddPrincipalsAction(Subject JavaDoc subject, Principal JavaDoc p)
95       {
96          this.subject = subject;
97          this.p = p;
98       }
99
100       public Object JavaDoc run()
101       {
102          subject.getPrincipals().add(p);
103          return null;
104       }
105    }
106
107    static class RemoveCredentialsAction implements PrivilegedAction JavaDoc
108    {
109       Subject JavaDoc subject;
110       ManagedConnectionFactory JavaDoc mcf;
111
112       RemoveCredentialsAction(Subject JavaDoc subject, ManagedConnectionFactory JavaDoc mcf)
113       {
114          this.subject = subject;
115          this.mcf = mcf;
116       }
117
118       public Object JavaDoc run()
119       {
120          Iterator JavaDoc i = subject.getPrivateCredentials().iterator();
121          while (i.hasNext())
122          {
123             Object JavaDoc o = i.next();
124             if (o instanceof PasswordCredential JavaDoc)
125             {
126                PasswordCredential JavaDoc pc = (PasswordCredential JavaDoc) o;
127                if (pc.getManagedConnectionFactory() == mcf)
128                   i.remove();
129             }
130          }
131          return null;
132       }
133    }
134
135    static void addCredentials(Subject JavaDoc subject, PasswordCredential JavaDoc cred)
136    {
137       AddCredentialsAction action = new AddCredentialsAction(subject, cred);
138       AccessController.doPrivileged(action);
139    }
140
141    static void addPrincipals(Subject JavaDoc subject, Principal JavaDoc p)
142    {
143       AddPrincipalsAction action = new AddPrincipalsAction(subject, p);
144       AccessController.doPrivileged(action);
145    }
146
147    static void removeCredentials(Subject JavaDoc subject, ManagedConnectionFactory JavaDoc mcf)
148    {
149       RemoveCredentialsAction action = new RemoveCredentialsAction(subject, mcf);
150       AccessController.doPrivileged(action);
151    }
152
153    static void addRoles(Subject JavaDoc subject, Set JavaDoc runAsRoles)
154    {
155       if( System.getSecurityManager() != null )
156       {
157          AddRolesActions.PRIVILEGED.addRoles(subject, runAsRoles);
158       }
159       else
160       {
161          AddRolesActions.NON_PRIVILEGED.addRoles(subject, runAsRoles);
162       }
163    }
164
165    private static Group JavaDoc addSubjectRoles(Subject JavaDoc theSubject, Set JavaDoc roles)
166    {
167       Set JavaDoc subjectGroups = theSubject.getPrincipals(Group JavaDoc.class);
168       Iterator JavaDoc iter = subjectGroups.iterator();
169       Group JavaDoc roleGrp = null;
170       while (iter.hasNext())
171       {
172          Group JavaDoc grp = (Group JavaDoc) iter.next();
173          String JavaDoc name = grp.getName();
174          if (name.equals("Roles"))
175             roleGrp = grp;
176       }
177
178       // Create the Roles group if it was not found
179
if (roleGrp == null)
180       {
181          roleGrp = new SimpleGroup("Roles");
182          theSubject.getPrincipals().add(roleGrp);
183       }
184
185       iter = roles.iterator();
186       while (iter.hasNext())
187       {
188          Principal JavaDoc role = (Principal JavaDoc) iter.next();
189          roleGrp.addMember(role);
190       }
191       return roleGrp;
192    }
193
194 }
195
Popular Tags