KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > 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.ejb;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
28 import java.security.AccessController JavaDoc;
29 import java.security.Principal JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31 import java.security.PrivilegedActionException JavaDoc;
32 import java.security.PrivilegedExceptionAction JavaDoc;
33
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.security.auth.Subject JavaDoc;
37 import javax.security.jacc.PolicyContext JavaDoc;
38 import javax.security.jacc.PolicyContextException JavaDoc;
39
40 import org.jboss.mx.util.MBeanProxy;
41 import org.jboss.security.RunAsIdentity;
42 import org.jboss.security.SecurityAssociation;
43
44 /** A collection of privileged actions for this package
45  *
46  * @author Scott.Stark@jboss.org
47  * @author Anil.Saldhana@jboss.org
48  * @version $Revison:$
49  */

50 class SecurityActions
51 {
52    private static class SetContextID implements PrivilegedAction JavaDoc
53    {
54       String JavaDoc contextID;
55       SetContextID(String JavaDoc contextID)
56       {
57          this.contextID = contextID;
58       }
59       public Object JavaDoc run()
60       {
61          String JavaDoc previousID = PolicyContext.getContextID();
62          PolicyContext.setContextID(contextID);
63          return previousID;
64       }
65    }
66
67    private static class PeekRunAsRoleAction implements PrivilegedAction JavaDoc
68    {
69       int depth;
70       PeekRunAsRoleAction(int depth)
71       {
72          this.depth = depth;
73       }
74       public Object JavaDoc run()
75       {
76          RunAsIdentity principal = SecurityAssociation.peekRunAsIdentity(depth);
77          return principal;
78       }
79    }
80
81    /**
82     * Wrap the MBeanProxy proxy in a priviledged action so that method
83     * dispatch is done from a PrivilegedExceptionAction
84     */

85    private static class MBeanProxyAction implements PrivilegedExceptionAction JavaDoc
86    {
87       Class JavaDoc iface;
88       ObjectName JavaDoc name;
89       MBeanServer JavaDoc server;
90
91       MBeanProxyAction(Class JavaDoc iface, ObjectName JavaDoc name, MBeanServer JavaDoc server)
92       {
93          this.iface = iface;
94          this.name = name;
95          this.server = server;
96       }
97       public Object JavaDoc run() throws Exception JavaDoc
98       {
99          Object JavaDoc proxy = MBeanProxy.get(iface, name, server);
100          Class JavaDoc[] ifaces = {iface};
101          InvocationHandler JavaDoc secureHandler = new InvocationHandlerAction(proxy);
102          Object JavaDoc secureProxy = Proxy.newProxyInstance(iface.getClassLoader(), ifaces, secureHandler);
103          return secureProxy;
104       }
105    }
106
107    private static class InvocationHandlerAction
108       implements InvocationHandler JavaDoc, PrivilegedExceptionAction JavaDoc
109    {
110       private Method JavaDoc method;
111       private Object JavaDoc[] args;
112       private Object JavaDoc mbean;
113       private InvocationHandlerAction(Object JavaDoc mbean)
114       {
115          this.mbean = mbean;
116       }
117       public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
118          throws Throwable JavaDoc
119       {
120          this.method = method;
121          this.args = args;
122          Object JavaDoc value;
123          try
124          {
125             value = AccessController.doPrivileged(this);
126          }
127          catch(PrivilegedActionException JavaDoc e)
128          {
129             throw e.getException();
130          }
131          return value;
132       }
133       public Object JavaDoc run() throws Exception JavaDoc
134       {
135          Object JavaDoc value = method.invoke(mbean, args);
136          return value;
137       }
138    }
139
140    static Object JavaDoc getMBeanProxy(Class JavaDoc iface, ObjectName JavaDoc name, MBeanServer JavaDoc server)
141       throws Exception JavaDoc
142    {
143       Object JavaDoc proxy;
144       if( System.getSecurityManager() == null )
145       {
146          proxy = MBeanProxy.get(iface, name, server);
147       }
148       else
149       {
150          MBeanProxyAction action = new MBeanProxyAction(iface, name, server);
151          proxy = AccessController.doPrivileged(action);
152       }
153       return proxy;
154    }
155    static ClassLoader JavaDoc getContextClassLoader()
156    {
157       return TCLAction.UTIL.getContextClassLoader();
158    }
159
160    static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
161    {
162       return TCLAction.UTIL.getContextClassLoader(thread);
163    }
164
165    static void setContextClassLoader(ClassLoader JavaDoc loader)
166    {
167       TCLAction.UTIL.setContextClassLoader(loader);
168    }
169
170    static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc loader)
171    {
172       TCLAction.UTIL.setContextClassLoader(thread, loader);
173    }
174
175    static String JavaDoc setContextID(String JavaDoc contextID)
176    {
177       PrivilegedAction JavaDoc action = new SetContextID(contextID);
178       String JavaDoc previousID = (String JavaDoc) AccessController.doPrivileged(action);
179       return previousID;
180    }
181    static RunAsIdentity peekRunAsIdentity(int depth)
182    {
183       PrivilegedAction JavaDoc action = new PeekRunAsRoleAction(depth);
184       RunAsIdentity principal = (RunAsIdentity) AccessController.doPrivileged(action);
185       return principal;
186    }
187
188    static Principal JavaDoc getCallerPrincipal()
189    {
190       return IdentityAction.UTIL.getIdentityAction().getCallerPrincipal();
191    }
192
193    interface IdentityAction
194    {
195       class UTIL
196       {
197          static IdentityAction getIdentityAction()
198          {
199             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
200          }
201       }
202       IdentityAction NON_PRIVILEGED = new IdentityAction()
203       {
204          public Principal JavaDoc getCallerPrincipal()
205          {
206             return SecurityAssociation.getCallerPrincipal();
207          }
208       };
209       IdentityAction PRIVILEGED = new IdentityAction()
210       {
211          private final PrivilegedAction JavaDoc getCallerPrincipalAction = new PrivilegedAction JavaDoc()
212          {
213             public Object JavaDoc run()
214             {
215                return SecurityAssociation.getCallerPrincipal();
216             }
217          };
218          public Principal JavaDoc getCallerPrincipal()
219          {
220             return (Principal JavaDoc)AccessController.doPrivileged(getCallerPrincipalAction);
221          }
222       };
223
224       Principal JavaDoc getCallerPrincipal();
225    }
226
227    interface TCLAction
228    {
229       class UTIL
230       {
231          static TCLAction getTCLAction()
232          {
233             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
234          }
235
236          static ClassLoader JavaDoc getContextClassLoader()
237          {
238             return getTCLAction().getContextClassLoader();
239          }
240
241          static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
242          {
243             return getTCLAction().getContextClassLoader(thread);
244          }
245
246          static void setContextClassLoader(ClassLoader JavaDoc cl)
247          {
248             getTCLAction().setContextClassLoader(cl);
249          }
250
251          static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
252          {
253             getTCLAction().setContextClassLoader(thread, cl);
254          }
255       }
256
257       TCLAction NON_PRIVILEGED = new TCLAction()
258       {
259          public ClassLoader JavaDoc getContextClassLoader()
260          {
261             return Thread.currentThread().getContextClassLoader();
262          }
263
264          public ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
265          {
266             return thread.getContextClassLoader();
267          }
268
269          public void setContextClassLoader(ClassLoader JavaDoc cl)
270          {
271             Thread.currentThread().setContextClassLoader(cl);
272          }
273
274          public void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
275          {
276             thread.setContextClassLoader(cl);
277          }
278       };
279
280       TCLAction PRIVILEGED = new TCLAction()
281       {
282          private final PrivilegedAction JavaDoc getTCLPrivilegedAction = new PrivilegedAction JavaDoc()
283          {
284             public Object JavaDoc run()
285             {
286                return Thread.currentThread().getContextClassLoader();
287             }
288          };
289
290          public ClassLoader JavaDoc getContextClassLoader()
291          {
292             return (ClassLoader JavaDoc)AccessController.doPrivileged(getTCLPrivilegedAction);
293          }
294
295          public ClassLoader JavaDoc getContextClassLoader(final Thread JavaDoc thread)
296          {
297             return (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
298             {
299                public Object JavaDoc run()
300                {
301                   return thread.getContextClassLoader();
302                }
303             });
304          }
305
306          public void setContextClassLoader(final ClassLoader JavaDoc cl)
307          {
308             AccessController.doPrivileged(
309                new PrivilegedAction JavaDoc()
310                {
311                   public Object JavaDoc run()
312                   {
313                      Thread.currentThread().setContextClassLoader(cl);
314                      return null;
315                   }
316                }
317             );
318          }
319
320          public void setContextClassLoader(final Thread JavaDoc thread, final ClassLoader JavaDoc cl)
321          {
322             AccessController.doPrivileged(
323                new PrivilegedAction JavaDoc()
324                {
325                   public Object JavaDoc run()
326                   {
327                      thread.setContextClassLoader(cl);
328                      return null;
329                   }
330                }
331             );
332          }
333       };
334
335       ClassLoader JavaDoc getContextClassLoader();
336
337       ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread);
338
339       void setContextClassLoader(ClassLoader JavaDoc cl);
340
341       void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl);
342    }
343    
344    interface PolicyContextActions
345    {
346       /** The JACC PolicyContext key for the current Subject */
347       static final String JavaDoc SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
348       PolicyContextActions PRIVILEGED = new PolicyContextActions()
349       {
350          private final PrivilegedExceptionAction JavaDoc exAction = new PrivilegedExceptionAction JavaDoc()
351          {
352             public Object JavaDoc run() throws Exception JavaDoc
353             {
354                return (Subject JavaDoc) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
355             }
356          };
357          public Subject JavaDoc getContextSubject()
358          throws PolicyContextException JavaDoc
359          {
360             try
361             {
362                return (Subject JavaDoc) AccessController.doPrivileged(exAction);
363             }
364             catch(PrivilegedActionException JavaDoc e)
365             {
366                Exception JavaDoc ex = e.getException();
367                if( ex instanceof PolicyContextException JavaDoc )
368                   throw (PolicyContextException JavaDoc) ex;
369                else
370                   throw new UndeclaredThrowableException JavaDoc(ex);
371             }
372          }
373       };
374
375       PolicyContextActions NON_PRIVILEGED = new PolicyContextActions()
376       {
377          public Subject JavaDoc getContextSubject()
378          throws PolicyContextException JavaDoc
379          {
380             return (Subject JavaDoc) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
381          }
382       };
383
384       Subject JavaDoc getContextSubject()
385       throws PolicyContextException JavaDoc;
386    }
387
388    static Subject JavaDoc getContextSubject()
389    
390    throws PolicyContextException JavaDoc
391    {
392       if(System.getSecurityManager() == null)
393       {
394          return PolicyContextActions.NON_PRIVILEGED.getContextSubject();
395       }
396       else
397       {
398          return PolicyContextActions.PRIVILEGED.getContextSubject();
399       }
400    }
401 }
402
Popular Tags