[jboss-svn-commits] JBoss Common SVN: r3522 - in tmpdpl/trunk: impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 15 18:04:37 EDT 2009


Author: ALRubinger
Date: 2009-09-15 18:04:37 -0400 (Tue, 15 Sep 2009)
New Revision: 3522

Modified:
   tmpdpl/trunk/api/src/main/java/org/jboss/tmpdpl/api/deployable/VfsVdfDeployableFactory.java
   tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java
   tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java
Log:
[TMPDPL-6] Create Deployables from Archive, URL, File, or InputStream

Modified: tmpdpl/trunk/api/src/main/java/org/jboss/tmpdpl/api/deployable/VfsVdfDeployableFactory.java
===================================================================
--- tmpdpl/trunk/api/src/main/java/org/jboss/tmpdpl/api/deployable/VfsVdfDeployableFactory.java	2009-09-14 19:40:02 UTC (rev 3521)
+++ tmpdpl/trunk/api/src/main/java/org/jboss/tmpdpl/api/deployable/VfsVdfDeployableFactory.java	2009-09-15 22:04:37 UTC (rev 3522)
@@ -16,8 +16,8 @@
  */
 package org.jboss.tmpdpl.api.deployable;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -57,10 +57,15 @@
    private static final String CLASS_NAME_ARCHIVE_TYPE = "org.jboss.shrinkwrap.api.Archive";
 
    /**
-    * Constructor used in creating new {@link Deployable} instances
+    * Name of the method used for creation
     */
-   private static Constructor<?> constructor;
+   private static final String METHOD_NAME_CREATE = "create";
 
+   /**
+    * Method used in creating new {@link Deployable} instances
+    */
+   private static Method method;
+
    //-------------------------------------------------------------------------------------||
    // Constructor ------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -91,31 +96,23 @@
          throw new IllegalArgumentException("Archive must be specified");
       }
 
-      // Get the implementation ctor
-      final Constructor<?> constructor = getConstructor();
-
-      // Make a new instance
       final Object obj;
       try
       {
-         obj = constructor.newInstance(archive);
+         obj = getMethod().invoke(null, archive);
          if (log.isLoggable(Level.FINER))
          {
             log.log(Level.FINER, "Created: " + obj);
          }
       }
-      catch (final InstantiationException e)
+      catch (final IllegalAccessException iae)
       {
-         throw new RuntimeException("Error in creating new " + Deployable.class.getName(), e);
+         throw new RuntimeException("Could not invoke static method: " + method, iae);
       }
-      catch (final IllegalAccessException e)
+      catch (final InvocationTargetException ite)
       {
-         throw new RuntimeException("Error in creating new " + Deployable.class.getName(), e);
+         throw new RuntimeException("Could not invoke static method: " + method, ite);
       }
-      catch (final InvocationTargetException e)
-      {
-         throw new RuntimeException("Error in creating new " + Deployable.class.getName(), e);
-      }
 
       // Cast 
       final Deployable deployable;
@@ -137,37 +134,31 @@
    //-------------------------------------------------------------------------------------||
 
    /**
-    * Obtains the constructor used in creating new Deployable instances.
+    * Obtains the method used for creating new Deployable instances.
     * Uses a cached copy unless not yet initialized.
     */
