KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > mbeanserver > MXBeanProxy


1 /*
2  * @(#)MXBeanProxy.java 1.8 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.mbeanserver;
9
10 import static com.sun.jmx.mbeanserver.Util.*;
11
12 import java.lang.reflect.InvocationHandler JavaDoc;
13 import java.lang.reflect.Method JavaDoc;
14 import java.lang.reflect.Proxy JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import javax.management.Attribute JavaDoc;
18 import javax.management.MBeanServerConnection JavaDoc;
19 import javax.management.NotCompliantMBeanException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 /**
23    <p>Helper class for an {@link InvocationHandler} that forwards methods from an
24    MXBean interface to a named
25    MXBean in an MBean Server and handles translation between the
26    arbitrary Java types in the interface and the Open Types used
27    by the MXBean.</p>
28
29    @since 1.6
30 */

31 public class MXBeanProxy {
32     public MXBeanProxy(Class JavaDoc<?> mxbeanInterface)
33         throws IllegalArgumentException JavaDoc {
34
35     if (mxbeanInterface == null)
36         throw new IllegalArgumentException JavaDoc("Null parameter");
37
38     final MBeanAnalyzer<ConvertingMethod> analyzer;
39         try {
40             analyzer =
41                 MXBeanIntrospector.getInstance().getAnalyzer(mxbeanInterface);
42         } catch (NotCompliantMBeanException JavaDoc e) {
43             throw new IllegalArgumentException JavaDoc(e);
44         }
45     analyzer.visit(new Visitor());
46     }
47
48     private class Visitor implements MBeanAnalyzer.MBeanVisitor<ConvertingMethod> {
49     public void visitAttribute(String JavaDoc attributeName,
50                    ConvertingMethod getter,
51                    ConvertingMethod setter) {
52         if (getter != null) {
53         getter.checkCallToOpen();
54         Method JavaDoc getterMethod = getter.getMethod();
55         handlerMap.put(getterMethod,
56                                new GetHandler(attributeName, getter));
57         }
58         if (setter != null) {
59         // return type is void, no need for checkCallToOpen
60
Method JavaDoc setterMethod = setter.getMethod();
61         handlerMap.put(setterMethod,
62                                new SetHandler(attributeName, setter));
63         }
64     }
65
66     public void visitOperation(String JavaDoc operationName,
67                    ConvertingMethod operation) {
68         operation.checkCallToOpen();
69         Method JavaDoc operationMethod = operation.getMethod();
70         String JavaDoc[] sig = operation.getOpenSignature();
71         handlerMap.put(operationMethod,
72                new InvokeHandler(operationName, sig, operation));
73     }
74     }
75
76     private static abstract class Handler {
77     Handler(String JavaDoc name, ConvertingMethod cm) {
78         this.name = name;
79             this.convertingMethod = cm;
80     }
81
82     String JavaDoc getName() {
83         return name;
84     }
85         
86         ConvertingMethod getConvertingMethod() {
87             return convertingMethod;
88         }
89
90     abstract Object JavaDoc invoke(MBeanServerConnection JavaDoc mbsc,
91                                ObjectName JavaDoc name, Object JavaDoc[] args) throws Exception JavaDoc;
92
93     private final String JavaDoc name;
94         private final ConvertingMethod convertingMethod;
95     }
96
97     private static class GetHandler extends Handler JavaDoc {
98     GetHandler(String JavaDoc attributeName, ConvertingMethod cm) {
99         super(attributeName, cm);
100     }
101
102     @Override JavaDoc
103         Object JavaDoc invoke(MBeanServerConnection JavaDoc mbsc, ObjectName JavaDoc name, Object JavaDoc[] args)
104                 throws Exception JavaDoc {
105         assert(args == null || args.length == 0);
106         return mbsc.getAttribute(name, getName());
107     }
108     }
109
110     private static class SetHandler extends Handler JavaDoc {
111     SetHandler(String JavaDoc attributeName, ConvertingMethod cm) {
112         super(attributeName, cm);
113     }
114
115     @Override JavaDoc
116         Object JavaDoc invoke(MBeanServerConnection JavaDoc mbsc, ObjectName JavaDoc name, Object JavaDoc[] args)
117                 throws Exception JavaDoc {
118         assert(args.length == 1);
119         Attribute JavaDoc attr = new Attribute JavaDoc(getName(), args[0]);
120         mbsc.setAttribute(name, attr);
121         return null;
122     }
123     }
124
125     private static class InvokeHandler extends Handler JavaDoc {
126     InvokeHandler(String JavaDoc operationName, String JavaDoc[] signature,
127                       ConvertingMethod cm) {
128         super(operationName, cm);
129         this.signature = signature;
130     }
131
132         Object JavaDoc invoke(MBeanServerConnection JavaDoc mbsc, ObjectName JavaDoc name, Object JavaDoc[] args)
133                 throws Exception JavaDoc {
134         return mbsc.invoke(name, getName(), args, signature);
135     }
136
137     private final String JavaDoc[] signature;
138     }
139
140     public Object JavaDoc invoke(MBeanServerConnection JavaDoc mbsc, ObjectName JavaDoc name,
141                          Method JavaDoc method, Object JavaDoc[] args)
142         throws Throwable JavaDoc {
143
144     Handler JavaDoc handler = handlerMap.get(method);
145     ConvertingMethod cm = handler.getConvertingMethod();
146         MXBeanLookup lookup = MXBeanLookup.lookupFor(mbsc);
147         Object JavaDoc[] openArgs = cm.toOpenParameters(lookup, args);
148         Object JavaDoc result = handler.invoke(mbsc, name, openArgs);
149         return cm.fromOpenReturnValue(lookup, result);
150     }
151
152     private final Map JavaDoc<Method JavaDoc, Handler JavaDoc> handlerMap = newMap();
153 }
154
Popular Tags