[jboss-cvs] JBossAS SVN: r64959 - in projects/microcontainer/trunk: deployers-impl/src/tests/org/jboss/test/deployers/managed/test and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Aug 30 00:58:26 EDT 2007


Author: scott.stark at jboss.org
Date: 2007-08-30 00:58:25 -0400 (Thu, 30 Aug 2007)
New Revision: 64959

Added:
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceAttributeMetaData.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaData.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java
Modified:
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/managed/test/DeployerManagedDeploymentUnitTestCase.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java
Log:
JBMICROCONT-181, add an InstanceClassFactory plugin to obtain attachment annotations and values

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceAttributeMetaData.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceAttributeMetaData.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceAttributeMetaData.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * 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.test.deployers.deployer.support;
+
+import java.io.Serializable;
+
+/**
+ * A service attribute
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+public class TestServiceAttributeMetaData
+   implements Serializable
+{
+   private static final long serialVersionUID = 1;
+   private Object value;
+   private String name;
+
+   public TestServiceAttributeMetaData(Object value, String name)
+   {
+      super();
+      this.value = value;
+      this.name = name;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+   public void setName(String name)
+   {
+      this.name = name;
+   }
+   public Object getValue()
+   {
+      return value;
+   }
+   public void setValue(Object value)
+   {
+      this.value = value;
+   }
+
+   
+}


Property changes on: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceAttributeMetaData.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaData.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaData.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaData.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -0,0 +1,83 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * 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.test.deployers.deployer.support;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * The mbean service metadata
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+public class TestServiceMetaData
+   implements Serializable
+{
+   private static final long serialVersionUID = 1;
+
+   /** The ObjectName */
+   private String objectName;
+   
+   /** The code */
+   private String code;
+   private List<TestServiceAttributeMetaData> attributes;
+
+   public String getCode()
+   {
+      return code;
+   }
+
+   public void setCode(String code)
+   {
+      this.code = code;
+   }
+
+   public String getObjectName()
+   {
+      return objectName;
+   }
+
+   public void setObjectName(String objectName)
+   {
+      this.objectName = objectName;
+   }
+
+   
+   public List<TestServiceAttributeMetaData> getAttributes()
+   {
+      return attributes;
+   }
+
+   /**
+    * Set the attributes.
+    * 
+    * @param attributes the attributes.
+    */
+   public void setAttributes(List<TestServiceAttributeMetaData> attributes)
+   {
+      if (attributes == null)
+         throw new IllegalArgumentException("Null attributes");
+      this.attributes = attributes;
+   }
+
+}


Property changes on: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaData.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Added: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * 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.test.deployers.deployer.support;
+
+import java.io.Serializable;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+
+/**
+ * An InstanceClassFactory implementation for services
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+public class TestServiceMetaDataICF implements InstanceClassFactory
+{
+   /** The meta value factory */
+   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance(); 
+
+   public Class<? extends Serializable> getManagedObjectClass(Serializable instance)
+      throws ClassNotFoundException
+   {
+      TestServiceMetaData md = (TestServiceMetaData) instance;
+      ClassLoader loader = instance.getClass().getClassLoader();
+      Class<? extends Serializable> moClass = (Class<? extends Serializable>)loader.loadClass(md.getCode());
+      return moClass;
+   }
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable instance)
+   {
+      TestServiceMetaData md = (TestServiceMetaData) instance;
+      // First look to the mapped name
+      String name = property.getMappedName();
+      if (name == null)
+         property.getName();
+
+      Object value = null;
+      for (TestServiceAttributeMetaData amd : md.getAttributes())
+      {
+         if (amd.getName().equals(name))
+         {
+            value = amd.getValue();
+         }
+      }
+
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
+      if (propertyInfo == null)
+      {
+         throw new IllegalStateException("Unable to find property: " + name
+               + " for " + instance.getClass().getName());
+      }
+
+      return metaValueFactory.create(value, propertyInfo.getType());
+   }
+
+}


Property changes on: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Modified: projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/managed/test/DeployerManagedDeploymentUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/managed/test/DeployerManagedDeploymentUnitTestCase.java	2007-08-29 22:47:13 UTC (rev 64958)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/managed/test/DeployerManagedDeploymentUnitTestCase.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -41,8 +41,6 @@
 import org.jboss.managed.api.factory.ManagedObjectFactory;
 import org.jboss.metatype.api.types.SimpleMetaType;
 import org.jboss.metatype.api.values.GenericValue;
