[jboss-svn-commits] JBoss Common SVN: r3501 - in declarchive/trunk: impl-base/src/main/java/org/jboss/declarchive/impl/base/export and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Aug 28 23:50:57 EDT 2009
Author: johnbailey
Date: 2009-08-28 23:50:56 -0400 (Fri, 28 Aug 2009)
New Revision: 3501
Added:
declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/FactoryUtil.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/AbstractExporterDelegate.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterDelegate.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExportDelegate.java
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExportTestBase.java
Modified:
declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ExplodedExporter.java
declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ZipExporter.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterImpl.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExporterImpl.java
declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExplodedExporterTestCase.java
declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ZipExporterTestCase.java
Log:
[TMPARCH-25] - Updated exporters to improve reuse. Also removed use for Java temporary files in tests.
Modified: declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ExplodedExporter.java
===================================================================
--- declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ExplodedExporter.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ExplodedExporter.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -17,8 +17,6 @@
package org.jboss.declarchive.api.export;
import java.io.File;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import org.jboss.declarchive.api.Archive;
@@ -57,6 +55,8 @@
* @param archive
* @param parentDirectory
* @return File for exploded archive contents
+ * @throws IllegalArgumentException if the archive or parent directory not valid
+ * @throws ArchiveExportException if the export process fails
*/
public static File exportExploded(Archive<?> archive, File parentDirectory)
{
@@ -64,7 +64,8 @@
}
/**
- * Get an instance of the ExplodedExporter implementation
+ * Get an instance of the ExplodedExporter implementation
+ *
* @return
*/
private synchronized static ExplodedExporter getInstance()
@@ -76,25 +77,14 @@
return instance;
}
+ /**
+ * Create an instance of the ExplodedEporter implementation
+ *
+ * @return
+ */
private static ExplodedExporter createInstance()
{
- return AccessController.doPrivileged(new PrivilegedAction<ExplodedExporter>()
- {
- @SuppressWarnings("unchecked")
- public ExplodedExporter run()
- {
- try
- {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- Class<? extends ExplodedExporter> exporterImpl = (Class<? extends ExplodedExporter>) classLoader.loadClass(IMPL_TYPE);
- return exporterImpl.newInstance();
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Unable to create ExplodedExporter implemenation.", e);
- }
- }
- });
+ return FactoryUtil.createInstance(IMPL_TYPE, ExplodedExporter.class);
}
//-------------------------------------------------------------------------------------||
@@ -103,6 +93,7 @@
/**
* Template export method for concrete implementations
+ *
* @param archive
* @param parentDirectory
* @return File for exploded archive contents
Added: declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/FactoryUtil.java
===================================================================
--- declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/FactoryUtil.java (rev 0)
+++ declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/FactoryUtil.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -0,0 +1,106 @@
+/*
+ * 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.declarchive.api.export;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.logging.Logger;
+
+/**
+ * FactoryUtil
+ *
+ * Package-private factory utilities
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class FactoryUtil
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(FactoryUtil.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Prohibit instantiation
+ */
+ private FactoryUtil()
+ {
+ throw new UnsupportedOperationException("No instances should be created; stateless utility class");
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Create an instance of the Class with the specified FQN, of the expected
+ * type
+ *
+ * @throws IllegalArgumentException If the specified type is not assignable to the instance
+ * of the class created from the specified class name, or if either argument is not
+ * supplied
+ */
+ static <T> T createInstance(final String className, final Class<T> type) throws IllegalArgumentException
+ {
+ // Precondition checks
+ if (className == null || className.length() == 0)
+ {
+ throw new IllegalArgumentException("className must be specified");
+ }
+ if (type == null)
+ {
+ throw new IllegalArgumentException("type must be specified");
+ }
+
+ return AccessController.doPrivileged(new PrivilegedAction<T>()
+ {
+ public T run()
+ {
+ try
+ {
+ final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ final Class<?> clazz = Class.forName(className, false, classLoader);
+ final Object obj = clazz.newInstance();
+ try
+ {
+ return type.cast(obj);
+ }
+ catch (final ClassCastException cee)
+ {
+ throw new IllegalArgumentException("Specified type " + type.getName() + " is not assignable to "
+ + obj);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Unable to create implemenation: " + className, e);
+ }
+ }
+ });
+ }
+
+}
Modified: declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ZipExporter.java
===================================================================
--- declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ZipExporter.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/api/src/main/java/org/jboss/declarchive/api/export/ZipExporter.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -17,8 +17,6 @@
package org.jboss.declarchive.api.export;
import java.io.InputStream;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import org.jboss.declarchive.api.Archive;
@@ -56,6 +54,8 @@
*
* @param archive
* @return InputStram for exported Zip
+ * @throws IllegalArgumentException if the archive is not valid
+ * @throws ArchiveExportException if the export process fails
*/
public static InputStream exportZip(Archive<?> archive)
{
@@ -75,26 +75,14 @@
return instance;
}
+ /**
+ * Create an instance of the ZipExporter implementation
+ *
+ * @return
+ */
private static ZipExporter createInstance()
{
- return AccessController.doPrivileged(new PrivilegedAction<ZipExporter>()
- {
- @SuppressWarnings("unchecked")
- public ZipExporter run()
- {
- try
- {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- Class<? extends ZipExporter> exporterImpl = (Class<? extends ZipExporter>) classLoader
- .loadClass(IMPL_TYPE);
- return exporterImpl.newInstance();
- }
- catch (Exception e)
- {
- throw new IllegalArgumentException("Unable to create ExplodedExporter implemenation.", e);
- }
- }
- });
+ return FactoryUtil.createInstance(IMPL_TYPE, ZipExporter.class);
}
//-------------------------------------------------------------------------------------||
@@ -103,6 +91,7 @@
/**
* Template export method for concrete implementations
+ *
* @param archive
* @return InputStream for exported Zip
*/
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/AbstractExporterDelegate.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/AbstractExporterDelegate.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/AbstractExporterDelegate.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -0,0 +1,133 @@
+/*
+ * 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.declarchive.impl.base.export;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.api.Asset;
+import org.jboss.declarchive.api.Path;
+
+/**
+ * AbstractExporterDelegate
+ *
+ * Abstract delegate used for archive export.
+ * Provides a template for exporters for handling archive contents.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public abstract class AbstractExporterDelegate<T>
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(AbstractExporterDelegate.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * The archive being exported
+ */
+ private final Archive<?> archive;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new abstract exporter delegate for the provided {@link Archive}
+ */
+ protected AbstractExporterDelegate(Archive<?> archive)
+ {
+ super();
+ this.archive = archive;
+ }
+
+ /**
+ * Primary method providing a template for exporting the contents of an archive
+ *
+ * @return
+ */
+ protected T export()
+ {
+ // Get archive
+ Archive<?> archive = getArchive();
+ if (log.isLoggable(Level.FINE))
+ {
+ log.fine("Exporting archive - " + archive.getName());
+ }
+
+ // Obtain all contents
+ final Map<Path, Asset> content = archive.getContent();
+
+ // For every Path in the Archive
+ for (final Entry<Path, Asset> contentEntry : content.entrySet())
+ {
+ // Get Asset information
+ final Path path = contentEntry.getKey();
+ final Asset asset = contentEntry.getValue();
+ // Write asset content to file
+ processAsset(path, asset);
+ }
+
+ return getResult();
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Contracts --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Template method for processing a single asset.
+ *
+ * @param path
+ * @param asset
+ */
+ protected abstract void processAsset(Path path, Asset asset);
+
+ /**
+ * Return the results of the export. Should process any tasks required to finalize the export.
+ *
+ * @return
+ */
+ protected abstract T getResult();
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Return the archive being exported
+ *
+ * @return
+ */
+ protected Archive<?> getArchive()
+ {
+ return archive;
+ }
+
+}
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterDelegate.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterDelegate.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterDelegate.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -0,0 +1,154 @@
+/*
+ * 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.declarchive.impl.base.export;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.api.Asset;
+import org.jboss.declarchive.api.Path;
+import org.jboss.declarchive.api.export.ArchiveExportException;
+import org.jboss.declarchive.impl.base.io.IOUtil;
+
+/**
+ * ExplodedExporterDelegate
+ *
+ * Delegate used to export an archive into an exploded directory structure.
+ *
+ * @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
+ * @version $Revision: $
+ */
+public class ExplodedExporterDelegate extends AbstractExporterDelegate<File>
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(ExplodedExporterDelegate.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Output directory to write the exploded content to.
+ */
+ private final File outputDirectory;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new exploded exporter delegate for the provided {@link Archive}
+ */
+ public ExplodedExporterDelegate(Archive<?> archive, File baseDirectory)
+ {
+ super(archive);
+ this.outputDirectory = initializeOutputDirectory(baseDirectory);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.declarchive.impl.base.export.AbstractExporterDelegate#processAsset(Path, Asset)
+ */
+ @Override
+ protected void processAsset(Path path, Asset asset)
+ {
+ // Get path to file
+ final String assetFilePath = path.get();
+
+ // Create a file for the asset
+ final File assetFile = new File(outputDirectory, assetFilePath);
+
+ // Get the assets parent parent directory and make sure it exists
+ final File assetParent = assetFile.getParentFile();
+ if (!assetParent.exists())
+ {
+ if (!assetParent.mkdirs())
+ {
+ throw new ArchiveExportException("Failed to write asset. Unable to create parent directory.");
+ }
+ }
+
+ try
+ {
+ if (log.isLoggable(Level.FINE))
+ {
+ log.fine("Writing asset " + path.get() + " to " + assetFile.getAbsolutePath());
+ }
+ // Get the asset streams
+ final InputStream assetInputStream = asset.getStream();
+ final FileOutputStream assetFileOutputStream = new FileOutputStream(assetFile);
+
+ // Write contents
+ IOUtil.copyWithClose(assetInputStream, assetFileOutputStream);
+ }
+ catch (Throwable t)
+ {
+ throw new ArchiveExportException("Failed to write asset " + path + " to " + assetFile);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.declarchive.impl.base.export.AbstractExporterDelegate#getResult()
+ */
+ @Override
+ protected File getResult()
+ {
+ return outputDirectory;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Initializes the output directory
+ *
+ * @param baseDirectory
+ * @return
+ */
+ private File initializeOutputDirectory(File baseDirectory)
+ {
+
+ // Get archive
+ Archive<?> archive = getArchive();
+
+ // Create output directory
+ final File outputDirectory = new File(baseDirectory, archive.getName());
+ if (!outputDirectory.mkdir())
+ {
+ throw new ArchiveExportException("Unable to create archive output directory - " + outputDirectory);
+ }
+
+ return outputDirectory;
+ }
+
+}
Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterImpl.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterImpl.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ExplodedExporterImpl.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -17,20 +17,12 @@
package org.jboss.declarchive.impl.base.export;
import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.util.Map;
-import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jboss.declarchive.api.Archive;
-import org.jboss.declarchive.api.Asset;
-import org.jboss.declarchive.api.Path;
-import org.jboss.declarchive.api.export.ArchiveExportException;
import org.jboss.declarchive.api.export.ExplodedExporter;
import org.jboss.declarchive.impl.base.Validate;
-import org.jboss.declarchive.impl.base.io.IOUtil;
/**
* ExplodedExporterImpl
@@ -56,9 +48,10 @@
// Required Implementations - ExplodedExporter ----------------------------------------||
//-------------------------------------------------------------------------------------||
- /*
- * (non-Javadoc)
- * @see org.jboss.declarchive.api.export.ExplodedExporter#doExportExploded(org.jboss.declarchive.api.Archive, java.io.File)
+
+ /**
+ * {@inheritDoc}
+ * @see ExplodedExporter#doExportExploded(Archive, File)
*/
@Override
protected File doExportExploded(Archive<?> archive, File baseDirectory)
@@ -77,76 +70,18 @@
throw new IllegalArgumentException("Provided parent directory is not a valid directory");
}
- // Obtain all contents
- final Map<Path, Asset> content = archive.getContent();
+ // Get the export delegate
+ ExplodedExporterDelegate exporterDelegate = new ExplodedExporterDelegate(archive, baseDirectory);
- // Create output directory
- final File outputDirectory = new File(baseDirectory, archive.getName());
- if (!outputDirectory.mkdir())
- {
- throw new ArchiveExportException("Unable to create archive output directory - " + outputDirectory);
- }
+ // Run the export
+ File explodedDirectory = exporterDelegate.export();
- // For every Path in the Archive
- for (final Entry<Path, Asset> contentEntry : content.entrySet())
- {
- // Get Asset information
- final Path path = contentEntry.getKey();
- final Asset asset = contentEntry.getValue();
- // Write asset content to file
- writeAsset(outputDirectory, path, asset);
- }
-
if (log.isLoggable(Level.FINE))
{
- log.fine("Created Exploded Atchive: " + outputDirectory.getAbsolutePath());
+ log.fine("Created Exploded Atchive: " + explodedDirectory.getAbsolutePath());
}
- // Return the output dir
- return outputDirectory;
+ // Return the exploded dir
+ return explodedDirectory;
}
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Write the asset file to the output directory
- *
- * @param outputDirectory
- * @param path
- * @param asset
- */
- private void writeAsset(File outputDirectory, Path path, Asset asset)
- {
- // Get path to file
- final String assetFilePath = path.get();
-
- // Create a file for the asset
- final File assetFile = new File(outputDirectory, assetFilePath);
-
- // Get the assets parent parent directory and make sure it exists
- final File assetParent = assetFile.getParentFile();
- if (!assetParent.exists())
- {
- if (!assetParent.mkdirs())
- {
- throw new ArchiveExportException("Failed to write asset. Unable to create parent directory.");
- }
- }
-
- try
- {
- // Get the asset streams
- final InputStream assetInputStream = asset.getStream();
- final FileOutputStream assetFileOutputStream = new FileOutputStream(assetFile);
-
- // Write contents
- IOUtil.copyWithClose(assetInputStream, assetFileOutputStream);
- }
- catch (Throwable t)
- {
- throw new ArchiveExportException("Failed to write asset " + path + " to " + assetFile);
- }
- }
-
}
Added: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExportDelegate.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExportDelegate.java (rev 0)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExportDelegate.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -0,0 +1,187 @@
+/*
+ * 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.declarchive.impl.base.export;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+import org.jboss.declarchive.api.Archive;
+import org.jboss.declarchive.api.Asset;
+import org.jboss.declarchive.api.Path;
+import org.jboss.declarchive.api.export.ArchiveExportException;
+import org.jboss.declarchive.impl.base.io.IOUtil;
+
+public class ZipExportDelegate extends AbstractExporterDelegate<InputStream>
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(ZipExportDelegate.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * OutputStream to hold the output contents
+ */
+ private final ByteArrayOutputStream output = new ByteArrayOutputStream();
+
+ /**
+ * ZipOutputStream used to write the zip entries
+ */
+ private ZipOutputStream zipOutputStream;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Creates a new exporter delegate for exporting archives as Zip
+ */
+ public ZipExportDelegate(Archive<?> archive)
+ {
+ super(archive);
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Required Implementations -----------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.declarchive.impl.base.export.AbstractExporterDelegate#export()
+ */
+ @Override
+ protected InputStream export()
+ {
+
+ // Enclose every IO Operation so we can close up cleanly
+ try
+ {
+ // Initialize the output streams
+ zipOutputStream = new ZipOutputStream(output);
+
+ return super.export();
+ }
+ catch (Exception ex)
+ {
+ // Problem occurred make sure the output is closed
+ closeOutputStream();
+ throw new ArchiveExportException("Failed to export archive " + getArchive().getName(), ex);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.declarchive.impl.base.export.AbstractExporterDelegate#processAsset(Path, Asset)
+ */
+ @Override
+ protected void processAsset(Path path, Asset asset)
+ {
+ final String pathName = path.get();
+ final ZipEntry entry = new ZipEntry(pathName);
+
+ final InputStream in = asset.getStream();
+ // Write the Asset under the same Path name in the Zip
+ try
+ {
+ // Make a Zip Entry
+ zipOutputStream.putNextEntry(entry);
+
+ // Read the contents of the asset and write to the JAR
+ IOUtil.copy(in, zipOutputStream);
+
+ // Close up the instream and the entry
+ zipOutputStream.closeEntry();
+ }
+ // Some error in writing this entry/asset
+ catch (final IOException ioe)
+ {
+ // Throw
+ throw new ArchiveExportException("Could not start new entry for " + pathName, ioe);
+ }
+ finally
+ {
+ // Try to close the instream. Out stream is closed in finally block below.
+ try
+ {
+ in.close();
+ }
+ catch (IOException ignored)
+ {
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.declarchive.impl.base.export.AbstractExporterDelegate#getResult()
+ */
+ @Override
+ protected InputStream getResult()
+ {
+ // Make sure the output is closed
+ closeOutputStream();
+
+ // Flush the output to a byte array
+ final byte[] zipContent = output.toByteArray();
+ if (log.isLoggable(Level.FINE))
+ {
+ log.fine("Created Zip of size: " + zipContent.length + " bytes");
+ }
+
+ // Make an instream
+ final InputStream inputStream = new ByteArrayInputStream(zipContent);
+
+ // Return
+ return inputStream;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Close the ZipOutputStream
+ */
+ private void closeOutputStream()
+ {
+ try
+ {
+ if (zipOutputStream != null)
+ {
+ zipOutputStream.close();
+ zipOutputStream = null;
+ }
+ }
+ catch (final IOException ignored)
+ {
+ }
+ }
+
+}
Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExporterImpl.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExporterImpl.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/export/ZipExporterImpl.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -16,24 +16,12 @@
*/
package org.jboss.declarchive.impl.base.export;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.InputStream;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
import org.jboss.declarchive.api.Archive;
-import org.jboss.declarchive.api.Asset;
-import org.jboss.declarchive.api.Path;
-import org.jboss.declarchive.api.export.ArchiveExportException;
import org.jboss.declarchive.api.export.ZipExporter;
import org.jboss.declarchive.impl.base.Validate;
-import org.jboss.declarchive.impl.base.io.IOUtil;
/**
* ZipExporterImpl
@@ -60,8 +48,8 @@
// Required Implementations - ZipExporter ---------------------------------------------||
//-------------------------------------------------------------------------------------||
- /*
- * (non-Javadoc)
+ /**
+ * {@inheritDoc}
* @see org.jboss.declarchive.api.export.ZipExporter#doExportZip(org.jboss.declarchive.api.Archive)
*/
@Override
@@ -69,115 +57,14 @@
{
Validate.notNull(archive, "No archive provided");
- // Obtain all contents
- final Map<Path, Asset> content = archive.getContent();
+ // Create export delegate
+ ZipExportDelegate exportDelegate = new ZipExportDelegate(archive);
- // Create OutputStreams
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
- ZipOutputStream zipOutputStream = null;
+ // Execute export
+ InputStream inputStream = exportDelegate.export();
- // Enclose every IO Operation so we can close up cleanly
- try
- {
- // Make a ZipOutputStream
- zipOutputStream = new ZipOutputStream(output);
-
- // For every Path in the Archive
- for (final Entry<Path, Asset> contentEntry : content.entrySet())
- {
- // Get Asset information
- final Path path = contentEntry.getKey();
- final Asset asset = contentEntry.getValue();
- // Write asset content to Zip
- writeAsset(zipOutputStream, path, asset);
- }
-
- }
- // We're done, close
- finally
- {
- try
- {
- if (zipOutputStream != null)
- {
- zipOutputStream.close();
- }
- }
- catch (final IOException ignored)
- {
- }
- }
-
- return getContentStream(output);
- }
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Write the content of a single asset to the Zip.
- *
- * @param zipOutputStream
- * @param path
- * @param asset
- */
- private void writeAsset(ZipOutputStream zipOutputStream, Path path, Asset asset)
- {
- final String pathName = path.get();
- final ZipEntry entry = new ZipEntry(pathName);
-
- final InputStream in = asset.getStream();
- // Write the Asset under the same Path name in the Zip
- try
- {
- // Make a Zip Entry
- zipOutputStream.putNextEntry(entry);
-
- // Read the contents of the asset and write to the JAR
- IOUtil.copy(in, zipOutputStream);
-
- // Close up the instream and the entry
- zipOutputStream.closeEntry();
- }
- // Some error in writing this entry/asset
- catch (final IOException ioe)
- {
- // Throw
- throw new ArchiveExportException("Could not start new entry for " + pathName, ioe);
- }
- finally
- {
- // Try to close the instream. Out stream is closed in finally block below.
- try
- {
- in.close();
- }
- catch (IOException ignored)
- {
- }
- }
- }
-
- /**
- * Get an InputStream representing the bytes from the Zip.
- *
- * @param outputStream
- * @return
- */
- private InputStream getContentStream(ByteArrayOutputStream outputStream)
- {
- // Flush the output to a byte array
- final byte[] zipContent = outputStream.toByteArray();
- if (log.isLoggable(Level.FINE))
- {
- log.fine("Created Zip of size: " + zipContent.length + " bytes");
- }
-
- // Make an instream
- final InputStream inputStream = new ByteArrayInputStream(zipContent);
-
- // Return
+ // Return input stream
return inputStream;
}
+
}
Modified: declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java
===================================================================
--- declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/impl-base/src/main/java/org/jboss/declarchive/impl/base/io/IOUtil.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -40,10 +40,6 @@
//-------------------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- //-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -193,12 +189,4 @@
+ ". It is either not a directory or does not exist.");
}
}
-
- //-------------------------------------------------------------------------------------||
- // Functional Methods -----------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- //-------------------------------------------------------------------------------------||
- // Internal Helper Methods ------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
}
Modified: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExplodedExporterTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExplodedExporterTestCase.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExplodedExporterTestCase.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -31,9 +31,7 @@
import org.jboss.declarchive.impl.base.io.IOUtil;
import org.jboss.declarchive.impl.base.path.BasicPath;
import org.jboss.declarchive.spi.MemoryMapArchive;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
/**
@@ -44,7 +42,7 @@
* @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
* @version $Revision: $
*/
-public class ExplodedExporterTestCase
+public class ExplodedExporterTestCase extends ExportTestBase
{
//-------------------------------------------------------------------------------------||
@@ -57,43 +55,6 @@
private static final Logger log = Logger.getLogger(ExplodedExporterTestCase.class.getName());
//-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Temp directory
- */
- private File tempDirectory;
-
- //-------------------------------------------------------------------------------------||
- // Lifecycle Methods ------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Create the temp directory used as the base for exploded archives
- *
- * @throws Exception
- */
- @Before
- public void createTempDirectory() throws Exception
- {
- tempDirectory = File.createTempFile("tmp", "");
- tempDirectory.delete();
- tempDirectory.mkdir();
- }
-
- /**
- * Clean up the temp directory
- *
- * @throws Exception
- */
- @After
- public void cleanTempDirectory() throws Exception
- {
- IOUtil.deleteDirectory(tempDirectory);
- }
-
- //-------------------------------------------------------------------------------------||
// Tests ------------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -107,6 +68,9 @@
{
log.info("testExportExploded");
+ // Get a temp directory
+ File tempDirectory = createTempDirectory("testExportExploded");
+
// Get an archive instance
MemoryMapArchive archive = new MemoryMapArchiveImpl("testArchive.jar");
@@ -117,7 +81,7 @@
Asset assetTwo = new ClassLoaderAsset("org/jboss/declarchive/impl/base/asset/Test2.properties");
Path pathTwo = new BasicPath("nested", "test2.properties");
archive.add(pathTwo, assetTwo);
-
+
// Add a nested archive for good measure
MemoryMapArchive nestedArchive = new MemoryMapArchiveImpl("nestedArchive.jar");
nestedArchive.add(pathOne, assetOne);
@@ -158,7 +122,7 @@
try
{
- ExplodedExporter.exportExploded(null, tempDirectory);
+ ExplodedExporter.exportExploded(null, getNonexistantDirectory());
Assert.fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException expected)
@@ -198,8 +162,7 @@
try
{
- File directory = new File("someNonExistentDirectory");
-
+ final File directory = this.getNonexistantDirectory();
ExplodedExporter.exportExploded(new MemoryMapArchiveImpl(), directory);
Assert.fail("Should have thrown IllegalArgumentException");
}
@@ -214,13 +177,12 @@
* @throws Exception
*/
@Test
- public void testExportExplodedRequiresValidDirectroy() throws Exception
+ public void testExportExplodedRequiresValidDirectory() throws Exception
{
- log.info("testExportExplodedRequiresValidDirectroy");
+ log.info("testExportExplodedRequiresValidDirectory");
try
{
- File directory = File.createTempFile("tmp", "");
-
+ final File directory = this.getNonexistantDirectory();
ExplodedExporter.exportExploded(new MemoryMapArchiveImpl(), directory);
Assert.fail("Should have thrown IllegalArgumentException");
}
@@ -234,6 +196,20 @@
//-------------------------------------------------------------------------------------||
/**
+ * Obtains a reference to a directory that does not exist
+ */
+ private File getNonexistantDirectory()
+ {
+ final File directory = new File(this.getTarget(), "someNonExistentDirectory");
+ if (directory.exists())
+ {
+ IOUtil.deleteDirectory(directory);
+ }
+ Assert.assertTrue("Precondition Failure: Directory should not exist: " + directory, !directory.exists());
+ return directory;
+ }
+
+ /**
* Assert an asset is actually in the exploded directory
*
* @throws FileNotFoundException
Added: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExportTestBase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExportTestBase.java (rev 0)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ExportTestBase.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -0,0 +1,84 @@
+/*
+ * 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.declarchive.impl.base.export;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.logging.Logger;
+
+import org.jboss.declarchive.impl.base.io.IOUtil;
+import org.junit.Assert;
+
+/**
+ * ExportTestBase
+ *
+ * Base support for the exporter test cases
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class ExportTestBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(ExportTestBase.class.getName());
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods -----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Returns a temp directory for a test. Needs the test
+ */
+ protected File createTempDirectory(String testName) throws Exception
+ {
+ // Qualify the temp directory by test case
+ File tempDirectoryParent = new File(this.getTarget(), this.getClass().getSimpleName());
+ // Qualify the temp directory by test name
+ File tempDirectory = new File(tempDirectoryParent, testName);
+ log.info("Temp Directory: " + tempDirectory.getCanonicalPath());
+ if (tempDirectory.exists())
+ {
+ IOUtil.deleteDirectory(tempDirectory);
+ }
+ Assert.assertTrue("Temp directory should be clear before start", !tempDirectory.exists());
+ tempDirectory.mkdirs();
+ return tempDirectory;
+ }
+
+ /**
+ * Returns the target directory
+ */
+ protected File getTarget()
+ {
+ try
+ {
+ return new File(new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()), "../");
+ }
+ catch (final URISyntaxException urise)
+ {
+ throw new RuntimeException("Could not obtain the target URI", urise);
+ }
+ }
+
+}
Modified: declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ZipExporterTestCase.java
===================================================================
--- declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ZipExporterTestCase.java 2009-08-28 15:24:21 UTC (rev 3500)
+++ declarchive/trunk/impl-base/src/test/java/org/jboss/declarchive/impl/base/export/ZipExporterTestCase.java 2009-08-29 03:50:56 UTC (rev 3501)
@@ -44,7 +44,7 @@
* @author <a href="mailto:baileyje at gmail.com">John Bailey</a>
* @version $Revision: $
*/
-public class ZipExporterTestCase
+public class ZipExporterTestCase extends ExportTestBase
{
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
@@ -68,6 +68,9 @@
{
log.info("testExportZip");
+ // Get a temp directory for the test
+ File tempDirectory = createTempDirectory("testExportZip");
+
// Get an archive instance
MemoryMapArchive archive = new MemoryMapArchiveImpl("testArchive.jar");
@@ -92,7 +95,8 @@
Assert.assertNotNull(zipStream);
// Create a temp file
- File outFile = File.createTempFile("test", ".zip");
+ File outFile = new File(tempDirectory, archive.getName());
+
// Write Zip contents to file
writeOutFile(outFile, zipStream);
More information about the jboss-svn-commits
mailing list