[jboss-cvs] JBossAS SVN: r74872 - trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jun 20 07:26:12 EDT 2008


Author: alesj
Date: 2008-06-20 07:26:12 -0400 (Fri, 20 Jun 2008)
New Revision: 74872

Added:
   trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXAnnotationPlugin.java
   trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXFieldAnnotationPlugin.java
   trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXPropertyAnnotationPlugin.java
Log:
Handle @JMX on getters and fields.

Added: trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXAnnotationPlugin.java
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXAnnotationPlugin.java	                        (rev 0)
+++ trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXAnnotationPlugin.java	2008-06-20 11:26:12 UTC (rev 74872)
@@ -0,0 +1,159 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.system.microcontainer.jmx;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.jboss.aop.microcontainer.aspects.jmx.JMX;
+import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
+import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
+import org.jboss.dependency.spi.Controller;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.kernel.plugins.annotations.AbstractAnnotationPlugin;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.reflect.spi.AnnotatedInfo;
+
+/**
+ * Supporting @JMX on attributes.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @param <T> exact info type
+ */
+public abstract class JMXAnnotationPlugin<T extends AnnotatedInfo> extends AbstractAnnotationPlugin<T, JMX>
+{
+   protected JMXAnnotationPlugin()
+   {
+      super(JMX.class);
+   }
+
+   protected List<? extends MetaDataVisitorNode> internalApplyAnnotation(T info, MetaData metaData, JMX jmx, KernelControllerContext context) throws Throwable
+   {
+      Class<?> exposedInterface = jmx.exposedInterface();
+      if (exposedInterface == null || void.class.equals(exposedInterface))
+         exposedInterface = getExposedInterface(info);
+      if (exposedInterface == null || exposedInterface.isInterface() == false)
+         throw new IllegalArgumentException("Illegal exposed interface: " + exposedInterface);
+
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder(createObjectName(context, info, jmx), exposedInterface.getName());
+      // TODO - uncomment with new MC release; builder.addAnnotation(jmx);
+      Object proxy = createProxy(context, info, exposedInterface);
+      KernelController controller = (KernelController)context.getController();
+      controller.install(builder.getBeanMetaData(), proxy);
+
+      // no change directly on context
+      return null;
+   }
+
+   /**
+    * Create proxy for attribute.
+    *
+    * @param context the context
+    * @param info the info
+    * @param exposedInterface the exposed interface
+    * @return attribute's proxy
+    * @throws Throwable for any error
+    */
+   protected Object createProxy(KernelControllerContext context, T info, Class<?> exposedInterface) throws Throwable
+   {
+      return Proxy.newProxyInstance(
+            context.getClassLoader(),
+            new Class<?>[]{exposedInterface},
+            new AttributeInvocationHandler(context, getName(info))
+      );
+   }
+
+   protected void internalCleanAnnotation(T info, MetaData metaData, JMX jmx, KernelControllerContext context) throws Throwable
+   {
+      Controller controller = context.getController();
+      controller.uninstall(createObjectName(context, info, jmx));
+   }
+
+   /**
+    * Get exposed interface from info.
+    *
+    * @param info the info
+    * @return exposed interface
+    */
+   protected abstract Class<?> getExposedInterface(T info);
+
+   /**
+    * Get name from info.
+    *
+    * @param info the info
+    * @return info's name
+    */
+   protected abstract String getName(T info);
+
+   /**
+    * Create object name.
+    *
+    * @param context the context
+    * @param info the info
+    * @param jmx the annotation
+    * @return obejct name
+    * @throws Exception for any error
+    */
+   protected String createObjectName(ControllerContext context, T info, JMX jmx) throws Exception
+   {
+      if (jmx != null)
+      {
+         String jmxName = jmx.name();
+         if (jmxName != null && jmxName.length() > 0)
+            return jmxName;
+      }
+
+      // try to build one from the bean name and  info param
+      String name = context.getName().toString();
+      String objectName = name;
+      if (name.contains(":") == false)
+      {
+         objectName = "jboss.pojo:name='" + name + "'";
+      }
+      return objectName + ",property=" + getName(info);
+   }
+
+   /**
+    * Attribute invocation handler.
+    */
+   protected class AttributeInvocationHandler implements InvocationHandler
+   {
+      private KernelControllerContext context;
+      private String property;
+
+      protected AttributeInvocationHandler(KernelControllerContext context, String property)
+      {
+         this.context = context;
+         this.property = property;
+      }
+
+      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+      {
+         Object target = context.get(property);
+         return method.invoke(target, args);
+      }
+   }
+}
\ No newline at end of file

Added: trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXFieldAnnotationPlugin.java
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXFieldAnnotationPlugin.java	                        (rev 0)
+++ trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXFieldAnnotationPlugin.java	2008-06-20 11:26:12 UTC (rev 74872)
@@ -0,0 +1,50 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.system.microcontainer.jmx;
+
+import java.lang.annotation.ElementType;
+
+import org.jboss.reflect.spi.FieldInfo;
+
+/**
+ * Supporting @JMX on fields.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JMXFieldAnnotationPlugin extends JMXAnnotationPlugin<FieldInfo>
+{
+   protected boolean isElementTypeSupported(ElementType elementType)
+   {
+      return ElementType.FIELD == elementType;
+   }
+
+   @SuppressWarnings("deprecation")
+   protected Class<?> getExposedInterface(FieldInfo info)
+   {
+      return info.getType().getType();
+   }
+
+   protected String getName(FieldInfo info)
+   {
+      return info.getName();
+   }
+}
\ No newline at end of file

Copied: trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXPropertyAnnotationPlugin.java (from rev 74870, trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/AbstractServiceControllerLifecycleCallback.java)
===================================================================
--- trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXPropertyAnnotationPlugin.java	                        (rev 0)
+++ trunk/system-jmx/src/main/org/jboss/system/microcontainer/jmx/JMXPropertyAnnotationPlugin.java	2008-06-20 11:26:12 UTC (rev 74872)
@@ -0,0 +1,51 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.system.microcontainer.jmx;
+
+import java.lang.annotation.ElementType;
+
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.kernel.plugins.annotations.PropertyAware;
+
+/**
+ * Supporting @JMX on getters.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JMXPropertyAnnotationPlugin extends JMXAnnotationPlugin<PropertyInfo> implements PropertyAware
+{
+   protected boolean isElementTypeSupported(ElementType elementType)
+   {
+      return ElementType.METHOD == elementType;
+   }
+
+   @SuppressWarnings("deprecation")
+   protected Class<?> getExposedInterface(PropertyInfo info)
+   {
+      return info.getType().getType();
+   }
+
+   protected String getName(PropertyInfo info)
+   {
+      return info.getName();
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list