[jboss-cvs] JBossAS SVN: r66092 - in projects/microcontainer/trunk: container/src/main/org/jboss/reflect/plugins/javassist and 11 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 12 12:31:37 EDT 2007


Author: alesj
Date: 2007-10-12 12:31:37 -0400 (Fri, 12 Oct 2007)
New Revision: 66092

Added:
   projects/microcontainer/trunk/deployers-vfs/src/main/org/jboss/deployers/vfs/deployer/kernel/managed/
   projects/microcontainer/trunk/deployers-vfs/src/main/org/jboss/deployers/vfs/deployer/kernel/managed/BeanInstanceClassFactory.java
Modified:
   projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/ClassInfoImpl.java
   projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
   projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/DelegateClassInfo.java
   projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/PrimitiveInfo.java
   projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/TypeInfo.java
   projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java
   projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractArrayMetaData.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/ManagedPropertyImpl.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
   projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
Log:
Extending InstanceClassFactory to support runtime component name, set value.
Adding MetaValueFactory.unwrap.
TypeInfo.newArrayInstance returning plain Object.

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/ClassInfoImpl.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/ClassInfoImpl.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/ClassInfoImpl.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -521,12 +521,11 @@
       return classInfoHelper.getTypeInfo(arrayClass);
    }
 
-   public Object[] newArrayInstance(int size) throws Throwable
+   public Object newArrayInstance(int size) throws Throwable
    {
-      Class clazz = getType();
-      if (clazz.isArray() == false)
-         throw new ClassCastException(clazz + " is not an array.");
-      return (Object[]) Array.newInstance(clazz.getComponentType(), size);
+      if (isArray() == false)
+         throw new ClassCastException(this + " is not an array.");
+      return Array.newInstance(getComponentType().getType(), size);
    }
 
    public boolean isAssignableFrom(TypeInfo info)

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/reflect/plugins/javassist/JavassistTypeInfo.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -363,12 +363,11 @@
       return factory.getTypeInfo(arrayClass);
    }
 
-   public Object[] newArrayInstance(int size) throws Throwable
+   public Object newArrayInstance(int size) throws Throwable
    {
-      Class clazz = getType();
-      if (clazz.isArray() == false)
-         throw new ClassCastException(clazz + " is not an array.");
-      return (Object[]) Array.newInstance(clazz.getComponentType(), size);
+      if (isArray() == false)
+         throw new ClassCastException(this + " is not an array.");
+      return Array.newInstance(getComponentType().getType(), size);
    }
 
    public boolean isAssignableFrom(TypeInfo info)

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/DelegateClassInfo.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/DelegateClassInfo.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/DelegateClassInfo.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -275,7 +275,7 @@
       return delegate.isPrimitive();
    }
 
-   public Object[] newArrayInstance(int size) throws Throwable
+   public Object newArrayInstance(int size) throws Throwable
    {
       return delegate.newArrayInstance(size);
    }

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/PrimitiveInfo.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/PrimitiveInfo.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/PrimitiveInfo.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -191,7 +191,7 @@
       return typeInfoFactory.getTypeInfo(arrayClass);
    }
 
-   public Object[] newArrayInstance(int size) throws Throwable
+   public Object newArrayInstance(int size) throws Throwable
    {
       throw new UnsupportedOperationException("Not an array " + name);
    }

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/TypeInfo.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/TypeInfo.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/reflect/spi/TypeInfo.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -139,7 +139,7 @@
     * @return the converted value
     * @throws Throwable for any error
     */
-   Object[] newArrayInstance(int size) throws Throwable;
+   Object newArrayInstance(int size) throws Throwable;
 
    /**
     * Mostly using

Modified: 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	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/deployers-impl/src/tests/org/jboss/test/deployers/deployer/support/TestServiceMetaDataICF.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -36,44 +36,63 @@
  * @author Scott.Stark at jboss.org
  * @version $Revision$
  */
