1 22 package org.jboss.ejb.txtimer; 23 24 import java.security.PrivilegedAction ; 25 import java.security.AccessController ; 26 27 import org.jboss.security.RunAsIdentity; 28 import org.jboss.security.SecurityAssociation; 29 30 36 public class SecurityActions 37 { 38 interface RunAsIdentityActions 39 { 40 RunAsIdentityActions PRIVILEGED = new RunAsIdentityActions() 41 { 42 private final PrivilegedAction peekAction = new PrivilegedAction () 43 { 44 public Object run() 45 { 46 return SecurityAssociation.peekRunAsIdentity(); 47 } 48 }; 49 50 private final PrivilegedAction popAction = new PrivilegedAction () 51 { 52 public Object 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 () 67 { 68 public Object 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 getContextClassLoader() 109 { 110 return TCLAction.UTIL.getContextClassLoader(); 111 } 112 113 static ClassLoader getContextClassLoader(Thread thread) 114 { 115 return TCLAction.UTIL.getContextClassLoader(thread); 116 } 117 118 static void setContextClassLoader(ClassLoader loader) 119 { 120 TCLAction.UTIL.setContextClassLoader(loader); 121 } 122 123 static void setContextClassLoader(Thread thread, ClassLoader 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 getContextClassLoader() 162 { 163 return getTCLAction().getContextClassLoader(); 164 } 165 166 static ClassLoader getContextClassLoader(Thread thread) 167 { 168 return getTCLAction().getContextClassLoader(thread); 169 } 170 171 static void setContextClassLoader(ClassLoader cl) 172 { 173 getTCLAction().setContextClassLoader(cl); 174 } 175 176 static void setContextClassLoader(Thread thread, ClassLoader cl) 177 { 178 getTCLAction().setContextClassLoader(thread, cl); 179 } 180 } 181 182 TCLAction NON_PRIVILEGED = new TCLAction() 183 { 184 public ClassLoader getContextClassLoader() 185 { 186 return Thread.currentThread().getContextClassLoader(); 187 } 188 189 public ClassLoader getContextClassLoader(Thread thread) 190 { 191 return thread.getContextClassLoader(); 192 } 193 194 public void setContextClassLoader(ClassLoader cl) 195 { 196 Thread.currentThread().setContextClassLoader(cl); 197 } 198 199 public void setContextClassLoader(Thread thread, ClassLoader cl) 200 { 201 thread.setContextClassLoader(cl); 202 } 203 }; 204 205 TCLAction PRIVILEGED = new TCLAction() 206 { 207 private final PrivilegedAction getTCLPrivilegedAction = new PrivilegedAction () 208 { 209 public Object run() 210 { 211 return Thread.currentThread().getContextClassLoader(); 212 } 213 }; 214 215 public ClassLoader getContextClassLoader() 216 { 217 return (ClassLoader )AccessController.doPrivileged(getTCLPrivilegedAction); 218 } 219 220 public ClassLoader getContextClassLoader(final Thread thread) 221 { 222 return (ClassLoader )AccessController.doPrivileged(new PrivilegedAction () 223 { 224 public Object run() 225 { 226 return thread.getContextClassLoader(); 227 } 228 }); 229 } 230 231 public void setContextClassLoader(final ClassLoader cl) 232 { 233 AccessController.doPrivileged( 234 new PrivilegedAction () 235 { 236 public Object run() 237 { 238 Thread.currentThread().setContextClassLoader(cl); 239 return null; 240 } 241 } 242 ); 243 } 244 245 public void setContextClassLoader(final Thread thread, final ClassLoader cl) 246 { 247 AccessController.doPrivileged( 248 new PrivilegedAction () 249 { 250 public Object run() 251 { 252 thread.setContextClassLoader(cl); 253 return null; 254 } 255 } 256 ); 257 } 258 }; 259 260 ClassLoader getContextClassLoader(); 261 262 ClassLoader getContextClassLoader(Thread thread); 263 264 void setContextClassLoader(ClassLoader cl); 265 266 void setContextClassLoader(Thread thread, ClassLoader cl); 267 } 268 } 269 | Popular Tags |