[jboss-cvs] JBossAS SVN: r73648 - in projects/jboss-deployers/trunk: deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat May 24 03:48:39 EDT 2008


Author: alesj
Date: 2008-05-24 03:48:39 -0400 (Sat, 24 May 2008)
New Revision: 73648

Modified:
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/AbstractVFSParsingDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/JBossExtensionDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/MultipleSchemaResolverDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/merge/support/TestBeanMergeDeployer.java
Log:
Improved metadata instances handling.

Modified: projects/jboss-deployers/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/merge/support/TestBeanMergeDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/merge/support/TestBeanMergeDeployer.java	2008-05-24 06:31:15 UTC (rev 73647)
+++ projects/jboss-deployers/trunk/deployers-vfs/src/tests/org/jboss/test/deployers/vfs/deployer/merge/support/TestBeanMergeDeployer.java	2008-05-24 07:48:39 UTC (rev 73648)
@@ -45,7 +45,7 @@
       setNames(new HashSet<String>(Arrays.asList("first-beans.xml", "snd-beans.xml")));
    }
 
-   protected KernelDeployment mergeFiles(VFSDeploymentUnit unit, KernelDeployment root, Set<VirtualFile> files, Set<String> missingFiles) throws Exception
+   protected KernelDeployment mergeFiles(VFSDeploymentUnit unit, KernelDeployment root, List<VirtualFile> files, Set<String> missingFiles) throws Exception
    {
       AbstractKernelDeployment deployment = new AbstractKernelDeployment();
       for (VirtualFile file : files)

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/AbstractVFSParsingDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/AbstractVFSParsingDeployer.java	2008-05-24 06:31:15 UTC (rev 73647)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/AbstractVFSParsingDeployer.java	2008-05-24 07:48:39 UTC (rev 73648)
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.Set;
 import java.util.HashSet;
+import java.util.ArrayList;
 import java.io.InputStream;
 import java.io.IOException;
 
@@ -137,7 +138,7 @@
 
       VFSDeploymentUnit  vfsDeploymentUnit = (VFSDeploymentUnit)unit;
 
-      Set<VirtualFile> files = new HashSet<VirtualFile>();
+      List<VirtualFile> files = new ArrayList<VirtualFile>();
       Set<String> missingFiles = new HashSet<String>();
 
       for (String name : names)
@@ -195,7 +196,7 @@
 
       VFSDeploymentUnit  vfsDeploymentUnit = (VFSDeploymentUnit)unit;
 
-      Set<VirtualFile> files = new HashSet<VirtualFile>();
+      List<VirtualFile> files = new ArrayList<VirtualFile>();
       Set<String> missingFiles = new HashSet<String>();
 
       for (String name : names)
@@ -224,7 +225,7 @@
     * @return merged metadata
     * @throws Exception for any error
     */
-   protected T mergeFiles(VFSDeploymentUnit unit, T root, Set<VirtualFile> files, Set<String> missingFiles) throws Exception
+   protected T mergeFiles(VFSDeploymentUnit unit, T root, List<VirtualFile> files, Set<String> missingFiles) throws Exception
    {
       return null;
    }

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/JBossExtensionDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/JBossExtensionDeployer.java	2008-05-24 06:31:15 UTC (rev 73647)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/JBossExtensionDeployer.java	2008-05-24 07:48:39 UTC (rev 73648)
@@ -22,6 +22,7 @@
 package org.jboss.deployers.vfs.spi.deployer;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -37,7 +38,9 @@
  */
 public abstract class JBossExtensionDeployer<U, V, T> extends MultipleSchemaResolverDeployer<T>
 {
+   private String specName;
    private Class<U> specClass;
+   private String jbossName;
    private Class<V> jbossClass;
 
    public JBossExtensionDeployer(Class<T> output, String specName, Class<U> specClass, String jbossName, Class<V> jbossClass)
@@ -52,7 +55,9 @@
          throw new IllegalArgumentException("Null spec class");
       if (jbossClass == null)
          throw new IllegalArgumentException("Null jboss class");
+      this.specName = specName;
       this.specClass = specClass;
+      this.jbossName = jbossName;
       this.jbossClass = jbossClass;
    }
 
@@ -64,14 +69,54 @@
       return map;
    }
 
