[exo-jcr-commits] exo-jcr SVN: r1228 - in kernel/trunk/exo.kernel.container/src: main/java/org/exoplatform/management/annotations and 5 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Dec 29 05:16:00 EST 2009


Author: julien_viet
Date: 2009-12-29 05:16:00 -0500 (Tue, 29 Dec 2009)
New Revision: 1228

Added:
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/annotations/package-info.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/annotations/package-info.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoMBeanInfoBuilder.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoModelMBean.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMX.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMXManagementProvider.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/MBeanScopingData.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ObjectNameBuilder.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertiesInfo.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertyInfo.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/package-info.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/package-info.java
Removed:
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/
Modified:
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/management/ManageableContainer.java
   kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/ManagedResource.java
   kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestExoMBean.java
   kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/TestNameTemplate.java
Log:
EXOJCR-330 : Enable plugability of management layer : added javadoc and did move the jmx stuff to the right place


Modified: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/management/ManageableContainer.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/management/ManageableContainer.java	2009-12-29 09:58:06 UTC (rev 1227)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/container/management/ManageableContainer.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -25,8 +25,8 @@
 import org.exoplatform.management.annotations.Managed;
 import org.exoplatform.management.annotations.ManagedDescription;
 import org.exoplatform.management.annotations.ManagedName;
+import org.exoplatform.management.jmx.impl.JMXManagementProvider;
 import org.exoplatform.management.spi.ManagementProvider;
-import org.exoplatform.management.spi.jmx.JMXManagementProvider;
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.PicoContainer;
 import org.picocontainer.PicoException;

Added: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/annotations/package-info.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/annotations/package-info.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/annotations/package-info.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,8 @@
+/**
+ * <p>This package defines the annotations the defines the management interface of a service. The annotations are
+ * agnostic of any management layer implementation to avoid the coupling to a specific management layer.</p>
+ *
+ * <p>It is possible for a management layer to define additional annotations that are used by that layer to
+ * provide more information to manage the resource.</p> 
+ */
+package org.exoplatform.management.annotations;
\ No newline at end of file

