001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.txtimer;
023:
024: import java.security.PrivilegedAction;
025: import java.security.AccessController;
026:
027: import org.jboss.security.RunAsIdentity;
028: import org.jboss.security.SecurityAssociation;
029:
030: /**
031: * A collection of privileged actions for this package
032: *
033: * @author Scott.Stark@jboss.org
034: * @version $Revision: 57209 $
035: */
036: public class SecurityActions {
037: interface RunAsIdentityActions {
038: RunAsIdentityActions PRIVILEGED = new RunAsIdentityActions() {
039: private final PrivilegedAction peekAction = new PrivilegedAction() {
040: public Object run() {
041: return SecurityAssociation.peekRunAsIdentity();
042: }
043: };
044:
045: private final PrivilegedAction popAction = new PrivilegedAction() {
046: public Object run() {
047: return SecurityAssociation.popRunAsIdentity();
048: }
049: };
050:
051: public RunAsIdentity peek() {
052: return (RunAsIdentity) AccessController
053: .doPrivileged(peekAction);
054: }
055:
056: public void push(final RunAsIdentity id) {
057: AccessController.doPrivileged(new PrivilegedAction() {
058: public Object run() {
059: SecurityAssociation.pushRunAsIdentity(id);
060: return null;
061: }
062: });
063: }
064:
065: public RunAsIdentity pop() {
066: return (RunAsIdentity) AccessController
067: .doPrivileged(popAction);
068: }
069: };
070:
071: RunAsIdentityActions NON_PRIVILEGED = new RunAsIdentityActions() {
072: public RunAsIdentity peek() {
073: return SecurityAssociation.peekRunAsIdentity();
074: }
075:
076: public void push(RunAsIdentity id) {
077: SecurityAssociation.pushRunAsIdentity(id);
078: }
079:
080: public RunAsIdentity pop() {
081: return SecurityAssociation.popRunAsIdentity();
082: }
083: };
084:
085: RunAsIdentity peek();
086:
087: void push(RunAsIdentity id);
088:
089: RunAsIdentity pop();
090: }
091:
092: static ClassLoader getContextClassLoader() {
093: return TCLAction.UTIL.getContextClassLoader();
094: }
095:
096: static ClassLoader getContextClassLoader(Thread thread) {
097: return TCLAction.UTIL.getContextClassLoader(thread);
098: }
099:
100: static void setContextClassLoader(ClassLoader loader) {
101: TCLAction.UTIL.setContextClassLoader(loader);
102: }
103:
104: static void setContextClassLoader(Thread thread, ClassLoader loader) {
105: TCLAction.UTIL.setContextClassLoader(thread, loader);
106: }
107:
108: static void pushRunAsIdentity(RunAsIdentity principal) {
109: if (System.getSecurityManager() == null) {
110: RunAsIdentityActions.NON_PRIVILEGED.push(principal);
111: } else {
112: RunAsIdentityActions.PRIVILEGED.push(principal);
113: }
114: }
115:
116: static RunAsIdentity popRunAsIdentity() {
117: if (System.getSecurityManager() == null) {
118: return RunAsIdentityActions.NON_PRIVILEGED.pop();
119: } else {
120: return RunAsIdentityActions.PRIVILEGED.pop();
121: }
122: }
123:
124: interface TCLAction {
125: class UTIL {
126: static TCLAction getTCLAction() {
127: return System.getSecurityManager() == null ? NON_PRIVILEGED
128: : PRIVILEGED;
129: }
130:
131: static ClassLoader getContextClassLoader() {
132: return getTCLAction().getContextClassLoader();
133: }
134:
135: static ClassLoader getContextClassLoader(Thread thread) {
136: return getTCLAction().getContextClassLoader(thread);
137: }
138:
139: static void setContextClassLoader(ClassLoader cl) {
140: getTCLAction().setContextClassLoader(cl);
141: }
142:
143: static void setContextClassLoader(Thread thread,
144: ClassLoader cl) {
145: getTCLAction().setContextClassLoader(thread, cl);
146: }
147: }
148:
149: TCLAction NON_PRIVILEGED = new TCLAction() {
150: public ClassLoader getContextClassLoader() {
151: return Thread.currentThread().getContextClassLoader();
152: }
153:
154: public ClassLoader getContextClassLoader(Thread thread) {
155: return thread.getContextClassLoader();
156: }
157:
158: public void setContextClassLoader(ClassLoader cl) {
159: Thread.currentThread().setContextClassLoader(cl);
160: }
161:
162: public void setContextClassLoader(Thread thread,
163: ClassLoader cl) {
164: thread.setContextClassLoader(cl);
165: }
166: };
167:
168: TCLAction PRIVILEGED = new TCLAction() {
169: private final PrivilegedAction getTCLPrivilegedAction = new PrivilegedAction() {
170: public Object run() {
171: return Thread.currentThread()
172: .getContextClassLoader();
173: }
174: };
175:
176: public ClassLoader getContextClassLoader() {
177: return (ClassLoader) AccessController
178: .doPrivileged(getTCLPrivilegedAction);
179: }
180:
181: public ClassLoader getContextClassLoader(final Thread thread) {
182: return (ClassLoader) AccessController
183: .doPrivileged(new PrivilegedAction() {
184: public Object run() {
185: return thread.getContextClassLoader();
186: }
187: });
188: }
189:
190: public void setContextClassLoader(final ClassLoader cl) {
191: AccessController.doPrivileged(new PrivilegedAction() {
192: public Object run() {
193: Thread.currentThread()
194: .setContextClassLoader(cl);
195: return null;
196: }
197: });
198: }
199:
200: public void setContextClassLoader(final Thread thread,
201: final ClassLoader cl) {
202: AccessController.doPrivileged(new PrivilegedAction() {
203: public Object run() {
204: thread.setContextClassLoader(cl);
205: return null;
206: }
207: });
208: }
209: };
210:
211: ClassLoader getContextClassLoader();
212:
213: ClassLoader getContextClassLoader(Thread thread);
214:
215: void setContextClassLoader(ClassLoader cl);
216:
217: void setContextClassLoader(Thread thread, ClassLoader cl);
218: }
219: }
|