-   protected T mergeMetaData(VFSDeploymentUnit unit, Map<Class<?>, Object> metadata) throws Exception
+   protected T mergeMetaData(VFSDeploymentUnit unit, T root, Map<Class<?>, List<Object>> metadata, Set<String> missingFiles) throws Exception
    {
-      U spec = specClass.cast(metadata.get(specClass));
-      V jboss = jbossClass.cast(metadata.get(jbossClass));
-      return mergeMetaData(unit, spec, jboss);
+      if (specClass.equals(jbossClass))
+      {
+         List<Object> instances = metadata.get(specClass);
+         if (instances == null || instances.isEmpty())
+            return mergeMetaData(unit, null, null);
+         else if (instances.size() == 1)
+         {
+            if (missingFiles.contains(jbossName))
+               return mergeMetaData(unit, specClass.cast(instances.iterator().next()), null);
+            else if (missingFiles.contains(specName))
+               return mergeMetaData(unit, null, jbossClass.cast(instances.iterator().next()));
+            else
+               throw new IllegalArgumentException("Should be either missing spec or jboss: " + missingFiles);
+         }
+         else
+            return mergeMetaData(unit, specClass.cast(instances.get(0)), jbossClass.cast(instances.get(1)));
+
+      }
+      else
+         return super.mergeMetaData(unit, root, metadata, missingFiles);
    }
 
+   protected T mergeMetaData(VFSDeploymentUnit unit, Map<Class<?>, List<Object>> metadata) throws Exception
+   {
+      return mergeMetaData(unit, getInstance(metadata, specClass), getInstance(metadata, jbossClass));
+   }
+
    /**
+    * Get metadata instance from metadata.
+    *
+    * @param metadata the metadatas map
+    * @param clazz metadata class
+    * @return matching metadata instance
+    */
+   protected <S> S getInstance(Map<Class<?>, List<Object>> metadata, Class<S> clazz)
+   {
+      List<Object> instances = metadata.get(clazz);
+      if (instances == null)
+         return null;
+      else if (instances.size() > 1)
+         throw new IllegalArgumentException("Expecting single instance: " + metadata);
+
+      return clazz.cast(instances.iterator().next());
+   }
+
+   /**
     * Merge spec and extension.
     *
     * @param unit deployment unit

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/MultipleSchemaResolverDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/MultipleSchemaResolverDeployer.java	2008-05-24 06:31:15 UTC (rev 73647)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/org/jboss/deployers/vfs/spi/deployer/MultipleSchemaResolverDeployer.java	2008-05-24 07:48:39 UTC (rev 73648)
@@ -22,10 +22,12 @@
 package org.jboss.deployers.vfs.spi.deployer;
 
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.HashSet;
-import java.util.HashMap;
+import java.util.ArrayList;
 
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
 import org.jboss.virtual.VirtualFile;
@@ -40,6 +42,7 @@
 public abstract class MultipleSchemaResolverDeployer<T> extends JBossXBDeployer<T>
 {
    private Map<String, Class<?>> mappings;
+   private Class<?> suffixClass;
    private Set<Class<?>> excluded;
    private Set<String> namespaces;
 
@@ -50,11 +53,20 @@
 
    public MultipleSchemaResolverDeployer(Class<T> output, Map<String, Class<?>> mappings, Set<Class<?>> excluded)
    {
+      this(output, mappings, null, null, excluded);
+   }
+
+   public MultipleSchemaResolverDeployer(Class<T> output, Map<String, Class<?>> mappings, String suffix, Class<?> suffixClass, Set<Class<?>> excluded)
+   {
       super(output);
       if (mappings == null || mappings.isEmpty())
          throw new IllegalArgumentException("Illegal mappings");
       this.mappings = mappings;
       setNames(mappings.keySet());
+      setSuffix(suffix);
+      if (suffix != null && suffixClass == null)
+         throw new IllegalArgumentException("Null suffix class");
+      this.suffixClass = suffixClass;
       if (excluded == null)
          excluded = Collections.emptySet();
       this.excluded = excluded;
@@ -68,18 +80,31 @@
    {
       for (Class<?> metadata : mappings.values())
       {
-         if (excluded.contains(metadata) == false)
-         {
-            String namespace = findNamespace(metadata);
-            if (namespace == null || JBossXmlConstants.DEFAULT.equals(namespace))
-               throw new IllegalArgumentException(
-                     "Registering schema with JBossXB is enabled, but cannot find namespace on class or package: " + metadata +
-                     ", perhaps missing @JBossXmlSchema or using default namespace attribute."
-               );
+         registerMetaDataClass(metadata);
+      }
+      if (suffixClass != null)
+         registerMetaDataClass(suffixClass);
+   }
 
-            addClassBinding(namespace, metadata);
-            namespaces.add(namespace);
-         }
+   /**
+    * Register metadata class as class binding,
+    * if not excluded.
+    *
+    * @param metadata the metadata class
+    */
+   protected void registerMetaDataClass(Class<?> metadata)
+   {
+      if (excluded.contains(metadata) == false)
+      {
+         String namespace = findNamespace(metadata);
+         if (namespace == null || JBossXmlConstants.DEFAULT.equals(namespace))
+            throw new IllegalArgumentException(
+                  "Registering schema with JBossXB is enabled, but cannot find namespace on class or package: " + metadata +
+                  ", perhaps missing @JBossXmlSchema or using default namespace attribute."
+            );
+
+         addClassBinding(namespace, metadata);
+         namespaces.add(namespace);
       }
    }
 