-   private synchronized static Constructor<?> getConstructor()
+   private synchronized static Method getMethod()
    {
       // If we haven't yet cached the ctor
-      if (constructor == null)
+      if (method == null)
       {
-         // Load the impl class
-         final String implClassName = CLASS_NAME_VFS_VDF_DEPLOYABLE;
-         final Class<?> implClass = getClass(implClassName);
-
-         // Load the ctor param class
-         final String paramClassName = CLASS_NAME_ARCHIVE_TYPE;
-         final Class<?> paramClass = getClass(paramClassName);
-
-         // Get and set the ctor
+         // Get the static method
+         final Class<?> clazz = getClass(CLASS_NAME_VFS_VDF_DEPLOYABLE);
+         final Class<?> paramType = getClass(CLASS_NAME_ARCHIVE_TYPE);
          try
          {
-            constructor = SecurityActions.getConstructor(implClass, paramClass);
+            method = clazz.getDeclaredMethod(METHOD_NAME_CREATE, paramType);
          }
          catch (final NoSuchMethodException nsme)
          {
-            throw new RuntimeException("Could not find constructor to be used in factory creation of a new "
-                  + Deployable.class.getSimpleName(), nsme);
+            throw new RuntimeException("Could not obtain method \"" + METHOD_NAME_CREATE + " from " + clazz.getName(),
+                  nsme);
          }
 
       }
 
       // Return
-      return constructor;
+      return method;
    }
 
    /**

Modified: tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java
===================================================================
--- tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java	2009-09-14 19:40:02 UTC (rev 3521)
+++ tmpdpl/trunk/impl-vdf/src/main/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImpl.java	2009-09-15 22:04:37 UTC (rev 3522)
@@ -23,6 +23,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.logging.Level;
@@ -39,8 +40,7 @@
 /**
  * VfsVdfDeployableImpl
  * 
- * A Virtual Deployer's Framework Deployment backed by 
- * a Virtual File System root
+ * A Virtual Deployer's Framework Deployment
  *
  * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
  * @version $Revision: $
@@ -71,22 +71,32 @@
     */
    private Deployment deployment;
 
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
    /**
-    * The underlying archive; used in calculating equals() and hashCode() only
+    * Internal Constructor
     */
-   private Archive<?> archive;
+   private VfsVdfDeployableImpl(final Deployment deployment) throws IllegalArgumentException
+   {
+      // Precondition check
+      assert deployment != null : "Deployment must be specified";
 
+      // Set
+      this.setDeployment(deployment);
+   }
+
    //-------------------------------------------------------------------------------------||
-   // Constructor ------------------------------------------------------------------------||
+   // Factory Methods --------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
    /**
-    * Constructor
+    * Creates a new {@link VdfDeployable} from the specified archive
     * 
-    * Creates a new Deployable from the specified archive
     * @throws IllegalArgumentException If the archive is not specified 
     */
