KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > classloading > 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.virtual.classloading;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.Policy JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27
28 import org.jboss.virtual.VFS;
29
30 /**
31  * Package priviledged actions
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 55183 $
35  */

36 class SecurityActions
37 {
38    static PrivilegedAction JavaDoc<Policy JavaDoc> getPolicyAction = new PrivilegedAction JavaDoc<Policy JavaDoc>() {
39       public Policy JavaDoc run()
40       {
41          return Policy.getPolicy();
42       }
43    };
44
45    interface ClassLoaderActions
46    {
47       ClassLoaderActions PRIVILEGED = new ClassLoaderActions() {
48          public VFSClassLoader newClassLoader(final String JavaDoc[] paths,
49                final VFS vfs, final ClassLoader JavaDoc parent)
50          {
51             PrivilegedAction JavaDoc<VFSClassLoader> action = new PrivilegedAction JavaDoc<VFSClassLoader>() {
52                public VFSClassLoader run()
53                {
54                   ClassLoader JavaDoc theParent = parent;
55                   if (parent == null)
56                      theParent = Thread.currentThread().getContextClassLoader();
57                   return new VFSClassLoader(paths, vfs, theParent);
58                }
59             };
60             return AccessController.doPrivileged(action);
61          }
62
63          public Policy JavaDoc getPolicy()
64          {
65             return AccessController.doPrivileged(getPolicyAction);
66          }
67       };
68
69       ClassLoaderActions NON_PRIVILEGED = new ClassLoaderActions() {
70          public VFSClassLoader newClassLoader(final String JavaDoc[] paths,
71                final VFS vfs, final ClassLoader JavaDoc parent)
72          {
73             ClassLoader JavaDoc theParent = parent;
74             if (parent == null)
75                theParent = Thread.currentThread().getContextClassLoader();
76             return new VFSClassLoader(paths, vfs, theParent);
77          }
78
79          public Policy JavaDoc getPolicy()
80          {
81             return Policy.getPolicy();
82          }
83       };
84
85       VFSClassLoader newClassLoader(final String JavaDoc[] paths, final VFS vfs, ClassLoader JavaDoc parent);
86
87       Policy JavaDoc getPolicy();
88    }
89
90    static VFSClassLoader newClassLoader(final String JavaDoc[] paths,
91          final VFS vfs)
92    {
93       if (System.getSecurityManager() == null)
94       {
95          return ClassLoaderActions.NON_PRIVILEGED.newClassLoader(paths, vfs, null);
96       }
97       else
98       {
99          return ClassLoaderActions.PRIVILEGED.newClassLoader(paths, vfs, null);
100       }
101    }
102
103    static VFSClassLoader newClassLoader(final String JavaDoc[] paths,
104          final VFS vfs, ClassLoader JavaDoc parent)
105    {
106       if (System.getSecurityManager() == null)
107       {
108          return ClassLoaderActions.NON_PRIVILEGED.newClassLoader(paths, vfs,
109                parent);
110       }
111       else
112       {
113          return ClassLoaderActions.PRIVILEGED
114                .newClassLoader(paths, vfs, parent);
115       }
116    }
117
118    static Policy JavaDoc getPolicy()
119    {
120       if (System.getSecurityManager() == null)
121       {
122          return ClassLoaderActions.NON_PRIVILEGED.getPolicy();
123       }
124       else
125       {
126          return ClassLoaderActions.PRIVILEGED.getPolicy();
127       }
128    }
129
130 }
131
Popular Tags