1 8 9 package mx4j.tools.remote; 10 11 import java.lang.reflect.InvocationHandler ; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 14 import java.security.AccessControlContext ; 15 import java.security.PrivilegedExceptionAction ; 16 import java.util.Map ; 17 import javax.management.remote.JMXServerErrorException ; 18 import javax.security.auth.Subject ; 19 20 import mx4j.remote.MX4JRemoteUtils; 21 22 25 public abstract class SubjectInvoker implements InvocationHandler 26 { 27 private final Object target; 28 private final Subject subject; 29 private final AccessControlContext context; 30 private Map environment; 31 32 protected SubjectInvoker(Object target, Subject subject, AccessControlContext context, Map environment) 33 { 34 this.target = target; 35 this.subject = subject; 36 this.context = context; 37 this.environment = environment; 38 } 39 40 protected boolean isPlainInvoke(Method method) 41 { 42 String methodName = method.getName(); 43 if ("toString".equals(methodName)) return true; 45 if ("hashCode".equals(methodName)) return true; 46 if ("equals".equals(methodName)) return true; 47 return false; 48 } 49 50 protected Object handleSpecialInvoke(Object target, Method method, Object [] args) throws Exception 51 { 52 throw new NoSuchMethodException (method.toString()); 53 } 54 55 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 56 { 57 if (isPlainInvoke(method)) return chain(target, method, args); 58 if (method.getParameterTypes()[args.length - 1] == Subject .class) 59 { 60 Subject delegate = (Subject )args[args.length - 1]; 61 return subjectInvoke(target, method, args, delegate); 62 } 63 else 64 { 65 return handleSpecialInvoke(target, method, args); 66 } 67 } 68 69 protected Object subjectInvoke(final Object proxy, final Method method, final Object [] args, Subject delegate) throws Exception 70 { 71 return MX4JRemoteUtils.subjectInvoke(subject, delegate, context, environment, new PrivilegedExceptionAction () 72 { 73 public Object run() throws Exception 74 { 75 return chain(proxy, method, args); 76 } 77 }); 78 } 79 80 protected Object chain(Object proxy, Method method, Object [] args) throws Exception 81 { 82 try 83 { 84 return method.invoke(proxy, args); 85 } 86 catch (InvocationTargetException x) 87 { 88 Throwable t = x.getTargetException(); 89 if (t instanceof Exception ) throw (Exception )t; 90 throw new JMXServerErrorException ("Error thrown during invocation", (Error )t); 91 } 92 } 93 } 94 | Popular Tags |