001: package org.pnuts.jmx;
002:
003: import pnuts.lang.*;
004: import java.util.*;
005: import java.io.*;
006: import javax.management.*;
007:
008: public class MBeanAdapter implements AbstractData {
009: private MBeanServerConnection server;
010: private ObjectName name;
011: private MBeanInfo beanInfo;
012: private Map/*<String,String[]>*/operations = new HashMap/*<String,String[]>*/();
013:
014: public MBeanAdapter(MBeanServerConnection server, String name)
015: throws JMException, IOException {
016: this (server, new ObjectName(name));
017: }
018:
019: public MBeanAdapter(MBeanServerConnection server, ObjectName name)
020: throws JMException, IOException {
021: this .server = server;
022: this .name = name;
023: this .beanInfo = server.getMBeanInfo(name);
024: MBeanOperationInfo[] operationInfos = beanInfo.getOperations();
025: for (int i = 0; i < operationInfos.length; i++) {
026: MBeanOperationInfo info = operationInfos[i];
027: String signature[] = createSignature(info);
028: String operationKey = createOperationKey(info.getName(),
029: signature.length);
030: operations.put(operationKey, signature);
031: }
032:
033: }
034:
035: public void set(String property, Object value, Context context) {
036: try {
037: server.setAttribute(name, new Attribute(property, value));
038: } catch (MBeanException e) {
039: throw new RuntimeException("Could not set property: "
040: + property + ". Reason: " + e, e
041: .getTargetException());
042: } catch (Exception e) {
043: throw new RuntimeException("Could not set property: "
044: + property + ". Reason: " + e, e);
045: }
046: }
047:
048: public Object get(String property, Context context) {
049: try {
050: return server.getAttribute(name, property);
051: } catch (MBeanException e) {
052: throw new RuntimeException("Could not access property: "
053: + property + ". Reason: " + e, e
054: .getTargetException());
055: } catch (Exception e) {
056: throw new RuntimeException("Could not access property: "
057: + property + ". Reason: " + e, e);
058: }
059: }
060:
061: public Object invoke(String method, Object[] arguments,
062: Context context) {
063: String operationKey = createOperationKey(method,
064: arguments.length);
065: String[] signature = (String[]) operations.get(operationKey);
066: if (signature != null) {
067: try {
068: return server
069: .invoke(name, method, arguments, signature);
070: } catch (MBeanException e) {
071: throw new RuntimeException("Could not invoke method: "
072: + method + ". Reason: " + e, e
073: .getTargetException());
074: } catch (Exception e) {
075: throw new RuntimeException("Could not invoke method: "
076: + method + ". Reason: " + e, e);
077: }
078: }
079: throw new RuntimeException("no such operation");
080: }
081:
082: protected String[] createSignature(MBeanOperationInfo info) {
083: MBeanParameterInfo[] params = info.getSignature();
084: String[] answer = new String[params.length];
085: for (int i = 0; i < params.length; i++) {
086: answer[i] = params[i].getType();
087: }
088: return answer;
089: }
090:
091: protected String createOperationKey(String operation, int params) {
092: return operation + "_" + params;
093: }
094:
095: public static MBeanInfo getMBeanInfo(MBeanServerConnection server,
096: ObjectName name) throws InstanceNotFoundException,
097: IntrospectionException, ReflectionException, IOException {
098: return server.getMBeanInfo(name);
099: }
100:
101: public static MBeanInfo getMBeanInfo(MBeanServerConnection server,
102: String name) throws JMException, IOException {
103: return server.getMBeanInfo(new ObjectName(name));
104: }
105:
106: public String toString() {
107: return name.getCanonicalName();
108: }
109: }
|