[jboss-cvs] JBossAS SVN: r70458 - in projects/microcontainer/trunk/managed/src: tests/org/jboss/test/managed/factory/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 5 21:58:29 EST 2008


Author: scott.stark at jboss.org
Date: 2008-03-05 21:58:29 -0500 (Wed, 05 Mar 2008)
New Revision: 70458

Added:
   projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/PropertyConfigurationAccess.java
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/DeploymentTemplateInfoFactory.java
   projects/microcontainer/trunk/managed/src/tests/org/jboss/test/managed/factory/test/DeploymentTemplateInfoUnitTestCase.java
Log:
JBMICROCONT-252, expand DeploymentTemplateInfoFactory to suppor creation of the template from the attachment class

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	2008-03-06 01:39:42 UTC (rev 70457)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/AbstractManagedObjectFactory.java	2008-03-06 02:58:29 UTC (rev 70458)
@@ -102,7 +102,7 @@
    private static final Logger log = Logger.getLogger(AbstractManagedObjectFactory.class);
 
    /** The configuration */
-   private static final Configuration configuration;
+   private static final Configuration configuration = PropertyConfigurationAccess.getConfiguration();
 
    /** The managed object meta type */
    public static final GenericMetaType MANAGED_OBJECT_META_TYPE = new GenericMetaType(ManagedObject.class.getName(), ManagedObject.class.getName());
@@ -122,17 +122,29 @@
    /** The instance to name transformers */
    private Map<TypeInfo, RuntimeComponentNameTransformer> transformers = new WeakHashMap<TypeInfo, RuntimeComponentNameTransformer>();
 
-   static
+   /**
+    * Create a ManagedProperty by looking to the factory for ctor(Fields)
+    * @param factory - the ManagedProperty implementation class
+    * @param fields - the fields to pass to the ctor
+    * @return the managed property if successful, null otherwise
+    */
+   public static ManagedProperty createManagedProperty(Class<? extends ManagedProperty> factory, Fields fields)
    {
-      configuration = AccessController.doPrivileged(new PrivilegedAction<Configuration>()
+      ManagedProperty property = null;
+      try
       {
-         public Configuration run()
-         {
-            return new PropertyConfiguration();
-         }
-      });
+         Class<?>[] sig = {Fields.class};
+         Constructor<? extends ManagedProperty> ctor = factory.getConstructor(sig);
+         Object[] args = {fields};
+         property = ctor.newInstance(args);
+      }
+      catch(Exception e)
+      {
+         log.debug("Failed to create ManagedProperty", e);
+      }
+      return property;
    }
-   
+
    @Override
    public <T extends Serializable> ManagedObject createManagedObject(Class<T> clazz)
    {
@@ -951,18 +963,6 @@
     */
    protected ManagedProperty getManagedProperty(Class<? extends ManagedProperty> factory, Fields fields)
    {
-      ManagedProperty property = null;
-      try
-      {
-         Class<?>[] sig = {Fields.class};
-         Constructor<? extends ManagedProperty> ctor = factory.getConstructor(sig);
-         Object[] args = {fields};
-         property = ctor.newInstance(args);
-      }
-      catch(Exception e)
-      {
-         log.debug("Failed to create ManagedProperty", e);
-      }
-      return property;
+      return createManagedProperty(factory, fields);
    }
 }

Modified: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/DeploymentTemplateInfoFactory.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/DeploymentTemplateInfoFactory.java	2008-03-06 01:39:42 UTC (rev 70457)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/DeploymentTemplateInfoFactory.java	2008-03-06 02:58:29 UTC (rev 70458)
@@ -21,24 +21,74 @@
  */
 package org.jboss.managed.plugins.factory;
 
+import java.io.Serializable;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.config.spi.Configuration;
+import org.jboss.logging.Logger;
 import org.jboss.managed.api.DeploymentTemplateInfo;
+import org.jboss.managed.api.Fields;
 import org.jboss.managed.api.ManagedObject;
 import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.api.annotation.ManagementConstants;
