KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > 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.ejb3;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import javax.security.jacc.PolicyContext JavaDoc;
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 $Revison:$
35  */

36 public class SecurityActions
37 {
38    private static class SetContextID implements PrivilegedAction JavaDoc
39    {
40       String JavaDoc contextID;
41
42       SetContextID(String JavaDoc contextID)
43       {
44          this.contextID = contextID;
45       }
46
47       public Object JavaDoc run()
48       {
49          String JavaDoc previousID = PolicyContext.getContextID();
50          PolicyContext.setContextID(contextID);
51          return previousID;
52       }
53    }
54
55    private static class PeekRunAsRoleAction implements PrivilegedAction JavaDoc
56    {
57       int depth;
58
59       PeekRunAsRoleAction(int depth)
60       {
61          this.depth = depth;
62       }
63
64       public Object JavaDoc run()
65       {
66          RunAsIdentity principal = SecurityAssociation.peekRunAsIdentity(depth);
67          return principal;
68       }
69    }
70
71    static ClassLoader JavaDoc getContextClassLoader()
72    {
73       return TCLAction.UTIL.getContextClassLoader();
74    }
75
76    static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
77    {
78       return TCLAction.UTIL.getContextClassLoader(thread);
79    }
80
81    static void setContextClassLoader(ClassLoader JavaDoc loader)
82    {
83       TCLAction.UTIL.setContextClassLoader(loader);
84    }
85
86    static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc loader)
87    {
88       TCLAction.UTIL.setContextClassLoader(thread, loader);
89    }
90
91    static String JavaDoc setContextID(String JavaDoc contextID)
92    {
93       PrivilegedAction JavaDoc action = new SetContextID(contextID);
94       String JavaDoc previousID = (String JavaDoc) AccessController.doPrivileged(action);
95       return previousID;
96    }
97
98    public static RunAsIdentity peekRunAsIdentity(int depth)
99    {
100       PrivilegedAction JavaDoc action = new PeekRunAsRoleAction(depth);
101       RunAsIdentity principal = (RunAsIdentity) AccessController.doPrivileged(action);
102       return principal;
103    }
104
105    interface TCLAction
106    {
107       class UTIL
108       {
109          static TCLAction getTCLAction()
110          {
111             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
112          }
113
114          static ClassLoader JavaDoc getContextClassLoader()
115          {
116             return getTCLAction().getContextClassLoader();
117          }
118
119          static ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
120          {
121             return getTCLAction().getContextClassLoader(thread);
122          }
123
124          static void setContextClassLoader(ClassLoader JavaDoc cl)
125          {
126             getTCLAction().setContextClassLoader(cl);
127          }
128
129          static void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
130          {
131             getTCLAction().setContextClassLoader(thread, cl);
132          }
133       }
134
135       TCLAction NON_PRIVILEGED = new TCLAction()
136       {
137          public ClassLoader JavaDoc getContextClassLoader()
138          {
139             return Thread.currentThread().getContextClassLoader();
140          }
141
142          public ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread)
143          {
144             return thread.getContextClassLoader();
145          }
146
147          public void setContextClassLoader(ClassLoader JavaDoc cl)
148          {
149             Thread.currentThread().setContextClassLoader(cl);
150          }
151
152          public void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl)
153          {
154             thread.setContextClassLoader(cl);
155          }
156       };
157
158       TCLAction PRIVILEGED = new TCLAction()
159       {
160          private final PrivilegedAction JavaDoc getTCLPrivilegedAction = new PrivilegedAction JavaDoc()
161          {
162             public Object JavaDoc run()
163             {
164                return Thread.currentThread().getContextClassLoader();
165             }
166          };
167
168          public ClassLoader JavaDoc getContextClassLoader()
169          {
170             return (ClassLoader JavaDoc) AccessController.doPrivileged(getTCLPrivilegedAction);
171          }
172
173          public ClassLoader JavaDoc getContextClassLoader(final Thread JavaDoc thread)
174          {
175             return (ClassLoader JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc()
176             {
177                public Object JavaDoc run()
178                {
179                   return thread.getContextClassLoader();
180                }
181             });
182          }
183
184          public void setContextClassLoader(final ClassLoader JavaDoc cl)
185          {
186             AccessController.doPrivileged(new PrivilegedAction JavaDoc()
187             {
188                public Object JavaDoc run()
189                {
190                   Thread.currentThread().setContextClassLoader(cl);
191                   return null;
192                }
193             });
194          }
195
196          public void setContextClassLoader(final Thread JavaDoc thread, final ClassLoader JavaDoc cl)
197          {
198             AccessController.doPrivileged(new PrivilegedAction JavaDoc()
199             {
200                public Object JavaDoc run()
201                {
202                   thread.setContextClassLoader(cl);
203                   return null;
204                }
205             });
206          }
207       };
208
209       ClassLoader JavaDoc getContextClassLoader();
210
211       ClassLoader JavaDoc getContextClassLoader(Thread JavaDoc thread);
212
213       void setContextClassLoader(ClassLoader JavaDoc cl);
214
215       void setContextClassLoader(Thread JavaDoc thread, ClassLoader JavaDoc cl);
216    }
217 }
218
Popular Tags