Added: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/annotations/package-info.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/annotations/package-info.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/annotations/package-info.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,4 @@
+/**
+ * This package defines additional meta data for the JMX management layer.
+ */
+package org.exoplatform.management.jmx.annotations;
\ No newline at end of file

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoMBeanInfoBuilder.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/ExoMBeanInfoBuilder.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoMBeanInfoBuilder.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoMBeanInfoBuilder.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.management.spi.ManagedMethodMetaData;
+import org.exoplatform.management.spi.ManagedMethodParameterMetaData;
+import org.exoplatform.management.spi.ManagedPropertyMetaData;
+import org.exoplatform.management.spi.ManagedTypeMetaData;
+import org.exoplatform.container.management.MetaDataBuilder;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.Descriptor;
+import javax.management.IntrospectionException;
+import javax.management.MBeanParameterInfo;
+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;
+
+/**
+ * <p>A class that build mbean meta data</p>
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class ExoMBeanInfoBuilder
+{
+
+   private static enum Role {
+      SET("setter"), IS("getter"), GET("getter"), OP("operation");
+
+      private final String name;
+
+      private Role(String role)
+      {
+         this.name = role;
+      }
+   }
+
+   private ManagedTypeMetaData typeMD;
+
+   /**
+    * Create a new builder.
+    *
+    * @param clazz the clazz
+    * @throws IllegalArgumentException if the class is null or does not contain meta data
+    */
+   public ExoMBeanInfoBuilder(Class clazz) throws IllegalArgumentException
+   {
+      this.typeMD = new MetaDataBuilder(clazz).build();
+   }
+
+   public ExoMBeanInfoBuilder(ManagedTypeMetaData typeMD) throws IllegalArgumentException
+   {
+      this.typeMD = typeMD;
+   }
+
+   private ModelMBeanOperationInfo buildOperationInfo(Method method, String description, Role role,
+      Collection<ManagedMethodParameterMetaData> parametersMD)
+   {
+      ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo(description, method);
+
+      //
+      if (description == null)
+      {
+         description = "Management operation";
+      }
+
+      //
+      MBeanParameterInfo[] parameterInfos = operationInfo.getSignature();
+      for (ManagedMethodParameterMetaData parameterMD : parametersMD)
+      {
+         int i = parameterMD.getIndex();
+         MBeanParameterInfo parameterInfo = parameterInfos[i];
+         String parameterName = parameterInfo.getName();
+         String parameterDescription = operationInfo.getSignature()[i].getDescription();
+         if (parameterMD.getName() != null)
+         {
+            parameterName = parameterMD.getName();
+         }
+         else if (parameterMD.getDescription() != null)
+         {
+            parameterDescription = parameterMD.getDescription();
+         }
+         parameterInfos[i] = new MBeanParameterInfo(parameterName, parameterInfo.getType(), parameterDescription);
+      }
+
+      //
+      Descriptor operationDescriptor = operationInfo.getDescriptor();
+      operationDescriptor.setField("role", role.name);
+
+      //
+      return new ModelMBeanOperationInfo(operationInfo.getName(), description, parameterInfos, operationInfo
+         .getReturnType(), operationInfo.getImpact(), operationDescriptor);
+   }
+
+   /**
+    * Build the info.
+    *
+    * @return returns the info
+    * @throws IllegalStateException raised by any build time issue
+    */
+   public ModelMBeanInfo build() throws IllegalStateException
+   {
+      String mbeanDescription = "Exo model mbean";
+      if (typeMD.getDescription() != null)
+      {
+         mbeanDescription = typeMD.getDescription();
+      }
+
+      //
+      ArrayList<ModelMBeanOperationInfo> operations = new ArrayList<ModelMBeanOperationInfo>();
+      for (ManagedMethodMetaData methodMD : typeMD.getMethods())
+      {
+         ModelMBeanOperationInfo operationInfo =
+            buildOperationInfo(methodMD.getMethod(), methodMD.getDescription(), Role.OP, methodMD.getParameters());
+         operations.add(operationInfo);
+      }
+
+      //
+      Map<String, ModelMBeanAttributeInfo> attributeInfos = new HashMap<String, ModelMBeanAttributeInfo>();
+      for (ManagedPropertyMetaData propertyMD : typeMD.getProperties())
+      {
+
+         Method getter = propertyMD.getGetter();
+         if (getter != null)
+         {
+            Role role;
+            String getterName = getter.getName();
+            if (getterName.startsWith("get") && getterName.length() > 3)
+            {
+               role = Role.GET;
+            }
+            else if (getterName.startsWith("is") && getterName.length() > 2)
+            {
+               role = Role.IS;
+            }
+            else
+            {
+               throw new AssertionError();
+            }
+            Collection<ManagedMethodParameterMetaData> blah = Collections.emptyList();
+            ModelMBeanOperationInfo operationInfo =
+               buildOperationInfo(getter, propertyMD.getGetterDescription(), role, blah);
+            operations.add(operationInfo);
+         }
+
+         //
+         Method setter = propertyMD.getSetter();
+         if (setter != null)
+         {
+            ManagedMethodParameterMetaData s = new ManagedMethodParameterMetaData(0);
+            s.setDescription(propertyMD.getSetterParameter().getDescription());
+            s.setName(propertyMD.getSetterParameter().getName());
+            Collection<ManagedMethodParameterMetaData> blah = Collections.singletonList(s);
+            ModelMBeanOperationInfo operationInfo =
+               buildOperationInfo(setter, propertyMD.getSetterDescription(), Role.SET, blah);
+            operations.add(operationInfo);
+         }
+
+         //
+         try
+         {
+            String attributeDescription =
+               propertyMD.getDescription() != null ? propertyMD.getDescription() : ("Managed attribute " + propertyMD
+                  .getName());
+
+            //
+            ModelMBeanAttributeInfo attributeInfo =
+               new ModelMBeanAttributeInfo(propertyMD.getName(), attributeDescription, getter, setter);
+
+            //
+            Descriptor attributeDescriptor = attributeInfo.getDescriptor();
+            if (getter != null)
+            {
+               attributeDescriptor.setField("getMethod", getter.getName());
+            }
+            if (setter != null)
+            {
+               attributeDescriptor.setField("setMethod", setter.getName());
+            }
+            attributeDescriptor.setField("currencyTimeLimit", "-1");
+            attributeDescriptor.setField("persistPolicy", "Never");
+            attributeInfo.setDescriptor(attributeDescriptor);
+
+            //
+            ModelMBeanAttributeInfo previous = attributeInfos.put(propertyMD.getName(), attributeInfo);
+            if (previous != null)
+            {
+               throw new IllegalArgumentException();
+            }
+         }
+         catch (IntrospectionException e)
+         {
+            throw new AssertionError(e);
+         }
+      }
+
+      //
+      return new ModelMBeanInfoSupport(typeMD.getType().getName(), mbeanDescription, attributeInfos.values().toArray(
+         new ModelMBeanAttributeInfo[attributeInfos.size()]), new ModelMBeanConstructorInfo[0], operations
+         .toArray(new ModelMBeanOperationInfo[operations.size()]), new ModelMBeanNotificationInfo[0]);
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoModelMBean.java (from rev 1226, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/ExoModelMBean.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoModelMBean.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ExoModelMBean.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.management.spi.ManagedResource;
+import org.exoplatform.management.ManagementAware;
+import org.exoplatform.management.jmx.annotations.NamingContext;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+import javax.management.MBeanRegistration;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
+import javax.management.RuntimeOperationsException;
+import javax.management.modelmbean.InvalidTargetObjectTypeException;
+import javax.management.modelmbean.ModelMBeanInfo;
+import javax.management.modelmbean.RequiredModelMBean;
+
+/**
+ * A convenient subclass of {@link RequiredModelMBean) that routes the invocation of the interface
+ * {@link MBeanRegistration} to the managed resource when it implements the method. 
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class ExoModelMBean extends RequiredModelMBean
+{
+
+   /** . */
+   private Object mr;
+
+   /** . */
+   private final ManagedResource context;
+
+   public ExoModelMBean(ManagedResource context, Object mr, ModelMBeanInfo mbi) throws MBeanException,
+      RuntimeOperationsException, InstanceNotFoundException, InvalidTargetObjectTypeException
+   {
+      super(mbi);
+
+      //
+      this.context = context;
+      this.mr = mr;
+
+      //
+      setManagedResource(mr, "ObjectReference");
+   }
+
+   @Override
+   public Object invoke(String opName, Object[] opArgs, String[] sig) throws MBeanException, ReflectionException
+   {
+      context.beforeInvoke(mr);
+      try
+      {
+         return super.invoke(opName, opArgs, sig);
+      }
+      finally
+      {
+         context.afterInvoke(mr);
+      }
+   }
+
+   @Override
+   public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
+   {
+      name = super.preRegister(server, name);
+
+      //
+      if (mr instanceof MBeanRegistration)
+      {
+         ((MBeanRegistration)mr).preRegister(server, name);
+      }
+
+      //
+      return name;
+   }
+
+   @Override
+   public void postRegister(Boolean registrationDone)
+   {
+      super.postRegister(registrationDone);
+
+      //
+      PropertiesInfo info = PropertiesInfo.resolve(mr.getClass(), NamingContext.class);
+
+      //
+      MBeanScopingData scopingData = info != null ? info.resolve(mr) : new MBeanScopingData();
+
+      //
+      context.setScopingData(MBeanScopingData.class, scopingData);
+
+      //
+      if (mr instanceof MBeanRegistration)
+      {
+         ((MBeanRegistration)mr).postRegister(registrationDone);
+      }
+   }
+
+   @Override
+   public void preDeregister() throws Exception
+   {
+      if (mr instanceof MBeanRegistration)
+      {
+         ((MBeanRegistration)mr).preDeregister();
+      }
+
+      //
+      if (mr instanceof ManagementAware)
+      {
+         ((ManagementAware)mr).setContext(null);
+      }
+
+      //
+      super.preDeregister();
+   }
+
+   @Override
+   public void postDeregister()
+   {
+      if (mr instanceof MBeanRegistration)
+      {
+         ((MBeanRegistration)mr).postDeregister();
+      }
+
+      //
+      super.postDeregister();
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMX.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/JMX.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMX.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMX.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+/**
+ * Various JMX utilities.
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class JMX
+{
+
+   private JMX()
+   {
+   }
+
+   /**
+    * This method create an object name from a generic map argument. The main reason is that
+    * the method {@link javax.management.ObjectName#getInstance(String, java.util.Hashtable)} has
+    * uses a non generic Hashtable with Java 5 and use a Hashtable<String, String> constructor in Java 6.
+    *
+    * The suitable solution is therefore to use a non generic Hashtable but that creates compilation warning therefore
+    * we encapsulate there this code in order to use the warning supression in that single place.
+    *
+    * @see ObjectName#getInstance(String, java.util.Hashtable)
+    *
+    * @param domain  The domain part of the object name.
+    * @param table A hash table containing one or more key
+    * properties.  The key of each entry in the table is the key of a
+    * key property in the object name.  The associated value in the
+    * table is the associated value in the object name.
+    *
+    * @return an ObjectName corresponding to the given domain and
+    * key mappings.
+    * @exception MalformedObjectNameException The <code>domain</code>
+    * contains an illegal character, or one of the keys or values in
+    * <code>table</code> contains an illegal character, or one of the
+    * values in <code>table</code> does not follow the rules for
+    * quoting.
+    * @exception NullPointerException One of the parameters is null.
+    */
+   @SuppressWarnings("unchecked")
+   public static ObjectName createObjectName(String domain, Map<String, String> table)
+      throws MalformedObjectNameException, NullPointerException
+   {
+      Hashtable tmp = new Hashtable(table);
+      return ObjectName.getInstance(domain, tmp);
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMXManagementProvider.java (from rev 1226, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/JMXManagementProvider.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMXManagementProvider.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/JMXManagementProvider.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.management.spi.ManagedResource;
+import org.exoplatform.management.spi.ManagementProvider;
+import org.exoplatform.management.jmx.annotations.NameTemplate;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.modelmbean.ModelMBeanInfo;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class JMXManagementProvider implements ManagementProvider
+{
+
+   /** . */
+   private final MBeanServer server;
+
+   public JMXManagementProvider()
+   {
+      this(MBeanServerFactory.createMBeanServer());
+   }
+
+   public JMXManagementProvider(MBeanServer server)
+   {
+      this.server = server;
+   }
+
+   public Object manage(ManagedResource context)
+   {
+      ExoModelMBean mbean = null;
+      try
+      {
+         ExoMBeanInfoBuilder infoBuilder = new ExoMBeanInfoBuilder(context.getMetaData());
+         ModelMBeanInfo info = infoBuilder.build();
+         mbean = new ExoModelMBean(context, context.getResource(), info);
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+      }
+
+      //
+      if (mbean != null)
+      {
+         ObjectName on = null;
+         PropertiesInfo oni = PropertiesInfo.resolve(context.getResource().getClass(), NameTemplate.class);
+         if (oni != null)
+         {
+            try
+            {
+               Map<String, String> foo = oni.resolve(context.getResource());
+               on = JMX.createObjectName("exo", foo);
+            }
+            catch (MalformedObjectNameException e)
+            {
+               e.printStackTrace();
+            }
+         }
+
+         if (on != null)
+         {
+            // Merge with the container hierarchy context
+            try
+            {
+               Map<String, String> props = new Hashtable<String, String>();
+
+               // Merge scoping properties
+               List<MBeanScopingData> list = context.getScopingData(MBeanScopingData.class);
+               for (MBeanScopingData scopingData : list)
+               {
+                  props.putAll(scopingData);
+               }
+
+               // Julien : I know it's does not look great but it's necessary
+               // for compiling under Java 5 and Java 6 properly. The methods
+               // ObjectName#getKeyPropertyList() returns an Hashtable with Java 5
+               // and a Hashtable<String, String> with Java 6.
+               for (Object o : on.getKeyPropertyList().entrySet())
+               {
+                  Map.Entry entry = (Map.Entry)o;
+                  String key = (String)entry.getKey();
+                  String value = (String)entry.getValue();
+                  props.put(key, value);
+               }
+
+               //
+               on = JMX.createObjectName(on.getDomain(), props);
+
+               //
+               attemptToRegister(on, mbean);
+
+               //
+               return on;
+            }
+            catch (MalformedObjectNameException e)
+            {
+               e.printStackTrace();
+            }
+         }
+      }
+
+      //
+      return null;  
+   }
+
+   private void attemptToRegister(ObjectName name, Object mbean)
+   {
+      synchronized (server)
+      {
+         try
+         {
+            server.registerMBean(mbean, name);
+         }
+         catch (InstanceAlreadyExistsException e)
+         {
+            try
+            {
+
+               server.unregisterMBean(name);
+               server.registerMBean(mbean, name);
+
+            }
+            catch (Exception e1)
+            {
+               throw new RuntimeException("Failed to register MBean '" + name + " due to " + e.getMessage(), e);
+            }
+         }
+         catch (Exception e)
+         {
+            throw new RuntimeException("Failed to register MBean '" + name + " due to " + e.getMessage(), e);
+         }
+      }
+   }
+
+   public void unmanage(Object key)
+   {
+      ObjectName name = (ObjectName)key;
+      try
+      {
+         server.unregisterMBean(name);
+      }
+      catch (InstanceNotFoundException e)
+      {
+         e.printStackTrace();
+      }
+      catch (MBeanRegistrationException e)
+      {
+         e.printStackTrace();
+      }
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/MBeanScopingData.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/MBeanScopingData.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/MBeanScopingData.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/MBeanScopingData.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class MBeanScopingData extends HashMap<String, String>
+{
+   public MBeanScopingData(int initialCapacity, float loadFactor)
+   {
+      super(initialCapacity, loadFactor);
+   }
+
+   public MBeanScopingData(int initialCapacity)
+   {
+      super(initialCapacity);
+   }
+
+   public MBeanScopingData()
+   {
+   }
+
+   public MBeanScopingData(Map<? extends String, ? extends String> m)
+   {
+      super(m);
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ObjectNameBuilder.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/ObjectNameBuilder.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ObjectNameBuilder.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/ObjectNameBuilder.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.management.jmx.annotations.NameTemplate;
+
+import java.util.Map;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+/**
+ * A builder for object name templates.
+ *
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class ObjectNameBuilder<T>
+{
+
+   /** . */
+   private String domain;
+
+   /** . */
+   private Class<? extends T> clazz;
+
+   /**
+    * Create a new builder.
+    *
+    * @param clazz the class
+    * @throws IllegalArgumentException if the object is null
+    */
+   public ObjectNameBuilder(String domain, Class<? extends T> clazz) throws IllegalArgumentException
+   {
+      if (clazz == null)
+      {
+         throw new IllegalArgumentException("Clazz cannot be null");
+      }
+      this.domain = domain;
+      this.clazz = clazz;
+   }
+
+   /**
+    * Build the object name or return null if the class is not annotated by
+    * {@link NameTemplate}.
+    *
+    * @param object the object
+    * @return the built name
+    * @throws IllegalStateException raised by a build time issue 
+    */
+   public ObjectName build(T object) throws IllegalStateException
+   {
+      PropertiesInfo info = PropertiesInfo.resolve(clazz, NameTemplate.class);
+
+      //
+      if (info != null)
+      {
+
+         try
+         {
+            Map<String, String> props = info.resolve(object);
+            return JMX.createObjectName(domain, props);
+         }
+         catch (MalformedObjectNameException e)
+         {
+            throw new IllegalArgumentException("ObjectName template is malformed", e);
+         }
+      }
+
+      //
+      return null;
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertiesInfo.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/PropertiesInfo.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertiesInfo.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertiesInfo.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.commons.reflect.AnnotationIntrospector;
+import org.exoplatform.management.jmx.annotations.NameTemplate;
+import org.exoplatform.management.jmx.annotations.NamingContext;
+import org.exoplatform.management.jmx.annotations.Property;
+
+import java.lang.annotation.Annotation;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class PropertiesInfo
+{
+
+   /** . */
+   private Map<String, PropertyInfo> properties;
+
+   public PropertiesInfo(Map<String, PropertyInfo> properties)
+   {
+      this.properties = properties;
+   }
+
+   public static PropertiesInfo resolve(Class clazz, Class<? extends Annotation> annotationClass)
+   {
+      Annotation tpl2 = AnnotationIntrospector.resolveClassAnnotations(clazz, annotationClass);
+      Property[] blah = null;
+      if (tpl2 instanceof NamingContext)
+      {
+         blah = ((NamingContext)tpl2).value();
+      }
+      else if (tpl2 instanceof NameTemplate)
+      {
+         blah = ((NameTemplate)tpl2).value();
+      }
+      if (blah != null)
+      {
+         Map<String, PropertyInfo> properties = new HashMap<String, PropertyInfo>();
+         for (Property property : blah)
+         {
+            PropertyInfo propertyInfo = new PropertyInfo(clazz, property);
+            properties.put(propertyInfo.getKey(), propertyInfo);
+         }
+         return new PropertiesInfo(properties);
+      }
+      else
+      {
+         return null;
+      }
+   }
+
+   public Collection<PropertyInfo> getProperties()
+   {
+      return properties.values();
+   }
+
+   public MBeanScopingData resolve(Object instance)
+   {
+      MBeanScopingData props = new MBeanScopingData();
+      for (PropertyInfo propertyInfo : properties.values())
+      {
+         String key = propertyInfo.getKey();
+         String value = propertyInfo.resolveValue(instance);
+         props.put(key, value);
+      }
+      return props;
+   }
+}

Copied: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertyInfo.java (from rev 1218, kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/jmx/PropertyInfo.java)
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertyInfo.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/PropertyInfo.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,161 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.management.jmx.impl;
+
+import org.exoplatform.management.jmx.annotations.Property;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import javax.management.ObjectName;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class PropertyInfo
+{
+
+   /** . */
+   private final String key;
+
+   /** . */
+   private final Value value;
+
+   public PropertyInfo(Class clazz, Property def)
+   {
+      String tmp = def.value();
+      Value value;
+      int length = tmp.length();
+      if (length > 2 && tmp.charAt(0) == '{' && tmp.charAt(length - 1) == '}')
+      {
+         String s = tmp.substring(1, length - 1);
+         String getterName = "get" + s;
+         Method getter;
+         try
+         {
+            getter = clazz.getMethod(getterName);
+         }
+         catch (NoSuchMethodException e)
+         {
+            throw new IllegalArgumentException("Getter parameter for property " + s + " on class " + clazz.getName()
+               + " does not exist", e);
+         }
+
+         //
+         if (getter.getReturnType() == void.class)
+         {
+            throw new IllegalArgumentException("Getter return type for property " + s + " on class " + clazz.getName()
+               + " cannot be void");
+         }
+         if (getter.getParameterTypes().length > 0)
+         {
+            throw new IllegalArgumentException("Getter parameter type for property " + s + " on class "
+               + clazz.getName() + " is not empty");
+         }
+         if (Modifier.isStatic(getter.getModifiers()))
+         {
+            throw new IllegalArgumentException("Getter for property " + s + " on class " + clazz.getName()
+               + " is static");
+         }
+
+         //
+         value = new DynamicValue(getter);
+      }
+      else
+      {
+         value = new LitteralValue(tmp);
+      }
+
+      //
+      this.key = def.key();
+      this.value = value;
+   }
+
+   public String resolveValue(Object instance)
+   {
+      return value.resolve(instance);
+   }
+
+   public String getKey()
+   {
+      return key;
+   }
+
+   private abstract static class Value
+   {
+      abstract String resolve(Object instance);
+   }
+
+   private class DynamicValue extends Value
+   {
+
+      /** . */
+      private final Method getter;
+
+      private DynamicValue(Method getter)
+      {
+         this.getter = getter;
+      }
+
+      String resolve(Object instance)
+      {
+         Object value;
+         try
+         {
+            value = getter.invoke(instance);
+         }
+         catch (IllegalAccessException e)
+         {
+            throw new IllegalArgumentException("Getter for property " + key + " on class "
+               + getter.getClass().getName() + " cannot be invoked", e);
+         }
+         catch (InvocationTargetException e)
+         {
+            throw new IllegalArgumentException("Getter for property " + key + " on class "
+               + getter.getClass().getName() + " threw an exception during invocation", e);
+         }
+         if (value == null)
+         {
+            throw new IllegalArgumentException("Getter for property " + key + " on class "
+               + getter.getClass().getName() + " returned a null value");
+         }
+         return ObjectName.quote(value.toString());
+      }
+   }
+
+   private static class LitteralValue extends Value
+   {
+
+      /** . */
+      private final String litteral;
+
+      private LitteralValue(String litteral)
+      {
+         this.litteral = litteral;
+      }
+
+      String resolve(Object instance)
+      {
+         return litteral;
+      }
+   }
+
+}

Added: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/package-info.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/package-info.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/jmx/impl/package-info.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,4 @@
+/**
+ * The implementation of the management SPI (@link org.exoplatform.management.spi} for the JMX management system.
+ */
+package org.exoplatform.management.jmx.impl;
\ No newline at end of file

Modified: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/ManagedResource.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/ManagedResource.java	2009-12-29 09:58:06 UTC (rev 1227)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/ManagedResource.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -23,7 +23,9 @@
 import java.util.List;
 
 /**
- * The contract between a management provider and the kernel.
+ * The managed resource provided by the kernel to a management provider. It gives access
+ * to the resource itself, the meta data of the managed resource, the attached scoped data
+ * and the before/after invoke contract when a resource is invoked from the management layer.
  *
  * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
  * @version $Revision$
@@ -57,7 +59,7 @@
    <S> List<S> getScopingData(Class<S> scopeType);
 
    /**
-    * Callback made by the provie to the resource to signal that scopgin data is used for the managed resource.
+    * Callback made by the provider to the resource to signal that scoping data that is used for the managed resource.
     *
     * @param scopeType the scope type
     * @param <S> the generic type of the scope type

Added: kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/package-info.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/package-info.java	                        (rev 0)
+++ kernel/trunk/exo.kernel.container/src/main/java/org/exoplatform/management/spi/package-info.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -0,0 +1,11 @@
+/**
+ * <p>The Service Provider Interface for the eXo management framework. The entry point for plugged management system
+ * is the implementation of the interface {@link org.exoplatform.management.spi.ManagementProvider}.</p>
+ *
+ * <p>A part of the package defines the meta data that is given to the provider to build the management interface
+ * of the managed resource.</p>
+ *
+ * <p>The {@link org.exoplatform.management.spi.ManagedResource} interface wraps a resource and is provided by the
+ * kernel to the provider.</p>
+ */
+package org.exoplatform.management.spi;
\ No newline at end of file

Modified: kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestExoMBean.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestExoMBean.java	2009-12-29 09:58:06 UTC (rev 1227)
+++ kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/AbstractTestExoMBean.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -19,7 +19,7 @@
 package org.exoplatform.container.jmx;
 
 import junit.framework.TestCase;
-import org.exoplatform.management.spi.jmx.ExoMBeanInfoBuilder;
+import org.exoplatform.management.jmx.impl.ExoMBeanInfoBuilder;
 
 import javax.management.MBeanServer;
 import javax.management.MBeanServerFactory;

Modified: kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/TestNameTemplate.java
===================================================================
--- kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/TestNameTemplate.java	2009-12-29 09:58:06 UTC (rev 1227)
+++ kernel/trunk/exo.kernel.container/src/test/java/org/exoplatform/container/jmx/TestNameTemplate.java	2009-12-29 10:16:00 UTC (rev 1228)
@@ -22,7 +22,7 @@
 
 import org.exoplatform.management.jmx.annotations.NameTemplate;
 import org.exoplatform.management.jmx.annotations.Property;
-import org.exoplatform.management.spi.jmx.ObjectNameBuilder;
+import org.exoplatform.management.jmx.impl.ObjectNameBuilder;
 
 import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;



More information about the exo-jcr-commits mailing list