Index: src/main/org/jboss/mx/server/AbstractMBeanInvoker.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/server/Attic/AbstractMBeanInvoker.java,v retrieving revision 1.34.2.4 diff -u -r1.34.2.4 AbstractMBeanInvoker.java --- src/main/org/jboss/mx/server/AbstractMBeanInvoker.java 29 Oct 2005 05:03:38 -0000 1.34.2.4 +++ src/main/org/jboss/mx/server/AbstractMBeanInvoker.java 27 Jan 2006 15:36:52 -0000 @@ -23,6 +23,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -68,6 +69,7 @@ * @author Juha Lindfors. * @author Scott.Stark@jboss.org * @author Dimitris Andreadis. + * @author Fabiano C. de Oliveira * @version $Revision: 1.34.2.4 $ */ public abstract class AbstractMBeanInvoker @@ -185,6 +187,7 @@ // TODO: __JBOSSMX_INVOCATION + log.debug("begin invoke(" + operationName + "," + args + ", " + signature + ")"); if (operationName == null) throw new ReflectionException(new IllegalArgumentException("Null operation name")); @@ -203,7 +206,7 @@ // get the server side invocation context OperationKey key = new OperationKey(opName, signature); InvocationContext ctx = (InvocationContext) operationContextMap.get(key); - + // if the server does not contain this context, we do not have the operation if (ctx == null) { @@ -255,6 +258,8 @@ try { + log.debug("calling invocation=" + invocation); + // the default invocation implementation will invoke each interceptor // declared in the invocation context before invoking the target method return invocation.invoke(); @@ -1004,8 +1009,12 @@ } } - protected void initOperationContexts(MBeanOperationInfo[] operations) + protected void initOperationContexts(MBeanOperationInfo[] originalOperations) { + + MBeanOperationInfo[] operations = convertAttributes2Operations(); + //MBeanOperationInfo[] operations = originalOperations; + // create invocation contexts for operations for (int i = 0; i < operations.length; ++i) { @@ -1042,10 +1051,13 @@ protected void initDispatchers() { - MBeanOperationInfo[] operations = info.getOperations(); + //MBeanOperationInfo[] original = info.getOperations(); Class clazz = (resource == null) ? null : resource.getClass(); MethodMapper mmap = new MethodMapper(clazz); + MBeanOperationInfo[] operations = convertAttributes2Operations(); + //MBeanOperationInfo[] operations = info.getOperations(); + // Set the dispatchers for the operations for (int i = 0; i < operations.length; ++i) { @@ -1255,6 +1267,46 @@ else throw new RuntimeMBeanException(new RuntimeException("Unhandled exception", t)); } + + private MBeanOperationInfo[] convertAttributes2Operations() + { + /* + * "if the property jmx.invoke.getters does not have an empty value, the + *code forbidding calling getters and setters through invoke is disabled." + */ + boolean disableForbidding = false; + String strInvokeGetters = System.getProperty("jmx.invoke.getters"); + disableForbidding = ((strInvokeGetters != null) && (!strInvokeGetters.trim().equals(""))); + + if (!disableForbidding) { + return info.getOperations(); + } + + MBeanAttributeInfo[] attributes = info.getAttributes(); + Class clazz = (resource == null) ? null : resource.getClass(); + MethodMapper mmap = new MethodMapper(clazz); + ArrayList attrOper = new ArrayList(Arrays.asList(info.getOperations())); + + for (int i = 0; i < attributes.length; ++i) + { + MBeanAttributeInfo attribute = attributes[i]; + Method getter = mmap.lookupGetter(attribute); + Method setter = mmap.lookupSetter(attribute); + + if (getter != null) { + attrOper.add(new MBeanOperationInfo("MBean Operation.", getter)); + } + + if (setter != null) { + attrOper.add(new MBeanOperationInfo("MBean Operation.", setter)); + } + } + + MBeanOperationInfo[] operations = (MBeanOperationInfo[]) + attrOper.toArray(new MBeanOperationInfo[attrOper.size()]); + + return operations; + } } Index: src/main/test/implementation/modelmbean/XMBeanTEST.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/test/implementation/modelmbean/XMBeanTEST.java,v retrieving revision 1.9.6.1 diff -u -r1.9.6.1 XMBeanTEST.java --- src/main/test/implementation/modelmbean/XMBeanTEST.java 29 Oct 2005 05:03:46 -0000 1.9.6.1 +++ src/main/test/implementation/modelmbean/XMBeanTEST.java 27 Jan 2006 15:36:53 -0000 @@ -26,6 +26,7 @@ import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; +import javax.management.ReflectionException; import javax.management.modelmbean.DescriptorSupport; import junit.framework.TestCase; @@ -41,6 +42,7 @@ * XMBean class and the MBean creation (this is the doc ;) * * @author Juha Lindfors. + * @author Fabiano C. de Oliveira * @version $Revision: 1.9.6.1 $ */ public class XMBeanTEST extends TestCase implements XMBeanConstants @@ -77,6 +79,9 @@ public void testCreateWithJBossXMBean10DTD() throws Exception { + System.setProperty("jmx.invoke.getters", "true"); + + ObjectName name = new ObjectName(":test=test"); MBeanServer server = MBeanServerFactory.createMBeanServer(); Descriptor d = new DescriptorSupport(); d.setField(RESOURCE_REFERENCE, new User()); @@ -85,13 +90,24 @@ XMBean mmb = new XMBean(d, DESCRIPTOR); - server.registerMBean(mmb, new ObjectName(":test=test")); + server.registerMBean(mmb, name); + + assertTrue(server.isRegistered(name)); - assertTrue(server.isRegistered(new ObjectName(":test=test"))); + server.setAttribute(name, new Attribute("Name", "Juha")); + + assertTrue(server.getAttribute(name, "Name").equals("Juha")); + + assertTrue(server.isRegistered(name)); + server.invoke(name, "addPhoneNumber", + new Object[] {"43211234"}, + new String[]{String.class.getName()}); - server.setAttribute(new ObjectName(":test=test"), new Attribute("Name", "Juha")); + String[] numbers = (String[])server.getAttribute(name, "PhoneNumbers"); + assertEquals("43211234", numbers[0]); - assertTrue(server.getAttribute(new ObjectName(":test=test"), "Name").equals("Juha")); + numbers = (String[])server.invoke(name, "getPhoneNumbers", null, null); + assertEquals("43211234", numbers[0]); }