KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > 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.invocation;
23
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.AccessController JavaDoc;
26
27 /**
28  * Package privileged actions
29  *
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 37459 $
32  */

33 class SecurityActions
34 {
35    interface TCLAction
36    {
37       class UTIL
38       {
39          static TCLAction getTCLAction()
40          {
41             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
42          }
43
44          static ClassLoader JavaDoc getContextClassLoader()
45          {
46             return getTCLAction().getContextClassLoader();
47          }
48
49          static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
50          {
51             return getTCLAction().getContextClassLoader(thread);
52          }
53
54          static void setContextClassLoader(ClassLoader JavaDoc cl)
55          {
56             getTCLAction().setContextClassLoader(cl);
57          }
58
59          static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
60          {
61             getTCLAction().setContextClassLoader(thread, cl);
62          }
63       }
64
65       TCLAction NON_PRIVILEGED = new TCLAction()
66       {
67          public ClassLoader JavaDoc getContextClassLoader()
68          {
69             return Thread.currentThread().getContextClassLoader();
70          }
71
72          public ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
73          {
74             return thread.getContextClassLoader();
75          }
76
77          public void setContextClassLoader(ClassLoader JavaDoc cl)
78          {
79             Thread.currentThread().setContextClassLoader(cl);
80          }
81
82          public void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
83          {
84             thread.setContextClassLoader(cl);
85          }
86       };
87
88       TCLAction PRIVILEGED = new TCLAction()
89       {
90          private final PrivilegedAction JavaDoc getTCLPrivilegedAction = new PrivilegedAction JavaDoc()
91          {
92             public Object JavaDoc run()
93             {
94                return Thread.currentThread().getContextClassLoader();
95             }
96          };
97
98          public ClassLoader JavaDoc getContextClassLoader()
99          {
100             return (ClassLoader JavaDoc) AccessController.doPrivileged(getTCLPrivilegedAction);
101          }
102
103          public ClassLoader JavaDoc getContextClassLoader(final Thread JavaDoc thread)
104          {
105             return (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
106             {
107                public Object JavaDoc run()
108                {
109                   return thread.getContextClassLoader();
110                }
111             });
112          }
113
114          public void setContextClassLoader(final ClassLoader JavaDoc cl)
115          {
116             AccessController.doPrivileged(
117                new PrivilegedAction JavaDoc()
118                {
119                   public Object JavaDoc run()
120                   {
121                      Thread.currentThread().setContextClassLoader(cl);
122                      return null;
123                   }
124                }
125             );
126          }
127
128          public void setContextClassLoader(final Thread JavaDoc thread, final ClassLoader JavaDoc cl)
129          {
130             AccessController.doPrivileged(
131                new PrivilegedAction JavaDoc()
132                {
133                   public Object JavaDoc run()
134                   {
135                      thread.setContextClassLoader(cl);
136                      return null;
137                   }
138                }
139             );
140          }
141       };
142
143       ClassLoader JavaDoc getContextClassLoader();
144
145       ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread);
146
147       void setContextClassLoader(ClassLoader JavaDoc cl);
148
149       void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl);
150    }
151
152    static ClassLoader JavaDoc getContextClassLoader()
153    {
154       return TCLAction.UTIL.getContextClassLoader();
155    }
156
157 }
158
Popular Tags