-   public VfsVdfDeployableImpl(final Archive<?> archive) throws IllegalArgumentException
+   public static VdfDeployable create(final Archive<?> archive) throws IllegalArgumentException
    {
       // Precondition check
       if (archive == null)
@@ -96,6 +106,32 @@
 
       // Export to ZIP
       final InputStream zipStream = ZipExporter.exportZip(archive);
+
+      // Return
+      final String name = archive.getName();
+      return create(name, zipStream);
+   }
+
+   /**
+    * Creates a new {@link VdfDeployable} from the specified archive and name 
+    * 
+    * @param name The name to assign the deployment
+    * @param archive an InputStream representing a series of bytes deployable by the container (eg, JAR, EAR, XML, etc)
+    * @throws IllegalArgumentException If the archive or name is not specified 
+    */
+   public static VdfDeployable create(final String name, final InputStream archive) throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (name == null || name.length() == 0)
+      {
+         throw new IllegalArgumentException("name must be specified");
+      }
+      if (archive == null)
+      {
+         throw new IllegalArgumentException("archive must be specified");
+      }
+
+      // Make a temp file
       final String tempDirLocation = AccessController.doPrivileged(new PrivilegedAction<String>()
       {
          @Override
@@ -113,8 +149,7 @@
       {
          throw new IllegalStateException("Temp location mus tbe a directory: " + tmpDir.getAbsolutePath());
       }
-      final String fileName = archive.getName();
-      final File tmpFile = new File(tmpDir, fileName);
+      final File tmpFile = new File(tmpDir, name);
       tmpFile.deleteOnExit();
 
       // Write the ZIP to the temp file
@@ -133,20 +168,20 @@
       try
       {
 
-         while (((read = zipStream.read(buffer)) != -1))
+         while (((read = archive.read(buffer)) != -1))
          {
             out.write(buffer, 0, read);
          }
       }
       catch (final IOException ioe)
       {
-         throw new RuntimeException("Error in obtainting bytes from " + zipStream, ioe);
+         throw new RuntimeException("Error in obtainting bytes from " + archive, ioe);
       }
       finally
       {
          try
          {
-            zipStream.close();
+            archive.close();
          }
          catch (final IOException ignore)
          {
@@ -166,8 +201,29 @@
          log.fine("Using temporary file to back deployable: " + tmpFile.getAbsolutePath());
       }
 
+      // Delegate
+      return create(tmpFile);
+   }
+
+   /**
+    * Creates a new {@link VdfDeployable} from the specified archive
+    * 
+    * @throws IllegalArgumentException If the archive is not specified or does not exist
+    */
+   public static VdfDeployable create(final File archive) throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (archive == null)
+      {
+         throw new IllegalArgumentException("archive must be specified");
+      }
+      if (!archive.exists())
+      {
+         throw new IllegalArgumentException("archive must exist: " + archive.getAbsolutePath());
+      }
+
       // Obtain as a VFS VirtualFile
-      final URI uri = tmpFile.toURI();
+      final URI uri = archive.toURI();
       final VirtualFile file;
       try
       {
@@ -175,24 +231,57 @@
       }
       catch (final IOException e)
       {
-         throw new RuntimeException("Could not create new VFS root from " + tmpFile, e);
+         throw new RuntimeException("Could not create new VFS root from " + archive, e);
       }
 
       // Create Deployment
       final Deployment deployment = VFSDeploymentFactory.getInstance().createVFSDeployment(file);
       log.fine("Created deployment: " + deployment);
 
-      // Set
-      this.setDeployment(deployment);
-      this.setArchive(archive);
+      // Create Deployable
+      final VdfDeployable deployable = new VfsVdfDeployableImpl(deployment);
+      return deployable;
    }
 
+   /**
+    * Constructor
+    * 
+    * Creates a new Deployable from the specified URL, 
+    * @throws IllegalArgumentException If the archive is not specified, or is not a valid archive 
+    */
+   public static VdfDeployable create(final URL archive) throws IllegalArgumentException
+   {
+      // Precondition check
+      if (archive == null)
+      {
+         throw new IllegalArgumentException("archive must be specified");
+      }
+
+      // Obtain InputStream
+      final InputStream in;
+      try
+      {
+         in = archive.openConnection().getInputStream();
+      }
+      catch (final IOException ioe)
+      {
+         throw new RuntimeException("Could not get stream from archive: " + archive, ioe);
+      }
+
+      // Obtain the name
+      final String name = archive.getPath();
+
+      // Create from stream
+      return create(name, in);
+   }
+
    //-------------------------------------------------------------------------------------||
    // Required Implementations -----------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
-   /* (non-Javadoc)
-    * @see org.jboss.embedded.core.incubation.deployable.spi.vdf.VdfDeployable#getDeployment()
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.tmpdpl.spi.vdf.VdfDeployable#getDeployment()
     */
    @Override
    public Deployment getDeployment()
@@ -201,79 +290,55 @@
    }
 
    //-------------------------------------------------------------------------------------||
-   // Accessors / Mutators ---------------------------------------------------------------||
+   // Overridden Implementations ---------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
    /**
-    * @param deployment the deployment to set
-    */
-   private void setDeployment(final Deployment deployment)
-   {
-      this.deployment = deployment;
-   }
-
-   /**
-    * @return the archive
-    */
-   private Archive<?> getArchive()
-   {
-      return archive;
-   }
-
-   /**
-    * @param archive the archive to set
-    */
-   private void setArchive(final Archive<?> archive)
-   {
-      this.archive = archive;
-   }
-
-   /**
-    * Calculates the hash code for this Deployable, taking into
-    * account the hash code of the underlying virtual archive 
+    * Delegates to the underlying deployment for hash code
+    * {@inheritDoc}
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
-      final int prime = 31;
-      int result = 1;
-      result = prime * result + ((archive == null) ? 0 : archive.hashCode());
-      return result;
+      return this.deployment.hashCode();
    }
 
    /**
-    * Determines whether this reference is equal by value to 
-    * the specified object.  Equal if and only if the objects compared are 
-    * the same type and have equal references to underlying archives.
+    * Deployables with equal underlying deployments are equal
+    * {@inheritDoc}
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj)
    {
-      // Equal references are equal
       if (this == obj)
          return true;
-      // Specified is null cannot be equal
       if (obj == null)
          return false;
-      // Different types are not equal
       if (getClass() != obj.getClass())
          return false;
-      // Equal refs to underlying archives are equal
       final VfsVdfDeployableImpl other = (VfsVdfDeployableImpl) obj;
-      final Archive<?> ourArchive = this.getArchive();
-      if (ourArchive == null)
+      if (deployment == null)
       {
-         throw new IllegalStateException("Underlying archive can never be null");
+         if (other.deployment != null)
+            return false;
       }
-      final Archive<?> otherArchive = other.getArchive();
-      if (ourArchive == otherArchive)
-      {
-         return true;
-      }
-      // Otherwise not equal 
-      return false;
+      else if (!deployment.equals(other.deployment))
+         return false;
+      return true;
    }
 
+   //-------------------------------------------------------------------------------------||
+   // Accessors / Mutators ---------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * @param deployment the deployment to set
+    */
+   private void setDeployment(final Deployment deployment)
+   {
+      this.deployment = deployment;
+   }
+
 }

