KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > 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.txtimer;
23
24 import java.security.PrivilegedAction JavaDoc;
25 import java.security.AccessController JavaDoc;
26
27 import org.jboss.security.RunAsIdentity;
28 import org.jboss.security.SecurityAssociation;
29
30 /**
31  * A collection of privileged actions for this package
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 37858 $
35  */

36 public class SecurityActions
37 {
38    interface RunAsIdentityActions
39    {
40       RunAsIdentityActions PRIVILEGED = new RunAsIdentityActions()
41       {
42          private final PrivilegedAction JavaDoc peekAction = new PrivilegedAction JavaDoc()
43          {
44             public Object JavaDoc run()
45             {
46                return SecurityAssociation.peekRunAsIdentity();
47             }
48          };
49
50          private final PrivilegedAction JavaDoc popAction = new PrivilegedAction JavaDoc()
51          {
52             public Object JavaDoc run()
53             {
54                return SecurityAssociation.popRunAsIdentity();
55             }
56          };
57
58          public RunAsIdentity peek()
59          {
60             return (RunAsIdentity)AccessController.doPrivileged(peekAction);
61          }
62
63          public void push(final RunAsIdentity id)
64          {
65             AccessController.doPrivileged(
66                new PrivilegedAction JavaDoc()
67                {
68                   public Object JavaDoc run()
69                   {
70                      SecurityAssociation.pushRunAsIdentity(id);
71                      return null;
72                   }
73                }
74             );
75          }
76
77          public RunAsIdentity pop()
78          {
79             return (RunAsIdentity)AccessController.doPrivileged(popAction);
80          }
81       };
82
83       RunAsIdentityActions NON_PRIVILEGED = new RunAsIdentityActions()
84       {
85          public RunAsIdentity peek()
86          {
87             return SecurityAssociation.peekRunAsIdentity();
88          }
89
90          public void push(RunAsIdentity id)
91          {
92             SecurityAssociation.pushRunAsIdentity(id);
93          }
94
95          public RunAsIdentity pop()
96          {
97             return SecurityAssociation.popRunAsIdentity();
98          }
99       };
100
101       RunAsIdentity peek();
102
103       void push(RunAsIdentity id);
104
105       RunAsIdentity pop();
106    }
107
108    static ClassLoader JavaDoc getContextClassLoader()
109    {
110       return TCLAction.UTIL.getContextClassLoader();
111    }
112
113    static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
114    {
115       return TCLAction.UTIL.getContextClassLoader(thread);
116    }
117
118    static void setContextClassLoader(ClassLoader JavaDoc loader)
119    {
120       TCLAction.UTIL.setContextClassLoader(loader);
121    }
122
123    static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc loader)
124    {
125       TCLAction.UTIL.setContextClassLoader(thread, loader);
126    }
127
128    static void pushRunAsIdentity(RunAsIdentity principal)
129    {
130       if(System.getSecurityManager() == null)
131       {
132          RunAsIdentityActions.NON_PRIVILEGED.push(principal);
133       }
134       else
135       {
136          RunAsIdentityActions.PRIVILEGED.push(principal);
137       }
138    }
139
140    static RunAsIdentity popRunAsIdentity()
141    {
142       if(System.getSecurityManager() == null)
143       {
144          return RunAsIdentityActions.NON_PRIVILEGED.pop();
145       }
146       else
147       {
148          return RunAsIdentityActions.PRIVILEGED.pop();
149       }
150    }
151
152    interface TCLAction
153    {
154       class UTIL
155       {
156          static TCLAction getTCLAction()
157          {
158             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
159          }
160
161          static ClassLoader JavaDoc getContextClassLoader()
162          {
163             return getTCLAction().getContextClassLoader();
164          }
165
166          static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
167          {
168             return getTCLAction().getContextClassLoader(thread);
169          }
170
171          static void setContextClassLoader(ClassLoader JavaDoc cl)
172          {
173             getTCLAction().setContextClassLoader(cl);
174          }
175
176          static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
177          {
178             getTCLAction().setContextClassLoader(thread, cl);
179          }
180       }
181
182       TCLAction NON_PRIVILEGED = new TCLAction()
183       {
184          public ClassLoader JavaDoc getContextClassLoader()
185          {
186             return Thread.currentThread().getContextClassLoader();
187          }
188
189          public ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
190          {
191             return thread.getContextClassLoader();
192          }
193
194          public void setContextClassLoader(ClassLoader JavaDoc cl)
195          {
196             Thread.currentThread().setContextClassLoader(cl);
197          }
198
199          public void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
200          {
201             thread.setContextClassLoader(cl);
202          }
203       };
204
205       TCLAction PRIVILEGED = new TCLAction()
206       {
207          private final PrivilegedAction JavaDoc getTCLPrivilegedAction = new PrivilegedAction JavaDoc()
208          {
209             public Object JavaDoc run()
210             {
211                return Thread.currentThread().getContextClassLoader();
212             }
213          };
214
215          public ClassLoader JavaDoc getContextClassLoader()
216          {
217             return (ClassLoader JavaDoc)AccessController.doPrivileged(getTCLPrivilegedAction);
218          }
219
220          public ClassLoader JavaDoc getContextClassLoader(final Thread JavaDoc thread)
221          {
222             return (ClassLoader JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
223             {
224                public Object JavaDoc run()
225                {
226                   return thread.getContextClassLoader();
227                }
228             });
229          }
230
231          public void setContextClassLoader(final ClassLoader JavaDoc cl)
232          {
233             AccessController.doPrivileged(
234                new PrivilegedAction JavaDoc()
235                {
236                   public Object JavaDoc run()
237                   {
238                      Thread.currentThread().setContextClassLoader(cl);
239                      return null;
240                   }
241                }
242             );
243          }
244
245          public void setContextClassLoader(final Thread JavaDoc thread, final ClassLoader JavaDoc cl)
246          {
247             AccessController.doPrivileged(
248                new PrivilegedAction JavaDoc()
249                {
250                   public Object JavaDoc run()
251                   {
252                      thread.setContextClassLoader(cl);
253                      return null;
254                   }
255                }
256             );
257          }
258       };
259
260       ClassLoader JavaDoc getContextClassLoader();
261
262       ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread);
263
264       void setContextClassLoader(ClassLoader JavaDoc cl);
265
266       void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl);
267    }
268 }
269
Popular Tags