+import org.jboss.managed.api.annotation.ManagementObjectID;
+import org.jboss.managed.api.annotation.ManagementObjectRef;
 import org.jboss.managed.api.annotation.ManagementProperty;
+import org.jboss.managed.api.annotation.ManagementRuntimeRef;
 import org.jboss.managed.plugins.BasicDeploymentTemplateInfo;
+import org.jboss.managed.plugins.DefaultFieldsImpl;
+import org.jboss.managed.plugins.WritethroughManagedPropertyImpl;
+import org.jboss.managed.spi.factory.ManagedPropertyConstraintsPopulator;
+import org.jboss.managed.spi.factory.ManagedPropertyConstraintsPopulatorFactory;
+import org.jboss.metatype.api.types.ArrayMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
+import org.jboss.metatype.api.types.MetaType;
+import org.jboss.metatype.api.types.MetaTypeFactory;
+import org.jboss.metatype.api.values.MetaValueFactory;
+import org.jboss.reflect.spi.TypeInfo;
 
 /**
- * Creates the DeploymentTemplateInfo from the ManagedObject view.
+ * Creates a DeploymentTemplateInfo from a ManagedObject view, or
+ * the deployment attachment class
  * 
  * @author Scott.Stark at jboss.org
  * @version $Revision$
  */
 public class DeploymentTemplateInfoFactory
 {
+   private static final Logger log = Logger.getLogger(DeploymentTemplateInfoFactory.class);
+   /** The configuration */
+   private static final Configuration configuration = PropertyConfigurationAccess.getConfiguration();
+   /** The meta type factory */
+   private MetaTypeFactory metaTypeFactory = MetaTypeFactory.getInstance(); 
+
+   /** The meta value factory */
+   private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
+ 
+   
+   public MetaTypeFactory getMetaTypeFactory()
+   {
+      return metaTypeFactory;
+   }
+   public void setMetaTypeFactory(MetaTypeFactory metaTypeFactory)
+   {
+      this.metaTypeFactory = metaTypeFactory;
+   }
+   public MetaValueFactory getMetaValueFactory()
+   {
+      return metaValueFactory;
+   }
+   public void setMetaValueFactory(MetaValueFactory metaValueFactory)
+   {
+      this.metaValueFactory = metaValueFactory;
+   }
+
    /**
     * Create a DeploymentTemplateInfo from the ManagedObject view. This is
     * based on locating the ManagedPropertys with a ManagementProperty
@@ -69,4 +119,184 @@
       DeploymentTemplateInfo info = new BasicDeploymentTemplateInfo(name, description, infoProps);
       return info;
    }
+   /**
+    * Create a DeploymentTemplateInfo by scanning the metadata attachment
+    * class for ManagementProperty annotations.
+    * @param attachmentClass - the metadata class to scan for ManagementProperty annotations
+    * @param name - the template name
+    * @param description - the template description
+    * @return the DeploymentTemplateInfo instance
+    * @throws Exception on failure to create the DeploymentTemplateInfo
+    */
+   public DeploymentTemplateInfo createTemplateInfo(Class<?> attachmentClass, String name,
+         String description)
+      throws Exception
+   {
+      return createTemplateInfo(BasicDeploymentTemplateInfo.class, attachmentClass, name,
+         description);
+   }
+   /**
+    * Create a DeploymentTemplateInfo by scanning the metadata attachment
+    * class for ManagementProperty annotations.
+    * @param infoClass - the DeploymentTemplateInfo implementation to use. Must
+    * have a ctor with sig (String,String,Map).
+    * @param attachmentClass - the metadata class to scan for ManagementProperty annotations
+    * @param name - the template name
+    * @param description - the template description
+    * @return the DeploymentTemplateInfo instance
+    * @throws Exception on failure to create the DeploymentTemplateInfo
+    */
+   public DeploymentTemplateInfo createTemplateInfo(Class<? extends DeploymentTemplateInfo> infoClass,
+         Class<?> attachmentClass, String name,
+         String description)
+      throws Exception
+   {      
+      BeanInfo beanInfo = configuration.getBeanInfo(attachmentClass);
+      Map<String, ManagedProperty> infoProps = new HashMap<String, ManagedProperty>();
+      Set<PropertyInfo> propertyInfos = beanInfo.getProperties();
+      if (propertyInfos != null && propertyInfos.isEmpty() == false)
+      {
+         for (PropertyInfo propertyInfo : propertyInfos)
+         {
+            ManagementProperty managementProperty = propertyInfo.getUnderlyingAnnotation(ManagementProperty.class);
+            if(managementProperty != null && managementProperty.includeInTemplate())
+            {
+               ManagedProperty mp = createProperty(propertyInfo, managementProperty);
+               infoProps.put(mp.getName(), mp);
+            }
+         }
+      }
+      Class[] parameterTypes = {String.class, String.class, Map.class};
+      Constructor<? extends DeploymentTemplateInfo> ctor = infoClass.getConstructor(parameterTypes);
+      DeploymentTemplateInfo info = ctor.newInstance(name, description, infoProps);
+      return info;
+   }
+
+   protected ManagedProperty createProperty(PropertyInfo propertyInfo, ManagementProperty managementProperty)
+   {
+      boolean trace = log.isTraceEnabled();
+      ManagedProperty property = null;
+      // Check for a simple property
+      boolean includeProperty = (managementProperty.ignored() == false);
+      if (includeProperty)
+      {
+         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>();
+         propAnnotations.put(ManagementProperty.class.getName(), managementProperty);
+
+         Fields fields = null;
+         if (managementProperty != null)
+         {
+            Class<? extends Fields> factory = managementProperty.fieldsFactory();
+            if (factory != ManagementProperty.NULL_FIELDS_FACTORY.class)
+            {
+               try
+               {
+                  fields = factory.newInstance();
+               }
+               catch (Exception e)
+               {
+                  log.debug("Failed to created Fields", e);
+               }
+            }
+         }
+         if (fields == null)
+            fields = new DefaultFieldsImpl();
+
+         if( propertyInfo instanceof Serializable )
+         {
+            Serializable info = Serializable.class.cast(propertyInfo);
+            fields.setField(Fields.PROPERTY_INFO, info);
+         }
+
+         String propertyName = propertyInfo.getName();
+         if (managementProperty != null)
+            propertyName = managementProperty.name();
+         if( propertyName.length() == 0 )
+            propertyName = propertyInfo.getName();
+         fields.setField(Fields.NAME, propertyName);
+
+         // This should probably always the the propertyInfo name?
+         String mappedName = propertyInfo.getName();
+         if (managementProperty != null)
+            mappedName = managementProperty.mappedName();
+         if( mappedName.length() == 0 )
+            mappedName = propertyInfo.getName();
+         fields.setField(Fields.MAPPED_NAME, mappedName);
+
+         String description = ManagementConstants.GENERATED;
+         if (managementProperty != null)
+            description = managementProperty.description();
+         if (description.equals(ManagementConstants.GENERATED))
+            description = propertyName;
+         fields.setField(Fields.DESCRIPTION, description);
+
+         if (trace)
+         {
+            log.trace("Building MangedProperty(name="+propertyName
+                  +",mappedName="+mappedName
+                  +") ,annotations="+propAnnotations);
+         }
+
+         boolean mandatory = false;
+         if (managementProperty != null)
+            mandatory = managementProperty.mandatory();
+         if (mandatory)
+            fields.setField(Fields.MANDATORY, Boolean.TRUE);
+         
+         boolean managed = false;
+         if (managementProperty != null)
+            managed = managementProperty.managed();
+         
+         MetaType metaType;
+         if (managed)
+         {
+            TypeInfo typeInfo = propertyInfo.getType();
+            if(typeInfo.isArray())
+               metaType = new ArrayMetaType(1, AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE);
+            else if (typeInfo.isCollection())
+               metaType = new CollectionMetaType(typeInfo.getName(), AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE);
+            else
+               metaType = AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE;
+         }
+         else
+         {
+            metaType = metaTypeFactory.resolve(propertyInfo.getType());
+         }
+         fields.setField(Fields.META_TYPE, metaType);
+         if (propAnnotations.isEmpty() == false)
+            fields.setField(Fields.ANNOTATIONS, propAnnotations);
+
+         // Delegate others (legal values, min/max etc.) to the constraints factory
+         try
+         {
+            Class<? extends ManagedPropertyConstraintsPopulatorFactory> factoryClass = managementProperty.constraintsFactory();
+            ManagedPropertyConstraintsPopulatorFactory factory = factoryClass.newInstance();
+            ManagedPropertyConstraintsPopulator populator = factory.newInstance();
+            if (populator != null)
+            {
+               Class clazz = propertyInfo.getBeanInfo().getClassInfo().getType();
+               populator.populateManagedProperty(clazz, propertyInfo, fields);
+            }
+         }
+         catch(Exception e)
+         {
+            log.debug("Failed to populate constraints for: "+propertyInfo, e);
+         }
+
+         if (managementProperty != null)
+         {
+            Class<? extends ManagedProperty> factory = managementProperty.propertyFactory();
+            if (factory != ManagementProperty.NULL_PROPERTY_FACTORY.class)
+               property = AbstractManagedObjectFactory.createManagedProperty(factory, fields);
+         }
+         // we should have write-through by default
+         // use factory to change this default behavior
+         if (property == null)
+            property = new WritethroughManagedPropertyImpl(fields, metaValueFactory, null);
+      }
+      return property;
+   }
 }

