2
|
1 |
/*
|
715
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.jmx.mbeanserver;
|
|
27 |
|
|
28 |
import static com.sun.jmx.mbeanserver.Util.*;
|
|
29 |
|
|
30 |
import java.lang.reflect.Method;
|
|
31 |
import java.util.Map;
|
|
32 |
|
|
33 |
import javax.management.Attribute;
|
|
34 |
import javax.management.MBeanServerConnection;
|
687
|
35 |
import javax.management.MalformedObjectNameException;
|
2
|
36 |
import javax.management.NotCompliantMBeanException;
|
|
37 |
import javax.management.ObjectName;
|
687
|
38 |
import javax.management.openmbean.MXBeanMappingFactory;
|
2
|
39 |
|
|
40 |
/**
|
|
41 |
<p>Helper class for an {@link InvocationHandler} that forwards methods from an
|
|
42 |
MXBean interface to a named
|
|
43 |
MXBean in an MBean Server and handles translation between the
|
|
44 |
arbitrary Java types in the interface and the Open Types used
|
|
45 |
by the MXBean.</p>
|
|
46 |
|
|
47 |
@since 1.6
|
|
48 |
*/
|
|
49 |
public class MXBeanProxy {
|
687
|
50 |
public MXBeanProxy(Class<?> mxbeanInterface, MXBeanMappingFactory factory) {
|
2
|
51 |
|
|
52 |
if (mxbeanInterface == null)
|
|
53 |
throw new IllegalArgumentException("Null parameter");
|
|
54 |
|
|
55 |
final MBeanAnalyzer<ConvertingMethod> analyzer;
|
|
56 |
try {
|
|
57 |
analyzer =
|
687
|
58 |
MXBeanIntrospector.getInstance(factory).getAnalyzer(mxbeanInterface);
|
2
|
59 |
} catch (NotCompliantMBeanException e) {
|
|
60 |
throw new IllegalArgumentException(e);
|
|
61 |
}
|
|
62 |
analyzer.visit(new Visitor());
|
|
63 |
}
|
|
64 |
|
687
|
65 |
private class Visitor
|
|
66 |
implements MBeanAnalyzer.MBeanVisitor<ConvertingMethod, RuntimeException> {
|
2
|
67 |
public void visitAttribute(String attributeName,
|
|
68 |
ConvertingMethod getter,
|
|
69 |
ConvertingMethod setter) {
|
|
70 |
if (getter != null) {
|
|
71 |
getter.checkCallToOpen();
|
|
72 |
Method getterMethod = getter.getMethod();
|
|
73 |
handlerMap.put(getterMethod,
|
|
74 |
new GetHandler(attributeName, getter));
|
|
75 |
}
|
|
76 |
if (setter != null) {
|
|
77 |
// return type is void, no need for checkCallToOpen
|
|
78 |
Method setterMethod = setter.getMethod();
|
|
79 |
handlerMap.put(setterMethod,
|
|
80 |
new SetHandler(attributeName, setter));
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
public void visitOperation(String operationName,
|
|
85 |
ConvertingMethod operation) {
|
|
86 |
operation.checkCallToOpen();
|
|
87 |
Method operationMethod = operation.getMethod();
|
|
88 |
String[] sig = operation.getOpenSignature();
|
|
89 |
handlerMap.put(operationMethod,
|
|
90 |
new InvokeHandler(operationName, sig, operation));
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
private static abstract class Handler {
|
|
95 |
Handler(String name, ConvertingMethod cm) {
|
|
96 |
this.name = name;
|
|
97 |
this.convertingMethod = cm;
|
|
98 |
}
|
|
99 |
|
|
100 |
String getName() {
|
|
101 |
return name;
|
|
102 |
}
|
|
103 |
|
|
104 |
ConvertingMethod getConvertingMethod() {
|
|
105 |
return convertingMethod;
|
|
106 |
}
|
|
107 |
|
|
108 |
abstract Object invoke(MBeanServerConnection mbsc,
|
|
109 |
ObjectName name, Object[] args) throws Exception;
|
|
110 |
|
|
111 |
private final String name;
|
|
112 |
private final ConvertingMethod convertingMethod;
|
|
113 |
}
|
|
114 |
|
|
115 |
private static class GetHandler extends Handler {
|
|
116 |
GetHandler(String attributeName, ConvertingMethod cm) {
|
|
117 |
super(attributeName, cm);
|
|
118 |
}
|
|
119 |
|
|
120 |
@Override
|
|
121 |
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
|
|
122 |
throws Exception {
|
|
123 |
assert(args == null || args.length == 0);
|
|
124 |
return mbsc.getAttribute(name, getName());
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
private static class SetHandler extends Handler {
|
|
129 |
SetHandler(String attributeName, ConvertingMethod cm) {
|
|
130 |
super(attributeName, cm);
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
|
|
135 |
throws Exception {
|
|
136 |
assert(args.length == 1);
|
|
137 |
Attribute attr = new Attribute(getName(), args[0]);
|
|
138 |
mbsc.setAttribute(name, attr);
|
|
139 |
return null;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
private static class InvokeHandler extends Handler {
|
|
144 |
InvokeHandler(String operationName, String[] signature,
|
|
145 |
ConvertingMethod cm) {
|
|
146 |
super(operationName, cm);
|
|
147 |
this.signature = signature;
|
|
148 |
}
|
|
149 |
|
|
150 |
Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
|
|
151 |
throws Exception {
|
|
152 |
return mbsc.invoke(name, getName(), args, signature);
|
|
153 |
}
|
|
154 |
|
|
155 |
private final String[] signature;
|
|
156 |
}
|
|
157 |
|
|
158 |
public Object invoke(MBeanServerConnection mbsc, ObjectName name,
|
|
159 |
Method method, Object[] args)
|
|
160 |
throws Throwable {
|
|
161 |
|
|
162 |
Handler handler = handlerMap.get(method);
|
|
163 |
ConvertingMethod cm = handler.getConvertingMethod();
|
687
|
164 |
String prefix = extractPrefix(name);
|
|
165 |
MXBeanLookup lookup = MXBeanLookup.lookupFor(mbsc, prefix);
|
|
166 |
MXBeanLookup oldLookup = MXBeanLookup.getLookup();
|
|
167 |
try {
|
|
168 |
MXBeanLookup.setLookup(lookup);
|
|
169 |
Object[] openArgs = cm.toOpenParameters(lookup, args);
|
|
170 |
Object result = handler.invoke(mbsc, name, openArgs);
|
|
171 |
return cm.fromOpenReturnValue(lookup, result);
|
|
172 |
} finally {
|
|
173 |
MXBeanLookup.setLookup(oldLookup);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
private static String extractPrefix(ObjectName name)
|
|
178 |
throws MalformedObjectNameException {
|
|
179 |
String domain = name.getDomain();
|
|
180 |
int slashslash = domain.lastIndexOf("//");
|
|
181 |
if (slashslash > 0 && domain.charAt(slashslash - 1) == '/')
|
|
182 |
slashslash--;
|
|
183 |
if (slashslash >= 0)
|
|
184 |
return domain.substring(0, slashslash + 2);
|
|
185 |
else
|
|
186 |
return null;
|
2
|
187 |
}
|
|
188 |
|
|
189 |
private final Map<Method, Handler> handlerMap = newMap();
|
|
190 |
}
|