[jboss-svn-commits] JBoss Common SVN: r3657 - in shrinkwrap/trunk: impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer and 7 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Nov 11 04:29:01 EST 2009


Author: aslak
Date: 2009-11-11 04:29:01 -0500 (Wed, 11 Nov 2009)
New Revision: 3657

Added:
   shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ExplodedImporter.java
   shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterImpl.java
   shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ExplodedImporter
   shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterTestCase.java
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/META-INF/
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/META-INF/MANIFEST.FM
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/Test.properties
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/jboss/
   shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/jboss/Test.properties
Log:
SHRINKWRAP-66 Added Importer to import Exploded directory structures

Added: shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ExplodedImporter.java
===================================================================
--- shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ExplodedImporter.java	                        (rev 0)
+++ shrinkwrap/trunk/api/src/main/java/org/jboss/shrinkwrap/api/importer/ExplodedImporter.java	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1,56 @@
+/*
+ * 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.io.File;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Specializer;
+
+/**
+ * ExplodedImporter
+ *
+ * Importer used to import Exploded directory structures into a {@link Archive}
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public interface ExplodedImporter extends Specializer
+{
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+   /**
+    * Import a directory structure as a archive.
+    * 
+    * @param file The directory to import 
+    * @return this
+    * @throws IllegalArgumentException if file is null
+    * @throws IllegalArgumentException if file is not a directory
+    */
+   public ExplodedImporter importDirectory(File file);
+   
+   /**
+    * Import a directory structure as a archive.
+    * 
+    * @param fileName The name of the directory to import
+    * @return this
+    * @throws IllegalArgumentException if file is null
+    * @throws IllegalArgumentException if file is not a directory
+    */
+   public ExplodedImporter importDirectory(String fileName);
+}

Added: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterImpl.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterImpl.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterImpl.java	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1,149 @@
+/*
+ * 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 org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
+import org.jboss.shrinkwrap.impl.base.SpecializedBase;
+import org.jboss.shrinkwrap.impl.base.Validate;
+import org.jboss.shrinkwrap.impl.base.asset.FileAsset;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+
+/**
+ * ExplodedImporterImpl
+ * 
+ * Importer used to import Exploded directory structures into a {@link Archive}
+ * 
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ExplodedImporterImpl extends SpecializedBase implements
+      ExplodedImporter
+{
+   // -------------------------------------------------------------------------------------||
+   // Instance Members --------------------------------------------------------------------||
+   // -------------------------------------------------------------------------------------||
+
+   /**
+    * Archive to import into.
+    */
+   private Archive<?> archive;
+
+   // -------------------------------------------------------------------------------------||
+   // Constructor -------------------------------------------------------------------------||
+   // -------------------------------------------------------------------------------------||
+
+   public ExplodedImporterImpl(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.ExplodedImporter#importDirectory(java
+    * .lang.String)
+    */
+   @Override
+   public ExplodedImporter importDirectory(String fileName)
+   {
+      Validate.notNull(fileName, "FileName must be specified");
+      return importDirectory(new File(fileName));
+   }
+
+   /*
+    * (non-Javadoc)
+    * 
+    * @see
+    * org.jboss.shrinkwrap.api.importer.ExplodedImporter#importDirectory(java
+    * .io.File)
+    */
+   @Override
+   public ExplodedImporter importDirectory(File file)
+   {
+      Validate.notNull(file, "FileName must be specified");
+      if (!file.isDirectory())
+      {
+         throw new IllegalArgumentException("Given file is not a directory "
+               + file.getAbsolutePath());
+      }
+
+      doImport(file, file.listFiles());
+      return this;
+   }
+
+   private void doImport(File root, File[] files)
+   {
+      for (File file : files)
+      {
+         if (file.isDirectory())
+         {
+            doImport(root, file.listFiles());
+         } else
+         {
+            archive.add(calculatePath(root, file), new FileAsset(file));
+         }
+      }
+   }
+
+   /**
+    * Calculate the relative child path.
+    * 
+    * @param root
+    *           The Archive root folder
+    * @param child
+    *           The Child file
+    * @return a Path fort he child relative to root
+    */
+   private Path calculatePath(File root, File child)
+   {
+      String rootPath = unifyPath(root.getPath());
+      String childPath = unifyPath(child.getPath());
+      String archiveChildPath = childPath.replaceFirst(rootPath, "");
+      return new BasicPath(archiveChildPath);
+   }
+
+   /**
+    * Windows vs Linux will return different path separators, unify the paths.
+    * 
+    * @return
+    */
+   private String unifyPath(String path)
+   {
+      return path.replaceAll("\\\\", "/");
+   }
+}

Added: shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ExplodedImporter
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ExplodedImporter	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/main/resources/META-INF/services/org.jboss.shrinkwrap.api.importer.ExplodedImporter	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1 @@
+org.jboss.shrinkwrap.impl.base.importer.ExplodedImporterImpl
\ No newline at end of file

Added: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterTestCase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterTestCase.java	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/importer/ExplodedImporterTestCase.java	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1,72 @@
+/*
+ * 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 org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.Archives;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * TestCase to ensure the correctness of the ExplodedImporter
+ *
+ * @author <a href="mailto:aslak at conduct.no">Aslak Knutsen</a>
+ * @version $Revision: $
+ */
+public class ExplodedImporterTestCase
+{
+
+   private static final String EXISTING_DIRECTORY_RESOURCE = "exploded_import_test";
+   
+   private static final String EXISTING_FILE_RESOURCE = "exploded_import_test/Test.properties";
+   
+   @Test
+   public void shouldBeAbleToImportADriectory() throws Exception {
+      
+      Archive<?> archive = Archives.create("test.jar", ExplodedImporter.class)
+                              .importDirectory(
+                                    Thread.currentThread().getContextClassLoader()
+                                       .getResource(EXISTING_DIRECTORY_RESOURCE).toURI().getPath()
+                              )
+                              .as(JavaArchive.class);
+      
+      Assert.assertTrue(
+            "Root files should be imported",
+            archive.contains(new BasicPath("/Test.properties")));      
+      
+      Assert.assertTrue(
+            "Nested files should be imported",
+            archive.contains(new BasicPath("/META-INF/MANIFEST.FM")));      
+
+      Assert.assertTrue(
+            "Nested files should be imported",
+            archive.contains(new BasicPath("/org/jboss/Test.properties")));  
+   }
+   
+   @Test(expected = IllegalArgumentException.class)
+   public void shouldThrowExceptionIfImportingAFile() throws Exception {
+    
+      Archives.create("test.jar", ExplodedImporter.class)
+                  .importDirectory(
+                        Thread.currentThread().getContextClassLoader()
+                           .getResource(EXISTING_FILE_RESOURCE).toURI().getPath()
+                  );
+   }
+}

Added: shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/META-INF/MANIFEST.FM
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/META-INF/MANIFEST.FM	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/META-INF/MANIFEST.FM	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1 @@
+test test
\ No newline at end of file

Added: shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/Test.properties
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/Test.properties	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/Test.properties	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1 @@
+test test
\ No newline at end of file

Added: shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/jboss/Test.properties
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/jboss/Test.properties	                        (rev 0)
+++ shrinkwrap/trunk/impl-base/src/test/resources/exploded_import_test/org/jboss/Test.properties	2009-11-11 09:29:01 UTC (rev 3657)
@@ -0,0 +1 @@
+test test
\ No newline at end of file



More information about the jboss-svn-commits mailing list