KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > 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.aop.util;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedActionException JavaDoc;
28 import java.security.PrivilegedExceptionAction JavaDoc;
29
30 /**
31  *
32  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
33  * @version $Revision: 45974 $
34  */

35 class SecurityActions
36 {
37    interface GetDeclaredMethodsAction
38    {
39       Method JavaDoc[] getDeclaredMethods(Class JavaDoc clazz);
40       
41       GetDeclaredMethodsAction PRIVILEGED = new GetDeclaredMethodsAction()
42       {
43          public Method JavaDoc[] getDeclaredMethods(final Class JavaDoc clazz)
44          {
45             try
46             {
47                return (Method JavaDoc[])AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
48                {
49                   public Object JavaDoc run() throws Exception JavaDoc
50                   {
51                      return clazz.getDeclaredMethods();
52                   }
53                });
54             }
55             catch (PrivilegedActionException JavaDoc e)
56             {
57                throw new RuntimeException JavaDoc(e);
58             }
59          }
60       };
61
62       GetDeclaredMethodsAction NON_PRIVILEGED = new GetDeclaredMethodsAction()
63       {
64          public Method JavaDoc[] getDeclaredMethods(Class JavaDoc clazz)
65          {
66             return clazz.getDeclaredMethods();
67          }
68       };
69    }
70    
71    interface GetDeclaredConstructorsAction
72    {
73       Constructor JavaDoc[] getDeclaredConstructors(Class JavaDoc clazz);
74       
75       GetDeclaredConstructorsAction PRIVILEGED = new GetDeclaredConstructorsAction()
76       {
77          public Constructor JavaDoc[] getDeclaredConstructors(final Class JavaDoc clazz)
78          {
79             try
80             {
81                return (Constructor JavaDoc[])AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
82                {
83                   public Object JavaDoc run() throws Exception JavaDoc
84                   {
85                      return clazz.getDeclaredConstructors();
86                   }
87                });
88             }
89             catch (PrivilegedActionException JavaDoc e)
90             {
91                throw new RuntimeException JavaDoc(e);
92             }
93          }
94       };
95
96       GetDeclaredConstructorsAction NON_PRIVILEGED = new GetDeclaredConstructorsAction()
97       {
98          public Constructor JavaDoc[] getDeclaredConstructors(Class JavaDoc clazz)
99          {
100             return clazz.getDeclaredConstructors();
101          }
102       };
103    }
104    
105    static Method JavaDoc[] getDeclaredMethods(Class JavaDoc clazz)
106    {
107       if (System.getSecurityManager() == null)
108       {
109          return GetDeclaredMethodsAction.NON_PRIVILEGED.getDeclaredMethods(clazz);
110       }
111       else
112       {
113          return GetDeclaredMethodsAction.PRIVILEGED.getDeclaredMethods(clazz);
114       }
115    }
116    
117    static Constructor JavaDoc[] getDeclaredConstructors(Class JavaDoc clazz)
118    {
119       if (System.getSecurityManager() == null)
120       {
121          return GetDeclaredConstructorsAction.NON_PRIVILEGED.getDeclaredConstructors(clazz);
122       }
123       else
124       {
125          return GetDeclaredConstructorsAction.PRIVILEGED.getDeclaredConstructors(clazz);
126       }
127    }
128 }
129
Popular Tags