jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanProxy.java
changeset 687 874e25a9844a
parent 2 90ce3da70b43
child 715 f16baef3a20e
equal deleted inserted replaced
686:d0c74839e1bd 687:874e25a9844a
    25 
    25 
    26 package com.sun.jmx.mbeanserver;
    26 package com.sun.jmx.mbeanserver;
    27 
    27 
    28 import static com.sun.jmx.mbeanserver.Util.*;
    28 import static com.sun.jmx.mbeanserver.Util.*;
    29 
    29 
    30 import java.lang.reflect.InvocationHandler;
       
    31 import java.lang.reflect.Method;
    30 import java.lang.reflect.Method;
    32 import java.util.Map;
    31 import java.util.Map;
    33 
    32 
    34 import javax.management.Attribute;
    33 import javax.management.Attribute;
    35 import javax.management.MBeanServerConnection;
    34 import javax.management.MBeanServerConnection;
       
    35 import javax.management.MalformedObjectNameException;
    36 import javax.management.NotCompliantMBeanException;
    36 import javax.management.NotCompliantMBeanException;
    37 import javax.management.ObjectName;
    37 import javax.management.ObjectName;
       
    38 import javax.management.openmbean.MXBeanMappingFactory;
    38 
    39 
    39 /**
    40 /**
    40    <p>Helper class for an {@link InvocationHandler} that forwards methods from an
    41    <p>Helper class for an {@link InvocationHandler} that forwards methods from an
    41    MXBean interface to a named
    42    MXBean interface to a named
    42    MXBean in an MBean Server and handles translation between the
    43    MXBean in an MBean Server and handles translation between the
    44    by the MXBean.</p>
    45    by the MXBean.</p>
    45 
    46 
    46    @since 1.6
    47    @since 1.6
    47 */
    48 */
    48 public class MXBeanProxy {
    49 public class MXBeanProxy {
    49     public MXBeanProxy(Class<?> mxbeanInterface)
    50     public MXBeanProxy(Class<?> mxbeanInterface, MXBeanMappingFactory factory) {
    50             throws IllegalArgumentException {
       
    51 
    51 
    52         if (mxbeanInterface == null)
    52         if (mxbeanInterface == null)
    53             throw new IllegalArgumentException("Null parameter");
    53             throw new IllegalArgumentException("Null parameter");
    54 
    54 
    55         final MBeanAnalyzer<ConvertingMethod> analyzer;
    55         final MBeanAnalyzer<ConvertingMethod> analyzer;
    56         try {
    56         try {
    57             analyzer =
    57             analyzer =
    58                 MXBeanIntrospector.getInstance().getAnalyzer(mxbeanInterface);
    58                 MXBeanIntrospector.getInstance(factory).getAnalyzer(mxbeanInterface);
    59         } catch (NotCompliantMBeanException e) {
    59         } catch (NotCompliantMBeanException e) {
    60             throw new IllegalArgumentException(e);
    60             throw new IllegalArgumentException(e);
    61         }
    61         }
    62         analyzer.visit(new Visitor());
    62         analyzer.visit(new Visitor());
    63     }
    63     }
    64 
    64 
    65     private class Visitor implements MBeanAnalyzer.MBeanVisitor<ConvertingMethod> {
    65     private class Visitor
       
    66             implements MBeanAnalyzer.MBeanVisitor<ConvertingMethod, RuntimeException> {
    66         public void visitAttribute(String attributeName,
    67         public void visitAttribute(String attributeName,
    67                                    ConvertingMethod getter,
    68                                    ConvertingMethod getter,
    68                                    ConvertingMethod setter) {
    69                                    ConvertingMethod setter) {
    69             if (getter != null) {
    70             if (getter != null) {
    70                 getter.checkCallToOpen();
    71                 getter.checkCallToOpen();
   158                          Method method, Object[] args)
   159                          Method method, Object[] args)
   159             throws Throwable {
   160             throws Throwable {
   160 
   161 
   161         Handler handler = handlerMap.get(method);
   162         Handler handler = handlerMap.get(method);
   162         ConvertingMethod cm = handler.getConvertingMethod();
   163         ConvertingMethod cm = handler.getConvertingMethod();
   163         MXBeanLookup lookup = MXBeanLookup.lookupFor(mbsc);
   164         String prefix = extractPrefix(name);
   164         Object[] openArgs = cm.toOpenParameters(lookup, args);
   165         MXBeanLookup lookup = MXBeanLookup.lookupFor(mbsc, prefix);
   165         Object result = handler.invoke(mbsc, name, openArgs);
   166         MXBeanLookup oldLookup = MXBeanLookup.getLookup();
   166         return cm.fromOpenReturnValue(lookup, result);
   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;
   167     }
   187     }
   168 
   188 
   169     private final Map<Method, Handler> handlerMap = newMap();
   189     private final Map<Method, Handler> handlerMap = newMap();
   170 }
   190 }