[jboss-cvs] JBossAS SVN: r85220 - in trunk/system/src: main/org/jboss/system/server/profileservice/persistence/deployer and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Mar 4 09:23:44 EST 2009
Author: emuckenhuber
Date: 2009-03-04 09:23:43 -0500 (Wed, 04 Mar 2009)
New Revision: 85220
Added:
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedGenericOverrideHandler.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverride.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreation.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/GenericSupportMetaData.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/SimpleGenericMetaData.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/GenericValueUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/PersistenceFormatTestSuite.java
Removed:
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverrideHandler.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPersistence.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreationHandler.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java
Modified:
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/AttachmentPropertyPopulator.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/MOCreatorPlugin.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java
trunk/system/src/main/org/jboss/system/server/profileservice/persistence/xml/PersistedGenericValue.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/ArrayValueUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/BasicPersistenceFormatUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/CollectionValueUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/MapCompositeUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TableValueUnitTestCase.java
trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TestNestedPeristenceFormatUnitTestCase.java
Log:
update attachment persistence to better handle a collection of genericValues
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/AttachmentPropertyPopulator.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/AttachmentPropertyPopulator.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/AttachmentPropertyPopulator.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -21,25 +21,17 @@
*/
package org.jboss.system.server.profileservice.persistence;
-import java.io.Serializable;
import java.lang.reflect.Proxy;
-import java.util.ArrayList;
-import java.util.List;
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.factory.ManagedObjectFactory;
import org.jboss.managed.spi.factory.InstanceClassFactory;
-import org.jboss.metatype.api.types.GenericMetaType;
import org.jboss.metatype.api.types.MetaType;
-import org.jboss.metatype.api.values.CollectionValue;
-import org.jboss.metatype.api.values.CollectionValueSupport;
-import org.jboss.metatype.api.values.GenericValue;
-import org.jboss.metatype.api.values.GenericValueSupport;
+import org.jboss.metatype.api.values.CompositeValue;
import org.jboss.metatype.api.values.MetaValue;
import org.jboss.metatype.api.values.MetaValueFactory;
@@ -88,7 +80,7 @@
if (propertyInfo == null || propertyInfo.isWritable() == false)
{
if (trace)
- log.debug("skipping property " + name + " is not writable");
+ log.debug("Skipping not writable property " + propertyInfo);
return;
}
@@ -96,13 +88,6 @@
MetaType metaType = property.getMetaType();
MetaValue value = property.getValue();
- // Skip collections and arrays for now
- if(metaType.isCollection()
- || metaType.isArray())
- {
- return;
- }
-
// FIXME skip CompositeValueInvocationHandler
if (metaType.isComposite())
{
@@ -110,41 +95,19 @@
Object unwrapped = metaValueFactory.unwrap(value, 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, value);
}
-
- /**
- * Hack generic values in collections.
- *
- * @param collection
- * @return
- */
- protected CollectionValue hackGenericCollectionSupport(CollectionValue collection)
+
+ protected void setObjectNameWorkaround(ManagedProperty property, Object attachment, CompositeValue value) throws Throwable
{
- if(collection.getMetaType().getElementType().isGeneric())
- {
- List<GenericValue> values = new ArrayList<GenericValue>();
- for(MetaValue value : collection.getElements())
- {
- Object o = ((GenericValue)value).getValue();
- if(o instanceof ManagedObject)
- {
- ManagedObject mo = (ManagedObject) o;
- GenericMetaType metaType = new GenericMetaType(mo.getAttachmentName(), mo.getName());
-
- values.add(new GenericValueSupport(metaType, (Serializable) mo.getAttachment()));
- //
- }
- }
- return new CollectionValueSupport(collection.getMetaType(), values.toArray( new MetaValue[values.size()] ));
- }
- return collection;
+
}
-
+
}
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/MOCreatorPlugin.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/MOCreatorPlugin.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/MOCreatorPlugin.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -23,6 +23,7 @@
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;
/**
@@ -42,7 +43,7 @@
public MOCreatorPlugin()
{
- this.managedObjectFactory = ManagedObjectFactory.getInstance();
+ this.managedObjectFactory = ManagedObjectFactoryBuilder.create();
}
public MOCreatorPlugin(ManagedObjectFactory managedObjectFactory)
Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedGenericOverrideHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedGenericOverrideHandler.java (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedGenericOverrideHandler.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,214 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.Collection;
+import java.util.Iterator;
+import java.util.Stack;
+
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.managed.api.Fields;
+import org.jboss.managed.api.ManagedObject;
+import org.jboss.managed.api.ManagedProperty;
+import org.jboss.managed.plugins.factory.AbstractManagedObjectFactory;
+import org.jboss.metatype.api.types.CollectionMetaType;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.GenericValue;
+import org.jboss.metatype.api.values.GenericValueSupport;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.system.server.profileservice.persistence.xml.ModificationInfo;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedGenericValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedProperty;
+
+/**
+ * A handler for updating generic values.
+ *
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ManagedGenericOverrideHandler extends ManagedObjectOverride
+{
+
+ /** The merging stack. */
+ private Stack<MergingProperties> properties = new Stack<MergingProperties>();
+
+ @Override
+ protected void processProperty(ManagedProperty property, PersistedProperty propertyElement, Object attachment,
+ boolean trace) throws Throwable
+ {
+
+ if (property == null)
+ throw new IllegalStateException("Null property");
+
+ // The propertyInfo
+ PropertyInfo info = property.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
+ if(info.isReadable() == false)
+ {
+ log.trace("Skipping not readable property " + info);
+ return;
+ }
+
+ // Push the merging item
+ MergingProperties item = new MergingProperties(attachment, info, propertyElement);
+ this.properties.push(item);
+ try
+ {
+ super.processProperty(property, propertyElement, attachment, trace);
+ }
+ finally
+ {
+ this.properties.pop();
+ }
+ }
+
+ @Override
+ protected void setValue(ManagedProperty property, MetaValue value, Object attachment) throws Throwable
+ {
+ if(value == null)
+ return;
+
+ if (value.getMetaType().isGeneric())
+ {
+ // FIXME
+ if(value.getMetaType().equals(GENERIC_PERSISTED_VALUE_TYPE))
+ {
+ // See if we need to recreate a managed object
+ PersistedGenericValue moElement = (PersistedGenericValue) ((GenericValue) value).getValue();
+ if (moElement.getManagedObject() == null)
+ return;
+
+ ManagedObject mo = createManagedObject(moElement.getManagedObject());
+ value = new GenericValueSupport(AbstractManagedObjectFactory.MANAGED_OBJECT_META_TYPE, mo);
+ }
+ }
+ //
+ super.setValue(property, value, attachment);
+ }
+
+
+ /**
+ * Create a merged collection value.
+ *
+ * @param original the original collection
+ * @param override the override collection
+ * @return the merged collection
+ * @throws Throwable
+ */
+ @Override
+ protected CollectionValue mergeCollection(CollectionValue original, CollectionValue override, boolean trace)
+ throws Throwable
+ {
+ CollectionMetaType metaType = original.getMetaType();
+ if (metaType.getElementType().isGeneric())
+ {
+ // Check if the recreation was wrapping the correct value
+// CollectionMetaType overrideMetaType = override.getMetaType();
+// if(overrideMetaType.getElementType().equals(GENERIC_PERSISTED_VALUE_TYPE) == false)
+// throw new IllegalStateException("Wrong generic value type: " + overrideMetaType.getElementType());
+
+ // Extract the information
+ MergingProperties item = this.properties.peek();
+ PropertyInfo propertyInfo = item.getPropertyInfo();
+ Object attachment = item.getAttachment();
+
+ // Extract original collection
+ Collection<?> c = (Collection<?>) propertyInfo.get(attachment);
+
+ // This should not happen
+ if(c.size() < override.getSize())
+ throw new IllegalStateException("c.size() < override.getSize()");
+
+ Iterator<?> rawIterator = c.iterator();
+ Iterator<MetaValue> originalIterator = original.iterator();
+ Iterator<MetaValue> overrideIterator = override.iterator();
+
+ while (rawIterator.hasNext())
+ {
+ // The raw object
+ rawIterator.next();
+
+ // Skip generic values which are not annotated
+ GenericValue originalGeneric = (GenericValue) originalIterator.next();
+
+ // Extract the generic information
+ GenericValue overrideGeneric = (GenericValue) overrideIterator.next();
+ PersistedGenericValue overrideGenericValue = (PersistedGenericValue) overrideGeneric.getValue();
+
+ // Skip not annotated managed objects
+ if(originalGeneric.getValue() == null)
+ continue;
+
+ // TODO Remove a ManagedObject
+ ModificationInfo modificationInfo = overrideGenericValue.getModificationInfo();
+ if(modificationInfo != null && modificationInfo == ModificationInfo.REMOVED)
+ {
+ rawIterator.remove();
+ continue;
+ }
+
+ // process the generic value
+ processGenericValue(originalGeneric, overrideGenericValue, trace);
+ }
+
+ // This does not need to get populated
+ return null;
+ }
+ else
+ {
+ // Normal merge
+ return super.mergeCollection(original, override, trace);
+ }
+ }
+
+ public static class MergingProperties
+ {
+ /** The attachment. */
+ private Object attachment;
+
+ /** The managed object. */
+ private PropertyInfo propertyInfo;
+
+ /** The persisted property. */
+ private PersistedProperty propertyElement;
+
+ public MergingProperties(Object attachment, PropertyInfo info, PersistedProperty propertyElement)
+ {
+ this.attachment = attachment;
+ this.propertyInfo = info;
+ this.propertyElement = propertyElement;
+ }
+
+ public Object getAttachment()
+ {
+ return attachment;
+ }
+ public PropertyInfo getPropertyInfo()
+ {
+ return propertyInfo;
+ }
+ public PersistedProperty getPropertyElement()
+ {
+ return propertyElement;
+ }
+ }
+
+}
Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverride.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverride.java (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverride.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,298 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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 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.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.GenericValue;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.TableValue;
+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.PersistedValue;
+
+/**
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ManagedObjectOverride extends ManagedObjectRecreation
+{
+
+ /** The logger. */
+ protected static final Logger log = Logger.getLogger(ManagedObjectOverride.class);
+
+ /** The attachment property populator. */
+ private AttachmentPropertyPopulator populator = new AttachmentPropertyPopulator();
+
+ public ManagedObject update(Object attachment, PersistedManagedObject element) throws Throwable
+ {
+ if (attachment == null)
+ throw new IllegalArgumentException("Null attachment");
+ if (element == null)
+ throw new IllegalArgumentException("Null persisted information.");
+
+ // The managed object
+ ManagedObject mo = buildManagedObject(attachment);
+
+ //
+ if(mo == null)
+ return null;
+
+ // Update
+ updateManagedObject(mo, element, attachment);
+
+ return mo;
+ }
+
+ public void updateManagedObject(ManagedObject mo, PersistedManagedObject element, Object attachment)
+ throws Throwable
+ {
+ if(mo == null)
+ throw new IllegalArgumentException("Null managed object.");
+ if(element == null)
+ throw new IllegalArgumentException("Null persisted information.");
+ //
+ if(attachment == null)
+ attachment = mo.getAttachment();
+ if(attachment == null)
+ throw new IllegalArgumentException("Null attachment.");
+
+ boolean trace = log.isTraceEnabled();
+
+ // Process properties
+ for (PersistedProperty persisted : element.getProperties())
+ {
+ ManagedProperty property = mo.getProperty(persisted.getName());
+ // Process
+ processProperty(property, persisted, attachment, trace);
+ }
+ }
+
+ protected void processProperty(ManagedProperty property, PersistedProperty propertyElement, Object attachment,
+ boolean trace) throws Throwable
+ {
+ if (property == null)
+ throw new IllegalStateException("Null property");
+
+ // Get the original values
+ MetaValue metaValue = property.getValue();
+ MetaType metaType = property.getMetaType();
+ if (metaValue != null)
+ metaType = metaValue.getMetaType();
+
+ //
+ PersistedValue valueElement = propertyElement.getValue();
+ if (valueElement.getModificationInfo() == null)
+ {
+ // TODO
+ }
+ // Recreate the metaValue
+ MetaValue override = createMetaValue(valueElement, metaType);
+
+ // Process
+ MetaValue value = processMetaValue(metaValue, override, trace);
+
+ //
+ setValue(property, value, attachment);
+ }
+
+ protected void setValue(ManagedProperty property, MetaValue value, Object attachment)
+ throws Throwable
+ {
+ // Skip null metaValues
+ if(value == null)
+ return;
+
+ // Set the values
+ property.setField(Fields.VALUE, value);
+ // Update meta type
+ property.setField(Fields.META_TYPE, value.getMetaType());
+
+ // Populate the values
+ populator.processManagedProperty(property.getName(), property, attachment);
+ }
+
+ protected MetaValue processMetaValue(MetaValue original, MetaValue override, boolean trace) throws Throwable
+ {
+ if (override == null)
+ return original;
+ if (original == null)
+ return override;
+
+ if (original.getMetaType() == null)
+ throw new IllegalStateException("Original meta value is null " + original);
+
+ if (original.getMetaType().getTypeName().equals(override.getMetaType().getTypeName()) == false)
+ {
+ // On generic values the restored is different from the original
+ if(original.getMetaType().isGeneric() == false)
+ throw new RuntimeException("Cannot merge two different types original: " + original.getMetaType()
+ + " override: " + override.getMetaType());
+ }
+
+
+ // Now try to merge
+ MetaType metaType = original.getMetaType();
+ MetaValue metaValue = null;
+ if (metaType.isSimple())
+ {
+ metaValue = override;
+ }
+ else if (metaType.isEnum())
+ {
+ metaValue = override;
+ }
+ else if (metaType.isCollection())
+ {
+ metaValue = mergeCollection(
+ (CollectionValue) original,
+ (CollectionValue) override, trace);
+ }
+ else if (metaType.isGeneric())
+ {
+ metaValue = mergeGenericValue(
+ (GenericValue) original,
+ (GenericValue) override, trace);
+ }
+ else if (metaType.isComposite())
+ {
+ metaValue = mergeCompositeValue(
+ (CompositeValue) original,
+ (CompositeValue) override, trace);
+ }
+ else if (metaType.isTable())
+ {
+ metaValue = mergeTableValue(
+ (TableValue) original,
+ (TableValue) override, trace);
+ }
+ else if (metaType.isArray())
+ {
+ metaValue = mergeArray(
+ (ArrayValue) original,
+ (ArrayValue) override, trace);
+ }
+ else
+ {
+ throw new IllegalStateException("unknown metaType");
+ }
+ return metaValue;
+ }
+
+ /**
+ * Merge a composite value.
+ *
+ * @param original
+ * @param override
+ * @return
+ */
+ protected CompositeValue mergeCompositeValue(CompositeValue original, CompositeValue override, boolean trace)
+ {
+ return override;
+ }
+
+ /**
+ * Merge a table
+ *
+ * @param original
+ * @param override
+ * @return
+ */
+ protected TableValue mergeTableValue(TableValue original, TableValue override, boolean trace)
+ {
+ return override;
+ }
+
+ /**
+ * Create a merged array value.
+ *
+ * @param original the original array
+ * @param override the override array
+ * @return the merged array
+ */
+ protected ArrayValue mergeArray(ArrayValue original, ArrayValue override, boolean trace)
+ {
+ return override;
+ }
+
+ /**
+ * Create a merged collection value.
+ *
+ * @param original the original collection
+ * @param override the override collection
+ * @return the merged collection
+ * @throws Throwable
+ */
+ protected CollectionValue mergeCollection(CollectionValue original, CollectionValue override, boolean trace)
+ throws Throwable
+ {
+ return override;
+ }
+
+ /**
+ * Merge a generic value.
+ *
+ * @param original the original generic value
+ * @param override the override generic value
+ * @return the merged value
+ */
+ protected GenericValue mergeGenericValue(GenericValue original, GenericValue override, boolean trace)
+ throws Throwable
+ {
+ Object v = override.getValue();
+ if (v instanceof PersistedGenericValue)
+ {
+ PersistedGenericValue moElement = (PersistedGenericValue) override.getValue();
+
+ return processGenericValue(original, moElement, trace);
+ }
+ // Don't do anything
+ return null;
+ }
+
+ protected GenericValue processGenericValue(GenericValue original, PersistedGenericValue override, boolean trace)
+ throws Throwable
+ {
+ // Don't do anything
+ if(override.getManagedObject() == null)
+ return null;
+
+ Object o = original.getValue();
+ if ((o instanceof ManagedObject))
+ {
+ ManagedObject originalMO = (ManagedObject) o;
+
+ // process
+ updateManagedObject(originalMO, override.getManagedObject(), originalMO.getAttachment());
+ return original;
+ }
+ throw new RuntimeException("Cannot merge original: " + original + ", override: " + override);
+ }
+
+}
Deleted: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverrideHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverrideHandler.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectOverrideHandler.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -1,444 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, 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.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-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.CollectionMetaType;
-import org.jboss.metatype.api.types.MetaType;
-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.GenericValue;
-import org.jboss.metatype.api.values.GenericValueSupport;
-import org.jboss.metatype.api.values.MapCompositeValueSupport;
-import org.jboss.metatype.api.values.MetaValue;
-import org.jboss.metatype.api.values.TableValue;
-import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
-
-/**
- * Override information of a managed object.
- *
- * FIXME merging of collections, arrays and tables does not really make sense
- *
- * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
- * @version $Revision$
- */
-public class ManagedObjectOverrideHandler
-{
-
- /** The managed object update handler */
- private ManagedObjectRecreationHandler updateHandler = new ManagedObjectRecreationHandler();
-
- /** The attachment property populator. */
- private AttachmentPropertyPopulator populator = new AttachmentPropertyPopulator();
-
- /** The logger. */
- protected static final Logger log = Logger.getLogger(ManagedObjectOverrideHandler.class);
-
- public ManagedObject updateManagedObject(PersistedManagedObject moElement, ManagedObject original) throws Exception
- {
- if(moElement == null)
- throw new IllegalArgumentException("Null persisted meta data.");
-
- // Create the override object
- ManagedObject override = updateHandler.createManagedObject(moElement);
-
- boolean trace = log.isTraceEnabled();
- return updateManagedObject(original, override, trace);
- }
-
- public ManagedObject updateManagedObject(ManagedObject original, ManagedObject override, boolean trace)
- {
- if(original == null)
- return override;
- if(override == null)
- return original;
-
- // Sanity check
- if(original.getName().equals(override.getName()) == false)
- throw new IllegalArgumentException("Name don't match [original="
- + original.getName() + ", override="
- + override.getName() + "]");
-
- // AttachmentName
- if(original.getAttachmentName().equals(override.getAttachmentName()) == false)
- throw new IllegalArgumentException("Attachments don't match [original="
- + original.getAttachmentName() + ", override="
- + override.getAttachmentName() + "]");
-
- for(String name : override.getPropertyNames())
- {
- updateProperty(
- original.getProperty(name),
- override.getProperty(name),
- original.getAttachment(),
- trace);
- }
- return original;
- }
-
- protected void updateProperty(ManagedProperty original, ManagedProperty override, Object attachment, boolean trace)
- {
- if(original == null)
- throw new IllegalArgumentException("Null original.");
- if(override == null)
- throw new IllegalArgumentException("Null override.");
-
- if(! original.hasViewUse(ViewUse.CONFIGURATION))
- return;
-
- // Skip not readable properties
- PropertyInfo propertyInfo = original.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
- if(propertyInfo != null && propertyInfo.isReadable() == false)
- return;
-
- if(trace)
- log.trace("processing property : " + original);
-
- // Process the meta value.
- MetaValue merged = getMetaValue(original.getValue(), override.getValue(), trace);
-
- // Set the values
- original.setField(Fields.VALUE, merged);
- // Update meta type
- if(merged != null)
- original.setField(Fields.META_TYPE, merged.getMetaType());
-
- try
- {
- this.populator.processManagedProperty(original.getName(), original, attachment);
- }
- catch(Throwable t)
- {
- log.debug("Cannot popluate value information of property: " + original);
- }
-
- }
-
- private MetaValue getMetaValue(MetaValue original, MetaValue override, boolean trace)
- {
- if(override == null)
- return original;
- if(original == null)
- return override;
-
- if(original.getMetaType() == null)
- throw new IllegalStateException("Original meta value is null "+ original);
-
- if(original.getMetaType().getTypeName().equals(override.getMetaType().getTypeName()) == false)
- throw new RuntimeException("Cannot merge two different types original: "+ original.getMetaType() + " override: " + override.getMetaType());
-
- // Now try to merge
- MetaType metaType = original.getMetaType();
- MetaValue metaValue = null;
- if(metaType.isSimple())
- {
- metaValue = override;
- }
- else if(metaType.isEnum())
- {
- metaValue = override;
- }
- else if(metaType.isCollection())
- {
- metaValue = mergeCollection(
- (CollectionValue) original,
- (CollectionValue) override, trace);
- }
- else if(metaType.isGeneric())
- {
- metaValue = mergeGenericValue(
- (GenericValue) original,
- (GenericValue) override, trace);
- }
- else if(metaType.isComposite())
- {
- mergeCompositeValue(
- (CompositeValue) original,
- (CompositeValue) original, trace);
- }
- else if(metaType.isTable())
- {
- mergeTableValue(
- (TableValue) original,
- (TableValue) override, trace);
- }
- else if(metaType.isArray())
- {
- metaValue = mergeArray(
- (ArrayValue) original,
- (ArrayValue) override, trace);
- }
- else
- {
- throw new IllegalStateException("unknown metaType");
- }
- return metaValue;
- }
-
- /**
- * Merge a composite value.
- * TODO this just returns the new composite value.
- *
- * @param original
- * @param override
- * @return
- */
- private CompositeValue mergeCompositeValue(CompositeValue original, CompositeValue override, boolean trace)
- {
- if(original instanceof MapCompositeValueSupport)
- return mergeMapCompositeValue((MapCompositeValueSupport)original, (MapCompositeValueSupport)override);
-
- Map<String, MetaValue> map = new HashMap<String, MetaValue>();
- // The override information
- for(String index : override.getMetaType().itemSet())
- {
- MetaValue originalValue = original.get(index);
- MetaValue overrideValue = override.get(index);
-
- MetaValue merged = getMetaValue(originalValue, overrideValue, trace);
- map.put(index, merged);
- }
- // Additionally add the original ones
- for(String index : original.getMetaType().itemSet())
- {
- if(map.containsKey(index) == false)
- {
- MetaValue value = original.get(index);
- map.put(index, value);
- }
- }
- return new CompositeValueSupport(original.getMetaType(), map);
- }
-
- /**
- * Merge a map composite value.
- * This overrides the values from the original items, with the
- * override item values.
- *
- * @param original
- * @param override
- * @return
- */
- private MapCompositeValueSupport mergeMapCompositeValue(MapCompositeValueSupport original, MapCompositeValueSupport override)
- {
- for(String index : override.getMetaType().itemSet())
- {
- MetaValue overrideValue = override.get(index);
- original.put(index, overrideValue);
- }
- return original;
- }
-
- /**
- * Merge a table
- * TODO this return just the override table
- *
- * @param original
- * @param override
- * @return
- */
- private TableValue mergeTableValue(TableValue original, TableValue override, boolean trace)
- {
- if(trace)
- log.trace("merging table value original[indexNames=" + original.getMetaType().getIndexNames()
- + "] override[indexNames=" + override.getMetaType().getIndexNames() +"]");
-
- // Use override
- return override;
- }
-
- /**
- * Create a merged array value.
- *
- * @param original the original array
- * @param override the override array
- * @return the merged array
- */
- private ArrayValue mergeArray(ArrayValue original , ArrayValue override, boolean trace)
- {
- if( original.getLength() != override.getLength())
- {
- log.debug("overriding array : " + original);
- return override;
- }
-
- List<Object> values = new ArrayList<Object>();
- Iterator<Object> io = original.iterator();
- Iterator<Object> iv = override.iterator();
-
- while(io.hasNext())
- {
- Object originalValue = io.next();
- Object overrideValue = iv.next();
- Object merged = null;
-
- if(originalValue instanceof MetaValue
- && overrideValue instanceof MetaValue)
- {
- merged = getMetaValue(
- (MetaValue) originalValue,
- (MetaValue) overrideValue, trace);
- }
- else if(originalValue != null && originalValue.getClass().isArray()
- && overrideValue != null && overrideValue.getClass().isArray())
- {
- merged = mergeSubArray(originalValue, overrideValue, trace);
- }
- else
- {
- throw new RuntimeException("Could not process values original: " + original +", override: " + override);
- }
- values.add(merged);
- }
- return new ArrayValueSupport(original.getMetaType(), values.toArray());
- }
-
- /**
- * Merge a sub array.
- *
- * @param original the original sub element
- * @param override the override sub element
- * @return the merged values
- */
- private Object mergeSubArray(Object original, Object override, boolean trace)
- {
- List<Object> values = new ArrayList<Object>();
- int originalSize = Array.getLength(original);
- int overrideSize = Array.getLength(override);
-
- if(originalSize != overrideSize)
- throw new RuntimeException("Could not process sub-array original size: " + originalSize + ", override size: " + overrideSize);
-
- for(int i = 0; i < originalSize; i++)
- {
- Object originalElement = Array.get(original, i);
- Object overrideElement = Array.get(override, i);
- Object merged = null;
-
- if(originalElement instanceof MetaValue
- && overrideElement instanceof MetaValue)
- {
- merged = getMetaValue((MetaValue) originalElement, (MetaValue) overrideElement, trace);
- }
- else if ( originalElement != null && originalElement.getClass().isArray()
- && overrideElement != null && overrideElement.getClass().isArray())
- {
- merged = mergeSubArray(originalElement, overrideElement, trace);
- }
- else
- {
- throw new RuntimeException("Could not process values original: " + original +", override: " + override);
- }
- values.add(merged);
- }
- return values.toArray();
- }
-
- /**
- * Create a merged collection value.
- *
- * @param original the original collection
- * @param override the override collection
- * @return the merged collection
- */
- private CollectionValue mergeCollection(CollectionValue original, CollectionValue override, boolean trace)
- {
-
- if(trace)
- log.trace("merging collection orginal[size=" + original.getSize() + "], override[size="+ override.getSize() + "]");
-
- CollectionMetaType metaType = original.getMetaType();
- if(original.getSize() != override.getSize())
- {
- // FIXME
- List<MetaValue> values = new ArrayList<MetaValue>();
- if(original.getElements() != null)
- values.addAll(Arrays.asList(original.getElements()));
- if(override.getElements() != null)
- values.addAll(Arrays.asList(override.getElements()));
-
- return new CollectionValueSupport(metaType, values.toArray( new MetaValue[ values.size() ]));
- }
-
- // The new values.
- List<MetaValue> values = new ArrayList<MetaValue>();
- Iterator<MetaValue> io = original.iterator();
- Iterator<MetaValue> iv = override.iterator();
- while(io.hasNext())
- {
- MetaValue originalValue = io.next();
- MetaValue overrideValue = iv.next();
- MetaValue merged = getMetaValue(originalValue, overrideValue, trace);
- values.add(merged);
- }
- return new CollectionValueSupport(metaType, values.toArray( new MetaValue[values.size()] ));
- }
-
- /**
- * Merge a generic value.
- *
- * @param original the original generic value
- * @param override the override generic value
- * @return the merged value
- */
- private GenericValue mergeGenericValue(GenericValue original, GenericValue override, boolean trace)
- {
- Object o = original.getValue();
- Object v = override.getValue();
-
- if( (o instanceof ManagedObject)
- && (v instanceof ManagedObject))
- {
- ManagedObject originalMO = (ManagedObject) o;
- ManagedObject overrideMO = (ManagedObject) v;
-
- if(trace)
- log.trace("trying to merge original[name=" + originalMO.getName() + ",attachment=" + originalMO.getAttachment().getClass()
- + "], override[name=" + overrideMO.getName() + ",attachment=" + overrideMO.getAttachment().getClass() + "]");
-
- // merge
- ManagedObject merged = updateManagedObject(originalMO , overrideMO, trace);
- return new GenericValueSupport(original.getMetaType(), merged);
- }
- if(o == null && v == null) return original;
- throw new RuntimeException("Cannot merge original: " + original + ", override: "+ override);
- }
-
-}
-
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPeristenceHandler.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -21,8 +21,40 @@
*/
package org.jboss.system.server.profileservice.persistence;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+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.metatype.api.values.TableValue;
+import org.jboss.system.server.profileservice.persistence.xml.ModificationInfo;
+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.
@@ -33,9 +65,12 @@
public class ManagedObjectPeristenceHandler
{
- /** The actual handler */
- private static final ManagedObjectPersistence handler = new ManagedObjectPersistence();
+ /** The meta value factory. */
+ private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
+ /** The logger. */
+ private static final Logger log = Logger.getLogger(ManagedObjectPeristenceHandler.class);
+
/**
* Create the persistence meta data for a ManagedObject.
*
@@ -44,7 +79,12 @@
*/
public PersistedManagedObject createPersistenceMetaData(ManagedObject mo)
{
- return handler.createPersistenceMetaData(mo);
+ if(mo == null)
+ throw new IllegalArgumentException("Null managedObject.");
+
+ PersistedManagedObject moElement = new PersistedManagedObject();
+ processManagedObject(moElement, mo);
+ return moElement;
}
/**
@@ -56,7 +96,401 @@
*/
public void processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
{
- handler.processManagedObject(moElement, mo);
+ if(moElement == null)
+ throw new IllegalArgumentException("Null rootElement.");
+ if(mo == null)
+ throw new IllegalArgumentException("null managedObject");
+
+ boolean trace = log.isTraceEnabled();
+
+ // Set the template and class-name
+ String className = mo.getAttachmentName();
+ if(mo.getAttachment() != null)
+ {
+ Class<?> attachment = mo.getAttachment().getClass();
+ className = attachment.getName();
+ // Set the template name
+ if(className.equals(mo.getAttachmentName()) == false)
+ {
+ // If the MO template is different from the actual attachment
+ moElement.setTemplateName(mo.getAttachmentName());
+ }
+ }
+
+ // Set the managed-object meta information
+ moElement.setName(mo.getName());
+ moElement.setClassName(className);
+
+ // Process the properties
+ List<PersistedProperty> properties = moElement.getProperties();
+ if(properties == null)
+ {
+ properties = new ArrayList<PersistedProperty>();
+ moElement.setProperties(properties);
+ }
+ for(String propertyName : mo.getPropertyNames())
+ {
+ ManagedProperty property = mo.getProperty(propertyName);
+ if(property == null)
+ throw new IllegalStateException("unable to find propery: "+ property);
+
+ if(! property.hasViewUse(ViewUse.CONFIGURATION))
+ continue;
+
+ // getProperty
+ PropertyInfo propertyInfo = property.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
+ // Skip not readable properties
+ if(propertyInfo != null && propertyInfo.isReadable() == false)
+ {
+ if(trace)
+ log.trace("skipping property "+ propertyName + "is not readable");
+ continue;
+ }
+
+ PersistedProperty propertyElement = createPersistedProperty(
+ propertyName,
+ property.getValue(),
+ property.getMetaType(), trace);
+
+ properties.add(propertyElement);
+ }
}
-
-}
\ No newline at end of file
+
+ /**
+ * Create the persisted property.
+ *
+ * @param name the property name
+ * @param value the meta value
+ * @param metaType the meta type
+ * @return the property
+ */
+ protected PersistedProperty createPersistedProperty(String name, MetaValue value, MetaType metaType, boolean trace)
+ {
+ if(name == null)
+ throw new IllegalArgumentException("Null name.");
+ if(metaType == null)
+ throw new IllegalArgumentException("Null meta type.");
+
+ if(trace)
+ log.trace("processing property name="+ name + "[metaValue="+ value + ", metaType="+ metaType + "]" );
+
+ // Create Property
+ PersistedProperty property = new PersistedProperty(name);
+ // Create persisted value
+ PersistedValue persistedValue = createPersistedValue(value, metaType);
+ persistedValue.setModificationInfo(ModificationInfo.MODIFIED);
+ //
+ property.setValue(persistedValue);
+ // Return
+ return property;
+ }
+
+ /**
+ * Create the peristed xml meta data.
+ *
+ * @param value the meta value
+ * @param metaType the meta type
+ * @return the xml value.
+ */
+ protected PersistedValue createPersistedValue(MetaValue value, MetaType metaType)
+ {
+ if(log.isTraceEnabled())
+ log.trace("creating persisted value for : " + value + " with metaType " + metaType);
+
+ if(value == null)
+ return emtpyPersistedValue(metaType);
+
+ // Override the metaType e.g. the MapCompositeValueSupport
+ metaType = value.getMetaType();
+
+ PersistedValue persistedValue = null;
+ if(metaType.isSimple())
+ {
+ persistedValue = createSimpleValue(
+ (SimpleValue) value);
+ }
+ else if(metaType.isEnum())
+ {
+ persistedValue = createEnumValue(
+ (EnumValue) value);
+ }
+ else if(metaType.isCollection())
+ {
+ persistedValue = createCollectionValue(
+ (CollectionValue) value);
+ }
+ else if(metaType.isGeneric())
+ {
+ persistedValue = createGenericValue(
+ (GenericValue) value);
+ }
+ else if(metaType.isComposite())
+ {
+ persistedValue = createCompositeValue(
+ (CompositeValue) value,
+ (CompositeMetaType) metaType);
+ }
+ else if(metaType.isArray())
+ {
+ persistedValue = createArrayValue(
+ (ArrayValue) value,
+ (ArrayMetaType) metaType);
+ }
+ else if(metaType.isTable())
+ {
+ persistedValue = createTableValue(
+ (TableValue) value);
+ }
+ else
+ {
+ throw new IllegalStateException("unknown metaType");
+ }
+ return persistedValue;
+ }
+
+ /**
+ * Create the persistence enum value.
+ *
+ * @param value the enum value
+ * @return the enum xml meta data
+ */
+ private PersistedEnumValue createEnumValue(EnumValue value)
+ {
+ PersistedEnumValue persistedValue = new PersistedEnumValue();
+ persistedValue.setValue(value.getValue());
+ return persistedValue;
+ }
+
+ /**
+ * Create the persistence simple value.
+ *
+ * @param value the simple value
+ * @return the simple xml meta data
+ */
+ private PersistedSimpleValue createSimpleValue(SimpleValue value)
+ {
+ PersistedSimpleValue persistedValue = new PersistedSimpleValue();
+ persistedValue.setValue(convertSimple2String(value));
+ return persistedValue;
+ }
+
+ /**
+ * Create the persistence collection value.
+ *
+ * @param value the collection value
+ * @return the collection xml meta data
+ */
+ private PersistedCollectionValue createCollectionValue(CollectionValue value)
+ {
+ PersistedCollectionValue collection = new PersistedCollectionValue();
+ for(MetaValue child : value.getElements())
+ {
+ PersistedValue persistedValue = createPersistedValue(child, child.getMetaType());
+ collection.addValue(persistedValue);
+ }
+ return collection;
+ }
+
+ /**
+ * Create the persistence generic value.
+ *
+ * @param value the generic value
+ * @return the generic xml meta data
+ */
+ private PersistedGenericValue createGenericValue(GenericValue value)
+ {
+ //
+ PersistedGenericValue generic = new PersistedGenericValue();
+ Object o = value.getValue();
+ if(o == null)
+ return generic;
+
+ if(o instanceof ManagedObject)
+ {
+ PersistedManagedObject mo = createPersistenceMetaData((ManagedObject) o);
+ generic.setManagedObject(mo);
+ }
+ else
+ {
+ throw new IllegalStateException("The value of GenericValue must be a ManagedObject: " + value);
+ }
+ return generic;
+ }
+
+ /**
+ * Create the persistence array value.
+ *
+ * @param value the array value
+ * @return
+ */
+ private PersistedArrayValue createArrayValue(ArrayValue value, ArrayMetaType metaType)
+ {
+ //
+ PersistedArrayValue array = new PersistedArrayValue();
+ MetaType elementType = metaType.getElementType();
+ for (int i = 0; i < value.getLength(); i++)
+ {
+ PersistedValue persistedValue = null;
+ Object subElement = value.getValue(i);
+
+ if (subElement instanceof MetaValue)
+ {
+ persistedValue = createPersistedValue((MetaValue) subElement, elementType);
+ }
+ else if (subElement != null && subElement.getClass().isArray())
+ {
+ persistedValue = unwrapArray(array, subElement, elementType);
+ }
+ // Add to parent
+ array.addValue(persistedValue);
+ }
+ return array;
+ }
+
+ /**
+ * Unwrap array.
+ *
+ * @param array the parent array
+ * @param element the array value
+ * @param type the element meta type
+ * @return the persistence xml meta data
+ */
+ protected PersistedArrayValue unwrapArray(PersistedArrayValue array, Object element, MetaType type)
+ {
+ PersistedArrayValue newElement = new PersistedArrayValue();
+ int subSize = Array.getLength(element);
+ for (int i = 0; i < subSize; i++)
+ {
+ PersistedValue persistedValue = null;
+ Object subElement = Array.get(element, i);
+ if (subElement instanceof MetaValue)
+ {
+ persistedValue = createPersistedValue((MetaValue) subElement, type);
+ }
+ else if (subElement != null && subElement.getClass().isArray())
+ {
+ persistedValue = unwrapArray(newElement, subElement, type);
+ }
+
+ newElement.addValue(persistedValue);
+ }
+ return newElement;
+ }
+
+ /**
+ * Create the persistence composite value.
+ *
+ * @param value the composite value
+ * @param metaType the composite meta type
+ * @return the persistence composite xml meta data
+ */
+ private PersistedCompositeValue createCompositeValue(CompositeValue value, CompositeMetaType metaType)
+ {
+ //
+ PersistedCompositeValue composite = new PersistedCompositeValue();
+ // Fix the values
+ List<PersistedValue> values = composite.getValues();
+ if(values == null)
+ {
+ values = new ArrayList<PersistedValue>();
+ composite.setValues(values);
+ }
+ for(String item : metaType.itemSet())
+ {
+ MetaType itemType = metaType.getType(item);
+ MetaValue itemValue = value.get(item);
+
+ // Create item
+ PersistedValue persistedValue = createPersistedValue(itemValue, itemType);
+ persistedValue.setName(item);
+
+ values.add(persistedValue);
+ }
+ return composite;
+ }
+
+ /**
+ * Create the persistence table value.
+ *
+ * @param value the table value
+ * @return the persistence table xml meta data
+ */
+ private PersistedTableValue createTableValue(TableValue value)
+ {
+ PersistedTableValue table = new PersistedTableValue();
+ // Fix the entries
+ List<PersistedCompositeValue> entries = table.getEntries();
+ if(entries == null)
+ {
+ entries = new ArrayList<PersistedCompositeValue>();
+ table.setEntries(entries);
+ }
+ // Process values
+ Collection<CompositeValue> values = value.values();
+ for(CompositeValue entry : values)
+ {
+ entries.add(createCompositeValue(entry, entry.getMetaType()));
+ }
+ return table;
+ }
+
+ /**
+ * Create a emtpy xml meta data, based on the meta type
+ *
+ * @param metaType the meta type
+ * @return the peristence value
+ */
+ protected static PersistedValue emtpyPersistedValue(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)
+ {
+ if(value == null)
+ throw new IllegalArgumentException("Null value.");
+
+ Object unwrappedValue = metaValueFactory.unwrap(value);
+ if(unwrappedValue == null)
+ return null;
+ // Convert to String
+ return ("" + unwrappedValue);
+ }
+}
Deleted: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPersistence.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPersistence.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectPersistence.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -1,493 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, 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.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-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.metatype.api.values.TableValue;
-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;
-
-/**
- * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
- * @version $Revision$
- */
-public class ManagedObjectPersistence
-{
-
- /** The meta value factory. */
- private MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
-
- /** The logger. */
- private static final Logger log = Logger.getLogger(ManagedObjectPersistence.class);
-
- /**
- * Create the persistence meta data for a ManagedObject.
- *
- * @param mo the managed object
- * @return the persistence meta data
- */
- public PersistedManagedObject createPersistenceMetaData(ManagedObject mo)
- {
- if(mo == null)
- throw new IllegalArgumentException("Null managedObject.");
-
- PersistedManagedObject moElement = new PersistedManagedObject();
- processManagedObject(moElement, mo);
- return moElement;
- }
-
- /**
- * Process a managed object.
- *
- * @param moElement the xml meta data.
- * @param mo the managed object
- * @return isModified
- */
- public void processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
- {
- if(moElement == null)
- throw new IllegalArgumentException("Null rootElement.");
- if(mo == null)
- throw new IllegalArgumentException("null managedObject");
-
- boolean trace = log.isTraceEnabled();
-
- // Set the template and class-name
- String className = mo.getAttachmentName();
- if(mo.getAttachment() != null)
- {
- Class<?> attachment = mo.getAttachment().getClass();
- className = attachment.getName();
- // Set the template name
- if(className.equals(mo.getAttachmentName()) == false)
- {
- // If the MO template is different from the actual attachment
- moElement.setTemplateName(mo.getAttachmentName());
- }
- }
-
- // Set the managed-object meta information
- moElement.setName(mo.getName());
- moElement.setClassName(className);
-
- // Process the properties
- List<PersistedProperty> properties = moElement.getProperties();
- if(properties == null)
- {
- properties = new ArrayList<PersistedProperty>();
- moElement.setProperties(properties);
- }
- for(String propertyName : mo.getPropertyNames())
- {
- ManagedProperty property = mo.getProperty(propertyName);
- if(property == null)
- throw new IllegalStateException("unable to find propery: "+ property);
-
- if(! property.hasViewUse(ViewUse.CONFIGURATION))
- continue;
-
- // getProperty
- PropertyInfo propertyInfo = property.getField(Fields.PROPERTY_INFO, PropertyInfo.class);
- // Skip not readable properties
- if(propertyInfo != null && propertyInfo.isReadable() == false)
- {
- if(trace)
- log.trace("skipping property "+ propertyName + "is not readable");
- continue;
- }
-
- PersistedProperty propertyElement = createPersistedProperty(
- propertyName,
- property.getValue(),
- property.getMetaType(), trace);
-
- properties.add(propertyElement);
- }
- }
-
- /**
- * Create the persisted property.
- *
- * @param name the property name
- * @param value the meta value
- * @param metaType the meta type
- * @return the property
- */
- protected PersistedProperty createPersistedProperty(String name, MetaValue value, MetaType metaType, boolean trace)
- {
- if(name == null)
- throw new IllegalArgumentException("Null name.");
- if(metaType == null)
- throw new IllegalArgumentException("Null meta type.");
-
- if(trace)
- log.trace("processing property name="+ name + "[metaValue="+ value + ", metaType="+ metaType + "]" );
-
- // Create Property
- PersistedProperty property = new PersistedProperty(name);
- // Create persisted value
- PersistedValue persistedValue = createPersistedValue(value, metaType);
- //
- property.setValue(persistedValue);
- // Return
- return property;
- }
-
- /**
- * Create the peristed xml meta data.
- *
- * @param value the meta value
- * @param metaType the meta type
- * @return the xml value.
- */
- protected PersistedValue createPersistedValue(MetaValue value, MetaType metaType)
- {
- if(log.isTraceEnabled())
- log.trace("creating persisted value for : " + value + " with metaType " + metaType);
-
- if(value == null)
- return emtpyPersistedValue(metaType);
-
- // Override the metaType e.g. the MapCompositeValueSupport
- metaType = value.getMetaType();
-
- PersistedValue persistedValue = null;
- if(metaType.isSimple())
- {
- persistedValue = createSimpleValue(
- (SimpleValue) value);
- }
- else if(metaType.isEnum())
- {
- persistedValue = createEnumValue(
- (EnumValue) value);
- }
- else if(metaType.isCollection())
- {
- persistedValue = createCollectionValue(
- (CollectionValue) value);
- }
- else if(metaType.isGeneric())
- {
- persistedValue = createGenericValue(
- (GenericValue) value);
- }
- else if(metaType.isComposite())
- {
- persistedValue = createCompositeValue(
- (CompositeValue) value,
- (CompositeMetaType) metaType);
- }
- else if(metaType.isArray())
- {
- persistedValue = createArrayValue(
- (ArrayValue) value,
- (ArrayMetaType) metaType);
- }
- else if(metaType.isTable())
- {
- persistedValue = createTableValue(
- (TableValue) value);
- }
- else
- {
- throw new IllegalStateException("unknown metaType");
- }
- return persistedValue;
- }
-
- /**
- * Create the persistence enum value.
- *
- * @param value the enum value
- * @return the enum xml meta data
- */
- private PersistedEnumValue createEnumValue(EnumValue value)
- {
- PersistedEnumValue persistedValue = new PersistedEnumValue();
- persistedValue.setValue(value.getValue());
- return persistedValue;
- }
-
- /**
- * Create the persistence simple value.
- *
- * @param value the simple value
- * @return the simple xml meta data
- */
- private PersistedSimpleValue createSimpleValue(SimpleValue value)
- {
- PersistedSimpleValue persistedValue = new PersistedSimpleValue();
- persistedValue.setValue(convertSimple2String(value));
- return persistedValue;
- }
-
- /**
- * Create the persistence collection value.
- *
- * @param value the collection value
- * @return the collection xml meta data
- */
- private PersistedCollectionValue createCollectionValue(CollectionValue value)
- {
- PersistedCollectionValue collection = new PersistedCollectionValue();
- for(MetaValue child : value.getElements())
- {
- PersistedValue persistedValue = createPersistedValue(child, child.getMetaType());
- collection.addValue(persistedValue);
- }
- return collection;
- }
-
- /**
- * Create the persistence generic value.
- *
- * @param value the generic value
- * @return the generic xml meta data
- */
- private PersistedGenericValue createGenericValue(GenericValue value)
- {
- //
- PersistedGenericValue generic = new PersistedGenericValue();
- Object o = value.getValue();
- if(o == null)
- return generic;
-
- if(o instanceof ManagedObject)
- {
- PersistedManagedObject mo = createPersistenceMetaData((ManagedObject) o);
- generic.setManagedObject(mo);
- }
- else
- {
- throw new IllegalStateException("The value of GenericValue must be a ManagedObject: " + value);
- }
- return generic;
- }
-
- /**
- * Create the persistence array value.
- *
- * @param value the array value
- * @return
- */
- private PersistedArrayValue createArrayValue(ArrayValue value, ArrayMetaType metaType)
- {
- //
- PersistedArrayValue array = new PersistedArrayValue();
- MetaType elementType = metaType.getElementType();
- for (int i = 0; i < value.getLength(); i++)
- {
- PersistedValue persistedValue = null;
- Object subElement = value.getValue(i);
-
- if (subElement instanceof MetaValue)
- {
- persistedValue = createPersistedValue((MetaValue) subElement, elementType);
- }
- else if (subElement != null && subElement.getClass().isArray())
- {
- persistedValue = unwrapArray(array, subElement, elementType);
- }
- // Add to parent
- array.addValue(persistedValue);
- }
- return array;
- }
-
- /**
- * Unwrap array.
- *
- * @param array the parent array
- * @param element the array value
- * @param type the element meta type
- * @return the persistence xml meta data
- */
- protected PersistedArrayValue unwrapArray(PersistedArrayValue array, Object element, MetaType type)
- {
- PersistedArrayValue newElement = new PersistedArrayValue();
- int subSize = Array.getLength(element);
- for (int i = 0; i < subSize; i++)
- {
- PersistedValue persistedValue = null;
- Object subElement = Array.get(element, i);
- if (subElement instanceof MetaValue)
- {
- persistedValue = createPersistedValue((MetaValue) subElement, type);
- }
- else if (subElement != null && subElement.getClass().isArray())
- {
- persistedValue = unwrapArray(newElement, subElement, type);
- }
-
- newElement.addValue(persistedValue);
- }
- return newElement;
- }
-
- /**
- * Create the persistence composite value.
- *
- * @param value the composite value
- * @param metaType the composite meta type
- * @return the persistence composite xml meta data
- */
- private PersistedCompositeValue createCompositeValue(CompositeValue value, CompositeMetaType metaType)
- {
- //
- PersistedCompositeValue composite = new PersistedCompositeValue();
- // Fix the values
- List<PersistedValue> values = composite.getValues();
- if(values == null)
- {
- values = new ArrayList<PersistedValue>();
- composite.setValues(values);
- }
- for(String item : metaType.itemSet())
- {
- MetaType itemType = metaType.getType(item);
- MetaValue itemValue = value.get(item);
-
- // Create item
- PersistedValue persistedValue = createPersistedValue(itemValue, itemType);
- persistedValue.setName(item);
-
- values.add(persistedValue);
- }
- return composite;
- }
-
- /**
- * Create the persistence table value.
- *
- * @param value the table value
- * @return the persistence table xml meta data
- */
- private PersistedTableValue createTableValue(TableValue value)
- {
- PersistedTableValue table = new PersistedTableValue();
- // Fix the entries
- List<PersistedCompositeValue> entries = table.getEntries();
- if(entries == null)
- {
- entries = new ArrayList<PersistedCompositeValue>();
- table.setEntries(entries);
- }
- // Process values
- Collection<CompositeValue> values = value.values();
- for(CompositeValue entry : values)
- {
- entries.add(createCompositeValue(entry, entry.getMetaType()));
- }
- return table;
- }
-
- /**
- * Create a emtpy xml meta data, based on the meta type
- *
- * @param metaType the meta type
- * @return the peristence value
- */
- protected static PersistedValue emtpyPersistedValue(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)
- {
- if(value == null)
- throw new IllegalArgumentException("Null value.");
-
- Object unwrappedValue = metaValueFactory.unwrap(value);
- if(unwrappedValue == null)
- return null;
- // Convert to String
- return ("" + unwrappedValue);
- }
-}
-
Added: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreation.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreation.java (rev 0)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreation.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,569 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.MutableManagedObject;
+import org.jboss.managed.api.annotation.ViewUse;
+import org.jboss.metadata.spi.MetaData;
+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.MapCompositeMetaType;
+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.MapCompositeValueSupport;
+import org.jboss.metatype.api.values.MetaValue;
+import org.jboss.metatype.api.values.SimpleValue;
+import org.jboss.metatype.api.values.SimpleValueSupport;
+import org.jboss.metatype.api.values.TableValue;
+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.PersistedTableValue;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedValue;
+
+/**
+ * This handler recreates only the ManagedObject based on the persisted meta data.
+ *
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class ManagedObjectRecreation
+{
+ /** The generic type of a persisted generic value */
+ public final static GenericMetaType GENERIC_PERSISTED_VALUE_TYPE = new GenericMetaType(PersistedGenericValue.class.getName(), "persisted generic value");
+
+ /** The meta data. */
+ private MetaData metaData;
+
+ /** The plugin. */
+ private MOCreatorPlugin plugin;
+
+ /** The simple types. */
+ private final static Map<String, Class<? extends Serializable>> simpleTypes = new HashMap<String, Class<? extends Serializable>>();
+
+ /** The logger. */
+ private final static Logger log = Logger.getLogger(ManagedObjectRecreation.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);
+ // primitive classes
+ simpleTypes.put(byte.class.getName(), byte.class);
+ simpleTypes.put(char.class.getName(), char.class);
+ simpleTypes.put(double.class.getName(), double.class);
+ simpleTypes.put(float.class.getName(), float.class);
+ simpleTypes.put(int.class.getName(), int.class);
+ simpleTypes.put(short.class.getName(), short.class);
+ simpleTypes.put(long.class.getName(), long.class);
+ simpleTypes.put(boolean.class.getName(), boolean.class);
+ }
+
+ public ManagedObjectRecreation()
+ {
+ this.plugin = new MOCreatorPlugin();
+ }
+
+ protected ManagedObject createManagedObject(PersistedManagedObject moElement) throws Exception
+ {
+ if(moElement == null)
+ throw new IllegalArgumentException("Null persisted meta data.");
+ if(moElement.getClassName() == null)
+ throw new IllegalArgumentException("Null class-name for managed object.");
+
+ // Check if we need to use a template for the managed object
+ String className = moElement.getTemplateName();
+ if(className == null)
+ {
+ className = moElement.getClassName();
+ }
+ ManagedObject mo = buildManagedObject(className);
+
+
+ if(mo instanceof MutableManagedObject)
+ {
+ // Update the name
+ ((MutableManagedObject) mo).setName(moElement.getName());
+
+ if(moElement.getTemplateName() != null)
+ {
+ // Override the actual attachment
+ Class<?> attachment = plugin.getClassLoader().loadClass(moElement.getClassName());
+ ((MutableManagedObject) mo).setAttachment(attachment.newInstance());
+ }
+ }
+
+ // Process
+ processManagedObject(moElement, mo);
+
+ // Return
+ return mo;
+ }
+
+ /**
+ * Process a ManagedObject.
+ *
+ * @param moElement the persisted xml meta data.
+ * @param mo the managed object
+ * @return the attachment meta data.
+ */
+ private void processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
+ {
+ if(moElement == null)
+ throw new IllegalArgumentException("Null rootElement.");
+ if(mo == null)
+ throw new IllegalArgumentException("null managedObject");
+
+ // Get the xml to see what we need to merge
+ if(moElement.getProperties() != null && moElement.getProperties().isEmpty() == false)
+ {
+ for(PersistedProperty propertyElement : moElement.getProperties())
+ {
+ ManagedProperty property = mo.getProperty(propertyElement.getName());
+ if(property == null)
+ throw new IllegalStateException("unable to find propery: "+ property);
+
+ // Skip all non configuration properties
+ if(! property.hasViewUse(ViewUse.CONFIGURATION))
+ continue;
+
+ // getProperty
+ processManagedProperty(propertyElement, propertyElement.getName(), property, mo.getAttachment());
+ }
+ }
+ }
+
+ /**
+ * Build a managed object based on the attachment.
+ *
+ * @param attachment the attachment
+ * @return the ManagedObject
+ * @throws Exception
+ */
+ protected ManagedObject buildManagedObject(Object attachment) throws Exception
+ {
+ return plugin.initManagedObject(attachment, null);
+ }
+
+
+ /**
+ * Build a managedObject based on the className
+ *
+ * @param className the name of the class
+ * @return the ManagedObject
+ * @throws Exception
+ */
+ protected ManagedObject buildManagedObject(String className) throws Exception
+ {
+ return plugin.createManagedObject(className, metaData);
+ }
+
+ /**
+ * Process a ManagedProperty.
+ *
+ * @param propertyElement the persisted xml meta data.
+ * @param name the property name.
+ * @param property the managed property.
+ */
+ protected void processManagedProperty(PersistedProperty propertyElement, String name, ManagedProperty property, Object attachment)
+ {
+ // Get MetaType
+ MetaType metaType = property.getMetaType();
+ // Process the value
+ PersistedValue persistedValue = propertyElement.getValue();
+ if(persistedValue != null)
+ {
+ // recurse = true
+ MetaValue value = createMetaValue(persistedValue, metaType);
+ // Set the value Field
+ property.setField(Fields.VALUE, value);
+ // Update meta type
+ if(value != null)
+ property.setField(Fields.META_TYPE, value.getMetaType());
+ }
+ // Done
+ }
+
+ /**
+ * Create the meta value, based on the xml persited
+ * value and the generated MetaType.
+ *
+ * @param valueElement the persisted xml element
+ * @param type the meta type.
+ * @return the created meta value.
+ */
+ protected MetaValue createMetaValue(PersistedValue valueElement, MetaType metaType)
+ {
+ if(log.isTraceEnabled())
+ {
+ log.trace("processing value " + valueElement + " type: " + metaType);
+ }
+
+ if(valueElement == null)
+ return null;
+
+ MetaValue metaValue = null;
+ try
+ {
+ if(metaType.isSimple())
+ {
+ metaValue = createSimpleValue(
+ (PersistedSimpleValue) valueElement,
+ (SimpleMetaType) metaType);
+ }
+ else if(metaType.isEnum())
+ {
+ metaValue = createEnumValue(
+ (PersistedEnumValue) valueElement,
+ (EnumMetaType) metaType);
+ }
+ else if(metaType.isCollection())
+ {
+ metaValue = createCollectionValue(
+ (PersistedCollectionValue) valueElement,
+ (CollectionMetaType) metaType);
+ }
+ else if(metaType.isGeneric())
+ {
+ metaValue = createGenericValue(
+ (PersistedGenericValue) valueElement,
+ (GenericMetaType) metaType);
+ }
+ else if(metaType.isComposite())
+ {
+ metaValue = createCompositeValue(
+ (PersistedCompositeValue) valueElement,
+ (CompositeMetaType) metaType);
+ }
+ else if(metaType.isTable())
+ {
+ metaValue = createTableValue(
+ (PersistedTableValue) valueElement,
+ (TableMetaType)metaType);
+ }
+ else if(metaType.isArray())
+ {
+ metaValue = createArrayValue(
+ (PersistedArrayValue) valueElement,
+ (ArrayMetaType) metaType);
+ }
+ else
+ {
+ throw new IllegalStateException("unknown metaType");
+ }
+ }
+ finally
+ {
+ //
+ }
+ return metaValue;
+ }
+
+ /**
+ * Create simple value.
+ *
+ * @param valueElement the persisted xml meta data.
+ * @param value the simple value.
+ * @return a simple value.
+ */
+ protected SimpleValue createSimpleValue(PersistedSimpleValue valueElement, SimpleMetaType metaType)
+ {
+ 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);
+ }
+
+ /**
+ * Process an Enum value.
+ *
+ * @param enumElement the persisted xml meta data.
+ * @param value the enum value.
+ * @return a enum value.
+ */
+ protected EnumValue createEnumValue(PersistedEnumValue enumElement, EnumMetaType type)
+ {
+ return new EnumValueSupport(type, enumElement.getValue());
+ }
+
+ /**
+ * Create composite value.
+ *
+ * @param composite the persisted xml meta data.
+ * @param value the composite value.
+ * @return a composite value.
+ */
+ protected CompositeValue createCompositeValue(PersistedCompositeValue composite, CompositeMetaType type)
+ {
+ // Handle the mapCompositeMetaType differently
+ if(type instanceof MapCompositeMetaType)
+ return handleMapCompositeMetaType(composite, (MapCompositeMetaType) type);
+
+ // Create composite value
+ Map<String, MetaValue> values = new HashMap<String, MetaValue>();
+ if(composite.getValues() != null && composite.getValues().isEmpty() == false)
+ {
+ for(PersistedValue persistedValue : composite.getValues())
+ {
+ MetaType elementType = type.getType(persistedValue.getName());
+ if(elementType == null)
+ throw new IllegalStateException("Failed to process composite value: " + persistedValue.getName());
+
+ // Create
+ MetaValue metaValue = createMetaValue(persistedValue, elementType);
+ // Put
+ values.put(persistedValue.getName(), metaValue);
+ }
+ }
+ return new CompositeValueSupport(type, values);
+ }
+
+ /**
+ * Create the MapCompositeValueSupport value.
+ *
+ * @param composite the persisted composite xml meta data
+ * @param type the MapComposite meta type
+ * @return the MapCompositeValueSupport
+ */
+ protected MapCompositeValueSupport handleMapCompositeMetaType(PersistedCompositeValue composite, MapCompositeMetaType type)
+ {
+ Map<String, MetaValue> values = new HashMap<String, MetaValue>();
+ if(composite.getValues() != null && composite.getValues().isEmpty() == false)
+ {
+ for(PersistedValue persistedValue : composite.getValues())
+ {
+ MetaValue value = createMetaValue(persistedValue, type.getValueType());
+ values.put(persistedValue.getName(), value);
+ }
+ }
+ return new MapCompositeValueSupport(values, type);
+ }
+
+ /**
+ * Process a collection.
+ *
+ * @param collection the persisted xml meta data.
+ * @param value the collection value.
+ * @return a collection value.
+ */
+ protected CollectionValue createCollectionValue(PersistedCollectionValue collection, CollectionMetaType type)
+ {
+ List<MetaValue> elementList = new ArrayList<MetaValue>();
+ if(collection.getValues() != null && collection.getValues().isEmpty() == false)
+ {
+ for(PersistedValue element : collection.getValues())
+ {
+ elementList.add(
+ createMetaValue(element, type.getElementType()));
+ }
+ }
+ // Generic values are getting wrapped differently
+ if(type.isGeneric())
+ {
+ type = new CollectionMetaType(type.getClassName(), "", GENERIC_PERSISTED_VALUE_TYPE);
+ }
+ return new CollectionValueSupport(type, elementList.toArray(new MetaValue[elementList.size()]));
+ }
+
+ /**
+ * Create generic value.
+ *
+ * @param genericElement the persisted generic xml meta data
+ * @param metaType the generic meta type
+ * @return the generic value
+ */
+ protected GenericValue createGenericValue(PersistedGenericValue genericElement, GenericMetaType metaType)
+ {
+ // If we don't recurse, someone else needs to extract the managed object
+ return new GenericValueSupport(GENERIC_PERSISTED_VALUE_TYPE, genericElement);
+ }
+
+ /**
+ * Create the table value.
+ *
+ * @param table the persisted table value.
+ * @param type the table meta type.
+ * @return the table value.
+ */
+ protected TableValue createTableValue(PersistedTableValue table, TableMetaType type)
+ {
+ TableValueSupport support = new TableValueSupport(type);
+ if(table.getEntries() != null && table.getEntries().isEmpty() == false)
+ {
+ for(PersistedCompositeValue entry : table.getEntries())
+ {
+ support.put(createCompositeValue(entry, type.getRowType()));
+ }
+ }
+ return support;
+ }
+
+ /**
+ * Create array value.
+ *
+ * @param valueElement the persisted array xml value
+ * @param type the array meta type
+ * @return the array value
+ */
+ @SuppressWarnings("unchecked")
+ protected ArrayValue createArrayValue(PersistedArrayValue valueElement, ArrayMetaType type)
+ {
+ int size = valueElement.size();
+ List values = new ArrayList(size);
+ for(PersistedValue elementValue : valueElement.getValues())
+ {
+ if(elementValue instanceof PersistedArrayValue)
+ {
+ values.add(
+ recreateArrayValue((PersistedArrayValue) elementValue, type.getElementType()));
+ }
+ else
+ {
+ MetaValue value = createMetaValue(elementValue, type.getElementType());
+ values.add(value);
+ }
+ }
+ return new ArrayValueSupport(type, values.toArray());
+ }
+
+ /**
+ * Recreate the array values.
+ *
+ * @param valueElement the persisted xml value
+ * @param type the element type
+ * @return the recreated array
+ */
+ @SuppressWarnings("unchecked")
+ protected Object recreateArrayValue(PersistedArrayValue valueElement, MetaType type)
+ {
+ List values = new ArrayList(valueElement.size());
+ for(PersistedValue elementValue : valueElement.getValues())
+ {
+ if(elementValue instanceof PersistedArrayValue)
+ {
+ values.add(
+ recreateArrayValue((PersistedArrayValue) elementValue, type));
+ }
+ else
+ {
+ MetaValue value = createMetaValue(elementValue, type);
+ values.add(value);
+ }
+ }
+ return values.toArray();
+ }
+
+ /**
+ * 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;
+ }
+ }
+
+}
Deleted: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreationHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreationHandler.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectRecreationHandler.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -1,567 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, 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.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-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.MutableManagedObject;
-import org.jboss.managed.api.annotation.ViewUse;
-import org.jboss.metadata.spi.MetaData;
-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.MapCompositeMetaType;
-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.MapCompositeValueSupport;
-import org.jboss.metatype.api.values.MetaValue;
-import org.jboss.metatype.api.values.SimpleValue;
-import org.jboss.metatype.api.values.SimpleValueSupport;
-import org.jboss.metatype.api.values.TableValue;
-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.PersistedTableValue;
-import org.jboss.system.server.profileservice.persistence.xml.PersistedValue;
-
-/**
- * This handler recreates only the ManagedObject based on the persisted meta data.
- *
- * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
- * @version $Revision$
- */
-public class ManagedObjectRecreationHandler
-{
- /** The meta data. */
- private MetaData metaData;
-
- /** The plugin. */
- private MOCreatorPlugin plugin;
-
- /** The simple types. */
- private final static Map<String, Class<? extends Serializable>> simpleTypes = new HashMap<String, Class<? extends Serializable>>();
-
- /** The logger. */
- private final static Logger log = Logger.getLogger(ManagedObjectRecreationHandler.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);
- // primitive classes
- simpleTypes.put(byte.class.getName(), byte.class);
- simpleTypes.put(char.class.getName(), char.class);
- simpleTypes.put(double.class.getName(), double.class);
- simpleTypes.put(float.class.getName(), float.class);
- simpleTypes.put(int.class.getName(), int.class);
- simpleTypes.put(short.class.getName(), short.class);
- simpleTypes.put(long.class.getName(), long.class);
- simpleTypes.put(boolean.class.getName(), boolean.class);
- }
-
- public ManagedObjectRecreationHandler()
- {
- this.plugin = new MOCreatorPlugin();
- }
-
- public ManagedObjectRecreationHandler(MOCreatorPlugin plugin)
- {
- this.plugin = plugin;
- }
-
- public ManagedObjectRecreationHandler(MOCreatorPlugin plugin, MetaData metaData)
- {
- this(plugin);
- this.metaData = metaData;
- }
-
- public ManagedObject createManagedObject(PersistedManagedObject moElement) throws Exception
- {
- if(moElement == null)
- throw new IllegalArgumentException("Null persisted meta data.");
- if(moElement.getClassName() == null)
- throw new IllegalArgumentException("Null class-name for managed object.");
-
- // Check if we need to use a template for the managed object
- String className = moElement.getTemplateName();
- if(className == null)
- {
- className = moElement.getClassName();
- }
- ManagedObject mo = buildManagedObject(className);
-
-
- if(mo instanceof MutableManagedObject)
- {
- // Update the name
- ((MutableManagedObject) mo).setName(moElement.getName());
-
- if(moElement.getTemplateName() != null)
- {
- // Override the actual attachment
- Class<?> attachment = plugin.getClassLoader().loadClass(moElement.getClassName());
- ((MutableManagedObject) mo).setAttachment(attachment.newInstance());
- }
- }
-
- // Process
- processManagedObject(moElement, mo);
-
- // Return
- return mo;
- }
-
- /**
- * Process a ManagedObject.
- *
- * @param moElement the persisted xml meta data.
- * @param mo the managed object
- * @return the attachment meta data.
- */
- public void processManagedObject(PersistedManagedObject moElement, ManagedObject mo)
- {
- if(moElement == null)
- throw new IllegalArgumentException("Null rootElement.");
- if(mo == null)
- throw new IllegalArgumentException("null managedObject");
-
- // Get the xml to see what we need to merge
- if(moElement.getProperties() != null && moElement.getProperties().isEmpty() == false)
- {
- for(PersistedProperty propertyElement : moElement.getProperties())
- {
- ManagedProperty property = mo.getProperty(propertyElement.getName());
- if(property == null)
- throw new IllegalStateException("unable to find propery: "+ property);
-
- // Skip all non configuration properties
- if(! property.hasViewUse(ViewUse.CONFIGURATION))
- continue;
-
- // getProperty
- processManagedProperty(propertyElement, propertyElement.getName(), property, mo.getAttachment());
- }
- }
- }
-
- /**
- * Build a managedObject based on the className
- *
- * @param className the name of the class
- * @return the ManagedObject
- * @throws Exception
- */
- protected ManagedObject buildManagedObject(String className) throws Exception
- {
- return plugin.createManagedObject(className, metaData);
- }
-
- /**
- * Process a ManagedProperty.
- *
- * @param propertyElement the persisted xml meta data.
- * @param name the property name.
- * @param property the managed property.
- */
- protected void processManagedProperty(PersistedProperty propertyElement, String name, ManagedProperty property, Object attachment)
- {
- // Get MetaType
- MetaType metaType = property.getMetaType();
- // Process the value
- PersistedValue persistedValue = propertyElement.getValue();
- if(persistedValue != null)
- {
- MetaValue value = createMetaValue(persistedValue, metaType);
- // Set the value Field
- property.setField(Fields.VALUE, value);
- // Update meta type
- if(value != null)
- property.setField(Fields.META_TYPE, value.getMetaType());
- }
- // Done
- }
-
- /**
- * Create the meta value, based on the xml persited
- * value and the generated MetaType.
- *
- * @param valueElement the persisted xml element
- * @param type the meta type.
- * @return the created meta value.
- */
- protected MetaValue createMetaValue(PersistedValue valueElement, MetaType metaType)
- {
- if(log.isTraceEnabled())
- {
- log.trace("processing value " + valueElement + " type: " + metaType);
- }
-
- if(valueElement == null)
- return null;
-
- MetaValue metaValue = null;
- if(metaType.isSimple())
- {
- metaValue = createSimpleValue(
- (PersistedSimpleValue) valueElement,
- (SimpleMetaType) metaType);
- }
- else if(metaType.isEnum())
- {
- metaValue = createEnumValue(
- (PersistedEnumValue) valueElement,
- (EnumMetaType) metaType);
- }
- else if(metaType.isCollection())
- {
- metaValue = createCollectionValue(
- (PersistedCollectionValue) valueElement,
- (CollectionMetaType) metaType);
- }
- else if(metaType.isGeneric())
- {
- metaValue = createGenericValue(
- (PersistedGenericValue) valueElement,
- (GenericMetaType) metaType);
- }
- else if(metaType.isComposite())
- {
- metaValue = createCompositeValue(
- (PersistedCompositeValue) valueElement,
- (CompositeMetaType) metaType);
- }
- else if(metaType.isTable())
- {
- metaValue = createTableValue(
- (PersistedTableValue) valueElement,
- (TableMetaType)metaType);
- }
- else if(metaType.isArray())
- {
- metaValue = createArrayValue(
- (PersistedArrayValue) valueElement,
- (ArrayMetaType) metaType);
- }
- else
- {
- throw new IllegalStateException("unknown metaType");
- }
- return metaValue;
- }
-
- /**
- * Create simple value.
- *
- * @param valueElement the persisted xml meta data.
- * @param value the simple value.
- * @return a simple value.
- */
- protected SimpleValue createSimpleValue(PersistedSimpleValue valueElement, SimpleMetaType metaType)
- {
- 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);
- }
-
- /**
- * Process an Enum value.
- *
- * @param enumElement the persisted xml meta data.
- * @param value the enum value.
- * @return a enum value.
- */
- protected EnumValue createEnumValue(PersistedEnumValue enumElement, EnumMetaType type)
- {
- return new EnumValueSupport(type, enumElement.getValue());
- }
-
- /**
- * Create composite value.
- *
- * @param composite the persisted xml meta data.
- * @param value the composite value.
- * @return a composite value.
- */
- protected CompositeValue createCompositeValue(PersistedCompositeValue composite, CompositeMetaType type)
- {
- // Handle the mapCompositeMetaType differently
- if(type instanceof MapCompositeMetaType)
- return handleMapCompositeMetaType(composite, (MapCompositeMetaType) type);
-
- // Create composite value
- Map<String, MetaValue> values = new HashMap<String, MetaValue>();
- if(composite.getValues() != null && composite.getValues().isEmpty() == false)
- {
- for(PersistedValue persistedValue : composite.getValues())
- {
- MetaType elementType = type.getType(persistedValue.getName());
- if(elementType == null)
- throw new IllegalStateException("Failed to process composite value: " + persistedValue.getName());
-
- // Create
- MetaValue metaValue = createMetaValue(persistedValue, elementType);
- // Put
- values.put(persistedValue.getName(), metaValue);
- }
- }
- return new CompositeValueSupport(type, values);
- }
-
- /**
- * Create the MapCompositeValueSupport value.
- *
- * @param composite the persisted composite xml meta data
- * @param type the MapComposite meta type
- * @return the MapCompositeValueSupport
- */
- protected MapCompositeValueSupport handleMapCompositeMetaType(PersistedCompositeValue composite, MapCompositeMetaType type)
- {
- Map<String, MetaValue> values = new HashMap<String, MetaValue>();
- if(composite.getValues() != null && composite.getValues().isEmpty() == false)
- {
- for(PersistedValue persistedValue : composite.getValues())
- {
- MetaValue value = createMetaValue(persistedValue, type.getValueType());
- values.put(persistedValue.getName(), value);
- }
- }
- return new MapCompositeValueSupport(values, type);
- }
-
- /**
- * Process a collection.
- *
- * @param collection the persisted xml meta data.
- * @param value the collection value.
- * @return a collection value.
- */
- protected CollectionValue createCollectionValue(PersistedCollectionValue collection, CollectionMetaType type)
- {
- List<MetaValue> elementList = new ArrayList<MetaValue>();
- if(collection.getValues() != null && collection.getValues().isEmpty() == false)
- {
- for(PersistedValue element : collection.getValues())
- {
- elementList.add(
- createMetaValue(element, type.getElementType()));
- }
- }
- return new CollectionValueSupport(type, elementList.toArray(new MetaValue[elementList.size()]));
- }
-
- /**
- * Create generic value.
- *
- * @param genericElement the persisted generic xml meta data
- * @param metaType the generic meta type
- * @return the generic value
- */
- protected GenericValue createGenericValue(PersistedGenericValue genericElement, GenericMetaType metaType)
- {
- if (ManagedObject.class.getName().equals(metaType.getTypeName()))
- {
- PersistedManagedObject persisted = genericElement.getManagedObject();
- ManagedObject mo = null;
- if(persisted != null)
- {
- try
- {
- mo = createManagedObject(persisted);
- }
- catch (Throwable t)
- {
- throw new RuntimeException("Could not create ManagedObject for class: " + persisted.getClassName(), t);
- }
- }
- return new GenericValueSupport(metaType, mo);
- }
- throw new IllegalStateException("Cannot reproduce generic object: " + metaType);
- }
-
- /**
- * Create the table value.
- *
- * @param table the persisted table value.
- * @param type the table meta type.
- * @return the table value.
- */
- protected TableValue createTableValue(PersistedTableValue table, TableMetaType type)
- {
- TableValueSupport support = new TableValueSupport(type);
- if(table.getEntries() != null && table.getEntries().isEmpty() == false)
- {
- for(PersistedCompositeValue entry : table.getEntries())
- {
- support.put(createCompositeValue(entry, type.getRowType()));
- }
- }
- return support;
- }
-
- /**
- * Create array value.
- *
- * @param valueElement the persisted array xml value
- * @param type the array meta type
- * @return the array value
- */
- @SuppressWarnings("unchecked")
- protected ArrayValue createArrayValue(PersistedArrayValue valueElement, ArrayMetaType type)
- {
- int size = valueElement.size();
- List values = new ArrayList(size);
- for(PersistedValue elementValue : valueElement.getValues())
- {
- if(elementValue instanceof PersistedArrayValue)
- {
- values.add(
- recreateArrayValue((PersistedArrayValue) elementValue, type.getElementType()));
- }
- else
- {
- MetaValue value = createMetaValue(elementValue, type.getElementType());
- values.add(value);
- }
- }
- return new ArrayValueSupport(type, values.toArray());
- }
-
- /**
- * Recreate the array values.
- *
- * @param valueElement the persisted xml value
- * @param type the element type
- * @return the recreated array
- */
- @SuppressWarnings("unchecked")
- protected Object recreateArrayValue(PersistedArrayValue valueElement, MetaType type)
- {
- List values = new ArrayList(valueElement.size());
- for(PersistedValue elementValue : valueElement.getValues())
- {
- if(elementValue instanceof PersistedArrayValue)
- {
- values.add(
- recreateArrayValue((PersistedArrayValue) elementValue, type));
- }
- else
- {
- MetaValue value = createMetaValue(elementValue, type);
- values.add(value);
- }
- }
- return values.toArray();
- }
-
- /**
- * 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;
- }
- }
-
-}
Deleted: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/ManagedObjectUpdateHandler.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -1,94 +0,0 @@
-/*
- * 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 org.jboss.beans.info.spi.PropertyInfo;
-import org.jboss.logging.Logger;
-import org.jboss.managed.api.Fields;
-import org.jboss.managed.api.ManagedProperty;
-import org.jboss.managed.api.factory.ManagedObjectFactory;
-import org.jboss.system.server.profileservice.persistence.xml.PersistedProperty;
-
-/**
- * Handler for merging the persisted information with a ManagedObject.
- *
- * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
- * @version $Revision$
- */
- at Deprecated
-public class ManagedObjectUpdateHandler extends ManagedObjectRecreationHandler
-{
-
- /** The attachment property populator. */
- private AttachmentPropertyPopulator populator;
-
- /** The logger. */
- private final static Logger log = Logger.getLogger(ManagedObjectUpdateHandler.class);
-
- public ManagedObjectUpdateHandler()
- {
- super();
- this.populator = new AttachmentPropertyPopulator(ManagedObjectFactory.getInstance());
- }
-
- public ManagedObjectUpdateHandler(MOCreatorPlugin plugin)
- {
- super(plugin);
- this.populator = new AttachmentPropertyPopulator(plugin.getMOF());
- }
-
- /**
- * 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.
- */
- @Override
- 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;
- }
-
- // Recreate the MO from xml
- super.processManagedProperty(propertyElement, name, property, attachment);
-
- try
- {
- this.populator.processManagedProperty(name, property, attachment);
- }
- catch(Throwable t)
- {
- log.debug("Cannot popluate value information of porperty" + property);
- }
- }
-
-}
Modified: 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 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/deployer/ProfileServicePersistenceDeployer.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -30,8 +30,9 @@
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.ManagedObjectOverrideHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
/**
@@ -48,9 +49,10 @@
public static final String PERSISTED_ATTACHMENT_PREFIX = "PERISTED";
/** The managed object override handler. */
- private ManagedObjectOverrideHandler overrideHandler = new ManagedObjectOverrideHandler();
+ private ManagedGenericOverrideHandler overrideHandler = new ManagedGenericOverrideHandler();
- private final ManagedObjectFactory factory = ManagedObjectFactory.getInstance();
+ /** The managed object factory. */
+ private final ManagedObjectFactory factory = ManagedObjectFactoryBuilder.create();
/** The Logger. */
private static final Logger log = Logger.getLogger(ProfileServicePersistenceDeployer.class);
@@ -65,8 +67,6 @@
@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();
@@ -80,15 +80,16 @@
Object instance = unit.getTransientManagedObjects().getAttachment(attachment);
try
{
- // Create the original
+ MetaData metaData = unit.getMetaData();
ManagedObject original = factory.initManagedObject(instance, metaData);
+
// Update the attachment
- original = overrideHandler.updateManagedObject(persistedManagedObject, original);
+ overrideHandler.updateManagedObject(original, persistedManagedObject, instance);
// Override
unit.addAttachment(attachment, original.getAttachment());
}
- catch(Exception e)
+ catch(Throwable e)
{
log.debug("Failed to update the persisted attachment information", e);
}
Modified: trunk/system/src/main/org/jboss/system/server/profileservice/persistence/xml/PersistedGenericValue.java
===================================================================
--- trunk/system/src/main/org/jboss/system/server/profileservice/persistence/xml/PersistedGenericValue.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/main/org/jboss/system/server/profileservice/persistence/xml/PersistedGenericValue.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -23,6 +23,8 @@
import static org.jboss.system.server.profileservice.persistence.xml.PersistenceConstants.MANAGED_OBJECT_ELEMENT_NAME;
+import java.io.Serializable;
+
import javax.xml.bind.annotation.XmlElement;
/**
@@ -31,9 +33,12 @@
* @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
* @version $Revision$
*/
-public class PersistedGenericValue extends AbstractPersisitedValue implements PersistedValue
+public class PersistedGenericValue extends AbstractPersisitedValue implements PersistedValue, Serializable
{
+ /** The serialVersionUID */
+ private static final long serialVersionUID = -6858198681439447173L;
+
/** A managed-object. */
private PersistedManagedObject managedObject;
Added: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/GenericSupportMetaData.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/GenericSupportMetaData.java (rev 0)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/GenericSupportMetaData.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.test.server.profileservice.persistence.support;
+
+import java.util.List;
+
+import org.jboss.managed.api.annotation.ManagementObject;
+import org.jboss.managed.api.annotation.ManagementProperty;
+
+/**
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+ at ManagementObject
+public class GenericSupportMetaData
+{
+
+ List<SimpleGenericMetaData> list;
+
+ @ManagementProperty(managed = true)
+ public List<SimpleGenericMetaData> getList()
+ {
+ return list;
+ }
+
+ public void setList(List<SimpleGenericMetaData> list)
+ {
+ this.list = list;
+ }
+
+}
+
Added: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/SimpleGenericMetaData.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/SimpleGenericMetaData.java (rev 0)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/support/SimpleGenericMetaData.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,89 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.test.server.profileservice.persistence.support;
+
+import org.jboss.managed.api.annotation.ManagementObject;
+import org.jboss.managed.api.annotation.ManagementObjectID;
+import org.jboss.managed.api.annotation.ManagementProperty;
+
+/**
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+ at ManagementObject
+public class SimpleGenericMetaData
+{
+
+ private String string;
+
+ private Integer integer;
+
+ private SimpleGenericMetaData child;
+
+ public SimpleGenericMetaData()
+ {
+ //
+ }
+
+ public SimpleGenericMetaData(String string, int integer, SimpleGenericMetaData child)
+ {
+ this.string = string;
+ this.integer = integer;
+ this.child = child;
+ }
+
+ @ManagementObjectID
+ @ManagementProperty
+ public String getString()
+ {
+ return string;
+ }
+
+ public void setString(String string)
+ {
+ this.string = string;
+ }
+
+ @ManagementProperty
+ public Integer getInteger()
+ {
+ return integer;
+ }
+
+ public void setInteger(Integer integer)
+ {
+ this.integer = integer;
+ }
+
+ @ManagementProperty
+ public SimpleGenericMetaData getChild()
+ {
+ return child;
+ }
+
+ public void setChild(SimpleGenericMetaData child)
+ {
+ this.child = child;
+ }
+
+}
+
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/ArrayValueUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/ArrayValueUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/ArrayValueUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -25,7 +25,7 @@
import org.jboss.managed.api.ManagedObject;
import org.jboss.metatype.api.values.ArrayValue;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectRecreationHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
import org.jboss.test.server.profileservice.persistence.support.SimpleArrayMetaData;
@@ -43,15 +43,15 @@
super(name);
}
- public void testSimpleArray() throws Exception
+ public void testSimpleArray() throws Throwable
{
ManagedObject mo = initMO();
PersistedManagedObject moElement = restore(mo);
assertNotNull(moElement);
- ManagedObjectRecreationHandler handler = new ManagedObjectRecreationHandler();
- ManagedObject restored = handler.createManagedObject(moElement);
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ ManagedObject restored = handler.update(new SimpleArrayMetaData(), moElement);
assertNotNull(restored.getProperties());
assertEquals(3, restored.getProperties().size());
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/BasicPersistenceFormatUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/BasicPersistenceFormatUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/BasicPersistenceFormatUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -36,7 +36,7 @@
import org.jboss.metatype.api.values.MetaValue;
import org.jboss.metatype.api.values.SimpleValue;
import org.jboss.metatype.api.values.SimpleValueSupport;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectUpdateHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
import org.jboss.test.server.profileservice.persistence.support.PrimitiveMetaData;
import org.jboss.test.server.profileservice.persistence.support.TestMetaData;
@@ -53,7 +53,7 @@
// FIXME SimplePersistenceFormatUnitTestCase constructor
super(name);
}
- public void testPrimitive() throws Exception
+ public void testPrimitive() throws Throwable
{
// Create initial model
ManagedObject mo = createPrimitiveMO();
@@ -72,21 +72,16 @@
PersistedManagedObject restored = restore(mo);
assertNotNull(restored);
- ManagedObjectUpdateHandler merger = new ManagedObjectUpdateHandler();
- // init again;
- mo = createPrimitiveMO();
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ mo = handler.update(createPrimitive(), restored);
- assertEquals("testName", getMVF().unwrap(mo.getProperty("name").getValue()));
-
- merger.processManagedObject(restored, mo);
-
assertEquals("newName", getMVF().unwrap(mo.getProperty("name").getValue()));
assertEquals(111, getMVF().unwrap(mo.getProperty("integer").getValue()));
assertEquals("optional", ((SimpleValue) mo.getProperty("optionalName").getValue()).getValue());
}
- public void testMetaDataPrimitive() throws Exception
+ public void testMetaDataPrimitive() throws Throwable
{
ManagedObject mo = createTestMetaDataMO();
assertNotNull(mo);
@@ -116,12 +111,9 @@
assertNotNull(restored);
// Create again
- mo = createTestMetaDataMO();
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ mo = handler.update(createTestMetaData(), restored);
- // Merge
- ManagedObjectUpdateHandler merger = new ManagedObjectUpdateHandler();
- merger.processManagedObject(restored, mo);
-
p = mo.getProperty("primitive");
nested = (CompositeValue) p.getValue();
name = nested.get("name");
@@ -135,7 +127,7 @@
}
- public void testMetaDataMap() throws Exception
+ public void testMetaDataMap() throws Throwable
{
ManagedObject mo = createTestMetaDataMO();
assertNotNull(mo);
@@ -154,11 +146,9 @@
PersistedManagedObject restored = restore(mo);
assertNotNull(restored);
- mo = createTestMetaDataMO();
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ mo = handler.update(createTestMetaData(), restored);
- ManagedObjectUpdateHandler handler = new ManagedObjectUpdateHandler();
- mo = handler.createManagedObject(restored);
-
p = mo.getProperty("testMap");
assertNotNull(p);
@@ -169,7 +159,7 @@
assertEquals("changedString", v.getValue());
}
- public void testPrimitiveArray() throws Exception
+ public void testPrimitiveArray() throws Throwable
{
ManagedObject mo = createTestMetaDataMO();
assertNotNull(mo);
@@ -188,11 +178,9 @@
assertNotNull(restored);
//
- mo = createTestMetaDataMO();
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ mo = handler.update(createTestMetaData(), restored);
- ManagedObjectUpdateHandler handler = new ManagedObjectUpdateHandler();
- handler.processManagedObject(restored, mo);
-
p = mo.getProperty("charArray");
assertNotNull(p);
@@ -242,7 +230,6 @@
test.setTestMap(map);
-
// Create primitive array
Character[] array = new Character[3];
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/CollectionValueUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/CollectionValueUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/CollectionValueUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -25,7 +25,7 @@
import java.util.List;
import org.jboss.managed.api.ManagedObject;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectUpdateHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
import org.jboss.test.server.profileservice.persistence.support.CollectionMetaData;
@@ -41,14 +41,15 @@
super(name);
}
- public void test() throws Exception
+ public void test() throws Throwable
{
ManagedObject mo = initMO();
PersistedManagedObject moElement = restore(mo);
assertNotNull(moElement);
- ManagedObjectUpdateHandler handler = new ManagedObjectUpdateHandler();
- ManagedObject restored = handler.createManagedObject(moElement);
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ ManagedObject restored = handler.update(createMetaData(), moElement);
+
assertNotNull(restored);
assertNotNull(restored.getProperty("collection"));
@@ -60,14 +61,18 @@
}
-
- protected ManagedObject initMO()
+ protected CollectionMetaData createMetaData()
{
CollectionMetaData instance = new CollectionMetaData();
instance.setCollection(initList());
- return getMOF().initManagedObject(instance, null);
+ return instance;
}
+ protected ManagedObject initMO()
+ {
+ return getMOF().initManagedObject(createMetaData(), null);
+ }
+
protected List<String> initList()
{
List<String> list = new ArrayList<String>();
Added: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/GenericValueUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/GenericValueUnitTestCase.java (rev 0)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/GenericValueUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.test.server.profileservice.persistence.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.managed.api.ManagedObject;
+import org.jboss.metatype.api.values.CollectionValue;
+import org.jboss.metatype.api.values.GenericValue;
+import org.jboss.metatype.api.values.SimpleValueSupport;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
+import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
+import org.jboss.test.server.profileservice.persistence.support.GenericSupportMetaData;
+import org.jboss.test.server.profileservice.persistence.support.SimpleGenericMetaData;
+
+/**
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class GenericValueUnitTestCase extends AbstractPersistenceFormatTest
+{
+
+ public GenericValueUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void test() throws Throwable
+ {
+
+ ManagedObject mo = createTestMO();
+
+ CollectionValue collection = (CollectionValue) mo.getProperty("list").getValue();
+ ManagedObject child2 = (ManagedObject) ((GenericValue) collection.getElements()[1]).getValue();
+ child2.getProperty("string").setValue(SimpleValueSupport.wrap("changedName"));
+
+ PersistedManagedObject moElement = restore(mo);
+
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ ManagedObject restored = handler.update(createTestMetaData(), moElement);
+
+ GenericSupportMetaData md = (GenericSupportMetaData) restored.getAttachment();
+ assertNotNull(md);
+
+ SimpleGenericMetaData child = md.getList().get(1);
+ assertEquals(child.getString(), "changedName");
+ }
+
+ private ManagedObject createTestMO()
+ {
+ return getMOF().initManagedObject(createTestMetaData(), null);
+ }
+
+ private GenericSupportMetaData createTestMetaData()
+ {
+ GenericSupportMetaData metaData = new GenericSupportMetaData();
+
+ List<SimpleGenericMetaData> list = new ArrayList<SimpleGenericMetaData>();
+
+ SimpleGenericMetaData child1 = createSImple("child1", 11, null);
+ SimpleGenericMetaData child2 = createSImple("child2", 21, null);
+
+ list.add(createSImple("parent1", 1, child1));
+ list.add(createSImple("parent2", 2, child2));
+
+ metaData.setList(list);
+
+ return metaData;
+ }
+
+ private SimpleGenericMetaData createSImple(String string, int integer, SimpleGenericMetaData child)
+ {
+ return new SimpleGenericMetaData(string, integer, child);
+ }
+}
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/MapCompositeUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/MapCompositeUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/MapCompositeUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -27,7 +27,7 @@
import org.jboss.managed.api.ManagedObject;
import org.jboss.managed.api.ManagedProperty;
import org.jboss.metatype.api.values.MapCompositeValueSupport;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectRecreationHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
import org.jboss.test.server.profileservice.persistence.support.SimpleStringMapMetaData;
@@ -43,7 +43,7 @@
super(name);
}
- public void test() throws Exception
+ public void test() throws Throwable
{
ManagedObject mo = initMO();
@@ -58,19 +58,23 @@
PersistedManagedObject moElement = restore(mo);
assertNotNull(moElement);
- ManagedObjectRecreationHandler handler = new ManagedObjectRecreationHandler();
- ManagedObject restored = handler.createManagedObject(moElement);
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ ManagedObject restored = handler.update(createTestMetaData(), moElement);
assertNotNull(restored);
}
- protected ManagedObject initMO()
+ protected SimpleStringMapMetaData createTestMetaData()
{
SimpleStringMapMetaData instance = new SimpleStringMapMetaData();
instance.setMap(initMap());
-
- return getMOF().initManagedObject(instance, null);
+ return instance;
}
+ protected ManagedObject initMO()
+ {
+ return getMOF().initManagedObject(createTestMetaData(), null);
+ }
+
protected Map<String, String> initMap()
{
Map<String, String> map = new HashMap<String, String>();
Added: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/PersistenceFormatTestSuite.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/PersistenceFormatTestSuite.java (rev 0)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/PersistenceFormatTestSuite.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, 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.test.server.profileservice.persistence.test;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class PersistenceFormatTestSuite extends TestSuite
+{
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("PS persistence format tests.");
+
+ suite.addTestSuite(ArrayValueUnitTestCase.class);
+ suite.addTestSuite(BasicPersistenceFormatUnitTestCase.class);
+ suite.addTestSuite(CollectionValueUnitTestCase.class);
+ suite.addTestSuite(GenericValueUnitTestCase.class);
+ suite.addTestSuite(MapCompositeUnitTestCase.class);
+ suite.addTestSuite(TableValueUnitTestCase.class);
+ suite.addTestSuite(TestNestedPeristenceFormatUnitTestCase.class);
+
+ return suite;
+ }
+
+}
+
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TableValueUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TableValueUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TableValueUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -33,7 +33,7 @@
import org.jboss.metatype.api.values.SimpleValue;
import org.jboss.metatype.api.values.SimpleValueSupport;
import org.jboss.metatype.api.values.TableValue;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectRecreationHandler;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
import org.jboss.test.server.profileservice.persistence.support.SimpleTableMetaData;
@@ -52,7 +52,7 @@
super(name);
}
- public void testSimpleTableValue() throws Exception
+ public void testSimpleTableValue() throws Throwable
{
ManagedObject mo = initMO();
assertNotNull(mo);
@@ -63,8 +63,8 @@
assertNotNull(moElement.getProperties());
// Recreate MO
- ManagedObjectRecreationHandler handler = new ManagedObjectRecreationHandler();
- ManagedObject restored = handler.createManagedObject(moElement);
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ ManagedObject restored = handler.update(createTestMetaData(), moElement);
// Assert
ManagedProperty p = restored.getProperty("map");
@@ -86,14 +86,18 @@
assertNotNull(simple);
assertEquals(value, simple.getValue());
}
-
- protected ManagedObject initMO()
+
+ protected SimpleTableMetaData createTestMetaData()
{
SimpleTableMetaData t = new SimpleTableMetaData();
t.setMap(initMap());
-
- return getMOF().initManagedObject(t, null);
+ return t;
}
+
+ protected ManagedObject initMO()
+ {
+ return getMOF().initManagedObject(createTestMetaData(), null);
+ }
protected Map<Integer, String> initMap()
{
Modified: trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TestNestedPeristenceFormatUnitTestCase.java
===================================================================
--- trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TestNestedPeristenceFormatUnitTestCase.java 2009-03-04 13:09:08 UTC (rev 85219)
+++ trunk/system/src/tests/org/jboss/test/server/profileservice/persistence/test/TestNestedPeristenceFormatUnitTestCase.java 2009-03-04 14:23:43 UTC (rev 85220)
@@ -36,8 +36,8 @@
import org.jboss.metatype.api.values.MetaValue;
import org.jboss.metatype.api.values.SimpleValue;
import org.jboss.metatype.api.values.SimpleValueSupport;
+import org.jboss.system.server.profileservice.persistence.ManagedGenericOverrideHandler;
import org.jboss.system.server.profileservice.persistence.ManagedObjectPeristenceHandler;
-import org.jboss.system.server.profileservice.persistence.ManagedObjectUpdateHandler;
import org.jboss.system.server.profileservice.persistence.xml.PersistedCollectionValue;
import org.jboss.system.server.profileservice.persistence.xml.PersistedGenericValue;
import org.jboss.system.server.profileservice.persistence.xml.PersistedManagedObject;
@@ -58,7 +58,7 @@
super(name);
}
- public void test() throws Exception
+ public void test() throws Throwable
{
ManagedObject mo = createNestedMO();
@@ -93,7 +93,7 @@
assertNotNull(property);
property.setValue(SimpleValueSupport.wrap("ChangedName3"));
- PersistedManagedObject restored = process(mo);
+ PersistedManagedObject restored = restore(mo);
assertNotNull(restored);
PersistedCollectionValue collection = (PersistedCollectionValue) restored.getProperties().get(0).getValue();
assertNotNull(collection);
@@ -103,8 +103,8 @@
enableTrace("org.jboss.system.server.profileservice.persistence");
// Recreate
- ManagedObjectUpdateHandler test = new ManagedObjectUpdateHandler();
- mo = test.createManagedObject(restored);
+ ManagedGenericOverrideHandler handler = new ManagedGenericOverrideHandler();
+ mo = handler.update(createNestedTestMetaData(), restored);
deployment = getDeployment("ChangedName", mo);
assertNotNull("changed name deployment null", deployment);
More information about the jboss-cvs-commits
mailing list