KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > remote > rmi > RMIConnectionSubjectInvoker


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.remote.rmi;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.Proxy JavaDoc;
13 import java.rmi.MarshalledObject JavaDoc;
14 import java.security.AccessControlContext JavaDoc;
15 import java.security.PrivilegedExceptionAction JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Map JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.management.remote.JMXServerErrorException JavaDoc;
20 import javax.management.remote.rmi.RMIConnection JavaDoc;
21 import javax.security.auth.Subject JavaDoc;
22
23 import mx4j.remote.MX4JRemoteUtils;
24
25 /**
26  * An RMIConnection proxy that wraps the call into a {@link Subject#doAsPrivileged} invocation,
27  * in order to execute the code under subject-based security, and to perform subject delegation.
28  *
29  * @version $Revision: 1.10 $
30  */

31 public class RMIConnectionSubjectInvoker extends RMIConnectionProxy
32 {
33    public static RMIConnection JavaDoc newInstance(RMIConnection JavaDoc nested, Subject JavaDoc subject, AccessControlContext JavaDoc context, Map JavaDoc environment)
34    {
35       RMIConnectionSubjectInvoker handler = new RMIConnectionSubjectInvoker(nested, subject, context, environment);
36       return (RMIConnection JavaDoc)Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class JavaDoc[]{RMIConnection JavaDoc.class}, handler);
37    }
38
39    private final Subject JavaDoc subject;
40    private final AccessControlContext JavaDoc context;
41    private Map JavaDoc environment;
42
43    private RMIConnectionSubjectInvoker(RMIConnection JavaDoc nested, Subject JavaDoc subject, AccessControlContext JavaDoc context, Map JavaDoc environment)
44    {
45       super(nested);
46       this.subject = subject;
47       this.context = context;
48       this.environment = environment;
49    }
50
51    public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args)
52            throws Throwable JavaDoc
53    {
54       String JavaDoc 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 JavaDoc[] delegates = (Subject JavaDoc[])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 JavaDoc ids = new ArrayList JavaDoc();
65          for (int i = 0; i < delegates.length; ++i)
66          {
67             ObjectName JavaDoc name = ((ObjectName JavaDoc[])args[0])[i];
68             MarshalledObject JavaDoc filter = ((MarshalledObject JavaDoc[])args[1])[i];
69             Subject JavaDoc delegate = delegates[i];
70             Object JavaDoc[] newArgs = new Object JavaDoc[]{new ObjectName JavaDoc[]{name}, new MarshalledObject JavaDoc[]{filter}, new Subject JavaDoc[]{delegate}};
71             Integer JavaDoc id = ((Integer JavaDoc[])subjectInvoke(proxy, method, newArgs, delegate))[0];
72             ids.add(id);
73          }
74          return (Integer JavaDoc[])ids.toArray(new Integer JavaDoc[ids.size()]);
75       }
76       else
77       {
78          // For all other methods, the subject is always the last argument
79
Subject JavaDoc delegate = (Subject JavaDoc)args[args.length - 1];
80          return subjectInvoke(proxy, method, args, delegate);
81       }
82    }
83
84    private Object JavaDoc subjectInvoke(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args, Subject JavaDoc delegate) throws Exception JavaDoc
85    {
86       return MX4JRemoteUtils.subjectInvoke(subject, delegate, context, environment, new PrivilegedExceptionAction JavaDoc()
87       {
88          public Object JavaDoc run() throws Exception JavaDoc
89          {
90             return chain(proxy, method, args);
91          }
92       });
93    }
94
95    private Object JavaDoc chain(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc
96    {
97       try
98       {
99          return super.invoke(proxy, method, args);
100       }
101       catch (Throwable JavaDoc x)
102       {
103          if (x instanceof Exception JavaDoc) throw (Exception JavaDoc)x;
104          throw new JMXServerErrorException JavaDoc("Error thrown during invocation", (Error JavaDoc)x);
105       }
106    }
107 }
108
Popular Tags