[jboss-cvs] JBossAS SVN: r81824 - in trunk/system/src/main/org/jboss/system/server/profileservice/persistence: deployer and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Nov 30 09:29:54 EST 2008


Author: emuckenhuber
Date: 2008-11-30 09:29:54 -0500 (Sun, 30 Nov 2008)
New Revision: 81824

Added:
   trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java
   trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java
   trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/
   trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java
Log:
[JBAS-3768] managedObject persistence handler

Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java	                        (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java	2008-11-30 14:29:54 UTC (rev 81824)
@@ -0,0 +1,418 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.system.server.profileservice.persistence;
+
+import java.util.Set;
+
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.logging.Logger;
+import org.jboss.managed.api.Fields;
+import org.jboss.managed.api.ManagedObject;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.api.annotation.ViewUse;
+import org.jboss.metatype.api.types.ArrayMetaType;
+import org.jboss.metatype.api.types.CompositeMetaType;
+import org.jboss.metatype.api.types.MetaType;
+import org.jboss.metatype.api.values.ArrayValue;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.CompositeValue;
+import org.jboss.metatype.api.values.EnumValue;
+import org.jboss.metatype.api.values.GenericValue;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+import org.jboss.metatype.api.values.SimpleValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedArrayValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedCollectionValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedCompositeValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedEnumValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedGenericValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedProperty;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedSimpleValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedTableValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedValue;
+
+/**
+ * Create a xml representation of a Managed Object.
+ * 
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ManagedObjectPeristenceHandler
+{
+   /** The meta value factory. */
+   MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
+   
+   /** The logger. */
+   private static final Logger log = Logger.getLogger(ManagedObjectPeristenceHandler.class);
+   
+   /**
+    * Process a managed object.
+    * 
+    * @param moElement the xml meta data.
+    * @param mo the managed object
+    * @return isModified
+    */
+   public boolean processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
+   {
+      if(moElement == null)
+         throw new IllegalArgumentException("Null rootElement.");
+      if(mo == null)
+         throw new IllegalArgumentException("null managedObject");
+      
+      moElement.setName(mo.getName());
+      moElement.setClassName(mo.getAttachmentName());
+      
+      // Store the original name, if it does not exist.
+      if(moElement.getOriginalName() == null)
+         moElement.setOriginalName(mo.getName());
+      
+      boolean changed = false;
+      Set<String> propertyNames = mo.getPropertyNames();
+      for(String propertyName : propertyNames)
+      {
+         ManagedProperty property = mo.getProperty(propertyName);
+         if(property == null)
+            throw new IllegalStateException("unable to find propery: "+ property);
+         
+         if(! property.hasViewUse(ViewUse.CONFIGURATION))
+            continue;
+         
+         // getProperty
+         PersistedProperty propertyElement = moElement.get(propertyName);
+         if(propertyElement == null)
+            propertyElement = new PersistedProperty(property.getName());
+         boolean propertyChanged = processManagedProperty(propertyElement, propertyName, property);
+         
+         if(propertyChanged)
+         {
+            changed = true;
+            moElement.put(propertyName, propertyElement);
+         }
+      }
+      return changed;
+   }
+   
+   /**
+    * Process a ManagedProperty.
+    * 
+    * @param persistedProperty the xml property meta data.
+    * @param name the property name.
+    * @param property the managed property.
+    * @return isModified.
+    */
+   protected boolean processManagedProperty(PersistedProperty persistedProperty, String name, ManagedProperty property)
+   {
+      boolean trace = log.isTraceEnabled();
+      PropertyInfo propertyInfo = property.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
+      // Skip not readable properties
+      if(propertyInfo != null && propertyInfo.isReadable() == false)
+      {
+         if(trace)
+            log.trace("property "+ name + "is not readable");
+         return false;
+      }
+      
+      // TODO Check if property was modified
+      // MetaType metaType = property.getMetaType();
+      // if( ! metaType.isCollection() && ! metaType.isArray() )
+      // {
+      //    if(property.isModified() == false)
+      //       return false;
+      // }
+      
+      // Get MetaValue
+      MetaValue value = property.getValue();
+      // Check if there is a previous persisted property value
+      PersistedValue persistedValue = persistedProperty.getValue();
+      // Process
+      boolean changed = false;
+      if(value != null)
+      {
+         // Create a new value, if needed
+         if(persistedValue == null)
+            persistedValue = createPersistedValue(value.getMetaType());
+         // Process
+         changed = processMetaValue(persistedValue, value);
+      }
+      
+      persistedProperty.setValue(persistedValue);
+      
+      return changed;
+   }
+   
+   /**
+    * Process a MetaValue.
+    * 
+    * @param valueElement the xml metadata.
+    * @param value the meta value.
+    * @return isModified.
+    */
+   protected boolean processMetaValue(PersistedValue valueElement, MetaValue value)
+   {
+      // FIXME a MetaValue should never be null ?
+      if(value == null)
+         return false;
+      
+      // TODO we need to know if the MetaValue was modified too.
+      boolean changed = false;
+      MetaType metaType = value.getMetaType();
+      
+      if(metaType.isSimple())
+      {
+         changed = processSimpleValue((PersistedSimpleValue) valueElement, (SimpleValue) value);
+      }
+      else if(metaType.isEnum())
+      {
+         changed = processEnumValue((PersistedEnumValue) valueElement, (EnumValue) value);
+      }
+      else if(metaType.isCollection())
+      {
+         changed = processCollectionValue((PersistedCollectionValue) valueElement, (CollectionValue) value);
+      }
+      else if(metaType.isGeneric())
+      {
+         changed = processGenericValue((PersistedGenericValue) valueElement, (GenericValue) value);
+      }
+      else if(metaType.isComposite())
+      {
+         changed = processCompositeValue((PersistedCompositeValue) valueElement, (CompositeValue) value);
+      }
+      else if(metaType.isArray())
+      {
+         changed = processArrayValue((PersistedArrayValue) valueElement, (ArrayValue) value);
+      }
+      else if(metaType.isTable())
+      {
+         throw new UnsupportedOperationException("Persisting a table value.");
+      }
+      else
+      {
+         throw new IllegalStateException("unknown metaType");
+      }
+      return changed;
+   }
+   
+   /**
+    * Process a simple value.
+    * 
+    * @param valueElement the xml meta data.
+    * @param value the simple value.
+    * @return isModified.
+    */
+   protected boolean processSimpleValue(PersistedSimpleValue valueElement, SimpleValue value)
+   {
+      valueElement.setValue(convertSimple2String(value));
+      return true;
+   }
+   
+   /**
+    * Process a enumValue.
+    * 
+    * @param enumElement the xml meta data.
+    * @param value the enum value.
+    */
+   protected boolean processEnumValue(PersistedEnumValue enumElement, EnumValue value)
+   {
+      enumElement.setValue(value.getValue());
+      return true;
+   }
+   
+   /**
+    * Process a collection value.
+    * 
+    * @param valueElement the xml meta data.
+    * @param value the collection value.
+    * @return isModified.
+    */
+   protected boolean processCollectionValue(PersistedCollectionValue valueElement, CollectionValue value)
+   {
+      boolean changed = false;
+      for(MetaValue child : value.getElements())
+      {
+         PersistedValue persistedValue = createPersistedValue(child.getMetaType());
+         
+         if( processMetaValue(persistedValue, child) == true)
+            changed = true;
+         
+         valueElement.addValue(persistedValue);
+      }
+      return changed;
+   }
+   
+   /**
+    * Process a generic value.
+    * 
+    * @param genericElement the xml metadata.
+    * @param value the generic value.
+    * @return isModified.
+    */
+   protected boolean processGenericValue(PersistedGenericValue genericElement, GenericValue value)
+   {
+      Object o = value.getValue();
+      if(o == null)
+         return false;
+      
+      boolean changed = false;
+      if(o instanceof ManagedObject)
+      {
+         PersistedManagedObject mo = new PersistedManagedObject();
+         if(processManagedObject(mo, (ManagedObject) o) == true)
+            changed = true;
+         genericElement.setManagedObject(mo);
+         return changed;
+      }
+      else
+      {
+         throw new IllegalStateException("The value of GenericValue must be a ManagedObject: " + value);
+      }
+   }
+   
+   /**
+    * Process the array value.
+    * 
+    * @param array the xml meta data.
+    * @param value the array value.
+    * @return isModified.
+    */
+   protected boolean processArrayValue(PersistedArrayValue array, ArrayValue value)
+   {
+      ArrayMetaType metaType = value.getMetaType();
+      
+      if(! metaType.getElementType().isSimple())
+         throw new UnsupportedOperationException("Persisting a non primitive array.");
+         
+      boolean changed = false;
+      for(int i = 0; i < value.getLength(); i++)
+      {
+         MetaValue metaValue = (MetaValue) value.getValue(i);
+         PersistedValue persistedValue = createPersistedValue(metaType.getElementType());
+         
+         if(processMetaValue(persistedValue, metaValue) == true)
+            changed = true;
+         
+         array.addValue(persistedValue);
+      }
+      return changed;
+   }
+   
+   /**
+    * Process a composite value.
+    * 
+    * @param composite the xml meta data.
+    * @param value the composite value.
+    * @return isModified.
+    */
+   protected boolean processCompositeValue(PersistedCompositeValue composite, CompositeValue value)
+   {
+      CompositeMetaType metaType = value.getMetaType();
+      
+      boolean changed = false;
+      for(String item : metaType.itemSet())
+      {
+         MetaValue itemValue = value.get(item);
+         
+         // FIXME a value should never be null ?
+         MetaType itemType = null;
+         if(itemValue != null)
+         {
+            itemType = itemValue.getMetaType();
+         }
+         else
+         {
+            itemType = metaType.getType(item);
+         }
+         
+         PersistedValue persistedValue = createPersistedValue(itemType);
+         persistedValue.setName(item);
+         
+         if(processMetaValue(persistedValue, itemValue) == true)
+            changed = true;
+         
+         composite.put(item, persistedValue);
+      }
+      return changed;
+   }
+  
+   /**
+    * Create an empty xml value representation. 
+    * 
+    * @param metaType the meta type.
+    * @return a empty xml meta data, based on the meta type.
+    */
+   protected static PersistedValue createPersistedValue(MetaType metaType)
+   {
+      if(metaType.isSimple())
+      {
+         return new PersistedSimpleValue(); 
+      }
+      else if(metaType.isEnum())
+      {
+         return new PersistedEnumValue();
+      }
+      else if(metaType.isCollection())
+      {
+         return new PersistedCollectionValue();
+      }
+      else if(metaType.isGeneric())
+      {
+         return new PersistedGenericValue();
+      }
+      else if(metaType.isComposite())
+      {
+         return new PersistedCompositeValue();
+      }
+      else if(metaType.isTable())
+      {
+         return new PersistedTableValue();
+      }
+      else if(metaType.isArray())
+      {
+         return new PersistedArrayValue();
+      }
+      else
+      {
+         throw new IllegalStateException("unknown metaType");
+      }
+   }
+   
+   /**
+    * Convert a simple meta value to a String.
+    * 
+    * @param value the simple meta value.
+    * @return the string.
+    */
+   protected String convertSimple2String(SimpleValue value)
+   {       
+      // TODO a metaValue should never be null?
+      if(value == null)
+         return null;
+      
+      Object unwrappedValue = metaValueFactory.unwrap(value);
+      
+      if(unwrappedValue == null)
+         return null; 
+      
+      return ("" + unwrappedValue);
+   }
+   
+}
\ No newline at end of file

Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java	                        (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java	2008-11-30 14:29:54 UTC (rev 81824)
@@ -0,0 +1,548 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.system.server.profileservice.persistence;
+
+import java.io.Serializable;
+import java.lang.reflect.Proxy;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+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.logging.Logger;
+import org.jboss.managed.api.Fields;
+import org.jboss.managed.api.ManagedObject;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.api.annotation.ViewUse;
+import org.jboss.managed.api.factory.ManagedObjectFactory;
+import org.jboss.managed.spi.factory.InstanceClassFactory;
+import org.jboss.metatype.api.types.ArrayMetaType;
+import org.jboss.metatype.api.types.CollectionMetaType;
+import org.jboss.metatype.api.types.CompositeMetaType;
+import org.jboss.metatype.api.types.EnumMetaType;
+import org.jboss.metatype.api.types.GenericMetaType;
+import org.jboss.metatype.api.types.MetaType;
+import org.jboss.metatype.api.types.Name;
+import org.jboss.metatype.api.types.SimpleMetaType;
+import org.jboss.metatype.api.types.TableMetaType;
+import org.jboss.metatype.api.values.ArrayValue;
+import org.jboss.metatype.api.values.ArrayValueSupport;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.CollectionValueSupport;
+import org.jboss.metatype.api.values.CompositeValue;
+import org.jboss.metatype.api.values.CompositeValueSupport;
+import org.jboss.metatype.api.values.EnumValue;
+import org.jboss.metatype.api.values.EnumValueSupport;
+import org.jboss.metatype.api.values.GenericValue;
+import org.jboss.metatype.api.values.GenericValueSupport;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+import org.jboss.metatype.api.values.SimpleValue;
+import org.jboss.metatype.api.values.SimpleValueSupport;
+import org.jboss.metatype.api.values.TableValueSupport;
+import org.jboss.metatype.plugins.types.StringName;
+import org.jboss.reflect.plugins.ValueConvertor;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedArrayValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedCollectionValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedCompositeValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedEnumValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedGenericValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedProperty;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedSimpleValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedValue;
+
+
+/**
+ * Handler for merging the persisted information with a ManagedObject.
+ * 
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ManagedObjectUpdateHandler
+{
+   
+   /** The managed object factory. */
+   ManagedObjectFactory managedObjectFactory = ManagedObjectFactory.getInstance();
+   
+   /** The meta value factory. */
+   MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
+   
+   /** The simple types. */
+   private static final Map<String, Class<? extends Serializable>> simpleTypes = new HashMap<String, Class<? extends Serializable>>();
+   
+   /** The logger. */
+   private final static Logger log = Logger.getLogger(ManagedObjectUpdateHandler.class);
+   
+   static
+   {
+      // Fill simple types map.
+      simpleTypes.put(BigDecimal.class.getName(), BigDecimal.class);
+      simpleTypes.put(BigInteger.class.getName(), BigInteger.class);
+      simpleTypes.put(Boolean.class.getName(), Boolean.class);
+      simpleTypes.put(Byte.class.getName(), Byte.class);
+      simpleTypes.put(Character.class.getName(), Character.class);
+      simpleTypes.put(Date.class.getName(), Date.class);
+      simpleTypes.put(Double.class.getName(), Double.class);
+      simpleTypes.put(Float.class.getName(), Float.class);
+      simpleTypes.put(Integer.class.getName(), Integer.class);
+      simpleTypes.put(Long.class.getName(), Long.class);
+      simpleTypes.put(Short.class.getName(), Short.class);
+      simpleTypes.put(String.class.getName(), String.class);
+      simpleTypes.put(Name.class.getName(), Name.class);
+   }
+   
+   /**
+    * Process a ManagedObject.
+    * 
+    * @param moElement the persisted xml meta data.
+    * @param mo the managed object
+    * @return the attachment meta data.
+    */
+   public Object processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
+   {
+      if(moElement == null)
+         throw new IllegalArgumentException("Null rootElement.");
+      if(mo == null)
+         throw new IllegalArgumentException("null managedObject");
+
+      Object attachment = mo.getAttachment();
+
+      // Get the xml to see what we need to merge
+      Set<String> propertyNames = moElement.keySet();
+      for(String propertyName : propertyNames)
+      {
+         ManagedProperty property = mo.getProperty(propertyName);
+         if(property == null)
+            throw new IllegalStateException("unable to find propery: "+ property);
+         
+         // Skip statistic - although they should not be persisted anyway
+         if(property.hasViewUse(ViewUse.STATISTIC))
+            continue;
+     
+         // getProperty
+         PersistedProperty propertyElement = moElement.get(propertyName);
+         processManagedProperty(propertyElement, propertyName, property, attachment);
+      }
+      return attachment;
+   }
+   
+   /**
+    * Process a ManagedProperty.
+    * 
+    * @param propertyElement the persisted xml meta data.
+    * @param name the property name.
+    * @param property the managed property.
+    * @param attachment the managed object attachment.
+    */
+   protected void processManagedProperty(PersistedProperty propertyElement, String name, ManagedProperty property, Object attachment)
+   {
+      boolean trace = log.isTraceEnabled();
+      PropertyInfo propertyInfo = property.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
+      
+      // Skip not readable properties
+      if(propertyInfo != null && propertyInfo.isReadable() == false)
+      {
+         if(trace)
+            log.trace("property "+ name + "is not readable");
+         return;
+      }
+     
+      // Get MetaValue
+      MetaValue value = property.getValue();
+      if(value == null)
+      {
+         value = createEmptyMetaValue(property.getMetaType());
+      }
+      
+      // Get the xml value
+      PersistedValue persistedValue = propertyElement.getValue();
+      // Skip null values
+      if(value != null && persistedValue != null)
+      {
+         // Process the meta value.
+         MetaValue merged = processMetaValue(persistedValue, value);
+         // If merged != null
+         if(merged != null)
+         {
+            // TODO Can this be null anyway ?
+            if(propertyInfo == null)
+               return;
+            // throw new IllegalStateException("null propertyInfo"); 
+            
+            // Skip not writable properties
+            if(propertyInfo.isWritable() == false)
+            {
+               if(trace)
+                  log.trace("property "+ name + "is not writable");
+               return;
+            }
+            
+            // FIXME Ignore some metaTypes for now
+            MetaType metaType = merged.getMetaType();
+            if( ! metaType.isCollection() 
+                  && ! metaType.isArray() 
+                  && ! metaType.isTable() )
+            {
+               
+               try
+               {
+                  // set the value Field
+                  property.setField(Fields.VALUE, merged);
+                  
+                  // FIXME skip CompositeValueInvocationHandler
+                  if( metaType.isComposite() )
+                  {
+                     // unwrap
+                     Object unwrapped = metaValueFactory.unwrap(merged, propertyInfo.getType());
+                     if(Proxy.isProxyClass(unwrapped.getClass()))
+                        return;                     
+                  }
+                  
+                  // Set value
+                  InstanceClassFactory icf = managedObjectFactory.getInstanceClassFactory(attachment.getClass(), null);
+                  BeanInfo beanInfo = propertyInfo.getBeanInfo();
+                  icf.setValue(beanInfo, property, attachment, merged);
+
+               }
+               catch(Throwable t)
+               {
+                  log.debug("failed to set value to property: "+ propertyInfo, t);
+               }
+            }
+         }
+      }
+      else 
+      {
+         return;
+      }
+   }
+   
+   /**
+    * Process a MetaValue.
+    * 
+    * @param valueElement the persisted xml meta data.
+    * @param value the meta value.
+    * @return a meta value.
+    */
+   protected MetaValue processMetaValue(PersistedValue valueElement, MetaValue value)
+   {
+      MetaType metaType = value.getMetaType();
+      
+      MetaValue metaValue = null;
+      if(metaType.isSimple())
+      {
+         metaValue = processSimpleValue(
+               (PersistedSimpleValue) valueElement,
+               (SimpleValue) value);
+      }
+      else if(metaType.isEnum())
+      {
+         metaValue = processEnumValue(
+               (PersistedEnumValue) valueElement,
+               (EnumValue) value);
+      }
+      else if(metaType.isCollection())
+      {
+         metaValue = processCollectionValue(
+               (PersistedCollectionValue) valueElement,
+               (CollectionValue) value);
+      }
+      else if(metaType.isGeneric())
+      {
+         metaValue = processGenericValue(
+               (PersistedGenericValue) valueElement,
+               (GenericValue) value);
+      }
+      else if(metaType.isComposite())
+      {
+         metaValue = processCompositeValue(
+               (PersistedCompositeValue) valueElement,
+               (CompositeValue) value);
+      }
+      else if(metaType.isTable())
+      {
+         // FIXME process Table 
+      }
+      else if(metaType.isArray())
+      {
+         metaValue = processArrayValue(
+               (PersistedArrayValue) valueElement,
+               (ArrayValue) value);
+      }
+      else
+      {
+         throw new IllegalStateException("unknown metaType");
+      }
+      return metaValue;
+   }
+   
+   /**
+    * Process an Enum value.
+    * 
+    * @param enumElement the persisted xml meta data.
+    * @param value the enum value.
+    * @return a enum value.
+    */
+   protected EnumValue processEnumValue(PersistedEnumValue enumElement, EnumValue value)
+   {
+      return new EnumValueSupport(value.getMetaType(), enumElement.getValue());
+   }
+   
+   /**
+    * Process a collection.
+    * TODO - support merging of collections.
+    * 
+    * @param collection the persisted xml meta data.
+    * @param value the collection value.
+    * @return a collection value.
+    */
+   protected CollectionValue processCollectionValue(PersistedCollectionValue collection, CollectionValue value)
+   {
+      if(collection.size() == 0)
+         return value;
+
+      // FIXME merge collections
+      if(collection.size() != value.getSize())
+      {
+         log.warn("unable to merge collection: " + value);
+         return value;
+      }
+      
+      ArrayList<MetaValue> elementList = new ArrayList<MetaValue>();
+      Iterator<PersistedValue> i = collection.getValues().iterator();
+      for(MetaValue item : value)
+      {
+         MetaValue newValue = processMetaValue(i.next(), item);
+         if(newValue != null)
+         {
+            MetaType metaType = newValue.getMetaType();
+            
+            // FIXME unwrap a managedObject
+            if( metaType.isGeneric() )
+            {
+               ManagedObject mo = (ManagedObject) ((GenericValue) newValue).getValue();
+               // Use the attachment instead of the MO, to avoid a CCE
+               newValue = new GenericValueSupport(
+                     new GenericMetaType(mo.getAttachmentName(), "mo"),
+                     mo.getAttachmentName()
+                     );
+            }
+            
+            elementList.add(newValue);
+         }
+      }
+      return new CollectionValueSupport(value.getMetaType(), elementList.toArray(new MetaValue[elementList.size()]));
+   }
+   
+   /**
+    * Process a GenericValue.
+    * 
+    * @param genericElement the persisted xml meta data.
+    * @param value the generic value.
+    * @return a generic value.
+    */
+   protected MetaValue processGenericValue(PersistedGenericValue genericElement, GenericValue value)
+   {
+      PersistedManagedObject po = genericElement.getManagedObject();
+      if(po == null)
+         return value;
+
+      if(value.getValue() instanceof ManagedObject)
+      {
+         ManagedObject mo = (ManagedObject) value.getValue();
+         processManagedObject(po, mo);
+         
+         return value;
+      }
+      else
+      {
+         throw new IllegalStateException("The value of GenericValue must be a ManagedObject: " + value);
+      }
+   }
+   
+   /**
+    * Process composite value.
+    * 
+    * @param composite the persisted xml meta data.
+    * @param value the composite value.
+    * @return a composite value.
+    */
+   protected CompositeValue processCompositeValue(PersistedCompositeValue composite, CompositeValue value)
+   {
+      CompositeMetaType metaType = value.getMetaType();
+      Map<String, MetaValue> values = new HashMap<String, MetaValue>();
+      
+      for(String key : composite.keySet())
+      {
+         MetaValue metaValue = value.get(key);
+         if(metaValue == null)
+         {
+            metaValue = createEmptyMetaValue(metaType.getType(key));
+         }
+         PersistedValue persistedValue = composite.get(key);
+
+         metaValue = processMetaValue(persistedValue, metaValue);
+         
+         values.put(key, metaValue);
+      }
+      return new CompositeValueSupport(metaType, values);
+   }
+   
+   /**
+    * process an arrayValue.
+    * FIXME - support merging of arrays.
+    * 
+    * @param array the persisted xml meta data.
+    * @param value the array value.
+    * @return a array value.
+    */
+   protected ArrayValue processArrayValue(PersistedArrayValue array, ArrayValue value)
+   {
+      // FIXME 
+      if(array.size() != value.getLength())
+      {
+         log.debug("cannot merge array: " + value);
+         return null; 
+      }
+      
+      ArrayList<MetaValue> arrayList = new ArrayList<MetaValue>();
+      for(int i = 0; i < value.getLength(); i++)
+      {
+         PersistedValue persisted = array.getValue(i);
+         MetaValue restored = processMetaValue(persisted, (MetaValue) value.getValue(i));
+         if(restored != null)
+            arrayList.add(restored);
+      }
+
+      return new ArrayValueSupport(value.getMetaType(), arrayList.toArray(new MetaValue[arrayList.size()]));
+   }
+   
+   /**
+    * Process simple value.
+    * 
+    * @param valueElement the persisted xml meta data.
+    * @param value the simple value.
+    * @return a simple value.
+    */
+   protected SimpleValue processSimpleValue(PersistedSimpleValue valueElement, SimpleValue value)
+   {
+      SimpleMetaType metaType = value.getMetaType();
+      String elementValue = valueElement.getValue();
+      
+      Serializable converted = null;
+      if(elementValue != null)
+      {
+         if(metaType.equals(SimpleMetaType.STRING))
+         {
+            converted = (String) elementValue;
+         }
+         else if (metaType.equals(SimpleMetaType.NAMEDOBJECT))
+         {
+            converted = new StringName(elementValue);
+         }
+         else if (metaType.equals(SimpleMetaType.VOID))
+         {
+            // 
+         }
+         else
+         {
+            converted = convert2Type(metaType.getTypeName(), elementValue);
+         }
+      }
+      return SimpleValueSupport.wrap(converted);
+   }
+ 
+   /**
+    * Create a empty meta value, based on a given MetaType.
+    * 
+    * @param metaType the meta type.
+    * @return a meta value.
+    */
+   protected MetaValue createEmptyMetaValue(MetaType metaType)
+   {
+      MetaValue metaValue = null;
+      if(metaType.isSimple())
+      {
+         metaValue = new SimpleValueSupport((SimpleMetaType) metaType, null);
+      }
+      else if(metaType.isEnum())
+      {
+         metaValue = new EnumValueSupport((EnumMetaType) metaType, (String) null);
+      }
+      else if(metaType.isCollection())
+      {
+         metaValue = new CollectionValueSupport((CollectionMetaType) metaType);
+      }
+      else if(metaType.isGeneric())
+      {
+         // TODO
+      }
+      else if(metaType.isComposite())
+      {
+         metaValue = new CompositeValueSupport((CompositeMetaType) metaType);
+      }
+      else if(metaType.isTable())
+      {
+         metaValue = new TableValueSupport((TableMetaType) metaType);
+      }
+      else if(metaType.isArray())
+      {
+         metaValue = new ArrayValueSupport((ArrayMetaType) metaType);
+      }      
+      return metaValue;
+   }
+   
+   /**
+    * Convert simple types.
+    * 
+    * @param clazz a primitive serializable class.
+    * @param value the String
+    * @return the converted object, null in case of any failure.
+    */
+   public Serializable convert2Type(String className, String value)
+   {
+      if(value == null)
+         return null;
+      
+      Class<?> clazz = simpleTypes.get(className);
+      if(clazz == null)
+         throw new IllegalStateException("Cannot find simple type entry for "+ value + " and class "+ className);
+      
+      try
+      {
+         return (Serializable) ValueConvertor.convertValue(clazz, value);
+      }
+      catch(Throwable t)
+      {
+         log.debug("could convert "+ value +" to " + clazz.getName());
+         return null;
+      }
+   }
+   
+}

Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java	                        (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java	2008-11-30 14:29:54 UTC (rev 81824)
@@ -0,0 +1,94 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.system.server.profileservice.persistence.deployer;
+
+import java.util.Set;
+
+import org.jboss.deployers.spi.DeploymentException;
+import org.jboss.deployers.spi.deployer.DeploymentStages;
+import org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer;
+import org.jboss.deployers.structure.spi.DeploymentUnit;
+import org.jboss.logging.Logger;
+import org.jboss.managed.api.ManagedObject;
+import org.jboss.managed.api.factory.ManagedObjectFactory;
+import org.jboss.managed.plugins.factory.ManagedObjectFactoryBuilder;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.system.server.profileservice.persistence.ManagedObjectUpdateHandler;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
+
+/**
+ * The ProfileService Persistence Deployer. This deployer applies the
+ * persisted changes to an attachment.
+ * 
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ProfileServicePersistenceDeployer extends AbstractRealDeployer
+{
+
+   /** The managed prefix. */
+   public static final String PERSISTED_ATTACHMENT_PREFIX = "PERISTED";
+   
+   /** The managed update handler. */
+   private ManagedObjectUpdateHandler handler = new ManagedObjectUpdateHandler();
+   
+   private final ManagedObjectFactory factory = ManagedObjectFactoryBuilder.create();
+   
+   /** The Logger. */
+   private static final Logger log = Logger.getLogger(ProfileServicePersistenceDeployer.class);
+   
+   public ProfileServicePersistenceDeployer()
+   {
+      super();
+      setAllInputs(true);
+      setStage(DeploymentStages.PRE_REAL);
+   }
+   
+   @Override
+   protected void internalDeploy(DeploymentUnit unit) throws DeploymentException
+   {
+      MetaData metaData = unit.getMetaData();
+      
+      // Check all attachments, if they have a managed attachment
+      // TODO there might be a better way to do that ? :)
+      Set<String> attachments = unit.getTransientManagedObjects().getAttachments().keySet();
+      for(String attachment : attachments)
+      {
+         PersistedManagedObject persistedManagedObject = (PersistedManagedObject) unit.getAttachment(PERSISTED_ATTACHMENT_PREFIX + attachment);
+         
+         if(persistedManagedObject != null)
+         {
+            // Get the transient attachment
+            Object instance = unit.getTransientManagedObjects().getAttachment(attachment);
+            // Create the managed object
+            ManagedObject mo = factory.initManagedObject(instance, metaData);
+            // Update the attachment
+            Object o = handler.processManagedObject(persistedManagedObject, mo);
+            // Debug
+            log.debug("updated attachment with persisted information: " + o);
+            // Override
+            unit.addAttachment(attachment, o);
+         }
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list