KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > SubjectInvoker


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.tools.remote;
10
11 import java.lang.reflect.InvocationHandler JavaDoc;
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.security.AccessControlContext JavaDoc;
15 import java.security.PrivilegedExceptionAction JavaDoc;
16 import java.util.Map JavaDoc;
17 import javax.management.remote.JMXServerErrorException JavaDoc;
18 import javax.security.auth.Subject JavaDoc;
19
20 import mx4j.remote.MX4JRemoteUtils;
21
22 /**
23  * @version $Revision: 1.4 $
24  */

25 public abstract class SubjectInvoker implements InvocationHandler JavaDoc
26 {
27    private final Object JavaDoc target;
28    private final Subject JavaDoc subject;
29    private final AccessControlContext JavaDoc context;
30    private Map JavaDoc environment;
31
32    protected SubjectInvoker(Object JavaDoc target, Subject JavaDoc subject, AccessControlContext JavaDoc context, Map JavaDoc 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 JavaDoc method)
41    {
42       String JavaDoc methodName = method.getName();
43       // java.lang.Object methods
44
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 JavaDoc handleSpecialInvoke(Object JavaDoc target, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc
51    {
52       throw new NoSuchMethodException JavaDoc(method.toString());
53    }
54
55    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
56    {
57       if (isPlainInvoke(method)) return chain(target, method, args);
58       if (method.getParameterTypes()[args.length - 1] == Subject JavaDoc.class)
59       {
60          Subject JavaDoc delegate = (Subject JavaDoc)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 JavaDoc subjectInvoke(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args, Subject JavaDoc delegate) throws Exception JavaDoc
70    {
71       return MX4JRemoteUtils.subjectInvoke(subject, delegate, context, environment, new PrivilegedExceptionAction JavaDoc()
72       {
73          public Object JavaDoc run() throws Exception JavaDoc
74          {
75             return chain(proxy, method, args);
76          }
77       });
78    }
79
80    protected Object JavaDoc chain(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Exception JavaDoc
81    {
82       try
83       {
84          return method.invoke(proxy, args);
85       }
86       catch (InvocationTargetException JavaDoc x)
87       {
88          Throwable JavaDoc t = x.getTargetException();
89          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
90          throw new JMXServerErrorException JavaDoc("Error thrown during invocation", (Error JavaDoc)t);
91       }
92    }
93 }
94
Popular Tags