1 22 package org.jboss.jmx.connector.invoker; 23 24 import java.security.AccessController ; 25 import java.security.Principal ; 26 import java.security.PrivilegedAction ; 27 import java.util.HashMap ; 28 29 import javax.security.auth.Subject ; 30 31 import org.jboss.security.SecurityAssociation; 32 import org.jboss.security.SecurityConstants; 33 import org.jboss.security.SecurityContext; 34 35 40 class SecurityActions 41 { 42 private static class GetSubjectAction implements PrivilegedAction 43 { 44 static PrivilegedAction ACTION = new GetSubjectAction(); 45 public Object run() 46 { 47 Subject subject = SecurityAssociation.getSubject(); 48 return subject; 49 } 50 } 51 private static class GetTCLAction implements PrivilegedAction 52 { 53 static PrivilegedAction ACTION = new GetTCLAction(); 54 public Object run() 55 { 56 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 57 return loader; 58 } 59 } 60 private static class SetTCLAction implements PrivilegedAction 61 { 62 ClassLoader loader; 63 SetTCLAction(ClassLoader loader) 64 { 65 this.loader = loader; 66 } 67 public Object run() 68 { 69 Thread.currentThread().setContextClassLoader(loader); 70 loader = null; 71 return null; 72 } 73 } 74 interface PrincipalInfoAction 75 { 76 PrincipalInfoAction PRIVILEGED = new PrincipalInfoAction() 77 { 78 public void push(final Principal principal, final Object credential, 79 final Subject subject) 80 { 81 AccessController.doPrivileged( 82 new PrivilegedAction () 83 { 84 public Object run() 85 { 86 SecurityAssociation.pushSubjectContext(subject, principal, credential); 87 return null; 88 } 89 } 90 ); 91 } 92 public void pop() 93 { 94 AccessController.doPrivileged( 95 new PrivilegedAction () 96 { 97 public Object run() 98 { 99 SecurityAssociation.popSubjectContext(); 100 return null; 101 } 102 } 103 ); 104 } 105 }; 106 107 PrincipalInfoAction NON_PRIVILEGED = new PrincipalInfoAction() 108 { 109 public void push(Principal principal, Object credential, Subject subject) 110 { 111 SecurityAssociation.pushSubjectContext(subject, principal, credential); 112 } 113 public void pop() 114 { 115 SecurityAssociation.popSubjectContext(); 116 } 117 }; 118 119 void push(Principal principal, Object credential, Subject subject); 120 void pop(); 121 } 122 123 static class SetSecurityContextAction implements PrivilegedAction 124 { 125 private SecurityContext securityContext; 126 private String securityDomain; 127 SetSecurityContextAction(SecurityContext sc, String sd) 128 { 129 this.securityContext = sc; 130 this.securityDomain = sd; 131 } 132 133 public Object run() 134 { 135 String sc = SecurityConstants.SECURITY_CONTEXT; 136 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 137 if(map == null) 138 { 139 map = new HashMap (); 140 SecurityAssociation.setContextInfo(sc, map); 141 } 142 map.put(securityDomain, securityContext); 143 SecurityAssociation.setContextInfo(sc, map); 144 return null; 145 } 146 } 147 148 static Subject getActiveSubject() 149 { 150 Subject subject = (Subject ) AccessController.doPrivileged(GetSubjectAction.ACTION); 151 return subject; 152 } 153 static ClassLoader getContextClassLoader() 154 { 155 ClassLoader loader = (ClassLoader ) AccessController.doPrivileged(GetTCLAction.ACTION); 156 return loader; 157 } 158 static void setContextClassLoader(ClassLoader loader) 159 { 160 PrivilegedAction action = new SetTCLAction(loader); 161 AccessController.doPrivileged(action); 162 } 163 164 static void pushSubjectContext(Principal principal, Object credential, 165 Subject subject) 166 { 167 if(System.getSecurityManager() == null) 168 { 169 PrincipalInfoAction.NON_PRIVILEGED.push(principal, credential, subject); 170 } 171 else 172 { 173 PrincipalInfoAction.PRIVILEGED.push(principal, credential, subject); 174 } 175 } 176 static void popSubjectContext() 177 { 178 if(System.getSecurityManager() == null) 179 { 180 PrincipalInfoAction.NON_PRIVILEGED.pop(); 181 } 182 else 183 { 184 PrincipalInfoAction.PRIVILEGED.pop(); 185 } 186 } 187 static void setSecurityContext(SecurityContext sc, String securityDomain) 188 { 189 SetSecurityContextAction action = new SetSecurityContextAction(sc,securityDomain); 190 AccessController.doPrivileged(action); 191 } 192 } 193 | Popular Tags |