[jboss-svn-commits] JBoss Common SVN: r3648 - in shrinkwrap/trunk: api/src/main/java/org/jboss/shrinkwrap/api/importer and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 10 10:55:57 EST 2009


Author: aslak
Date: 2009-11-10 10:55:56 -0500 (Tue, 10 Nov 2009)
New Revision: 3648

Added:
   shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/
   shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ArchiveImporterException.java
   shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/ZipFileEntryAsset.java
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java
   shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ZipImporter
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java
   shrinkwrap/trunk/impl-base/src/test/resources/org/jboss/shrinkwrap/impl/base/importer/
   shrinkwrap/trunk/impl-base/src/test/resources/org/jboss/shrinkwrap/impl/base/importer/test.zip
Log:
SHRINKWRAP-7 Added ZipImporter based on the extension API.

Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ArchiveImporterException.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ArchiveImporterException.java	                        (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ArchiveImporterException.java	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.api.importer;
+
+/**
+ * ArchiveImporterException
+ * 
+ * Exception thrown if error importing a Archive.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ArchiveImporterException extends RuntimeException
+{
+
+   private static final long serialVersionUID = 1L;
+
+   public ArchiveImporterException()
+   {
+      super();
+   }
+
+   public ArchiveImporterException(String message, Throwable cause)
+   {
+      super(message, cause);
+   }
+
+   public ArchiveImporterException(String message)
+   {
+      super(message);
+   }
+
+   public ArchiveImporterException(Throwable cause)
+   {
+      super(cause);
+   }
+
+}

Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java	                        (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ZipImporter.java	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.api.importer;
+
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Specializer;
+
+/**
+ * ZipImporter
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface ZipImporter extends Specializer
+{
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+    /**
+    * Imports provided {@link ZipInputStream} as a {@link Archive}.
+    * 
+    * @param stream the stream to import
+    * @return Archive of the imported Zip
+    * @throws ArchiveImporterException if IOException during import
+    */
+   public ZipImporter importZip(ZipInputStream stream) throws ArchiveImporterException;
+   
+   /**
+    * Imports provided {@link ZipInputStream} as a {@link Archive}.
+    * 
+    * @param file the file to import
+    * @return Archive of the imported Zip
+    * @throws ArchiveImporterException if IOException during import
+    */
+   public ZipImporter importZip(ZipFile file) throws ArchiveImporterException;
+}

Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/ZipFileEntryAsset.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/ZipFileEntryAsset.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/ZipFileEntryAsset.java	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.impl.base.asset;
+
+import java.io.InputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.impl.base.Validate;
+
+/**
+ * Holds a reference to the ZipFile and the ZipEntry this
+ * Asset represents for lazy loading.
+ * 
+ * Used by the ZipImporter.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ZipFileEntryAsset implements Asset
+{
+   private ZipFile file;
+   private ZipEntry entry;
+   
+   public ZipFileEntryAsset(ZipFile file, ZipEntry entry)
+   {
+      Validate.notNull(file, "File must be specified");
+      Validate.notNull(entry, "Entry must be specified");
+      
+      this.file = file;
+      this.entry = entry;
+   }
+   
+   /* (non-Javadoc)
+    * @see org.jboss.declarchive.api.Asset#getStream()
+    */
+   @Override
+   // TODO: create AssetStreamException ?
+   public InputStream openStream()
+   {
+      try 
+      {
+         return file.getInputStream(entry);
+      }
+      catch (Exception e) 
+      {
+         throw new RuntimeException("Could not open zip file stream", e); 
+      }
+   }
+}

Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImpl.java	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1,151 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.impl.base.importer;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.importer.ArchiveImporterException;
+import org.jboss.shrinkwrap.api.importer.ZipImporter;
+import org.jboss.shrinkwrap.impl.base.SpecializedBase;
+import org.jboss.shrinkwrap.impl.base.Validate;
+import org.jboss.shrinkwrap.impl.base.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.impl.base.asset.ZipFileEntryAsset;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+
+/**
+ * Used to import existing Zip files/streams into the given {@link Archive}  
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ZipImporterImpl extends SpecializedBase implements ZipImporter  
+{
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+   
+   /**
+    * Archive to import into. 
+    */
+   private Archive<?> archive; 
+   
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   public ZipImporterImpl(Archive<?> archive) 
+   {
+      Validate.notNull(archive, "Archive must be specified");
+      this.archive = archive;
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /* (non-Javadoc)
+    * @see org.jboss.shrinkwrap.impl.base.SpecializedBase#getArchive()
+    */
+   @Override
+   protected Archive<?> getArchive()
+   {
+      return archive;
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.shrinkwrap.api.importer.ZipImporter#importZip(java.util.zip.ZipInputStream)
+    */
+   // TODO: create a ZipEntryAsset that can stream directly from the stream somehow?
+   @Override
+   public ZipImporter importZip(ZipInputStream stream)
+   {
+      Validate.notNull(stream, "Stream must be specified");
+      try 
+      {
+         ZipEntry entry;
+         while( (entry = stream.getNextEntry()) != null) 
+         {
+            /*
+             *  TODO: we skip directories since the whole path is auto created and we have to directory concept. 
+             *  we lose the empty directories, ok?
+             */
+            if(entry.isDirectory()) 
+            {
+               continue; 
+            }
+            String entryName = entry.getName();
+
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
+            byte[] content = new byte[1024];
+            int readBytes;
+            while( (readBytes = stream.read(content, 0, content.length)) != -1)
+            {
+               output.write(content, 0, readBytes);
+            }
+            archive.add(entryName, new ByteArrayAsset(output.toByteArray()));
+            stream.closeEntry();
+         }
+      }
+      catch (IOException e) 
+      {
+         throw new ArchiveImporterException("Could not import stream", e);
+      }
+      finally 
+      {
+         try 
+         {
+            stream.close();
+         } 
+         catch (Exception ingore) 
+         {
+         }
+      }
+      return this;
+   }
+   
+   /* (non-Javadoc)
+    * @see org.jboss.shrinkwrap.api.importer.ZipImporter#importZip(java.util.zip.ZipFile)
+    */
+   @Override
+   public ZipImporter importZip(ZipFile file)
+   {
+      Validate.notNull(file, "File must be specified");
+      
+      Enumeration<? extends ZipEntry> entries = file.entries();
+      while(entries.hasMoreElements())
+      {
+         ZipEntry entry = entries.nextElement();
+         /*
+          *  TODO: we skip directories since the whole path is auto created and we have to directory concept. 
+          *  we lose the empty directories, ok?
+          */
+         if(entry.isDirectory()) {
+            continue;
+         }
+         String entryName = entry.getName();
+         
+         archive.add(new BasicPath(entryName), new ZipFileEntryAsset(file, entry));
+      }
+      return this;
+   }
+}

Added: shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ZipImporter
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ZipImporter	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ZipImporter	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1 @@
+org.jboss.shrinkwrap.impl.base.importer.ZipImporterImpl
\ No newline at end of file

Added: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ZipImporterImplTestCase.java	2009-11-10 15:55:56 UTC (rev 3648)
@@ -0,0 +1,203 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.impl.base.importer;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
+
+import junit.framework.Assert;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.export.ZipExporter;
+import org.jboss.shrinkwrap.api.importer.ZipImporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.Archives;
+import org.jboss.shrinkwrap.impl.base.asset.ClassLoaderAsset;
+import org.jboss.shrinkwrap.impl.base.io.IOUtil;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+import org.junit.Test;
+
+
+/**
+ * ZipImporterImplTest
+ * 
+ * TestCase to verify the ZipImporter functionality.
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ZipImporterImplTestCase
+{
+   
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(ZipImporterImplTestCase.class.getName());
+
+   private static final String EXISTING_ZIP_RESOURCE = "org/jboss/shrinkwrap/impl/base/importer/test.zip";
+   
+   private static final String EXISTING_RESOURCE = "org/jboss/shrinkwrap/impl/base/asset/Test.properties";
+   
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+   
+   @Test
+   public void shouldBeAbleToimportZipFile() throws Exception 
+   {
+      ZipFile testZip = new ZipFile(
+            new File(
+                  Thread.currentThread().getContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI()));
+      
+      Archive<?> archive = Archives.create("test.jar")
+                              .as(ZipImporter.class)
+                                 .importZip(testZip)
+                              .as(JavaArchive.class);
+
+      Assert.assertNotNull("Should not return a null archive", archive);
+      
+      assertContent(
+            archive, 
+            Thread.currentThread().getContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
+   }
+   
+
+   @Test
+   public void shouldBeAbleToImportAddAndExport() throws Exception
+   {
+      ZipInputStream stream = new ZipInputStream(
+            Thread.currentThread().getContextClassLoader().getResourceAsStream(EXISTING_ZIP_RESOURCE));
+      
+      Archive<?> archive = Archives.create("test.jar")
+                              .as(ZipImporter.class)
+                                 .importZip(stream)
+                              .as(JavaArchive.class);
+
+      Assert.assertNotNull("Should not return a null archive", archive);
+
+      archive.add(new BasicPath("test.properties"), new ClassLoaderAsset(EXISTING_RESOURCE));
+      
+      File tempFile = new File("target/test.zip");
+      tempFile.deleteOnExit();
+      InputStream zipStream = ZipExporter.exportZip(archive);
+      IOUtil.copyWithClose(zipStream, new FileOutputStream(tempFile));
+      
+      assertContent(archive, tempFile.toURI());
+   }
+   
+   @Test
+   public void shouldBeAbleToImportZipInputStream() throws Exception
+   {
+      ZipInputStream stream = new ZipInputStream(
+            Thread.currentThread().getContextClassLoader().getResourceAsStream(EXISTING_ZIP_RESOURCE));
+      
+      Archive<?> archive = Archives.create("test.jar")
+                              .as(ZipImporter.class)
+                                 .importZip(stream)
+                              .as(JavaArchive.class);
+
+      Assert.assertNotNull("Should not return a null archive", archive);
+      
+      assertContent(
+            archive, 
+            Thread.currentThread().getContextClassLoader().getResource(EXISTING_ZIP_RESOURCE).toURI());
+   }
+   
+   /**
+    * Compare the content of the original file and what was imported.
+    * 
+    * @param importedArchive The archive used for import
+    * @param originalSource The original classpath resource file
+    */
+   private void assertContent(Archive<?> importedArchive, URI originalSource) throws Exception
+   {
+      Assert.assertFalse(
+            "Should have imported something",
+            importedArchive.getContent().isEmpty());
+      
+      ZipFile testZip = new ZipFile(
+            new File(originalSource));
+
+      List<? extends ZipEntry> entries = Collections.list(testZip.entries());
+      
+      Assert.assertFalse(
+            "Test zip should contain data", 
+            entries.isEmpty());
+      Assert.assertEquals(
+            "Should have imported all files",
+            findNumberOfFiles(entries), 
+            importedArchive.getContent().size());
+      
+      for(ZipEntry originalEntry : entries) 
+      {
+         if(originalEntry.isDirectory()) 
+         {
+            continue;
+         }
+
+         Assert.assertTrue(
+               "Importer should have imported " + originalEntry.getName() + " from " + originalSource,
+               importedArchive.contains(new BasicPath(originalEntry.getName())));
+         
+         byte[] originalContent = IOUtil.asByteArray(testZip.getInputStream(originalEntry));
+         byte[] importedContent = IOUtil.asByteArray(
+               importedArchive.get(new BasicPath(originalEntry.getName())).openStream());
+
+         log.fine(
+               Arrays.equals(importedContent, originalContent) + "\t" +
+               originalContent.length + "\t" +
+               importedContent.length + "\t" +
+               originalEntry.getName());
+         
+         Assert.assertTrue(
+               "The content of " + originalEntry.getName() + " should be equal to the imported content",
+               Arrays.equals(importedContent, originalContent));
+      }
+   }
+
+   /**
+    * Find the number of files in original zip, exclude directories.
+    * 
+    * @param enties
+    * @return
+    */
+   private int findNumberOfFiles(List<? extends ZipEntry> enties) 
+   {
+      int count = 0;
+      for(ZipEntry entry : enties) 
+      {
+         if(!entry.isDirectory()) 
+         {
+            count++;
+         }
+      }
+      return count;
+   }
+}

Added: shrinkwrap/trunk/impl-base/src/test/resources/org/jboss/shrinkwrap/impl/base/importer/test.zip
===================================================================
(Binary files differ)


Property changes on: shrinkwrap/trunk/impl-base/src/test/resources/org/jboss/shrinkwrap/impl/base/importer/test.zip
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream



More information about the jboss-svn-commits mailing list