Index: api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java (revision 4266) +++ api/src/main/java/org/jboss/shrinkwrap/api/ArchiveFactory.java (revision ) @@ -16,10 +16,14 @@ */ package org.jboss.shrinkwrap.api; +import java.util.Map; +import java.util.UUID; + /** * * * @author ALR + * @author Ken Gullaksen * @version $Revision: $ */ public final class ArchiveFactory @@ -74,22 +78,95 @@ * @return An {@link Assignable} archive base * @throws IllegalArgumentException If either argument is not specified */ + @Deprecated public T create(final String archiveName, final Class type) throws IllegalArgumentException { + return create(type, archiveName); + } + + /** + * TODO: [kenglxn] Document + * @param type + * @param + * @return + * @throws IllegalArgumentException + */ + public T create(final Class type) + throws IllegalArgumentException + { // Precondition checks - if (archiveName == null) + if (type == null) { - throw new IllegalArgumentException("ArchiveName must be specified"); + throw new IllegalArgumentException("Type must be specified"); } + + String archiveName = UUID.randomUUID().toString(); + + ExtensionType extensionType = configuration.getAssignableExtensionTypeMapping().get(type); + if (extensionType != null) + { + archiveName += extensionType; + } + + return create(type, archiveName); + } + + /** + * TODO: [kenglxn] Document + * + * @param type + * @param archiveName + * @param + * @return + * @throws IllegalArgumentException + */ + public T create(final Class type, final String archiveName) + throws IllegalArgumentException + { + // Precondition checks if (type == null) { throw new IllegalArgumentException("Type must be specified"); } + if (archiveName == null) + { + throw new IllegalArgumentException("ArchiveName must be specified"); + } final Archive archive = SecurityActions.newInstance(ARCHIVE_IMPL, new Class[] {String.class, Configuration.class}, new Object[] {archiveName, configuration}, Archive.class); return archive.as(type); } + + /** + * TODO: [kenglxn] Document + * @param type + * @param archiveName + * @param extensionType + * @param + * @return + * @throws IllegalArgumentException + */ + public T create(final Class type, final String archiveName, final ExtensionType extensionType) + throws IllegalArgumentException + { + // Precondition checks + if (type == null) + { + throw new IllegalArgumentException("Type must be specified"); -} + } + if (archiveName == null) + { + throw new IllegalArgumentException("ArchiveName must be specified"); + } + if (extensionType == null) + { + throw new IllegalArgumentException("ExtensionType must be specified"); + } + + return create(type, archiveName+extensionType); + } + +} Index: impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java =================================================================== --- impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java (revision 4311) +++ impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java (revision ) @@ -22,17 +22,12 @@ import junit.framework.Assert; import junit.framework.TestCase; -import org.jboss.shrinkwrap.api.Archive; -import org.jboss.shrinkwrap.api.ArchivePath; -import org.jboss.shrinkwrap.api.ArchivePaths; -import org.jboss.shrinkwrap.api.Assignable; -import org.jboss.shrinkwrap.api.Configuration; -import org.jboss.shrinkwrap.api.ConfigurationBuilder; -import org.jboss.shrinkwrap.api.Domain; -import org.jboss.shrinkwrap.api.ExtensionLoader; -import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.*; import org.jboss.shrinkwrap.api.formatter.Formatter; +import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.impl.base.container.ContainerBase; import org.junit.Test; @@ -190,6 +185,22 @@ } + @Test + public void shouldCreateArchiveWithCorrectExtensionBasedOnType() throws Exception + { + JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class); + Assert.assertTrue("JavaArchive should have proper extension: " + ExtensionType.JAR, javaArchive.getName().endsWith(ExtensionType.JAR.toString())); + + WebArchive webArchive = ShrinkWrap.create(WebArchive.class); + Assert.assertTrue("WebArchive should have proper extension: " + ExtensionType.WAR, webArchive.getName().endsWith(ExtensionType.WAR.toString())); + + EnterpriseArchive enterpriseArchive = ShrinkWrap.create(EnterpriseArchive.class); + Assert.assertTrue("EnterpriseArchive should have proper extension: " + ExtensionType.EAR, enterpriseArchive.getName().endsWith(ExtensionType.EAR.toString())); + + ResourceAdapterArchive resourceAdapterArchive = ShrinkWrap.create(ResourceAdapterArchive.class); + Assert.assertTrue("ResourceAdapterArchive should have proper extension: " + ExtensionType.RAR, resourceAdapterArchive.getName().endsWith(ExtensionType.RAR.toString())); + } + //-------------------------------------------------------------------------------------|| // Internal Helper Members ------------------------------------------------------------|| //-------------------------------------------------------------------------------------|| Index: api/src/main/java/org/jboss/shrinkwrap/api/ExtensionType.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/ExtensionType.java (revision ) +++ api/src/main/java/org/jboss/shrinkwrap/api/ExtensionType.java (revision ) @@ -0,0 +1,45 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2010, 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; + +/** + * ExtensionType + * Implemented as a class to provide extensibility + * + * @author Ken Gullaksen + * @version $Revision: $ + */ +public class ExtensionType +{ + public static final ExtensionType WAR = new ExtensionType("war"); + public static final ExtensionType JAR = new ExtensionType("jar"); + public static final ExtensionType EAR = new ExtensionType("ear"); + public static final ExtensionType RAR = new ExtensionType("rar"); + private final String extension; + + public ExtensionType(String extension) + { + this.extension = extension; + } + + @Override + public String toString() + { + return "." + extension; + } +} Index: api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java (revision 4282) +++ api/src/main/java/org/jboss/shrinkwrap/api/ShrinkWrap.java (revision ) @@ -30,6 +30,7 @@ * {@link ShrinkWrap#createDomain(Configuration)}. * * @author ALR + * @author Ken Gullaksen * @version $Revision: $ */ public final class ShrinkWrap @@ -132,6 +133,7 @@ * @return An {@link Assignable} archive base * @throws IllegalArgumentException If either argument is not specified */ + @Deprecated public static T create(final String archiveName, final Class type) throws IllegalArgumentException { @@ -149,6 +151,84 @@ return ShrinkWrap.getDefaultDomain().getArchiveFactory().create(archiveName, type); } + /** + * TODO: [kenglxn] Document + * TODO: [kenglxn] Add test coverage + * @param type + * @param + * @return + * @throws IllegalArgumentException + */ + public static T create(final Class type) + throws IllegalArgumentException + { + // Precondition checks + if (type == null) + { + throw new IllegalArgumentException("Type must be specified"); + } + + // Delegate to the default domain's archive factory for creation + return ShrinkWrap.getDefaultDomain().getArchiveFactory().create(type); + } + + /** + * TODO: [kenglxn] Document + * TODO: [kenglxn] add test coverage + * @param type + * @param archiveName + * @param + * @return + * @throws IllegalArgumentException + */ + public static T create(final Class type, final String archiveName) + throws IllegalArgumentException + { + // Precondition checks + if (type == null) + { + throw new IllegalArgumentException("Type must be specified"); + } + if (archiveName == null || archiveName.length() == 0) + { + throw new IllegalArgumentException("ArchiveName must be specified"); + } + + // Delegate to the default domain's archive factory for creation + return ShrinkWrap.getDefaultDomain().getArchiveFactory().create(type, archiveName); + } + + /** + * TODO: [kenglxn] Document + * TODO: [kenglxn] add test coverage + * @param type + * @param archiveName + * @param extensionType + * @param + * @return + * @throws IllegalArgumentException + */ + public static T create(final Class type, final String archiveName, final ExtensionType extensionType) + throws IllegalArgumentException + { + // Precondition checks + if (type == null) + { + throw new IllegalArgumentException("Type must be specified"); + } + if (archiveName == null || archiveName.length() == 0) + { + throw new IllegalArgumentException("ArchiveName must be specified"); + } + if (extensionType == null) + { + throw new IllegalArgumentException("ExtensionType must be specified"); + } + + // Delegate to the default domain's archive factory for creation + return ShrinkWrap.getDefaultDomain().getArchiveFactory().create(type, archiveName, extensionType); + } + //-------------------------------------------------------------------------------------|| // Internal Helper Members ------------------------------------------------------------|| //-------------------------------------------------------------------------------------|| Index: api/src/main/java/org/jboss/shrinkwrap/api/ConfigurationBuilder.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/ConfigurationBuilder.java (revision 4284) +++ api/src/main/java/org/jboss/shrinkwrap/api/ConfigurationBuilder.java (revision ) @@ -16,6 +16,13 @@ */ package org.jboss.shrinkwrap.api; +import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive; +import org.jboss.shrinkwrap.api.spec.WebArchive; + +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,6 +40,7 @@ * constructed by calling upon {@link ConfigurationBuilder#build()}. * * @author ALR + * @author Ken Gullaksen * @version $Revision: $ */ public class ConfigurationBuilder @@ -65,6 +73,11 @@ */ private ExecutorService executorService; + /** + * Mapping between {@link org.jboss.shrinkwrap.api.Assignable} and {@link org.jboss.shrinkwrap.api.ExtensionType} + */ + private Map, ExtensionType> assignableExtensionTypeMapping; + //-------------------------------------------------------------------------------------|| // Constructor ------------------------------------------------------------------------|| //-------------------------------------------------------------------------------------|| @@ -99,6 +112,14 @@ } /** + * @return the assignableExtensionTypeMapping + */ + public Map, ExtensionType> getAssignableExtensionTypeMapping() + { + return assignableExtensionTypeMapping; + } + + /** * Sets the {@link ExtensionLoader} to be used, returning this instance * * @param extensionLoader @@ -122,6 +143,17 @@ } /** + * Sets the assignableExtensionTypeMapping to be used, returning this instance + * @param assignableExtensionTypeMapping + * @return + */ + public ConfigurationBuilder assignableExtensionTypeMapping(final Map, ExtensionType> assignableExtensionTypeMapping) + { + this.assignableExtensionTypeMapping = assignableExtensionTypeMapping; + return this; + } + + /** * Builds a new {@link Configuration} using the properties contained * in this builder. In the case a property has not been specified, it will be defaulted * according to the rules set forth in this {@link ConfigurationBuilder}'s contract. @@ -157,10 +189,19 @@ } this.extensionLoader(loader); } + if(getAssignableExtensionTypeMapping() == null) { + final Map, ExtensionType> assignableExtensionTypeMapping = createDefaultAssignableExtensionTypeMapping(); + if (log.isLoggable(Level.FINER)) + { + log.finer("User has not defined an explicit assignableExtensionTypeMapping; defaulting to " + + assignableExtensionTypeMapping); - } + } + this.assignableExtensionTypeMapping(assignableExtensionTypeMapping); + } + } /** - * Obtains the default {@link ExtensionLoader} to be used if none + * Obtains the default {@link ExtensionLoader} to be used if none * is specified * @return */ @@ -171,4 +212,20 @@ {}, ExtensionLoader.class); } + /** + * Obtains the default assignableExtensionTypeMapping to be used if none is specified + * @return + */ + private Map, ExtensionType> createDefaultAssignableExtensionTypeMapping() + { + assignableExtensionTypeMapping = new HashMap, ExtensionType>(); + + assignableExtensionTypeMapping.put(WebArchive.class, ExtensionType.WAR); + assignableExtensionTypeMapping.put(JavaArchive.class, ExtensionType.JAR); + assignableExtensionTypeMapping.put(EnterpriseArchive.class, ExtensionType.EAR); + assignableExtensionTypeMapping.put(ResourceAdapterArchive.class, ExtensionType.RAR); + + return assignableExtensionTypeMapping; -} + } + +} Index: api/src/main/java/org/jboss/shrinkwrap/api/Configuration.java =================================================================== --- api/src/main/java/org/jboss/shrinkwrap/api/Configuration.java (revision 4283) +++ api/src/main/java/org/jboss/shrinkwrap/api/Configuration.java (revision ) @@ -16,6 +16,8 @@ */ package org.jboss.shrinkwrap.api; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ExecutorService; /** @@ -31,6 +33,7 @@ * {@link Domain}s when creating {@link Archive}s. * * @author ALR + * @author Ken Gullaksen * @version $Revision: $ */ public class Configuration @@ -53,6 +56,8 @@ */ private final ExecutorService executorService; + private final Map, ExtensionType> assignableExtensionTypeMapping; + //-------------------------------------------------------------------------------------|| // Constructor ------------------------------------------------------------------------|| //-------------------------------------------------------------------------------------|| @@ -75,6 +80,7 @@ // Set this.extensionLoader = builder.getExtensionLoader(); this.executorService = builder.getExecutorService(); + this.assignableExtensionTypeMapping = builder.getAssignableExtensionTypeMapping(); } //-------------------------------------------------------------------------------------|| @@ -97,4 +103,11 @@ return executorService; } + /** + * @return the assignableExtensionTypeMapping + */ + public Map, ExtensionType> getAssignableExtensionTypeMapping() + { + return assignableExtensionTypeMapping; -} + } +}