User: juhalindfors
Date: 01/12/06 14:50:53
Modified: src/main/org/jboss/mx/server StandardMBean.java
Log:
Revision Changes Path
1.2 +108 -150 jmx/src/main/org/jboss/mx/server/StandardMBean.java
Index: StandardMBean.java
===================================================================
RCS file: /cvsroot/jboss/jmx/src/main/org/jboss/mx/server/StandardMBean.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StandardMBean.java 2001/12/05 14:24:55 1.1
+++ StandardMBean.java 2001/12/06 22:50:53 1.2
@@ -1,5 +1,8 @@
/*
- * LGPL
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
*/
package org.jboss.mx.server;
@@ -26,198 +29,153 @@
import javax.management.loading.DefaultLoaderRepository;
import org.jboss.mx.metadata.StandardMetaData;
+import org.jboss.mx.interceptor.Interceptor;
+import org.jboss.mx.interceptor.Invocation;
+import org.jboss.mx.interceptor.StandardMBeanInterceptor;
+import org.jboss.mx.interceptor.LogInterceptor;
+import org.jboss.mx.interceptor.SecurityInterceptor;
+import org.jboss.mx.interceptor.InvocationException;
+
+
+/**
+ * Represents standard MBean in the server.
+ *
+ * @see org.jboss.mx.interceptor.StandardMBeanInterceptor
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Juha Lindfors</a>.
+ * @version $Revision: 1.2 $
+ *
+ */
+public class StandardMBean
+ implements DynamicMBean
+{
-public class StandardMBean implements DynamicMBean {
-
- public static Class getMBeanInterface(Object resource) {
+ // Attributes ----------------------------------------------------
+ private MBeanInfo info = null;
+ private Object resource = null;
+ private Interceptor stack = null;
+
+ // Constructors --------------------------------------------------
+ public StandardMBean(Object resource) throws NotCompliantMBeanException,
ReflectionException
+ {
+ this.resource = resource;
+ this.info = new StandardMetaData(resource).build();
+ Interceptor security = new SecurityInterceptor();
+ security.insertLast(new LogInterceptor());
+ security.insertLast(new StandardMBeanInterceptor(resource, info));
+ stack = security;
+ }
+
+ // Public --------------------------------------------------------
+ public static Class getMBeanInterface(Object resource)
+ {
Class clazz = resource.getClass();
-
- while (clazz != null) {
+
+ while (clazz != null)
+ {
Class[] interfaces = clazz.getInterfaces();
-
- for (int i = 0; i < interfaces.length; ++i) {
-
+
+ for (int i = 0; i < interfaces.length; ++i)
+ {
if (interfaces[i].getName().equals(clazz.getName() + "MBean"))
return interfaces[i];
-
+
Class[] superInterfaces = interfaces[i].getInterfaces();
- for (int j = 0; j < superInterfaces.length; ++j) {
+ for (int j = 0; j < superInterfaces.length; ++j)
+ {
if (superInterfaces[j].getName().equals(clazz.getName() + "MBean"))
return superInterfaces[j];
}
- }
- clazz = clazz.getSuperclass();
- }
-
- return null;
- }
-
- public static Class[] getSignatureAsClassArray(MBeanParameterInfo[] signature,
ClassLoader cl) throws ClassNotFoundException {
-
- Class[] sign = new Class[signature.length];
- for (int i = 0; i < signature.length; ++i) {
- try {
- String type = signature[i].getType();
- if (isPrimitive(type))
- sign[i] = getPrimitive(type);
- else
- sign[i] = cl.loadClass(signature[i].getName());
- }
- catch (ClassNotFoundException e) {
- // if the explicit CL fails, go to the repository... allow CNFE to be
thrown
- DefaultLoaderRepository.loadClass(signature[i].getName());
}
+ clazz = clazz.getSuperclass();
}
- return sign;
- }
-
- public static boolean isPrimitive(String type) {
- if (int.class.getName().equals(type)) return true;
- else if (float.class.getName().equals(type)) return true;
- else if (double.class.getName().equals(type)) return true;
- else if (long.class.getName().equals(type)) return true;
- else if (byte.class.getName().equals(type)) return true;
- else if (boolean.class.getName().equals(type)) return true;
- else if (char.class.getName().equals(type)) return true;
- else if (void.class.getName().equals(type)) return true;
- else if (short.class.getName().equals(type)) return true;
- return false;
- }
-
- public static Class getPrimitive(String type) {
- if (int.class.getName().equals(type)) return Integer.TYPE;
- else if (float.class.getName().equals(type)) return Float.TYPE;
- else if (double.class.getName().equals(type)) return Double.TYPE;
- else if (long.class.getName().equals(type)) return Long.TYPE;
- else if (byte.class.getName().equals(type)) return Byte.TYPE;
- else if (boolean.class.getName().equals(type)) return Boolean.TYPE;
- else if (char.class.getName().equals(type)) return Character.TYPE;
- else if (void.class.getName().equals(type)) return Void.TYPE;
- else if (short.class.getName().equals(type)) return Short.TYPE;
return null;
}
-
-
-
- private MBeanInfo info = null;
- private Object resource = null;
- private Class invocationInterface = null;
- private Map operationMap = new HashMap();
- private Map attributeMap = new HashMap();
-
- public StandardMBean(Object resource) throws NotCompliantMBeanException,
ReflectionException {
-
- this.resource = resource;
- this.info = new StandardMetaData(resource).build();
- this.invocationInterface = StandardMBean.getMBeanInterface(resource);
-
- MBeanOperationInfo[] operations = info.getOperations();
- for (int i = 0; i < operations.length; ++i) {
- try {
- // FIXME: name is not enough for overloaded operations!
- operationMap.put(operations[i].getName(), invocationInterface.getMethod(
- operations[i].getName(),
- getSignatureAsClassArray(operations[i].getSignature(),
resource.getClass().getClassLoader())
- ));
- }
- catch (ClassNotFoundException e) {
- throw new ReflectionException(e, "Unable to load operation " +
operations[i].getName() + " parameter types: " + e.getMessage());
- }
- catch (NoSuchMethodException e) {
- throw new ReflectionException(e);
- }
- }
- }
-
- public Object invoke(String operationName, Object[] args, String[] signature)
throws MBeanException, ReflectionException {
-
- // FIXME: Interceptors
-
- try {
- Method m = (Method)operationMap.get(operationName);
- return m.invoke(resource, args);
- }
- catch (IllegalAccessException e) {
- throw new ReflectionException(e, "Illegal access to method " +
operationName);
- }
- catch (IllegalArgumentException e) {
- throw new ReflectionException(e, "Illegal operation arguments in " +
operationName + ": " + e.getMessage());
- }
- catch (InvocationTargetException e) {
- if (e.getTargetException() instanceof Exception) {
- Exception e2 = (Exception)e.getTargetException();
- throw new MBeanException(e2, "Operation " + operationName + " on MBean
" + info.getClassName() + " has thrown an exception: " + e2.toString());
- }
- else {
- Error err = (Error)e.getTargetException();
- throw new RuntimeErrorException(err, "Operation " + operationName + "
on MBean " + info.getClassName() + " has thrown an errpr: " + err.toString());
- }
- }
- catch (NullPointerException e) {
- throw new ReflectionException(e, "Operation " + operationName + " is not a
declared management operation in " + invocationInterface + " interface.");
- }
- }
-
- public Object getAttribute(java.lang.String attribute)
- throws AttributeNotFoundException, MBeanException, ReflectionException {
+ // DynamicMBean implementation -----------------------------------
+ public Object getAttribute(java.lang.String attribute)
+ throws AttributeNotFoundException, MBeanException, ReflectionException
+ {
throw new Error("NYI");
// FIXME: deal with runtime exceptions and errors from the resource
}
public void setAttribute(Attribute attribute)
- throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException {
+ throws AttributeNotFoundException, InvalidAttributeValueException,
MBeanException, ReflectionException
+ {
throw new Error("NYI");
// FIXME: deal with runtime exceptions and errors from the resource
}
- public AttributeList getAttributes(java.lang.String[] attributes) {
-
- if (attributes == null)
+ public AttributeList getAttributes(java.lang.String[] attributes)
+ {
+ if (attributes == null) // FIXME: runtimeoperationsexception?
throw new IllegalArgumentException("null array");
-
+
AttributeList list = new AttributeList();
-
- for (int i = 0; i < attributes.length; ++i) {
- try {
- list.add(new Attribute(attributes[i], getAttribute(attributes[i])));
- }
- catch (JMException ignored) {
- // if the attribute could not be retrieved, skip it
+
+ for (int i = 0; i < attributes.length; ++i)
+ {
+ try
+ {
+ list.add(new Attribute(attributes[i], getAttribute(attributes[i])));
+ }
+ catch (JMException ignored)
+ {
+ // if the attribute could not be retrieved, skip it
}
- }
-
+ }
+
return list;
}
-
- public AttributeList setAttributes(AttributeList attributes) {
-
- if (attributes == null)
+
+ public AttributeList setAttributes(AttributeList attributes)
+ {
+
+ if (attributes == null) // FIXME: runtimeoperationsexception?
throw new IllegalArgumentException("null list");
-
+
AttributeList results = new AttributeList();
Iterator it = attributes.iterator();
-
- while (it.hasNext()) {
- try {
+
+ while (it.hasNext())
+ {
+ try
+ {
Attribute attr = (Attribute)it.next();
setAttribute(attr);
results.add(attr);
}
- catch (JMException ignored) {
+ catch (JMException ignored)
+ {
// if unable to set the attribute, skip it
}
- }
-
+ }
+
return results;
}
-
- public MBeanInfo getMBeanInfo() {
+
+ public MBeanInfo getMBeanInfo()
+ {
return info;
}
-
+
+ // Interceptor overrides -----------------------------------------
+ public Object invoke(String operationName, Object[] args, String[] signature)
throws MBeanException, ReflectionException
+ {
+ try {
+ Invocation invocation = new Invocation(operationName,
Invocation.OPERATION, 0, args, signature, null);
+ return stack.invoke(invocation);
+ }
+ catch (InvocationException e) {
+ if (e.getTargetException() instanceof Exception)
+ throw new MBeanException((Exception)e.getTargetException(),
e.getTargetException().toString());
+ else
+ throw new RuntimeErrorException((Error)e.getTargetException(),
e.getTargetException().toString());
+ }
+ }
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development