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