2
|
1 |
/*
|
|
2 |
* Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
import java.io.InvalidObjectException;
|
|
28 |
import java.lang.reflect.InvocationTargetException;
|
|
29 |
import java.lang.reflect.Method;
|
|
30 |
import java.lang.reflect.Type;
|
|
31 |
|
|
32 |
import javax.management.Descriptor;
|
|
33 |
import javax.management.MBeanException;
|
687
|
34 |
import javax.management.openmbean.MXBeanMapping;
|
|
35 |
import javax.management.openmbean.MXBeanMappingFactory;
|
2
|
36 |
import javax.management.openmbean.OpenDataException;
|
|
37 |
import javax.management.openmbean.OpenType;
|
|
38 |
|
|
39 |
final class ConvertingMethod {
|
687
|
40 |
static ConvertingMethod from(Method m, MXBeanMappingFactory mappingFactory) {
|
2
|
41 |
try {
|
687
|
42 |
return new ConvertingMethod(m, mappingFactory);
|
2
|
43 |
} catch (OpenDataException ode) {
|
|
44 |
final String msg = "Method " + m.getDeclaringClass().getName() +
|
|
45 |
"." + m.getName() + " has parameter or return type that " +
|
|
46 |
"cannot be translated into an open type";
|
|
47 |
throw new IllegalArgumentException(msg, ode);
|
|
48 |
}
|
|
49 |
}
|
|
50 |
|
|
51 |
Method getMethod() {
|
|
52 |
return method;
|
|
53 |
}
|
|
54 |
|
|
55 |
Descriptor getDescriptor() {
|
|
56 |
return Introspector.descriptorForElement(method);
|
|
57 |
}
|
|
58 |
|
|
59 |
Type getGenericReturnType() {
|
|
60 |
return method.getGenericReturnType();
|
|
61 |
}
|
|
62 |
|
|
63 |
Type[] getGenericParameterTypes() {
|
|
64 |
return method.getGenericParameterTypes();
|
|
65 |
}
|
|
66 |
|
|
67 |
String getName() {
|
|
68 |
return method.getName();
|
|
69 |
}
|
|
70 |
|
|
71 |
OpenType getOpenReturnType() {
|
687
|
72 |
return returnMapping.getOpenType();
|
2
|
73 |
}
|
|
74 |
|
|
75 |
OpenType[] getOpenParameterTypes() {
|
687
|
76 |
final OpenType[] types = new OpenType[paramMappings.length];
|
|
77 |
for (int i = 0; i < paramMappings.length; i++)
|
|
78 |
types[i] = paramMappings[i].getOpenType();
|
2
|
79 |
return types;
|
|
80 |
}
|
|
81 |
|
|
82 |
/* Check that this method will be callable when we are going from
|
|
83 |
* open types to Java types, for example when we are going from
|
|
84 |
* an MXBean wrapper to the underlying resource.
|
|
85 |
* The parameters will be converted to
|
|
86 |
* Java types, so they must be "reconstructible". The return
|
|
87 |
* value will be converted to an Open Type, so if it is convertible
|
|
88 |
* at all there is no further check needed.
|
|
89 |
*/
|
687
|
90 |
void checkCallFromOpen() {
|
2
|
91 |
try {
|
687
|
92 |
for (MXBeanMapping paramConverter : paramMappings)
|
2
|
93 |
paramConverter.checkReconstructible();
|
|
94 |
} catch (InvalidObjectException e) {
|
|
95 |
throw new IllegalArgumentException(e);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
/* Check that this method will be callable when we are going from
|
|
100 |
* Java types to open types, for example when we are going from
|
|
101 |
* an MXBean proxy to the open types that it will be mapped to.
|
|
102 |
* The return type will be converted back to a Java type, so it
|
|
103 |
* must be "reconstructible". The parameters will be converted to
|
|
104 |
* open types, so if it is convertible at all there is no further
|
|
105 |
* check needed.
|
|
106 |
*/
|
687
|
107 |
void checkCallToOpen() {
|
2
|
108 |
try {
|
687
|
109 |
returnMapping.checkReconstructible();
|
2
|
110 |
} catch (InvalidObjectException e) {
|
|
111 |
throw new IllegalArgumentException(e);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
String[] getOpenSignature() {
|
687
|
116 |
if (paramMappings.length == 0)
|
2
|
117 |
return noStrings;
|
|
118 |
|
687
|
119 |
String[] sig = new String[paramMappings.length];
|
|
120 |
for (int i = 0; i < paramMappings.length; i++)
|
|
121 |
sig[i] = paramMappings[i].getOpenClass().getName();
|
2
|
122 |
return sig;
|
|
123 |
}
|
|
124 |
|
|
125 |
final Object toOpenReturnValue(MXBeanLookup lookup, Object ret)
|
|
126 |
throws OpenDataException {
|
687
|
127 |
return returnMapping.toOpenValue(ret);
|
2
|
128 |
}
|
|
129 |
|
|
130 |
final Object fromOpenReturnValue(MXBeanLookup lookup, Object ret)
|
|
131 |
throws InvalidObjectException {
|
687
|
132 |
return returnMapping.fromOpenValue(ret);
|
2
|
133 |
}
|
|
134 |
|
|
135 |
final Object[] toOpenParameters(MXBeanLookup lookup, Object[] params)
|
|
136 |
throws OpenDataException {
|
|
137 |
if (paramConversionIsIdentity || params == null)
|
|
138 |
return params;
|
|
139 |
final Object[] oparams = new Object[params.length];
|
|
140 |
for (int i = 0; i < params.length; i++)
|
687
|
141 |
oparams[i] = paramMappings[i].toOpenValue(params[i]);
|
2
|
142 |
return oparams;
|
|
143 |
}
|
|
144 |
|
687
|
145 |
final Object[] fromOpenParameters(Object[] params)
|
2
|
146 |
throws InvalidObjectException {
|
|
147 |
if (paramConversionIsIdentity || params == null)
|
|
148 |
return params;
|
|
149 |
final Object[] jparams = new Object[params.length];
|
|
150 |
for (int i = 0; i < params.length; i++)
|
687
|
151 |
jparams[i] = paramMappings[i].fromOpenValue(params[i]);
|
2
|
152 |
return jparams;
|
|
153 |
}
|
|
154 |
|
|
155 |
final Object toOpenParameter(MXBeanLookup lookup,
|
|
156 |
Object param,
|
|
157 |
int paramNo)
|
|
158 |
throws OpenDataException {
|
687
|
159 |
return paramMappings[paramNo].toOpenValue(param);
|
2
|
160 |
}
|
|
161 |
|
|
162 |
final Object fromOpenParameter(MXBeanLookup lookup,
|
|
163 |
Object param,
|
|
164 |
int paramNo)
|
|
165 |
throws InvalidObjectException {
|
687
|
166 |
return paramMappings[paramNo].fromOpenValue(param);
|
2
|
167 |
}
|
|
168 |
|
|
169 |
Object invokeWithOpenReturn(MXBeanLookup lookup,
|
|
170 |
Object obj, Object[] params)
|
|
171 |
throws MBeanException, IllegalAccessException,
|
|
172 |
InvocationTargetException {
|
687
|
173 |
MXBeanLookup old = MXBeanLookup.getLookup();
|
|
174 |
try {
|
|
175 |
MXBeanLookup.setLookup(lookup);
|
|
176 |
return invokeWithOpenReturn(obj, params);
|
|
177 |
} finally {
|
|
178 |
MXBeanLookup.setLookup(old);
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
private Object invokeWithOpenReturn(Object obj, Object[] params)
|
|
183 |
throws MBeanException, IllegalAccessException,
|
|
184 |
InvocationTargetException {
|
2
|
185 |
final Object[] javaParams;
|
|
186 |
try {
|
687
|
187 |
javaParams = fromOpenParameters(params);
|
2
|
188 |
} catch (InvalidObjectException e) {
|
|
189 |
// probably can't happen
|
|
190 |
final String msg = methodName() + ": cannot convert parameters " +
|
|
191 |
"from open values: " + e;
|
|
192 |
throw new MBeanException(e, msg);
|
|
193 |
}
|
|
194 |
final Object javaReturn = method.invoke(obj, javaParams);
|
|
195 |
try {
|
687
|
196 |
return returnMapping.toOpenValue(javaReturn);
|
2
|
197 |
} catch (OpenDataException e) {
|
|
198 |
// probably can't happen
|
|
199 |
final String msg = methodName() + ": cannot convert return " +
|
|
200 |
"value to open value: " + e;
|
|
201 |
throw new MBeanException(e, msg);
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
private String methodName() {
|
|
206 |
return method.getDeclaringClass() + "." + method.getName();
|
|
207 |
}
|
|
208 |
|
687
|
209 |
private ConvertingMethod(Method m, MXBeanMappingFactory mappingFactory)
|
|
210 |
throws OpenDataException {
|
2
|
211 |
this.method = m;
|
687
|
212 |
returnMapping =
|
|
213 |
mappingFactory.mappingForType(m.getGenericReturnType(), mappingFactory);
|
2
|
214 |
Type[] params = m.getGenericParameterTypes();
|
687
|
215 |
paramMappings = new MXBeanMapping[params.length];
|
2
|
216 |
boolean identity = true;
|
|
217 |
for (int i = 0; i < params.length; i++) {
|
687
|
218 |
paramMappings[i] = mappingFactory.mappingForType(params[i], mappingFactory);
|
|
219 |
identity &= DefaultMXBeanMappingFactory.isIdentity(paramMappings[i]);
|
2
|
220 |
}
|
|
221 |
paramConversionIsIdentity = identity;
|
|
222 |
}
|
|
223 |
|
|
224 |
private static final String[] noStrings = new String[0];
|
|
225 |
|
|
226 |
private final Method method;
|
687
|
227 |
private final MXBeanMapping returnMapping;
|
|
228 |
private final MXBeanMapping[] paramMappings;
|
2
|
229 |
private final boolean paramConversionIsIdentity;
|
|
230 |
}
|