1 22 package org.jboss.ejb3.security; 23 24 import java.lang.reflect.UndeclaredThrowableException ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.security.PrivilegedActionException ; 28 import java.security.PrivilegedExceptionAction ; 29 import javax.security.auth.Subject ; 30 import javax.security.jacc.PolicyContext ; 31 import javax.security.jacc.PolicyContextException ; 32 33 34 46 class SecurityActions 47 { 48 private static class SetContextID implements PrivilegedAction 49 { 50 String contextID; 51 52 SetContextID(String contextID) 53 { 54 this.contextID = contextID; 55 } 56 57 public Object run() 58 { 59 String previousID = PolicyContext.getContextID(); 60 PolicyContext.setContextID(contextID); 61 return previousID; 62 } 63 } 64 65 interface PolicyContextActions 66 { 67 70 static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container"; 71 PolicyContextActions PRIVILEGED = new PolicyContextActions() 72 { 73 private final PrivilegedExceptionAction exAction = new PrivilegedExceptionAction () 74 { 75 public Object run() throws Exception 76 { 77 return (Subject ) PolicyContext.getContext(SUBJECT_CONTEXT_KEY); 78 } 79 }; 80 81 public Subject getContextSubject() 82 throws PolicyContextException 83 { 84 try 85 { 86 return (Subject ) AccessController.doPrivileged(exAction); 87 } 88 catch (PrivilegedActionException e) 89 { 90 Exception ex = e.getException(); 91 if (ex instanceof PolicyContextException ) 92 throw (PolicyContextException ) ex; 93 else 94 throw new UndeclaredThrowableException (ex); 95 } 96 } 97 }; 98 99 PolicyContextActions NON_PRIVILEGED = new PolicyContextActions() 100 { 101 public Subject getContextSubject() 102 throws PolicyContextException 103 { 104 return (Subject ) PolicyContext.getContext(SUBJECT_CONTEXT_KEY); 105 } 106 }; 107 108 Subject getContextSubject() 109 throws PolicyContextException ; 110 } 111 112 static Subject getContextSubject() 113 throws PolicyContextException 114 { 115 if (System.getSecurityManager() == null) 116 { 117 return PolicyContextActions.NON_PRIVILEGED.getContextSubject(); 118 } 119 else 120 { 121 return PolicyContextActions.PRIVILEGED.getContextSubject(); 122 } 123 } 124 125 static String setContextID(String contextID) 126 { 127 PrivilegedAction action = new SetContextID(contextID); 128 String previousID = (String ) AccessController.doPrivileged(action); 129 return previousID; 130 } 131 } 132 | Popular Tags |