Added: projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/PropertyConfigurationAccess.java
===================================================================
--- projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/PropertyConfigurationAccess.java	                        (rev 0)
+++ projects/microcontainer/trunk/managed/src/main/org/jboss/managed/plugins/factory/PropertyConfigurationAccess.java	2008-03-06 02:58:29 UTC (rev 70458)
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, 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.plugins.factory;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import org.jboss.config.plugins.property.PropertyConfiguration;
+import org.jboss.config.spi.Configuration;
+
+/**
+ * A package protected PrivilegedAction<Configuration> impl
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision$
+ */
+class PropertyConfigurationAccess
+   implements PrivilegedAction<Configuration>
+{
+   public Configuration run()
+   {
+      return new PropertyConfiguration();
+   }
+   static Configuration getConfiguration()
+   {
+      SecurityManager secMgr = System.getSecurityManager();
+      Configuration config = null;
+      if(secMgr != null)
+      {
+         PropertyConfigurationAccess pca = new PropertyConfigurationAccess();
+         config = AccessController.doPrivileged(pca);
+      }
+      else
+      {
+         config = new PropertyConfiguration();
+      }
+      return config;
+   }
+}


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

Modified: projects/microcontainer/trunk/managed/src/tests/org/jboss/test/managed/factory/test/DeploymentTemplateInfoUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/managed/src/tests/org/jboss/test/managed/factory/test/DeploymentTemplateInfoUnitTestCase.java	2008-03-06 01:39:42 UTC (rev 70457)
+++ projects/microcontainer/trunk/managed/src/tests/org/jboss/test/managed/factory/test/DeploymentTemplateInfoUnitTestCase.java	2008-03-06 02:58:29 UTC (rev 70458)
@@ -73,6 +73,15 @@
       assertEquals("testExplicit", info.getDescription());
       assertTrue("property1 is in template info", info.getProperties().containsKey("property1"));
    }
-   
+   public void testReflectionOfExplicit()
+      throws Exception
+   {
+      DeploymentTemplateInfoFactory factory = new DeploymentTemplateInfoFactory();
+      DeploymentTemplateInfo info = factory.createTemplateInfo(ManagementObjectExplicit.class, "testReflectionOfExplicit", "testReflectionOfExplicit");
+      log.info(info);
+      assertEquals("testReflectionOfExplicit", info.getName());
+      assertEquals("testReflectionOfExplicit", info.getDescription());
+      assertTrue("property1 is in template info", info.getProperties().containsKey("property1"));      
+   }
 
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list