-public class TestServiceMetaDataICF implements InstanceClassFactory
+public class TestServiceMetaDataICF implements InstanceClassFactory<TestServiceMetaData>
 {
    /** The meta value factory */
    private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance(); 
 
-   public Class<? extends Serializable> getManagedObjectClass(Serializable instance)
+   public Class<? extends Serializable> getManagedObjectClass(TestServiceMetaData instance)
       throws ClassNotFoundException
    {
-      TestServiceMetaData md = (TestServiceMetaData) instance;
       ClassLoader loader = instance.getClass().getClassLoader();
-      Class moClass = loader.loadClass(md.getCode());
+      Class moClass = loader.loadClass(instance.getCode());
       return moClass;
    }
-   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable instance)
+
+   protected String getPropertyName(ManagedProperty property)
    {
-      TestServiceMetaData md = (TestServiceMetaData) instance;
       // First look to the mapped name
       String name = property.getMappedName();
       if (name == null)
          property.getName();
+      return name;
+   }
 
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, TestServiceMetaData instance)
+   {
+      String name = getPropertyName(property);
+
       Object value = null;
-      for (TestServiceAttributeMetaData amd : md.getAttributes())
+      for (TestServiceAttributeMetaData amd : instance.getAttributes())
       {
          if (amd.getName().equals(name))
          {
             value = amd.getValue();
+            break;
          }
       }
 
       PropertyInfo propertyInfo = beanInfo.getProperty(name);
-      if (propertyInfo == null)
+      return metaValueFactory.create(value, propertyInfo.getType());
+   }
+
+   public void setValue(BeanInfo beanInfo, ManagedProperty property, TestServiceMetaData object, MetaValue value)
+   {
+      String name = getPropertyName(property);
+
+      for (TestServiceAttributeMetaData amd : object.getAttributes())
       {
-         throw new IllegalStateException("Unable to find property: " + name
-               + " for " + instance.getClass().getName());
+         if (amd.getName().equals(name))
+         {
+            Class clazz = value != null ? value.getClass() : null;
+            amd.setValue(metaValueFactory.unwrap(value, clazz));
+            break;
+         }
       }
+   }
 
-      return metaValueFactory.create(value, propertyInfo.getType());
+   public Object getComponentName(BeanInfo beanInfo, ManagedProperty property, TestServiceMetaData object, MetaValue value)
+   {
+      return (beanInfo == null || property == null || value == null) ? object.getObjectName() : null;
    }
-
 }

Added: projects/microcontainer/trunk/deployers-vfs/src/main/org/jboss/deployers/vfs/deployer/kernel/managed/BeanInstanceClassFactory.java
===================================================================
--- projects/microcontainer/trunk/deployers-vfs/src/main/org/jboss/deployers/vfs/deployer/kernel/managed/BeanInstanceClassFactory.java	                        (rev 0)
+++ projects/microcontainer/trunk/deployers-vfs/src/main/org/jboss/deployers/vfs/deployer/kernel/managed/BeanInstanceClassFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -0,0 +1,146 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.deployers.vfs.deployer.kernel.managed;
+
+import java.io.Serializable;
+import java.lang.reflect.UndeclaredThrowableException;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.beans.metadata.plugins.AbstractPropertyMetaData;
+import org.jboss.beans.metadata.plugins.AbstractValueMetaData;
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.beans.metadata.spi.PropertyMetaData;
+import org.jboss.beans.metadata.spi.ValueMetaData;
+import org.jboss.kernel.plugins.config.Configurator;
+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;
+
+/**
+ * The InstanceClassFactory implementation for BeanMetaData.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class BeanInstanceClassFactory implements InstanceClassFactory<BeanMetaData>
+{
+   /** The meta value factory */
+   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
+
+   /**
+    * Get the classloader.
+    *
+    * @param beanMetaData the bean meta data
+    * @return meta data's classloader
+    * @throws UndeclaredThrowableException for any error
+    */
+   protected ClassLoader getClassLoader(BeanMetaData beanMetaData)
+   {
+      try
+      {
+         return Configurator.getClassLoader(beanMetaData);
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   @SuppressWarnings("unchecked")
+   public Class<? extends Serializable> getManagedObjectClass(BeanMetaData attachment) throws ClassNotFoundException
+   {
+      Class clazz = getClassLoader(attachment).loadClass(attachment.getBean());
+      return clazz;
+   }
+
+   /**
+    * Get the property meta data.
+    *
+    * @param attachment bean meta data
+    * @param name property name
+    * @return property meta data or null if no match
+    */
+   protected PropertyMetaData getPropertyMetaData(BeanMetaData attachment, String name)
+   {
+      PropertyMetaData propertyMetaData = null;
+      for(PropertyMetaData pmd : attachment.getProperties())
+      {
+         if (name.equals(pmd.getName()))
+         {
+            propertyMetaData = pmd;
+            break;
+         }
+      }
+      return propertyMetaData;
+   }
+
+   /**
+    * Get the property meta data.
+    *
+    * @param attachment bean meta data
+    * @param name property name
+    * @return property meta data or exception if no match
+    * @throws IllegalArgumentException for no matching property meta data
+    */
+   protected PropertyMetaData getExactPropertyMetaData(BeanMetaData attachment, String name)
+   {
+      PropertyMetaData propertyMetaData = getPropertyMetaData(attachment, name);
+      if (propertyMetaData == null)
+         throw new IllegalArgumentException("No matching property meta data: " + name + "/" + attachment);
+      return propertyMetaData;
+   }
+
+   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, BeanMetaData attachment)
+   {
+      String name = property.getName();
+      PropertyMetaData pmd = getExactPropertyMetaData(attachment, name);
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
+      ValueMetaData valueMetaData = pmd.getValue();
+      try
+      {
+         Object value = valueMetaData.getValue(propertyInfo.getType(), getClassLoader(attachment));
+         return metaValueFactory.create(value, propertyInfo.getType());
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   public void setValue(BeanInfo beanInfo, ManagedProperty property, BeanMetaData attachment, MetaValue value)
+   {
+      String name = property.getName();
+      PropertyMetaData pmd = getExactPropertyMetaData(attachment, name);
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
+      if (pmd instanceof AbstractPropertyMetaData)
+      {
+         AbstractPropertyMetaData apmd = (AbstractPropertyMetaData)pmd;
+         apmd.setValue(new AbstractValueMetaData(metaValueFactory.unwrap(value, propertyInfo.getType())));
+      }
+   }
+
+   public Object getComponentName(BeanInfo beanInfo, ManagedProperty property, BeanMetaData attachment, MetaValue value)
+   {
+      return (beanInfo == null || property == null || value == null) ? attachment.getName() : null;
+   }
+}

Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractArrayMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractArrayMetaData.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractArrayMetaData.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -23,6 +23,7 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.lang.reflect.Array;
 
 import org.jboss.reflect.spi.ClassInfo;
 import org.jboss.reflect.spi.TypeInfo;
@@ -77,8 +78,11 @@
          typeInfo = info;
       }
 
-      Object[] array = typeInfo.newArrayInstance(result.size());
-      return result.toArray(array);
+      Object array = typeInfo.newArrayInstance(result.size());
+      int i = 0;
+      for(Object element : result)
+         Array.set(array, i++, element);
+      return array;
    }
 
    protected <T> T getTypeInstance(TypeInfo info, ClassLoader cl, Class<T> expected) throws Throwable

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-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/api/factory/ManagedObjectFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -82,10 +82,24 @@
     * @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 <T> the class type