-import org.jboss.metatype.api.values.SimpleValue;
-import org.jboss.metatype.api.values.SimpleValueSupport;
 import org.jboss.test.deployers.AbstractDeployerTest;
 import org.jboss.test.deployers.deployer.support.ConnMetaData;
 import org.jboss.test.deployers.deployer.support.DSMetaData;
@@ -51,6 +49,9 @@
 import org.jboss.test.deployers.deployer.support.MCFDeployer;
 import org.jboss.test.deployers.deployer.support.SecMetaData;
 import org.jboss.test.deployers.deployer.support.SecurityDeployment;
+import org.jboss.test.deployers.deployer.support.TestServiceAttributeMetaData;
+import org.jboss.test.deployers.deployer.support.TestServiceMetaData;
+import org.jboss.test.deployers.deployer.support.TestServiceMetaDataICF;
 import org.jboss.test.deployers.deployer.support.XADataSourceMetaData;
 import org.jboss.test.deployers.managed.support.MockProfileService;
 
@@ -90,6 +91,9 @@
    {
       DeployerClient main = getMainDeployer();
       MockProfileService ps = new MockProfileService(main);
+      ManagedObjectFactory mof = ManagedObjectFactory.getInstance();
+      TestServiceMetaDataICF tsicf = new TestServiceMetaDataICF();
+      mof.setInstanceClassFactory(TestServiceMetaData.class, tsicf);
       
       // Deploy a datasource with local and xa factories
       Deployment ctx1 = createSimpleDeployment("deployment1");
@@ -125,9 +129,12 @@
       MutableAttachments a1 = (MutableAttachments) ctx1.getPredeterminedManagedObjects();
       a1.addAttachment(DSMetaData.class, dsmd);
       // The mbeans associated with the local DS
-      DSServiceMetaData localMBeans = new DSServiceMetaData();
-      localMBeans.setManagementName("java:DefaultDS1");
-      a1.addAttachment(DSServiceMetaData.class, localMBeans);
+      TestServiceMetaData localMBeans = new TestServiceMetaData();
+      localMBeans.setCode(DSServiceMetaData.class.getName());
+      ArrayList<TestServiceAttributeMetaData> localMBeanAttrs = new ArrayList<TestServiceAttributeMetaData>();
+      localMBeanAttrs.add(new TestServiceAttributeMetaData("java:DefaultDS1", "managementName"));
+      localMBeans.setAttributes(localMBeanAttrs);
+      a1.addAttachment(TestServiceMetaData.class, localMBeans);
       ps.addDeployment(ctx1);
 
       // Deploy security domain1

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java	2007-08-29 22:47:13 UTC (rev 64958)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -25,6 +25,7 @@
 
 import org.jboss.managed.api.ManagedObject;
 import org.jboss.managed.plugins.factory.ManagedObjectFactoryBuilder;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
 import org.jboss.managed.spi.factory.ManagedObjectBuilder;
 
 /**
@@ -81,4 +82,10 @@
     * @param builder the builder (null to remove the builder)
     */
    public abstract void setBuilder(Class<?> clazz, ManagedObjectBuilder builder);
+   /**
+    * Set the InstanceClassFactory for an instance type.
+    * @param factory - the factory used to obtain the class to scan for
+    * management annotations.
+    */
+   public abstract void setInstanceClassFactory(Class<?> clazz, InstanceClassFactory factory);
 }

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java	2007-08-29 22:47:13 UTC (rev 64958)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -62,6 +62,7 @@
 import org.jboss.managed.plugins.ManagedOperationImpl;
 import org.jboss.managed.plugins.ManagedParameterImpl;
 import org.jboss.managed.plugins.ManagedPropertyImpl;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
 import org.jboss.managed.spi.factory.ManagedObjectBuilder;
 import org.jboss.managed.spi.factory.ManagedObjectPopulator;
 import org.jboss.managed.spi.factory.ManagedParameterConstraintsPopulator;
@@ -90,7 +91,9 @@
  * @version $Revision: 1.1 $
  */
 public class AbstractManagedObjectFactory extends ManagedObjectFactory