Modified: tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java
===================================================================
--- tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java	2009-09-14 19:40:02 UTC (rev 3521)
+++ tmpdpl/trunk/impl-vdf/src/test/java/org/jboss/tmpdpl/impl/vdf/VfsVdfDeployableImplTestCase.java	2009-09-15 22:04:37 UTC (rev 3522)
@@ -18,9 +18,8 @@
 
 import java.util.logging.Logger;
 
+import org.jboss.shrinkwrap.api.JavaArchiveFactory;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.jboss.shrinkwrap.impl.base.MemoryMapArchiveImpl;
-import org.jboss.shrinkwrap.impl.base.spec.JavaArchiveImpl;
 import org.jboss.tmpdpl.api.deployable.Deployable;
 import org.jboss.virtual.VFS;
 import org.junit.Assert;
@@ -77,30 +76,31 @@
       // Log
       log.info("testEquals");
 
+      // Make names
+      final String name1 = "archive1";
+      final String name2 = "archive2";
+
       // Make some test archives
-      final JavaArchive archive1 = new JavaArchiveImpl(new MemoryMapArchiveImpl("archive1")); // TODO Use factory
-      final JavaArchive archive2 = new JavaArchiveImpl(new MemoryMapArchiveImpl("archive2")); // TODO Use factory
+      final JavaArchive archive1 = JavaArchiveFactory.create(name1);
+      final JavaArchive archive2 = JavaArchiveFactory.create(name2);
 
       // Make some test Deployables from the archives
-      final Deployable deployable1 = new VfsVdfDeployableImpl(archive1);
-      final Deployable deployable2 = new VfsVdfDeployableImpl(archive2);
-      final Deployable deployable3 = new VfsVdfDeployableImpl(archive1);
+      final Deployable deployable1 = VfsVdfDeployableImpl.create(archive1);
+      final Deployable deployable2 = VfsVdfDeployableImpl.create(archive2);
 
       // Test equals
-      Assert.assertTrue("Deployables pointing to same archive should be equal by value", deployable1
-            .equals(deployable3));
-      Assert.assertTrue("Deployables must be equal by symmetry; a == b then b == a", deployable3.equals(deployable1));
+      Assert
+            .assertTrue("Deployables pointing to same itself should be equal by value", deployable1.equals(deployable1));
       Assert.assertFalse("Deployables pointing to different archives should not be equal by value", deployable1
             .equals(deployable2));
       Assert.assertFalse("Deployables must not be equal to null value", deployable1.equals(null));
 
       // Get hashCodes
       final int deployable1HashCode = deployable1.hashCode();
-      final int deployable3HashCode = deployable3.hashCode();
+      final int deployable1HashCodeAgain = deployable1.hashCode();
 
       // Test hashCodes
-      Assert.assertEquals("Deployables with equal values must have equal hash codes", deployable1HashCode,
-            deployable3HashCode);
+      Assert.assertEquals("hash code op must be indempotent", deployable1HashCode, deployable1HashCodeAgain);
 
       /*
        * Note we don't check for inequal hash codes between deployable1 and deployable2 because technically



More information about the jboss-svn-commits mailing list