+    * @param clazz the class
     * @param factory - the factory used to obtain the class to scan for
     * management annotations.
     */
-   public abstract void setInstanceClassFactory(Class<?> clazz, InstanceClassFactory factory);
+   public abstract <T extends Serializable> void setInstanceClassFactory(Class<T> clazz, InstanceClassFactory<T> factory);
+
+   /**
+    * Get the InstanceClassFactory for an instance type.
+    *
+    * @param <T> the class type
+    * @param clazz the class
+    * @return the factory used to obtain the class to scan for
+    * management annotations. 
+    */
+   public abstract <T extends Serializable> InstanceClassFactory<T> getInstanceClassFactory(Class<T> clazz);
 }

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/ManagedPropertyImpl.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/ManagedPropertyImpl.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/ManagedPropertyImpl.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -209,6 +209,7 @@
       Object set = getField(Fields.ANNOTATIONS, Object.class);
       return (Map<String, Annotation>) set;
    }
+
    public void setAnnotations(Map<String, Annotation> annotations)
    {
       setField(Fields.ANNOTATIONS, (Serializable) annotations);      

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/WritethroughManagedPropertyImpl.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -23,27 +23,34 @@
 
 import java.io.ObjectStreamException;
 import java.io.Serializable;
-import java.lang.reflect.UndeclaredThrowableException;
 
+import org.jboss.beans.info.spi.BeanInfo;
 import org.jboss.beans.info.spi.PropertyInfo;
 import org.jboss.managed.api.Fields;
 import org.jboss.managed.api.ManagedObject;
+import org.jboss.managed.api.factory.ManagedObjectFactory;
+import org.jboss.managed.plugins.factory.ManagedObjectFactoryBuilder;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
 import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+import org.jboss.metatype.api.types.MetaType;
+import org.jboss.metatype.plugins.values.MetaValueFactoryBuilder;
 
 /**
  * An extension of ManagedPropertyImpl.
  *
  * @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
  * @version $Revision$
  */
 public class WritethroughManagedPropertyImpl extends ManagedPropertyImpl
 {
    private static final long serialVersionUID = 1;
 
-   public WritethroughManagedPropertyImpl(String name)
-   {
-      super(name);
-   }
+   /** The meta value factory */
+   private transient MetaValueFactory valueFactory;
+   /** The managed object factory */
+   private transient ManagedObjectFactory objectFactory;
 
    public WritethroughManagedPropertyImpl(Fields fields)
    {
@@ -55,33 +62,57 @@
       super(managedObject, fields);
    }
 
+   public WritethroughManagedPropertyImpl(Fields fields, MetaValueFactory valueFactory, ManagedObjectFactory objectFactory)
+   {
+      super(fields);
+      this.valueFactory = valueFactory;
+      this.objectFactory = objectFactory;
+   }
+
+   protected ManagedObjectFactory getObjectFactory()
+   {
+      if (objectFactory == null)
+         objectFactory = ManagedObjectFactoryBuilder.create();
+      return objectFactory;
+   }
+
+   protected MetaValueFactory getValueFactory()
+   {
+      if (valueFactory == null)
+         valueFactory = MetaValueFactoryBuilder.create();
+      return valueFactory;
+   }
+
    /**
     * Write the value back to the attachment if there is a PropertyInfo
     * in the Fields.PROPERTY_INFO field.
-    * TODO: this ignored MetaValues as the tests pass in the corresponding
-    * primative
     */
    @Override
+   @SuppressWarnings("unchecked")
    public void setValue(Serializable value)
    {
       super.setValue(value);
-      // Skip MetaValues
-      if( (value instanceof MetaValue) )
-         return;
 
-      // Write the value back to the attachment if there is a PropertyInfo
-      PropertyInfo info = super.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
-      if (info != null)
+      PropertyInfo propertyInfo = getField(Fields.PROPERTY_INFO, PropertyInfo.class);
+      if (propertyInfo != null)
       {
-         Object bean = getManagedObject().getAttachment();
-         try
+         Serializable attachment = getManagedObject().getAttachment();
+         if (attachment != null)
          {
-            info.set(bean, value);
+            MetaValue metaValue;
+            if (value instanceof MetaValue == false)
+               metaValue = getValueFactory().create(value, propertyInfo.getType());
+            else
+               metaValue = (MetaValue)value;
+
+            MetaType metaType = metaValue.getMetaType();
+            if (metaType.isTable() == false && metaType.isComposite() == false)
+            {
+               InstanceClassFactory icf = getObjectFactory().getInstanceClassFactory(attachment.getClass());
+               BeanInfo beanInfo = propertyInfo.getBeanInfo();
+               icf.setValue(beanInfo, this, attachment, metaValue);
+            }
          }
-         catch(Throwable t)
-         {
-            throw new UndeclaredThrowableException(t);
-         }
       }
    }
 

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-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -95,7 +95,7 @@
  * @version $Revision: 1.1 $
  */
 public class AbstractManagedObjectFactory extends ManagedObjectFactory
-   implements ManagedObjectBuilder, InstanceClassFactory, ManagedObjectPopulator<Serializable>
+   implements ManagedObjectBuilder, InstanceClassFactory<Serializable>, ManagedObjectPopulator<Serializable>
 {
    private static final Logger log = Logger.getLogger(AbstractManagedObjectFactory.class);
 
@@ -109,7 +109,7 @@
    private MetaTypeFactory metaTypeFactory = MetaTypeFactory.getInstance(); 
 
    /** The meta value factory */
-   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance(); 
+   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
    
    /** The managed object builders */
    private Map<Class, WeakReference<ManagedObjectBuilder>> builders = new WeakHashMap<Class, WeakReference<ManagedObjectBuilder>>();
@@ -118,7 +118,7 @@
    private Map<Class, WeakReference<InstanceClassFactory>> instanceFactories = new WeakHashMap<Class, WeakReference<InstanceClassFactory>>();
 
    /** The instance to name transformers */
-   private Map<Class<?>, WeakReference<RuntimeComponentNameTransformer>> transformers = new WeakHashMap<Class<?>, WeakReference<RuntimeComponentNameTransformer>>();
+   private Map<TypeInfo, WeakReference<RuntimeComponentNameTransformer>> transformers = new WeakHashMap<TypeInfo, WeakReference<RuntimeComponentNameTransformer>>();
 
    static
    {
@@ -145,13 +145,14 @@
    }
 
    @Override
+   @SuppressWarnings("unchecked")
    public ManagedObject initManagedObject(Serializable object, String name, String nameType)
    {
       if (object == null)
          throw new IllegalArgumentException("Null object");
 
       Class<? extends Serializable> clazz = object.getClass();
-      InstanceClassFactory icf = getInstanceFactory(clazz);
+      InstanceClassFactory icf = getInstanceClassFactory(clazz);
       Class<? extends Serializable> moClass;
       try
       {
@@ -186,7 +187,7 @@
    }
 
    @Override
-   public void setInstanceClassFactory(Class<?> clazz, InstanceClassFactory factory)
+   public <T extends Serializable> void setInstanceClassFactory(Class<T> clazz, InstanceClassFactory<T> factory)
    {
       synchronized (instanceFactories)
       {
@@ -199,12 +200,18 @@
 
    public void setNameTransformers(Class<?> clazz, RuntimeComponentNameTransformer transformer)
    {
+      TypeInfo type = configuration.getTypeInfo(clazz);
+      setNameTransformers(type, transformer);
+   }
+
+   public void setNameTransformers(TypeInfo type, RuntimeComponentNameTransformer transformer)
+   {
       synchronized (transformers)
       {
          if (transformer == null)
-            transformers.remove(clazz);
+            transformers.remove(type);
          else
-            transformers.put(clazz, new WeakReference<RuntimeComponentNameTransformer>(transformer));
+            transformers.put(type, new WeakReference<RuntimeComponentNameTransformer>(transformer));
       }
    }
 
@@ -317,6 +324,7 @@
             ManagementProperty managementProperty = propertyInfo.getUnderlyingAnnotation(ManagementProperty.class);
             ManagementObjectID id = propertyInfo.getUnderlyingAnnotation(ManagementObjectID.class);
             ManagementObjectRef ref = propertyInfo.getUnderlyingAnnotation(ManagementObjectRef.class);
+            ManagementRuntimeRef runtimeRef = propertyInfo.getUnderlyingAnnotation(ManagementRuntimeRef.class);
             HashMap<String, Annotation> propAnnotations = new HashMap<String, Annotation>();
             if (managementProperty != null)
                propAnnotations.put(ManagementProperty.class.getName(), managementProperty);
@@ -328,6 +336,8 @@
             }
             if (ref != null)
                propAnnotations.put(ManagementObjectRef.class.getName(), ref);
+            if (runtimeRef != null)
+               propAnnotations.put(ManagementRuntimeRef.class.getName(), runtimeRef);
 
             // Check for a simple property
             boolean includeProperty = (propertyType == ManagementProperties.ALL);
@@ -499,7 +509,7 @@
     */
    protected ManagedProperty createDefaultManagedProperty(Fields fields)
    {
-      return new WritethroughManagedPropertyImpl(fields);
+      return new WritethroughManagedPropertyImpl(fields, metaValueFactory, this);
    }
 
    public void createObject(ManagedObject managedObject, Class<? extends Serializable> clazz)
@@ -552,9 +562,10 @@
     * @param managedObject the managed object
     * @param object the object
     */
+   @SuppressWarnings("unchecked")
    protected void populateValues(ManagedObjectImpl managedObject, Serializable object)
    {
-      InstanceClassFactory icf = getInstanceFactory(object.getClass());
+      InstanceClassFactory icf = getInstanceClassFactory(object.getClass());
       Class moClass;
       try
       {
@@ -566,37 +577,7 @@
       }
       BeanInfo beanInfo = configuration.getBeanInfo(moClass);
 
-      Set<PropertyInfo> propertyInfos = beanInfo.getProperties();
-      if (propertyInfos != null && propertyInfos.isEmpty() == false)
-      {
-         for(PropertyInfo propertyInfo : propertyInfos)
-         {
-            ManagementRuntimeRef componentRef = propertyInfo.getUnderlyingAnnotation(ManagementRuntimeRef.class);
-            if (componentRef != null)
-            {
-               try
-               {
-                  Class<? extends RuntimeComponentNameTransformer> tClass = componentRef.transformer();
-                  RuntimeComponentNameTransformer transformer;
-                  if (tClass != ManagementRuntimeRef.DEFAULT_NAME_TRANSFORMER.class)
-                     transformer = getComponentNameTransformer(tClass);
-                  else
-                     transformer = getComponentNameTransformer(propertyInfo.getType().getType());
-
-                  Object value = propertyInfo.get(object);
-                  Object componentName = (transformer != null) ? transformer.transform(value) : value;
-
-                  managedObject.setComponentName(componentName);
-                  break;
-               }
-               catch (Throwable t)
-               {
-                  throw new UndeclaredThrowableException(t);
-               }
-            }
-         }
-      }
-
+      Object componentName = null;
       Map<String, ManagedProperty> properties = managedObject.getProperties();
       if (properties != null && properties.size() > 0)
       {
@@ -624,11 +605,69 @@
                String name = "" + svalue.getValue();
                managedObject.setName(name);
             }
+            ManagementRuntimeRef runtimeRef = (ManagementRuntimeRef) annotations.get(ManagementRuntimeRef.class.getName());
+            if (runtimeRef != null)
+            {
+               componentName = icf.getComponentName(beanInfo, property, object, value);
+               // let's try this as well
+               if (componentName == null && icf != this)
+                  componentName = getComponentName(beanInfo, property, object, value);
+            }
          }
       }
+      if (componentName == null)
+         componentName = icf.getComponentName(null, null, object, null);
+      // set it, even if it's null
+      managedObject.setComponentName(componentName);
    }
 
    /**
+    * Get the property name.
+    *
+    * @param property managed property
+    * @return property name
+    */
+   protected String getPropertyName(ManagedProperty property)
+   {
+      // First look to the mapped name
+      String name = property.getMappedName();
+      if (name == null)
+         property.getName();
+      return name;
+   }
+
+   public Object getComponentName(BeanInfo beanInfo, ManagedProperty property, Serializable object, MetaValue value)
+   {
+      if (beanInfo != null && property != null && value != null)
+      {
+         String name = getPropertyName(property);
+         PropertyInfo propertyInfo = beanInfo.getProperty(name);
+
+         ManagementRuntimeRef componentRef = propertyInfo.getUnderlyingAnnotation(ManagementRuntimeRef.class);
+         if (componentRef != null)
+         {
+            Object original = metaValueFactory.unwrap(value, propertyInfo.getType());
+            try
+            {
+               Class<? extends RuntimeComponentNameTransformer> tClass = componentRef.transformer();
+               RuntimeComponentNameTransformer transformer;
+               if (tClass != ManagementRuntimeRef.DEFAULT_NAME_TRANSFORMER.class)
+                  transformer = getComponentNameTransformer(configuration.getTypeInfo(tClass));
+               else
+                  transformer = getComponentNameTransformer(propertyInfo.getType());
+
+               return (transformer != null) ? transformer.transform(original) : original;
+            }
+            catch (Throwable t)
+            {
+               throw new UndeclaredThrowableException(t);
+            }
+         }
+      }
+      return null;
+   }
+
+   /**
     * Get a value
     * 
     * @param beanInfo the bean info
@@ -638,15 +677,9 @@
     */
    public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable object)
    {
-      // First look to the mapped name
-      String name = property.getMappedName();
-      if (name == null)
-         property.getName();
+      String name = getPropertyName(property);
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
 
-      PropertyInfo propertyInfo = beanInfo.getProperty(name);
-      if (propertyInfo == null)
-         throw new IllegalStateException("Unable to find property: " + name + " for " + object.getClass().getName());
-      
       Object value;
       try
       {
@@ -706,6 +739,30 @@
    }
 
    /**
+    * Set a value
+    *
+    * @param beanInfo the bean info
+    * @param property the property
+    * @param object the object
+    * @param the meta value
+    */
+   public void setValue(BeanInfo beanInfo, ManagedProperty property, Serializable object, MetaValue value)
+   {
+      String name = getPropertyName(property);
+      PropertyInfo propertyInfo = beanInfo.getProperty(name);
+
+      Object plainValue = metaValueFactory.unwrap(value, propertyInfo.getType());
+      try
+      {
+         propertyInfo.set(object, plainValue);
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   /**
     * 
     * @param methodInfo
     * @param opAnnotation
@@ -800,7 +857,8 @@
     * @param clazz the class
     * @return the InstanceClassFactory
     */
-   protected InstanceClassFactory getInstanceFactory(Class<?> clazz)
+   @SuppressWarnings("unchecked")
+   public <T extends Serializable> InstanceClassFactory<T> getInstanceClassFactory(Class<T> clazz)
    {
       synchronized (instanceFactories)
       {
@@ -808,29 +866,30 @@
          if (weak != null)
             return weak.get();
       }
-      return this;
+      return (InstanceClassFactory<T>)this;
    }
 
    /**
     * Get component name transformer.
     *
-    * @param clazz the transformer class
+    * @param type the type info
     * @return transformer instance
     * @throws Exception for any error
     */
-   protected RuntimeComponentNameTransformer getComponentNameTransformer(Class<?> clazz)
-         throws Exception
+   protected RuntimeComponentNameTransformer getComponentNameTransformer(TypeInfo type) throws Throwable
    {
       synchronized(transformers)
       {
-         WeakReference<RuntimeComponentNameTransformer> weak = transformers.get(clazz);
+         WeakReference<RuntimeComponentNameTransformer> weak = transformers.get(type);
          if (weak != null)
             return weak.get();
 
-         if (RuntimeComponentNameTransformer.class.isAssignableFrom(clazz))
+         TypeInfo rcntType = configuration.getTypeInfo(RuntimeComponentNameTransformer.class);
+         if (rcntType.isAssignableFrom(type))
          {
-            RuntimeComponentNameTransformer transformer = (RuntimeComponentNameTransformer)clazz.newInstance();
-            transformers.put(clazz, new WeakReference<RuntimeComponentNameTransformer>(transformer));
+            BeanInfo beanInfo = configuration.getBeanInfo(type);
+            RuntimeComponentNameTransformer transformer = (RuntimeComponentNameTransformer)beanInfo.newInstance();
+            transformers.put(type, new WeakReference<RuntimeComponentNameTransformer>(transformer));
             return transformer;
          }
 

Modified: 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	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/spi/factory/InstanceClassFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -30,20 +30,52 @@
 /**
  * A plugin for obtaining the class to scan for management object
  * related annotations.
- * 
+ *
+ * @param <T> actual attachment type
  * @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
  * @version $Revision$
  */
-public interface InstanceClassFactory
+public interface InstanceClassFactory<T>
 {
    /**
     * 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.
+    * @param attachment - the instance a ManagedObject is to be created for.
     * @return the Class that represents the root ManagedObject.
+    * @throws ClassNotFoundException if MO class not found
     */
-   public Class<? extends Serializable> getManagedObjectClass(Serializable instance)
-      throws ClassNotFoundException;
-   public MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, Serializable object);
+   Class<? extends Serializable> getManagedObjectClass(T attachment) throws ClassNotFoundException;
+
+   /**
+    * Get the value from object.
+    *
+    * @param beanInfo managed object's bean info
+    * @param property managed property
+    * @param attachment attachment
+    * @return meta value
+    */
+   MetaValue getValue(BeanInfo beanInfo, ManagedProperty property, T attachment);
+
+   /**
+    * Set the value to object.
+    *
+    * @param beanInfo managed object's bean info
+    * @param property managed property
+    * @param attachment attachment
+    * @param value meta value
+    */
+   void setValue(BeanInfo beanInfo, ManagedProperty property, T attachment, MetaValue value);
+
+   /**
+    * Get the runtime component name.
+    *
+    * @param beanInfo managed object's bean info
+    * @param property managed property
+    * @param attachment attachment
+    * @param value original value
+    * @return meta value
+    */
+   Object getComponentName(BeanInfo beanInfo, ManagedProperty property, T attachment, MetaValue value);
 }

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -81,4 +81,24 @@
     * @param builder the builder
     */
    public abstract void setBuilder(Class<?> clazz, MetaValueBuilder builder);
+
+   /**
+    * Unwrap meta value.
+    * Supports simple and generic meta value.
+    *
+    * @param metaValue meta value
+    * @param type the type
+    * @return meta value's value
+    */
+   public abstract Object unwrap(MetaValue metaValue, Type type);
+
+   /**
+    * Unwrap meta value.
+    * Supports simple and generic meta value.
+    *
+    * @param metaValue meta value
+    * @param type the type
+    * @return meta value's value
+    */
+   public abstract Object unwrap(MetaValue metaValue, TypeInfo type);
 }

Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-10-12 14:14:06 UTC (rev 66091)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java	2007-10-12 16:31:37 UTC (rev 66092)
@@ -25,6 +25,7 @@
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Array;
 import java.lang.reflect.Type;
+import java.lang.reflect.UndeclaredThrowableException;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Collection;
@@ -97,7 +98,6 @@
       {
          return new Stack<Map<Object, MetaValue>>();
       }
-      
    };
 
    /** The builders */
@@ -172,11 +172,14 @@
     * 
     * @param type - the primitive array class type info.
     * @param value - the primitive array instance.
-    * @return
+    * @return object array
     */
    public static Object[] convertPrimativeArray(TypeInfo type, Object value)
    {
-      Object[] oa = null;
+      if (value == null)
+         return null;
+
+      Object[] oa;
       if( type instanceof ArrayInfo )
       {
          // Get the Object form of the element
@@ -202,33 +205,19 @@
       
       return oa;      
    }
+
+   /**
+    * Transform a primitive array into an Object[]. Converts
+    * a primitive array like char[] to Object[].
+    *
+    * @param value - the primitive array instance.
+    * @return object array
+    */
    public static Object[] convertPrimativeArray(Object value)
    {
-      Object[] oa = null;
-      Class type = value.getClass();
-      if( type.isArray() )
-      {
-         // Get the Object form of the element
-         Class etype = type.getComponentType();
-         int size = Array.getLength(value);
-         oa = new Object[size];
-         for(int n = 0; n < size; n ++)
-         {
-            Object nvalue = Array.get(value, n);
-            // Recursively convert nested array elements
-            if (etype.isArray())
-            {
-               oa[n] = convertPrimativeArray(nvalue);
-            }
-            oa[n] = nvalue;
-         }
-      }
-      else
-      {
-         oa = (Object[]) value;
-      }
-      
-      return oa;      
+      if (value == null)
+         return null;
+      return convertPrimativeArray(configuration.getTypeInfo(value.getClass()), value);
    }
 
    /**
@@ -247,12 +236,12 @@
       ArrayValueSupport result = new ArrayValueSupport(type);
       mapping.put(value, result);
       
-      Object[] array = null;
+      Object[] array;
       
       MetaType elementType = type.getElementType();
       int dimension = type.getDimension();
       
-      Object[] oldArray = null;
+      Object[] oldArray;
       Class<?> componentType;
       try
       {
@@ -347,7 +336,6 @@
       CompositeValueSupport result = new CompositeValueSupport(type);
       mapping.put(value, result);
 
-      
       BeanInfo beanInfo;
       try
       {
@@ -365,7 +353,7 @@
       for (String name : type.keySet())
       {
          MetaType itemType = type.getType(name);
-         Object itemValue = null;
+         Object itemValue;
          try
          {
             itemValue = beanInfo.getProperty(value, name);
@@ -441,7 +429,62 @@
    {
       return internalCreate(value, type, null);
    }
-   
+
+   public Object unwrap(MetaValue metaValue, Type type)
+   {
+      TypeInfo typeInfo = configuration.getTypeInfo(type);
+      return unwrap(metaValue, typeInfo);
+   }
+
+   public Object unwrap(MetaValue metaValue, TypeInfo type)
+   {
+      if (metaValue == null)
+         return null;
+
+      if (type == null)
+         throw new IllegalArgumentException("Null type info.");
+
+      MetaType metaType = metaValue.getMetaType();
+      if (metaType.isTable() || metaType.isComposite())
+         throw new IllegalArgumentException("Cannot get value from " + metaValue + ", unsupported.");
+
+      if (metaType.isSimple())
+         return convertValue(((SimpleValue)metaValue).getValue(), type);
+      else if (metaType.isEnum())
+         return convertValue(((EnumValue)metaValue).getValue(), type);
+      else if (metaType.isGeneric())
+      {
+         // todo
+         return convertValue(((GenericValue)metaValue).getValue(), type);
+      }
+      else if (metaType.isArray())
+      {
+         ArrayValue arrayValue = (ArrayValue)metaValue;
+         Object array;
+         try
+         {
+            array = type.newArrayInstance(arrayValue.getLength());
+         }
+         catch (Throwable t)
+         {
+            throw new UndeclaredThrowableException(t);
+         }
+         for (int i = 0; i < Array.getLength(array); i++)
+         {
+            Object element = arrayValue.getValue(i);
+            if (element instanceof MetaValue)
+            {
+               TypeInfo elementType = configuration.getTypeInfo(element.getClass());
+               element = unwrap((MetaValue)element, elementType);
+            }
+            Array.set(array, i, element);
+         }
+         return array;
+      }
+
+      throw new IllegalArgumentException("Unsupported meta value: " + metaValue);
+   }
+
    /**
     * Create a meta value from the object
     * 
@@ -457,16 +500,19 @@
 
       if (type == null)
          type = configuration.getTypeInfo(value.getClass());
-      
-      boolean start = (metaType == null);
+
+      value = convertValue(value, type);
+
+      boolean start = false;
       if (metaType == null)
       {
+         start = true;
          metaType = metaTypeFactory.resolve(type);
       }
       
       // For more complicated values we need to keep a mapping of objects to meta values
       // this avoids duplicate meta value construction and recursion 
-      Map<Object, MetaValue> mapping = null;
+      Map<Object, MetaValue> mapping;
       if (start)
       {
          // This is the start of the mapping
@@ -512,8 +558,28 @@
             mappingStack.get().pop();
       }
    }
-   
+
    /**
+    * Convert the value.
+    *
+    * @param value the value
+    * @param typeInfo type info
+    * @return converted value if type info not null
+    * @throws UndeclaredThrowableException for any error
+    */
+   protected Object convertValue(Object value, TypeInfo typeInfo)
+   {
+      try
+      {
+         return typeInfo != null ? typeInfo.convertValue(value) : value;
+      }
+      catch (Throwable t)
+      {
+         throw new UndeclaredThrowableException(t);
+      }
+   }
+
+   /**
     * Check for a builder
     * 
     * @param metaType the meta type




More information about the jboss-cvs-commits mailing list