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