KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.plugins;
8
9 import java.security.PrivilegedAction JavaDoc;
10 import java.security.AccessController JavaDoc;
11 import java.security.PrivilegedExceptionAction JavaDoc;
12 import java.security.PrivilegedActionException JavaDoc;
13 import java.security.Principal JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import javax.security.auth.Subject JavaDoc;
17 import javax.security.auth.login.LoginContext JavaDoc;
18 import javax.security.auth.login.LoginException JavaDoc;
19 import javax.security.auth.callback.CallbackHandler JavaDoc;
20
21 import org.jboss.security.SecurityAssociation;
22
23 /** Common PrivilegedAction used by classes in this package.
24  *
25  * @author Scott.Stark@jboss.org
26  * @version $Revison:$
27  */

28 class SubjectActions
29 {
30    private static class ToStringSubjectAction implements PrivilegedAction JavaDoc
31    {
32       Subject JavaDoc subject;
33       ToStringSubjectAction(Subject JavaDoc subject)
34       {
35          this.subject = subject;
36       }
37       public Object JavaDoc run()
38       {
39          StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
40          tmp.append("Subject(");
41          tmp.append(System.identityHashCode(subject));
42          tmp.append(").principals=");
43          Iterator JavaDoc principals = subject.getPrincipals().iterator();
44          while( principals.hasNext() )
45          {
46             Object JavaDoc p = principals.next();
47             Class JavaDoc c = p.getClass();
48             tmp.append(c.getName());
49             tmp.append('@');
50             tmp.append(System.identityHashCode(c));
51             tmp.append('(');
52             tmp.append(p);
53             tmp.append(')');
54          }
55          return tmp.toString();
56       }
57    }
58
59    private static class GetSubjectAction implements PrivilegedAction JavaDoc
60    {
61       static PrivilegedAction JavaDoc ACTION = new GetSubjectAction();
62       public Object JavaDoc run()
63       {
64          Subject JavaDoc subject = SecurityAssociation.getSubject();
65          return subject;
66       }
67    }
68
69    private static class CopySubjectAction implements PrivilegedAction JavaDoc
70    {
71       Subject JavaDoc fromSubject;
72       Subject JavaDoc toSubject;
73       boolean setReadOnly;
74       CopySubjectAction(Subject JavaDoc fromSubject, Subject JavaDoc toSubject, boolean setReadOnly)
75       {
76          this.fromSubject = fromSubject;
77          this.toSubject = toSubject;
78          this.setReadOnly = setReadOnly;
79       }
80       public Object JavaDoc run()
81       {
82          Set JavaDoc principals = fromSubject.getPrincipals();
83          Set JavaDoc principals2 = toSubject.getPrincipals();
84          Iterator JavaDoc iter = principals.iterator();
85          while( iter.hasNext() )
86             principals2.add(iter.next());
87          Set JavaDoc privateCreds = fromSubject.getPrivateCredentials();
88          Set JavaDoc privateCreds2 = toSubject.getPrivateCredentials();
89          iter = privateCreds.iterator();
90          while( iter.hasNext() )
91             privateCreds2.add(iter.next());
92          Set JavaDoc publicCreds = fromSubject.getPublicCredentials();
93          Set JavaDoc publicCreds2 = toSubject.getPublicCredentials();
94          iter = publicCreds.iterator();
95          while( iter.hasNext() )
96             publicCreds2.add(iter.next());
97          if( setReadOnly == true )
98             toSubject.setReadOnly();
99          return null;
100       }
101    }
102
103    private static class LoginContextAction implements PrivilegedExceptionAction JavaDoc
104    {
105       String JavaDoc securityDomain;
106       Subject JavaDoc subject;
107       CallbackHandler JavaDoc handler;
108       LoginContextAction(String JavaDoc securityDomain, Subject JavaDoc subject,
109          CallbackHandler JavaDoc handler)
110       {
111          this.securityDomain = securityDomain;
112          this.subject = subject;
113          this.handler = handler;
114       }
115       public Object JavaDoc run() throws Exception JavaDoc
116       {
117          LoginContext JavaDoc lc = new LoginContext JavaDoc(securityDomain, subject, handler);
118          return lc;
119       }
120    }
121
122    private static class GetTCLAction implements PrivilegedAction JavaDoc
123    {
124       static PrivilegedAction JavaDoc ACTION = new GetTCLAction();
125       public Object JavaDoc run()
126       {
127          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
128          return loader;
129       }
130    }
131
132    private static class SetContextInfoAction implements PrivilegedAction JavaDoc
133    {
134       Object JavaDoc key;
135       Object JavaDoc value;
136       SetContextInfoAction(Object JavaDoc key, Object JavaDoc value)
137       {
138          this.key = key;
139          this.value = value;
140       }
141       public Object JavaDoc run()
142       {
143          return SecurityAssociation.setContextInfo(key, value);
144       }
145    }
146
147    interface PrincipalInfoAction
148    {
149       PrincipalInfoAction PRIVILEGED = new PrincipalInfoAction()
150       {
151          public void push(final Principal JavaDoc principal, final Object JavaDoc credential,
152             final Subject JavaDoc subject)
153          {
154             AccessController.doPrivileged(
155                new PrivilegedAction JavaDoc()
156                {
157                   public Object JavaDoc run()
158                   {
159                      SecurityAssociation.pushSubjectContext(subject, principal, credential);
160                      return null;
161                   }
162                }
163             );
164          }
165          public void pop()
166          {
167             AccessController.doPrivileged(
168                new PrivilegedAction JavaDoc()
169                {
170                   public Object JavaDoc run()
171                   {
172                      SecurityAssociation.popSubjectContext();
173                      return null;
174                   }
175                }
176             );
177          }
178       };
179
180       PrincipalInfoAction NON_PRIVILEGED = new PrincipalInfoAction()
181       {
182          public void push(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
183          {
184             SecurityAssociation.pushSubjectContext(subject, principal, credential);
185          }
186          public void pop()
187          {
188             SecurityAssociation.popSubjectContext();
189          }
190       };
191
192       void push(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject);
193       void pop();
194    }
195
196    static Subject JavaDoc getActiveSubject()
197    {
198       Subject JavaDoc subject = (Subject JavaDoc) AccessController.doPrivileged(GetSubjectAction.ACTION);
199       return subject;
200    }
201    static void copySubject(Subject JavaDoc fromSubject, Subject JavaDoc toSubject)
202    {
203       copySubject(fromSubject, toSubject, false);
204    }
205    static void copySubject(Subject JavaDoc fromSubject, Subject JavaDoc toSubject, boolean setReadOnly)
206    {
207       CopySubjectAction action = new CopySubjectAction(fromSubject, toSubject, setReadOnly);
208       if( System.getSecurityManager() != null )
209          AccessController.doPrivileged(action);
210       else
211          action.run();
212    }
213
214    static LoginContext JavaDoc createLoginContext(String JavaDoc securityDomain, Subject JavaDoc subject,
215       CallbackHandler JavaDoc handler)
216       throws LoginException JavaDoc
217    {
218       LoginContextAction action = new LoginContextAction(securityDomain, subject, handler);
219       try
220       {
221          LoginContext JavaDoc lc = (LoginContext JavaDoc) AccessController.doPrivileged(action);
222          return lc;
223       }
224       catch(PrivilegedActionException JavaDoc e)
225       {
226          Exception JavaDoc ex = e.getException();
227          if( ex instanceof LoginException JavaDoc )
228             throw (LoginException JavaDoc) ex;
229          else
230             throw new LoginException JavaDoc(ex.getMessage());
231       }
232    }
233
234    static ClassLoader JavaDoc getContextClassLoader()
235    {
236       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) AccessController.doPrivileged(GetTCLAction.ACTION);
237       return loader;
238    }
239
240    static Object JavaDoc setContextInfo(Object JavaDoc key, Object JavaDoc value)
241    {
242       SetContextInfoAction action = new SetContextInfoAction(key, value);
243       Object JavaDoc prevInfo = AccessController.doPrivileged(action);
244       return prevInfo;
245    }
246
247    static void pushSubjectContext(Principal JavaDoc principal, Object JavaDoc credential,
248       Subject JavaDoc subject)
249    {
250       if(System.getSecurityManager() == null)
251       {
252          PrincipalInfoAction.NON_PRIVILEGED.push(principal, credential, subject);
253       }
254       else
255       {
256          PrincipalInfoAction.PRIVILEGED.push(principal, credential, subject);
257       }
258    }
259    static void popSubjectContext()
260    {
261       if(System.getSecurityManager() == null)
262       {
263          PrincipalInfoAction.NON_PRIVILEGED.pop();
264       }
265       else
266       {
267          PrincipalInfoAction.PRIVILEGED.pop();
268       }
269    }
270
271    
272    static String JavaDoc toString(Subject JavaDoc subject)
273    {
274       ToStringSubjectAction action = new ToStringSubjectAction(subject);
275       String JavaDoc info = (String JavaDoc) AccessController.doPrivileged(action);
276       return info;
277    }
278 }
279
280
Popular Tags