1 8 9 package mx4j.remote.rmi; 10 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Proxy ; 13 import java.rmi.MarshalledObject ; 14 import java.security.AccessControlContext ; 15 import java.security.PrivilegedExceptionAction ; 16 import java.util.ArrayList ; 17 import java.util.Map ; 18 import javax.management.ObjectName ; 19 import javax.management.remote.JMXServerErrorException ; 20 import javax.management.remote.rmi.RMIConnection ; 21 import javax.security.auth.Subject ; 22 23 import mx4j.remote.MX4JRemoteUtils; 24 25 31 public class RMIConnectionSubjectInvoker extends RMIConnectionProxy 32 { 33 public static RMIConnection newInstance(RMIConnection nested, Subject subject, AccessControlContext context, Map environment) 34 { 35 RMIConnectionSubjectInvoker handler = new RMIConnectionSubjectInvoker(nested, subject, context, environment); 36 return (RMIConnection )Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class []{RMIConnection .class}, handler); 37 } 38 39 private final Subject subject; 40 private final AccessControlContext context; 41 private Map environment; 42 43 private RMIConnectionSubjectInvoker(RMIConnection nested, Subject subject, AccessControlContext context, Map environment) 44 { 45 super(nested); 46 this.subject = subject; 47 this.context = context; 48 this.environment = environment; 49 } 50 51 public Object invoke(final Object proxy, final Method method, final Object [] args) 52 throws Throwable 53 { 54 String methodName = method.getName(); 55 if ("fetchNotifications".equals(methodName) || "close".equals(methodName) || "getConnectionId".equals(methodName)) return chain(proxy, method, args); 56 57 if ("addNotificationListeners".equals(methodName)) 58 { 59 Subject [] delegates = (Subject [])args[args.length - 1]; 60 if (delegates == null || delegates.length == 0) return chain(proxy, method, args); 61 62 if (delegates.length == 1) return subjectInvoke(proxy, method, args, delegates[0]); 63 64 ArrayList ids = new ArrayList (); 65 for (int i = 0; i < delegates.length; ++i) 66 { 67 ObjectName name = ((ObjectName [])args[0])[i]; 68 MarshalledObject filter = ((MarshalledObject [])args[1])[i]; 69 Subject delegate = delegates[i]; 70 Object [] newArgs = new Object []{new ObjectName []{name}, new MarshalledObject []{filter}, new Subject []{delegate}}; 71 Integer id = ((Integer [])subjectInvoke(proxy, method, newArgs, delegate))[0]; 72 ids.add(id); 73 } 74 return (Integer [])ids.toArray(new Integer [ids.size()]); 75 } 76 else 77 { 78 Subject delegate = (Subject )args[args.length - 1]; 80 return subjectInvoke(proxy, method, args, delegate); 81 } 82 } 83 84 private Object subjectInvoke(final Object proxy, final Method method, final Object [] args, Subject delegate) throws Exception 85 { 86 return MX4JRemoteUtils.subjectInvoke(subject, delegate, context, environment, new PrivilegedExceptionAction () 87 { 88 public Object run() throws Exception 89 { 90 return chain(proxy, method, args); 91 } 92 }); 93 } 94 95 private Object chain(Object proxy, Method method, Object [] args) throws Exception 96 { 97 try 98 { 99 return super.invoke(proxy, method, args); 100 } 101 catch (Throwable x) 102 { 103 if (x instanceof Exception ) throw (Exception )x; 104 throw new JMXServerErrorException ("Error thrown during invocation", (Error )x); 105 } 106 } 107 } 108 | Popular Tags |