1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82:
83:
89: public class BeanImpl
90: extends StandardMBean
91: {
92:
93:
96: private OpenMBeanInfo openInfo;
97:
98:
107: protected BeanImpl(Class iface)
108: throws NotCompliantMBeanException
109: {
110: super(iface);
111: }
112:
113: protected void cacheMBeanInfo(MBeanInfo info)
114: {
115: if (info == null)
116: return;
117: try
118: {
119: MBeanAttributeInfo[] oldA = info.getAttributes();
120: OpenMBeanAttributeInfo[] attribs =
121: new OpenMBeanAttributeInfoSupport[oldA.length];
122: for (int a = 0; a < oldA.length; ++a)
123: {
124: OpenMBeanParameterInfo param = Translator.translate(oldA[a].getType());
125: if (param.getMinValue() == null)
126: {
127: Object[] lv;
128: if (param.getLegalValues() == null)
129: lv = null;
130: else
131: lv = param.getLegalValues().toArray();
132: attribs[a] = new OpenMBeanAttributeInfoSupport(oldA[a].getName(),
133: oldA[a].getDescription(),
134: ((OpenType<Object>)
135: param.getOpenType()),
136: oldA[a].isReadable(),
137: oldA[a].isWritable(),
138: oldA[a].isIs(),
139: param.getDefaultValue(),
140: lv);
141: }
142: else
143: attribs[a] = new OpenMBeanAttributeInfoSupport(oldA[a].getName(),
144: oldA[a].getDescription(),
145: ((OpenType<Object>)
146: param.getOpenType()),
147: oldA[a].isReadable(),
148: oldA[a].isWritable(),
149: oldA[a].isIs(),
150: param.getDefaultValue(),
151: ((Comparable<Object>)
152: param.getMinValue()),
153: ((Comparable<Object>)
154: param.getMaxValue()));
155: }
156: MBeanConstructorInfo[] oldC = info.getConstructors();
157: OpenMBeanConstructorInfo[] cons = new OpenMBeanConstructorInfoSupport[oldC.length];
158: for (int a = 0; a < oldC.length; ++a)
159: cons[a] =
160: new OpenMBeanConstructorInfoSupport(oldC[a].getName(),
161: oldC[a].getDescription(),
162: translateSignature(oldC[a].getSignature()));
163: MBeanOperationInfo[] oldO = info.getOperations();
164: OpenMBeanOperationInfo[] ops = new OpenMBeanOperationInfoSupport[oldO.length];
165: for (int a = 0; a < oldO.length; ++a)
166: ops[a] =
167: new OpenMBeanOperationInfoSupport(oldO[a].getName(),
168: oldO[a].getDescription(),
169: translateSignature(oldO[a].getSignature()),
170: Translator.translate(oldO[a].getReturnType()).getOpenType(),
171: oldO[a].getImpact());
172: openInfo = new OpenMBeanInfoSupport(info.getClassName(), info.getDescription(),
173: attribs, cons, ops, info.getNotifications());
174: }
175: catch (OpenDataException e)
176: {
177: throw (InternalError) (new InternalError("A problem occurred creating the open type " +
178: "descriptors.").initCause(e));
179: }
180: }
181:
182: protected void checkMonitorPermissions()
183: {
184: SecurityManager sm = System.getSecurityManager();
185: if (sm != null)
186: sm.checkPermission(new ManagementPermission("monitor"));
187: }
188:
189: protected void checkControlPermissions()
190: {
191: SecurityManager sm = System.getSecurityManager();
192: if (sm != null)
193: sm.checkPermission(new ManagementPermission("control"));
194: }
195:
196: public Object getAttribute(String attribute)
197: throws AttributeNotFoundException, MBeanException,
198: ReflectionException
199: {
200: Object value = super.getAttribute(attribute);
201: if (value instanceof Enum)
202: return ((Enum) value).name();
203: Class vClass = value.getClass();
204: if (vClass.isArray())
205: vClass = vClass.getComponentType();
206: String cName = vClass.getName();
207: String[] allowedTypes = OpenType.ALLOWED_CLASSNAMES;
208: for (int a = 0; a < allowedTypes.length; ++a)
209: if (cName.equals(allowedTypes[a]))
210: return value;
211: OpenMBeanInfo info = (OpenMBeanInfo) getMBeanInfo();
212: MBeanAttributeInfo[] attribs =
213: (MBeanAttributeInfo[]) info.getAttributes();
214: OpenType type = null;
215: for (int a = 0; a < attribs.length; ++a)
216: if (attribs[a].getName().equals(attribute))
217: type = ((OpenMBeanAttributeInfo) attribs[a]).getOpenType();
218: if (value instanceof List)
219: {
220: try
221: {
222: Class e =
223: Class.forName(((ArrayType) type).getElementOpenType().getClassName());
224: List l = (List) value;
225: Object[] array = (Object[]) Array.newInstance(e, l.size());
226: return l.toArray(array);
227: }
228: catch (ClassNotFoundException e)
229: {
230: throw (InternalError) (new InternalError("The class of the list " +
231: "element type could not " +
232: "be created").initCause(e));
233: }
234: }
235: if (value instanceof Map)
236: {
237: TabularType ttype = (TabularType) type;
238: TabularData data = new TabularDataSupport(ttype);
239: Iterator it = ((Map) value).entrySet().iterator();
240: while (it.hasNext())
241: {
242: Map.Entry entry = (Map.Entry) it.next();
243: try
244: {
245: data.put(new CompositeDataSupport(ttype.getRowType(),
246: new String[] {
247: "key",
248: "value"
249: },
250: new Object[] {
251: entry.getKey(),
252: entry.getValue()
253: }));
254: }
255: catch (OpenDataException e)
256: {
257: throw (InternalError) (new InternalError("A problem occurred " +
258: "converting the map " +
259: "to a composite data " +
260: "structure.").initCause(e));
261: }
262: }
263: return data;
264: }
265: CompositeType cType = (CompositeType) type;
266: Set names = cType.keySet();
267: Iterator it = names.iterator();
268: List values = new ArrayList(names.size());
269: while (it.hasNext())
270: {
271: String field = (String) it.next();
272: Method getter = null;
273: try
274: {
275: getter = vClass.getMethod("get" + field);
276: }
277: catch (NoSuchMethodException e)
278: {
279:
280: }
281: try
282: {
283: values.add(getter.invoke(value));
284: }
285: catch (IllegalAccessException e)
286: {
287: throw new ReflectionException(e, "Failed to retrieve " + field);
288: }
289: catch (IllegalArgumentException e)
290: {
291: throw new ReflectionException(e, "Failed to retrieve " + field);
292: }
293: catch (InvocationTargetException e)
294: {
295: throw new MBeanException((Exception) e.getCause(),
296: "The getter of " + field +
297: " threw an exception");
298: }
299: }
300: try
301: {
302: return new CompositeDataSupport(cType,
303: (String[])
304: names.toArray(new String[names.size()]),
305: values.toArray());
306: }
307: catch (OpenDataException e)
308: {
309: throw (InternalError) (new InternalError("A problem occurred " +
310: "converting the value " +
311: "to a composite data " +
312: "structure.").initCause(e));
313: }
314: }
315:
316: protected MBeanInfo getCachedMBeanInfo()
317: {
318: return (MBeanInfo) openInfo;
319: }
320:
321:
331: protected String getDescription(MBeanConstructorInfo constructor,
332: MBeanParameterInfo parameter,
333: int sequenceNo)
334: {
335: String desc = parameter.getDescription();
336: if (desc == null)
337: return "param" + sequenceNo;
338: else
339: return desc;
340: }
341:
342:
352: protected String getDescription(MBeanOperationInfo operation,
353: MBeanParameterInfo parameter,
354: int sequenceNo)
355: {
356: String desc = parameter.getDescription();
357: if (desc == null)
358: return "param" + sequenceNo;
359: else
360: return desc;
361: }
362:
363:
373: protected String getParameterName(MBeanConstructorInfo constructor,
374: MBeanParameterInfo parameter,
375: int sequenceNo)
376: {
377: String name = parameter.getName();
378: if (name == null)
379: return "param" + sequenceNo;
380: else
381: return name;
382: }
383:
384:
394: protected String getParameterName(MBeanOperationInfo operation,
395: MBeanParameterInfo parameter,
396: int sequenceNo)
397: {
398: String name = parameter.getName();
399: if (name == null)
400: return "param" + sequenceNo;
401: else
402: return name;
403: }
404:
405: public MBeanInfo getMBeanInfo()
406: {
407: super.getMBeanInfo();
408: return getCachedMBeanInfo();
409: }
410:
411: private OpenMBeanParameterInfo[] translateSignature(MBeanParameterInfo[] oldS)
412: throws OpenDataException
413: {
414: OpenMBeanParameterInfo[] sig = new OpenMBeanParameterInfoSupport[oldS.length];
415: for (int a = 0; a < oldS.length; ++a)
416: {
417: OpenMBeanParameterInfo param = Translator.translate(oldS[a].getType());
418: if (param.getMinValue() == null)
419: {
420: Object[] lv;
421: if (param.getLegalValues() == null)
422: lv = null;
423: else
424: lv = param.getLegalValues().toArray();
425: sig[a] = new OpenMBeanParameterInfoSupport(oldS[a].getName(),
426: oldS[a].getDescription(),
427: ((OpenType<Object>)
428: param.getOpenType()),
429: param.getDefaultValue(),
430: lv);
431: }
432: else
433: sig[a] = new OpenMBeanParameterInfoSupport(oldS[a].getName(),
434: oldS[a].getDescription(),
435: ((OpenType<Object>)
436: param.getOpenType()),
437: param.getDefaultValue(),
438: ((Comparable<Object>)
439: param.getMinValue()),
440: ((Comparable<Object>)
441: param.getMaxValue()));
442: }
443: return sig;
444: }
445:
446:
447: }