KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > SecurityAssociationActions


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.web.tomcat.security;
23
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 import javax.security.auth.Subject JavaDoc;
30
31 import org.jboss.security.SecurityAssociation;
32 import org.jboss.security.RunAsIdentity;
33 import org.jboss.security.SecurityConstants;
34 import org.jboss.security.SecurityContext;
35
36 /** A PrivilegedAction implementation for setting the SecurityAssociation
37  * principal and credential
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revison:$
41  */

42 class SecurityAssociationActions
43 {
44    public static final String JavaDoc AUTH_EXCEPTION_KEY = "org.jboss.security.exception";
45
46    private static class SetPrincipalInfoAction implements PrivilegedAction JavaDoc
47    {
48       Principal JavaDoc principal;
49       Object JavaDoc credential;
50       Subject JavaDoc subject;
51       SetPrincipalInfoAction(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
52       {
53          this.principal = principal;
54          this.credential = credential;
55          this.subject = subject;
56       }
57
58       public Object JavaDoc run()
59       {
60          SecurityAssociation.pushSubjectContext(subject, principal, credential);
61          credential = null;
62          principal = null;
63          subject = null;
64          return null;
65       }
66    }
67    private static class SetServerAction implements PrivilegedAction JavaDoc
68    {
69       static PrivilegedAction JavaDoc ACTION = new SetServerAction();
70       public Object JavaDoc run()
71       {
72          SecurityAssociation.setServer();
73          return null;
74       }
75    }
76    private static class ClearAction implements PrivilegedAction JavaDoc
77    {
78       static PrivilegedAction JavaDoc ACTION = new ClearAction();
79       public Object JavaDoc run()
80       {
81          SecurityAssociation.clear();
82          return null;
83       }
84    }
85    private static class GetSubjectAction implements PrivilegedAction JavaDoc
86    {
87       static PrivilegedAction JavaDoc ACTION = new GetSubjectAction();
88       public Object JavaDoc run()
89       {
90          Subject JavaDoc subject = SecurityAssociation.getSubject();
91          return subject;
92       }
93    }
94    private static class GetPrincipalAction implements PrivilegedAction JavaDoc
95    {
96       static PrivilegedAction JavaDoc ACTION = new GetPrincipalAction();
97       public Object JavaDoc run()
98       {
99          Principal JavaDoc principal = SecurityAssociation.getPrincipal();
100          return principal;
101       }
102    }
103    private static class GetCredentialAction implements PrivilegedAction JavaDoc
104    {
105       static PrivilegedAction JavaDoc ACTION = new GetCredentialAction();
106       public Object JavaDoc run()
107       {
108          Object JavaDoc credential = SecurityAssociation.getCredential();
109          return credential;
110       }
111    }
112    private static class GetSecurityContextAction implements PrivilegedAction JavaDoc
113    {
114       private String JavaDoc securityDomain;
115       GetSecurityContextAction(String JavaDoc sd)
116       {
117          this.securityDomain = sd;
118       }
119       
120       public Object JavaDoc run()
121       {
122          String JavaDoc sc = SecurityConstants.SECURITY_CONTEXT;
123          HashMap JavaDoc map = (HashMap JavaDoc)SecurityAssociation.getContextInfo(sc);
124          if(map == null)
125          {
126             map = new HashMap JavaDoc();
127             SecurityAssociation.setContextInfo(sc, map);
128          }
129          SecurityAssociation.setContextInfo(sc, map);
130          return map.get(this.securityDomain);
131       }
132    }
133    
134    private static class SetSecurityContextAction implements PrivilegedAction JavaDoc
135    {
136       private SecurityContext securityContext;
137       private String JavaDoc securityDomain;
138       SetSecurityContextAction(SecurityContext sc, String JavaDoc sd)
139       {
140          this.securityContext = sc;
141          this.securityDomain = sd;
142       }
143       
144       public Object JavaDoc run()
145       {
146          String JavaDoc sc = SecurityConstants.SECURITY_CONTEXT;
147          HashMap JavaDoc map = (HashMap JavaDoc)SecurityAssociation.getContextInfo(sc);
148          if(map == null)
149          {
150             map = new HashMap JavaDoc();
151             SecurityAssociation.setContextInfo(sc, map);
152          }
153          map.put(securityDomain, securityContext);
154          SecurityAssociation.setContextInfo(sc, map);
155          return null;
156       }
157    }
158    
159    private static class PushRunAsRoleAction implements PrivilegedAction JavaDoc
160    {
161       RunAsIdentity principal;
162       PushRunAsRoleAction(RunAsIdentity principal)
163       {
164          this.principal = principal;
165       }
166       public Object JavaDoc run()
167       {
168          SecurityAssociation.pushRunAsIdentity(principal);
169          return null;
170       }
171    }
172
173    private static class PopRunAsRoleAction implements PrivilegedAction JavaDoc
174    {
175       static PrivilegedAction JavaDoc ACTION = new PopRunAsRoleAction();
176       public Object JavaDoc run()
177       {
178          RunAsIdentity principal = SecurityAssociation.popRunAsIdentity();
179          return principal;
180       }
181    }
182    private static class GetAuthExceptionAction implements PrivilegedAction JavaDoc
183    {
184       static PrivilegedAction JavaDoc ACTION = new GetAuthExceptionAction();
185       public Object JavaDoc run()
186       {
187          Object JavaDoc exception = SecurityAssociation.getContextInfo(AUTH_EXCEPTION_KEY);
188          return exception;
189       }
190    }
191    private static class ClearAuthExceptionAction implements PrivilegedAction JavaDoc
192    {
193       static PrivilegedAction JavaDoc ACTION = new ClearAuthExceptionAction();
194       public Object JavaDoc run()
195       {
196          Object JavaDoc exception = SecurityAssociation.setContextInfo(AUTH_EXCEPTION_KEY, null);
197          return exception;
198       }
199    }
200    private static class ClearSecurityContextAction implements PrivilegedAction JavaDoc
201    {
202       private String JavaDoc securityDomain;
203       
204       public ClearSecurityContextAction(String JavaDoc securityDomain)
205       {
206          this.securityDomain = securityDomain;
207       }
208
209       public Object JavaDoc run()
210       {
211          String JavaDoc sc = SecurityConstants.SECURITY_CONTEXT;
212          HashMap JavaDoc map = (HashMap JavaDoc)SecurityAssociation.getContextInfo(sc);
213          if(map == null)
214          {
215             map = new HashMap JavaDoc();
216             SecurityAssociation.setContextInfo(sc, map);
217          }
218          if(map.containsKey(securityDomain))
219             map.remove(securityDomain);
220          
221          SecurityAssociation.setContextInfo(sc, map);
222          return null;
223       }
224    }
225
226    static void clearSecurityContext(String JavaDoc securityDomain)
227    {
228       ClearSecurityContextAction action = new ClearSecurityContextAction(securityDomain);
229       AccessController.doPrivileged(action);
230    }
231    
232    static SecurityContext getSecurityContext(String JavaDoc securityDomain)
233    {
234       GetSecurityContextAction action = new GetSecurityContextAction(securityDomain);
235       return (SecurityContext)AccessController.doPrivileged(action);
236    }
237    
238    static void setSecurityContext(SecurityContext sc, String JavaDoc securityDomain)
239    {
240       SetSecurityContextAction action = new SetSecurityContextAction(sc,securityDomain);
241       AccessController.doPrivileged(action);
242    }
243    
244    static void setPrincipalInfo(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
245    {
246       SetPrincipalInfoAction action = new SetPrincipalInfoAction(principal, credential, subject);
247       AccessController.doPrivileged(action);
248    }
249    static void setServer()
250    {
251       AccessController.doPrivileged(SetServerAction.ACTION);
252    }
253    static void clear()
254    {
255       AccessController.doPrivileged(ClearAction.ACTION);
256    }
257    static Subject JavaDoc getSubject()
258    {
259       Subject JavaDoc subject = (Subject JavaDoc) AccessController.doPrivileged(GetSubjectAction.ACTION);
260       return subject;
261    }
262    static Principal JavaDoc getPrincipal()
263    {
264       Principal JavaDoc principal = (Principal JavaDoc) AccessController.doPrivileged(GetPrincipalAction.ACTION);
265       return principal;
266    }
267    static Object JavaDoc getCredential()
268    {
269       Object JavaDoc credential = AccessController.doPrivileged(GetCredentialAction.ACTION);
270       return credential;
271    }
272    static void pushRunAsIdentity(RunAsIdentity principal)
273    {
274       PushRunAsRoleAction action = new PushRunAsRoleAction(principal);
275       AccessController.doPrivileged(action);
276    }
277    static RunAsIdentity popRunAsIdentity()
278    {
279       RunAsIdentity principal = (RunAsIdentity) AccessController.doPrivileged(PopRunAsRoleAction.ACTION);
280       return principal;
281    }
282
283    static Throwable JavaDoc getAuthException()
284    {
285       Throwable JavaDoc ex = (Throwable JavaDoc) AccessController.doPrivileged(GetAuthExceptionAction.ACTION);
286       return ex;
287    }
288    static void clearAuthException()
289    {
290       AccessController.doPrivileged(ClearAuthExceptionAction.ACTION);
291    }
292 }
293
Popular Tags