@@ -105,18 +130,15 @@
       Class<?> result = mappings.get(fileName);
       if (result == null)
       {
-         for(Map.Entry<String, Class<?>> entry : mappings.entrySet())
-         {
-            if (fileName.endsWith(entry.getKey()))
-            {
-               result = entry.getValue();
-               break;
-            }
-         }
+         if (getSuffix() != null && fileName.endsWith(getSuffix()))
+            result = suffixClass;
       }
 
       if (result == null)
-         throw new IllegalArgumentException("Should not be here, file '" + file + "' must macth some mapping: " + mappings);
+         throw new IllegalArgumentException(
+               "Should not be here, file '" + file +
+               "' must macth some mapping " + mappings + " or suffix class " + suffixClass
+         );
 
       return result;
    }
@@ -131,14 +153,20 @@
       return (T)parse(expectedClass, file);
    }
 
-   protected T mergeFiles(VFSDeploymentUnit unit, T root, Set<VirtualFile> files, Set<String> missingFiles) throws Exception
+   protected T mergeFiles(VFSDeploymentUnit unit, T root, List<VirtualFile> files, Set<String> missingFiles) throws Exception
    {
-      Map<Class<?> , Object> metadata = new HashMap<Class<?>, Object>();
+      Map<Class<?> , List<Object>> metadata = new HashMap<Class<?>, List<Object>>();
       for (VirtualFile file : files)
       {
          Class<?> clazz = matchFileToClass(file);
+         List<Object> instances = metadata.get(clazz);
+         if (instances == null)
+         {
+            instances = new ArrayList<Object>();
+            metadata.put(clazz, instances);
+         }
          Object instance = parse(clazz, file);
-         metadata.put(clazz, instance);
+         instances.add(instance);
       }
       return mergeMetaData(unit, root, metadata, missingFiles);
    }
@@ -153,7 +181,7 @@
     * @return merged metadata
     * @throws Exception for any error
     */
-   protected T mergeMetaData(VFSDeploymentUnit unit, T root, Map<Class<?>, Object> metadata, Set<String> missingFiles) throws Exception
+   protected T mergeMetaData(VFSDeploymentUnit unit, T root, Map<Class<?>, List<Object>> metadata, Set<String> missingFiles) throws Exception
    {
       return mergeMetaData(unit, metadata);
    }
@@ -166,5 +194,5 @@
     * @return merged metadata
     * @throws Exception for any error
     */
-   protected abstract T mergeMetaData(VFSDeploymentUnit unit, Map<Class<?>, Object> metadata) throws Exception;
+   protected abstract T mergeMetaData(VFSDeploymentUnit unit, Map<Class<?>, List<Object>> metadata) throws Exception;
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list