KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > connector > invoker > SecurityActions


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.jmx.connector.invoker;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.Principal JavaDoc;
26 import java.security.PrivilegedAction 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.SecurityConstants;
33 import org.jboss.security.SecurityContext;
34
35 /** Common PrivilegedAction used by classes in this package.
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revison:$
39  */

40 class SecurityActions
41 {
42    private static class GetSubjectAction implements PrivilegedAction JavaDoc
43    {
44       static PrivilegedAction JavaDoc ACTION = new GetSubjectAction();
45       public Object JavaDoc run()
46       {
47          Subject JavaDoc subject = SecurityAssociation.getSubject();
48          return subject;
49       }
50    }
51    private static class GetTCLAction implements PrivilegedAction JavaDoc
52    {
53       static PrivilegedAction JavaDoc ACTION = new GetTCLAction();
54       public Object JavaDoc run()
55       {
56          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
57          return loader;
58       }
59    }
60    private static class SetTCLAction implements PrivilegedAction JavaDoc
61    {
62       ClassLoader JavaDoc loader;
63       SetTCLAction(ClassLoader JavaDoc loader)
64       {
65          this.loader = loader;
66       }
67       public Object JavaDoc run()
68       {
69          Thread.currentThread().setContextClassLoader(loader);
70          loader = null;
71          return null;
72       }
73    }
74    interface PrincipalInfoAction
75    {
76       PrincipalInfoAction PRIVILEGED = new PrincipalInfoAction()
77       {
78          public void push(final Principal JavaDoc principal, final Object JavaDoc credential,
79             final Subject JavaDoc subject)
80          {
81             AccessController.doPrivileged(
82                new PrivilegedAction JavaDoc()
83                {
84                   public Object JavaDoc run()
85                   {
86                      SecurityAssociation.pushSubjectContext(subject, principal, credential);
87                      return null;
88                   }
89                }
90             );
91          }
92          public void pop()
93          {
94             AccessController.doPrivileged(
95                new PrivilegedAction JavaDoc()
96                {
97                   public Object JavaDoc run()
98                   {
99                      SecurityAssociation.popSubjectContext();
100                      return null;
101                   }
102                }
103             );
104          }
105       };
106
107       PrincipalInfoAction NON_PRIVILEGED = new PrincipalInfoAction()
108       {
109          public void push(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject)
110          {
111             SecurityAssociation.pushSubjectContext(subject, principal, credential);
112          }
113          public void pop()
114          {
115             SecurityAssociation.popSubjectContext();
116          }
117       };
118
119       void push(Principal JavaDoc principal, Object JavaDoc credential, Subject JavaDoc subject);
120       void pop();
121    }
122    
123    static class SetSecurityContextAction implements PrivilegedAction JavaDoc
124    {
125       private SecurityContext securityContext;
126       private String JavaDoc securityDomain;
127       SetSecurityContextAction(SecurityContext sc, String JavaDoc sd)
128       {
129          this.securityContext = sc;
130          this.securityDomain = sd;
131       }
132       
133       public Object JavaDoc run()
134       {
135          String JavaDoc sc = SecurityConstants.SECURITY_CONTEXT;
136          HashMap JavaDoc map = (HashMap JavaDoc)SecurityAssociation.getContextInfo(sc);
137          if(map == null)
138          {
139             map = new HashMap JavaDoc();
140             SecurityAssociation.setContextInfo(sc, map);
141          }
142          map.put(securityDomain, securityContext);
143          SecurityAssociation.setContextInfo(sc, map);
144          return null;
145       }
146    }
147
148    static Subject JavaDoc getActiveSubject()
149    {
150       Subject JavaDoc subject = (Subject JavaDoc) AccessController.doPrivileged(GetSubjectAction.ACTION);
151       return subject;
152    }
153    static ClassLoader JavaDoc getContextClassLoader()
154    {
155       ClassLoader JavaDoc loader = (ClassLoader JavaDoc) AccessController.doPrivileged(GetTCLAction.ACTION);
156       return loader;
157    }
158    static void setContextClassLoader(ClassLoader JavaDoc loader)
159    {
160       PrivilegedAction JavaDoc action = new SetTCLAction(loader);
161       AccessController.doPrivileged(action);
162    }
163
164    static void pushSubjectContext(Principal JavaDoc principal, Object JavaDoc credential,
165       Subject JavaDoc subject)
166    {
167       if(System.getSecurityManager() == null)
168       {
169          PrincipalInfoAction.NON_PRIVILEGED.push(principal, credential, subject);
170       }
171       else
172       {
173          PrincipalInfoAction.PRIVILEGED.push(principal, credential, subject);
174       }
175    }
176    static void popSubjectContext()
177    {
178       if(System.getSecurityManager() == null)
179       {
180          PrincipalInfoAction.NON_PRIVILEGED.pop();
181       }
182       else
183       {
184          PrincipalInfoAction.PRIVILEGED.pop();
185       }
186    }
187    static void setSecurityContext(SecurityContext sc, String JavaDoc securityDomain)
188    {
189       SetSecurityContextAction action = new SetSecurityContextAction(sc,securityDomain);
190       AccessController.doPrivileged(action);
191    }
192 }
193
Popular Tags