-   implements ManagedObjectBuilder, ManagedObjectPopulator<Serializable>
+   implements ManagedObjectBuilder,
+   InstanceClassFactory,
+   ManagedObjectPopulator<Serializable>
 {
    private static final Logger log = Logger.getLogger(AbstractManagedObjectFactory.class);
 
@@ -108,6 +111,8 @@
    
    /** The managed object builders */
    private Map<Class, WeakReference<ManagedObjectBuilder>> builders = new WeakHashMap<Class, WeakReference<ManagedObjectBuilder>>();
+   /** The instance to class factories */
+   private Map<Class, WeakReference<InstanceClassFactory>> instanceFactories = new WeakHashMap<Class, WeakReference<InstanceClassFactory>>();
 
    static
    {
@@ -141,13 +146,23 @@
          throw new IllegalArgumentException("Null object");
 
       Class<? extends Serializable> clazz = object.getClass();
-      ManagedObject result = createSkeletonManagedObject(clazz);
+      InstanceClassFactory icf = getInstanceFactory(clazz);
+      Class<? extends Serializable> moClass = null;
+      try
+      {
+         moClass = icf.getManagedObjectClass(object);
+      }
+      catch(ClassNotFoundException e)
+      {
+         return null;
+      }
+      ManagedObject result = createSkeletonManagedObject(moClass);
       if (result == null )
       {
-         log.debug("Null ManagedObject created for: "+clazz);
+         log.debug("Null ManagedObject created for: "+moClass);
          return null;
       }
-      ManagedObjectPopulator<Serializable> populator = getPopulator(clazz);
+      ManagedObjectPopulator<Serializable> populator = getPopulator(moClass);
       populator.populateManagedObject(result, object);
 
       return result;
@@ -163,8 +178,28 @@
          builders.put(clazz, new WeakReference<ManagedObjectBuilder>(builder));
       }
    }
-   
+
+   @Override
+   public void setInstanceClassFactory(Class<?> clazz, InstanceClassFactory factory)
+   {
+      synchronized (instanceFactories)
+      {
+         if (instanceFactories == null)
+            instanceFactories.remove(clazz);
+         instanceFactories.put(clazz, new WeakReference<InstanceClassFactory>(factory));
+      }      
+   }
+
    /**
+    * Default InstanceClassFactory implementation simply returns the
+    * instance class. 
+    */
+   public Class<? extends Serializable> getManagedObjectClass(Serializable instance)
+   {
+      return instance.getClass();
+   }
+
+   /**
     * Create a skeleton managed object
     * 
     * @param <T> the type
@@ -479,14 +514,24 @@
     */
    protected void populateValues(ManagedObjectImpl managedObject, Serializable object)
    {
-      BeanInfo beanInfo = configuration.getBeanInfo(object.getClass());
+      InstanceClassFactory icf = getInstanceFactory(object.getClass());
+      Class moClass = null;
+      try
+      {
+         moClass = icf.getManagedObjectClass(object);
+      }
+      catch(ClassNotFoundException e)
+      {
+         throw new IllegalStateException(e);
+      }
+      BeanInfo beanInfo = configuration.getBeanInfo(moClass);
 
       Map<String, ManagedProperty> properties = managedObject.getProperties();
       if (properties != null && properties.size() > 0)
       {
          for (ManagedProperty property : properties.values())
          {
-            MetaValue value = getValue(beanInfo, property, object);
+            MetaValue value = icf.getValue(beanInfo, property, object);
             if (value != null)
                property.setField(Fields.VALUE, value);
             /* Need to look for a ManagementObjectID at the property level which
@@ -519,7 +564,7 @@
     * @param object the object
     * @return the meta value
     */
-   protected MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable object)
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable object)
    {
       // First look to the mapped name
       String name = property.getMappedName();
@@ -679,8 +724,25 @@
       }
       return this;
    }
-   
+
    /**
+    * Get the instance factory for a class
+    * 
+    * @param clazz the class
+    * @return the InstanceClassFactory
+    */
+   protected InstanceClassFactory getInstanceFactory(Class<?> clazz)
+   {
+      synchronized (instanceFactories)
+      {
+         WeakReference<InstanceClassFactory> weak = instanceFactories.get(clazz);
+         if (weak != null)
+            return weak.get();
+      }
+      return this;
+   }
+
+   /**
     * Get the populator for a class
     * 
     * @param clazz the class

Added: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java	                        (rev 0)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java	2007-08-30 04:58:25 UTC (rev 64959)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * 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.managed.spi.factory;
+
+import java.io.Serializable;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.metatype.api.values.MetaValue;
+
+/**
+ * A plugin for obtaining the class to scan for management object
+ * related annotations.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+public interface InstanceClassFactory
+{
+   /**
+    * Return the Class that represents the root ManagedObject to scan
+    * for management object related annotations.
+    * 
+    * @param instance - the instance a ManagedObject is to be created for.
+    * @return the Class that represents the root ManagedObject.
+    */
+   public Class<? extends Serializable> getManagedObjectClass(Serializable instance)
+      throws ClassNotFoundException;
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable object);
+}


Property changes on: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native




More information about the jboss-cvs-commits mailing list