1 7 package org.jboss.security; 8 9 import java.lang.reflect.InvocationTargetException ; 10 import java.lang.reflect.UndeclaredThrowableException ; 11 import java.lang.reflect.Method ; 12 import java.security.PrivilegedExceptionAction ; 13 import java.security.PrivilegedActionException ; 14 import javax.security.auth.Subject ; 15 16 import org.jboss.security.SubjectSecurityManager; 17 18 33 public class SubjectSecurityProxy extends AbstractSecurityProxy 34 { 35 private SubjectSecurityManager subjectSecurityManager; 36 37 SubjectSecurityProxy(Object delegate) 38 { 39 super(delegate); 40 } 41 42 public void init(Class beanHome, Class beanRemote, Object securityMgr) 43 throws InstantiationException 44 { 45 init(beanHome, beanRemote, null, null, securityMgr); 46 } 47 48 public void init(Class beanHome, Class beanRemote, 49 Class beanLocalHome, Class beanLocal, Object securityMgr) 50 throws InstantiationException 51 { 52 if ((securityMgr instanceof SubjectSecurityManager) == false) 53 { 54 String msg = "SubjectSecurityProxy requires a SubjectSecurityManager" 55 + " instance, securityMgr=" + securityMgr; 56 throw new InstantiationException (msg); 57 } 58 subjectSecurityManager = (SubjectSecurityManager) securityMgr; 59 super.init(beanHome, beanRemote, beanLocalHome, beanLocal, securityMgr); 60 } 61 62 protected void invokeHomeOnDelegate(final Method m, final Object [] args, final Object delegate) 63 throws SecurityException 64 { final Subject subject = subjectSecurityManager.getActiveSubject(); 66 if (subject == null) 67 throw new SecurityException ("No subject associated with secure proxy"); 68 69 try 70 { 71 Subject.doAsPrivileged(subject, new PrivilegedExceptionAction () 72 { 73 public Object run() throws Exception 74 { 75 m.invoke(delegate, args); 76 return null; 77 } 78 }, 79 null 80 ); 81 } 82 catch (PrivilegedActionException e) 83 { 84 Throwable t = e.getException(); 85 if (t instanceof InvocationTargetException ) 86 { 87 t = ((InvocationTargetException ) t).getTargetException(); 88 } 89 else if (t instanceof UndeclaredThrowableException ) 90 { 91 t = ((UndeclaredThrowableException ) t).getUndeclaredThrowable(); 92 } 93 if (t instanceof SecurityException ) 94 throw (SecurityException ) t; 95 t.printStackTrace(); 96 throw new SecurityException ("Unexpected error during security proxy execution:" + t.getMessage()); 97 } 98 } 99 100 protected void invokeOnDelegate(final Method m, final Object [] args, final Object delegate) 101 throws Exception 102 { final Subject subject = subjectSecurityManager.getActiveSubject(); 104 if (subject == null) 105 throw new SecurityException ("No subject associated with secure proxy"); 106 107 try 108 { 109 Subject.doAsPrivileged(subject, new PrivilegedExceptionAction () 110 { 111 public Object run() throws Exception 112 { 113 m.invoke(delegate, args); 114 return null; 115 } 116 }, 117 null 118 ); 119 } 120 catch (PrivilegedActionException e) 121 { 122 Throwable t = e.getException(); 123 if (t instanceof InvocationTargetException ) 124 { 125 InvocationTargetException ex = (InvocationTargetException ) t; 127 t = ex.getTargetException(); 128 throw (Exception ) t; 129 } 130 else if (t instanceof UndeclaredThrowableException ) 131 { 132 t = ((UndeclaredThrowableException ) t).getUndeclaredThrowable(); 133 } 134 if (t instanceof SecurityException ) 135 throw (SecurityException ) t; 136 throw new SecurityException ("Unexpected error during security proxy execution:" + t.getMessage()); 137 } 138 } 139 140 } 141 | Popular Tags |