[jboss-cvs] JBossAS SVN: r62180 - in trunk: testsuite/src/main/org/jboss/test/jmx/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Apr 8 10:15:29 EDT 2007
Author: mageshbk at jboss.com
Date: 2007-04-08 10:15:29 -0400 (Sun, 08 Apr 2007)
New Revision: 62180
Modified:
trunk/server/src/main/org/jboss/jmx/connector/invoker/serializablepolicy/StripModelMBeanInfoPolicy.java
trunk/testsuite/src/main/org/jboss/test/jmx/test/RMIAdaptorUnitTestCase.java
Log:
[JBAS-1955] Updated the RMIAdaptor's SerializablePolicy so that it works for getAttributes similar to twiddle and also updated the test case to test the same.
Modified: trunk/server/src/main/org/jboss/jmx/connector/invoker/serializablepolicy/StripModelMBeanInfoPolicy.java
===================================================================
--- trunk/server/src/main/org/jboss/jmx/connector/invoker/serializablepolicy/StripModelMBeanInfoPolicy.java 2007-04-08 12:46:12 UTC (rev 62179)
+++ trunk/server/src/main/org/jboss/jmx/connector/invoker/serializablepolicy/StripModelMBeanInfoPolicy.java 2007-04-08 14:15:29 UTC (rev 62180)
@@ -21,53 +21,259 @@
*/
package org.jboss.jmx.connector.invoker.serializablepolicy;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamClass;
+import java.io.Serializable;
+import java.rmi.MarshalledObject;
+import java.util.ArrayList;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanConstructorInfo;
import javax.management.MBeanInfo;
+import javax.management.MBeanNotificationInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.modelmbean.ModelMBeanAttributeInfo;
+import javax.management.modelmbean.ModelMBeanConstructorInfo;
import javax.management.modelmbean.ModelMBeanInfo;
+import javax.management.modelmbean.ModelMBeanInfoSupport;
+import javax.management.modelmbean.ModelMBeanNotificationInfo;
+import javax.management.modelmbean.ModelMBeanOperationInfo;
import org.jboss.invocation.MarshalledInvocation;
import org.jboss.jmx.connector.invoker.SerializablePolicy;
+import org.jboss.logging.Logger;
/**
- * A policy that converts ModelMBeanInfo to MBeanInfo.
- *
+ * A policy that strips non serializable information from ModelMBeanInfo and MBeanInfo.
+ * Only the getMBeanInfo and getAttributes methods are stripped, if the getAttribute method
+ * is called the default NotSerializableException or ClassNotFoundException will be thrown
+ * depending on the attribute types.
+ *
+ * @author <a href="mailto:mageshbk at jboss.com">Magesh Kumar B</a>
* @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
* @author <a href="mailto:fabcipriano at yahoo.com.br">Fabiano C. de Oliveira</a>
* @version $Revision: 57209 $
*/
public class StripModelMBeanInfoPolicy implements SerializablePolicy
-{
+{
+ private static Logger log = Logger.getLogger(StripModelMBeanInfoPolicy.class);
+
public Object filter(MarshalledInvocation input, Object result) throws Throwable
{
if ("getMBeanInfo".equals(input.getMethod().getName()) && (result instanceof ModelMBeanInfo))
{
- MBeanInfo info = (MBeanInfo)result;
+ result = translateTo(result);
+ }
+ else if (result instanceof AttributeList)
+ {
+ AttributeList list = (AttributeList)result;
+ Attribute attr = null;
+ String className = null;
+ int listCount = list.size();
- result = new MBeanInfo(
- info.getClassName(),
- info.getDescription(),
- deepCopy(info.getAttributes()), // Strip the Descriptors
- info.getConstructors(),
- info.getOperations(),
- info.getNotifications());
+ // Remove all non-serializable objects
+ for (int i = 0; i < listCount; i++)
+ {
+ attr = (Attribute)list.get(i);
+ className = attr.getValue()==null?null:attr.getValue().getClass().getName();
+ if (log.isDebugEnabled())
+ {
+ log.debug("Attribute Name:"+attr.getName() + ", Class:"+className);
+ }
+ if (attr.getName().toUpperCase().indexOf("INSTANCE") >= 0
+ || (className != null && className.indexOf("org.jboss.resource.adapter.jms.JmsManagedConnectionFactory") >= 0 ))
+ {
+ // It is an instance reference, no point in transfering the reference when it may not be serialized
+ // or the class may not be found e.g., org.jboss.resource.adapter.jms.JmsManagedConnectionFactory
+ // and attribute name 'McfInstance'
+ list.set(i, new Attribute(attr.getName(),attr.getValue().toString()));
+ }
+ else if (!isSerializableObject(attr.getValue()))
+ {
+ // Mark all non serializable objects as 'Not Serializable'
+ list.set(i, new Attribute(attr.getName(),"Not Serializable"));
+ }
+ }
}
return result;
}
-
- private MBeanAttributeInfo[] deepCopy(MBeanAttributeInfo[] attrs)
+
+ private Object translateTo(Object result)
{
- MBeanAttributeInfo[] copy = new MBeanAttributeInfo[attrs.length];
+ ArrayList list = new ArrayList();
+ MBeanInfo info = (MBeanInfo)result;
+ MBeanAttributeInfo[] attrs = info.getAttributes();
+ String className = null;
+ MBeanAttributeInfo copy = null;
+ ModelMBeanAttributeInfo copy2 = null;
+
for (int i = 0; i < attrs.length; i++)
{
- MBeanAttributeInfo attr = attrs[i];
- copy[i] = new MBeanAttributeInfo(
- attr.getName(),
- attr.getType(),
- attr.getDescription(),
- attr.isReadable(),
- attr.isWritable(),
- attr.isIs());
+ className = attrs[i].getType();
+ try
+ {
+ //TODO: Should the Stats be serializable ???
+ if ((isPrimitive(className)) ||
+ (isSerializable(getClass().getClassLoader().loadClass(className))) ||
+ (isJSR77Stats(getClass().getClassLoader().loadClass(className))))
+ {
+ if (attrs[i] instanceof ModelMBeanAttributeInfo)
+ {
+ copy2 = new ModelMBeanAttributeInfo(
+ attrs[i].getName(),
+ attrs[i].getType(),
+ attrs[i].getDescription(),
+ attrs[i].isReadable(),
+ attrs[i].isWritable(),
+ attrs[i].isIs());
+ list.add(copy2);
+ }
+ else
+ {
+ copy = new MBeanAttributeInfo(
+ attrs[i].getName(),
+ attrs[i].getType(),
+ attrs[i].getDescription(),
+ attrs[i].isReadable(),
+ attrs[i].isWritable(),
+ attrs[i].isIs());
+ list.add(copy);
+ }
+ }
+ }
+ catch(ClassNotFoundException cnfe)
+ {
+ if (log.isDebugEnabled())
+ {
+ log.debug("we consider this a non-serializable Class", cnfe);
+ }
+ }
}
- return copy;
+ if (list.size() < attrs.length)
+ {
+ if (info instanceof ModelMBeanInfoSupport)
+ {
+ ModelMBeanInfoSupport modelInfo = (ModelMBeanInfoSupport)info;
+ result = new ModelMBeanInfoSupport(modelInfo.getClassName(),
+ modelInfo.getDescription(),
+ (ModelMBeanAttributeInfo[])list.toArray(new ModelMBeanAttributeInfo[list.size()]),
+ (ModelMBeanConstructorInfo[])modelInfo.getConstructors(),
+ (ModelMBeanOperationInfo[])modelInfo.getOperations(),
+ (ModelMBeanNotificationInfo[])modelInfo.getNotifications());
+ }
+ else
+ {
+ result = new MBeanInfo(info.getClassName(),
+ info.getDescription(),
+ (MBeanAttributeInfo[])list.toArray(new MBeanAttributeInfo[list.size()]),
+ (MBeanConstructorInfo[])info.getConstructors(),
+ (MBeanOperationInfo[])info.getOperations(),
+ (MBeanNotificationInfo[])info.getNotifications());
+ }
+ }
+
+ return result;
}
+
+ private boolean isJSR77Stats(Class clazz)
+ {
+ boolean result = false;
+ try
+ {
+ Class stats = getClass().getClassLoader().loadClass("javax.management.j2ee.statistics.Stats");
+ result = stats.isAssignableFrom(clazz);
+ }
+ catch (ClassNotFoundException e)
+ {
+ //ignore exception
+ if (log.isDebugEnabled())
+ {
+ log.debug("Ignored class not found exception",e);
+ }
+ }
+ return result;
+ }
+
+ private boolean isSerializable(Class clazz)
+ {
+ return (Serializable.class.isAssignableFrom(clazz));
+ }
+
+ private boolean isPrimitive(String className)
+ {
+ String[] primitives = new String[] {"int", "double", "long", "boolean",
+ "byte", "short"};
+ boolean result = false;
+
+ for (int i=0; i < primitives.length; i++)
+ {
+ if (className.indexOf(primitives[i]) != -1)
+ {
+ result = true;
+ break;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Check if a given Object and its references are Serializable
+ *
+ * @param obj
+ * @return boolean
+ */
+ private boolean isSerializableObject(Object obj)
+ {
+ boolean result = false;
+
+ if (obj == null)
+ {
+ result = true;
+ }
+ else if (obj instanceof Serializable)
+ {
+ ByteArrayOutputStream baos = null;
+ ObjectOutputStream tstOOS = null;
+ try
+ {
+ baos = new ByteArrayOutputStream();
+ tstOOS = new ObjectOutputStream(baos);
+
+ // Validate that the object's references are serializable
+ baos.reset();
+ tstOOS.writeObject(obj);
+ // Try marshalling ourselves before sending
+ Object ois = new MarshalledObject(obj).get();
+ result = true;
+ }
+ catch (Exception ignore)
+ {
+ //ignore exception
+ if (log.isDebugEnabled())
+ {
+ log.debug("Ignored exception", ignore);
+ }
+ }
+ finally
+ {
+ try
+ {
+ baos.close();
+ tstOOS.close();
+ }
+ catch (Exception e)
+ {
+ //ignore exception
+ if (log.isDebugEnabled())
+ {
+ log.debug("Ignored stream close exception", e);
+ }
+ }
+ }
+ }
+ return result;
+ }
}
Modified: trunk/testsuite/src/main/org/jboss/test/jmx/test/RMIAdaptorUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/jmx/test/RMIAdaptorUnitTestCase.java 2007-04-08 12:46:12 UTC (rev 62179)
+++ trunk/testsuite/src/main/org/jboss/test/jmx/test/RMIAdaptorUnitTestCase.java 2007-04-08 14:15:29 UTC (rev 62180)
@@ -21,8 +21,11 @@
*/
package org.jboss.test.jmx.test;
+
import java.util.Iterator;
+import javax.management.AttributeList;
+import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.ObjectName;
import javax.naming.InitialContext;
@@ -31,8 +34,9 @@
import org.jboss.test.JBossTestCase;
/**
- * Tests over the RMIAdaptor
+ * Tests over the RMIAdaptor and the SerializablePolicy
*
+ * @author <a href="mailto:mageshbk at jboss.com">Magesh Kumar B</a>
* @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
* @version <tt>$Revision$</tt>
*/
@@ -65,6 +69,17 @@
try
{
MBeanInfo mbeanInfo = rmiAdaptor.getMBeanInfo(objectName);
+ // This gives only serializable attributes
+ MBeanAttributeInfo[] attrInfos = mbeanInfo.getAttributes();
+ int attrCount = attrInfos.length;
+ String[] attrNames = new String[attrCount];
+ for (int i = 0; i < attrCount; i++)
+ {
+ attrNames[i] = ((MBeanAttributeInfo)attrInfos[i]).getName();
+ }
+ // Now call the getAttributes on these serializable attributes
+ // even ClassNotFoundExceptions are overcome with this method
+ AttributeList attrs = rmiAdaptor.getAttributes(objectName,attrNames);
}
catch (Throwable t)
{
More information about the jboss-cvs-commits
mailing list