Author: snicoll Date: Sun Dec 19 17:10:52 2010 New Revision: 1050900 URL: http://svn.apache.org/viewvc?rev=1050900&view=rev Log: MEAR-135: rationalize the use of JavaEEVersion in its own class.
Added: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/InvalidJavaEEVersion.java maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/JavaEEVersion.java maven/plugins/trunk/maven-ear-plugin/src/test/java/org/apache/maven/plugin/ear/util/JavaEEVersionTest.java Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java?rev=1050900&r1=1050899&r2=1050900&view=diff ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java (original) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/AbstractEarMojo.java Sun Dec 19 17:10:52 2010 @@ -25,6 +25,7 @@ import org.apache.maven.plugin.AbstractM import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService; +import org.apache.maven.plugin.ear.util.JavaEEVersion; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; @@ -44,15 +45,6 @@ import java.util.Set; public abstract class AbstractEarMojo extends AbstractMojo { - - public static final String VERSION_1_3 = "1.3"; - - public static final String VERSION_1_4 = "1.4"; - - public static final String VERSION_5 = "5"; - - public static final String VERSION_6 = "6"; - public static final String APPLICATION_XML_URI = "META-INF/application.xml"; public static final String META_INF = "META-INF"; @@ -158,6 +150,7 @@ public abstract class AbstractEarMojo public void execute() throws MojoExecutionException, MojoFailureException { + final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version ); getLog().debug( "Resolving artifact type mappings ..." ); ArtifactTypeMappingService typeMappingService; try @@ -227,7 +220,7 @@ public abstract class AbstractEarMojo if ( !isArtifactRegistered( artifact, allModules ) && !artifact.isOptional() && filter.include( artifact ) ) { - EarModule module = EarModuleFactory.newEarModule( artifact, version, defaultLibBundleDir, + EarModule module = EarModuleFactory.newEarModule( artifact, javaEEVersion, defaultLibBundleDir, includeLibInApplicationXml, typeMappingService ); module.setEarExecutionContext( earExecutionContext ); allModules.add( module ); Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java?rev=1050900&r1=1050899&r2=1050900&view=diff ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java (original) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/ApplicationXmlWriter.java Sun Dec 19 17:10:52 2010 @@ -19,6 +19,7 @@ package org.apache.maven.plugin.ear; * under the License. */ +import org.apache.maven.plugin.ear.util.JavaEEVersion; import org.codehaus.plexus.util.xml.XMLWriter; import java.io.Writer; @@ -41,11 +42,11 @@ final class ApplicationXmlWriter private static final String APPLICATION_ELEMENT = "application"; - private final String version; + private final JavaEEVersion version; private final Boolean generateModuleId; - ApplicationXmlWriter( String version, String encoding, Boolean generateModuleId ) + ApplicationXmlWriter( JavaEEVersion version, String encoding, Boolean generateModuleId ) { super( encoding ); this.version = version; @@ -58,31 +59,32 @@ final class ApplicationXmlWriter Writer w = initializeWriter( context.getDestinationFile() ); XMLWriter writer = null; - if ( GenerateApplicationXmlMojo.VERSION_1_3.equals( version ) ) + if ( JavaEEVersion.OneDotThree.eq( version ) ) { writer = initializeRootElementOneDotThree( w ); } - else if ( GenerateApplicationXmlMojo.VERSION_1_4.equals( version ) ) + else if ( JavaEEVersion.OneDotFour.eq( version ) ) { writer = initializeRootElementOneDotFour( w ); } - else if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) ) + else if ( JavaEEVersion.Five.eq( version ) ) { writer = initializeRootElementFive( w ); } - else if ( GenerateApplicationXmlMojo.VERSION_6.equals( version ) ) + else if ( JavaEEVersion.Six.eq( version ) ) { writer = initializeRootElementSix( w ); } - // JavaEE6 only - if (GenerateApplicationXmlMojo.VERSION_6.equals( version )) { + // As from JavaEE6 + if ( version.ge( JavaEEVersion.Six ) ) + { writeApplicationName( context.getApplicationName(), writer ); } // IMPORTANT: the order of the description and display-name elements was // reversed between J2EE 1.3 and J2EE 1.4. - if ( GenerateApplicationXmlMojo.VERSION_1_3.equals( version ) ) + if ( version.eq( JavaEEVersion.OneDotThree ) ) { writeDisplayName( context.getDisplayName(), writer ); writeDescription( context.getDescription(), writer ); @@ -93,8 +95,9 @@ final class ApplicationXmlWriter writeDisplayName( context.getDisplayName(), writer ); } - // JavaEE6 only - if (GenerateApplicationXmlMojo.VERSION_6.equals( version )) { + // As from JavaEE6 + if ( version.ge( JavaEEVersion.Six ) ) + { writeInitializeInOrder( context.getInitializeInOrder(), writer ); } @@ -104,7 +107,7 @@ final class ApplicationXmlWriter while ( moduleIt.hasNext() ) { EarModule module = (EarModule) moduleIt.next(); - module.appendModule( writer, version, generateModuleId ); + module.appendModule( writer, version.getVersion(), generateModuleId ); } final Iterator securityRoleIt = context.getSecurityRoles().iterator(); @@ -114,8 +117,7 @@ final class ApplicationXmlWriter securityRole.appendSecurityRole( writer ); } - if ( GenerateApplicationXmlMojo.VERSION_5.equals( version ) || - GenerateApplicationXmlMojo.VERSION_6.equals( version ) ) + if ( version.ge( JavaEEVersion.Five ) ) { writeLibraryDirectory( context.getLibraryDirectory(), writer ); } @@ -155,7 +157,7 @@ final class ApplicationXmlWriter } } - private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer ) + private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer ) { if ( initializeInOrder != null ) { Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java?rev=1050900&r1=1050899&r2=1050900&view=diff ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java (original) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarModuleFactory.java Sun Dec 19 17:10:52 2010 @@ -21,6 +21,7 @@ package org.apache.maven.plugin.ear; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService; +import org.apache.maven.plugin.ear.util.JavaEEVersion; import java.util.ArrayList; import java.util.Collections; @@ -65,7 +66,7 @@ public final class EarModuleFactory * @return an ear module for this artifact * @throws UnknownArtifactTypeException if the artifact is not handled */ - public static EarModule newEarModule( Artifact artifact, String javaEEVersion, String defaultLibBundleDir, + public static EarModule newEarModule( Artifact artifact, JavaEEVersion javaEEVersion, String defaultLibBundleDir, Boolean includeInApplicationXml, ArtifactTypeMappingService typeMappingService ) throws UnknownArtifactTypeException @@ -92,8 +93,7 @@ public final class EarModuleFactory else if ( "ejb-client".equals( artifactType ) ) { // Somewhat weird way to tackle the problem described in MEAR-85 - if ( AbstractEarMojo.VERSION_1_3.equals( javaEEVersion ) || - AbstractEarMojo.VERSION_1_4.equals( javaEEVersion ) ) + if ( javaEEVersion.le( JavaEEVersion.OneDotFour ) ) { return new EjbClientModule( artifact, null ); } Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java?rev=1050900&r1=1050899&r2=1050900&view=diff ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java (original) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/EarMojo.java Sun Dec 19 17:10:52 2010 @@ -25,6 +25,7 @@ import org.apache.maven.execution.MavenS import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.ear.util.EarMavenArchiver; +import org.apache.maven.plugin.ear.util.JavaEEVersion; import org.apache.maven.project.MavenProjectHelper; import org.apache.maven.shared.filtering.MavenFileFilter; import org.apache.maven.shared.filtering.MavenFilteringException; @@ -242,6 +243,8 @@ public class EarMojo // Initializes ear modules super.execute(); + final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version ); + // Initializes unpack types List unpackTypesList = new ArrayList(); if ( unpackTypes != null ) @@ -384,7 +387,7 @@ public class EarMojo // Check if deployment descriptor is there File ddFile = new File( getWorkDirectory(), APPLICATION_XML_URI ); - if ( !ddFile.exists() && ( !( version.equals( VERSION_5 ) || version.equals( VERSION_6 ) ) ) ) + if ( !ddFile.exists() && ( javaEEVersion.lt( JavaEEVersion.Five ) ) ) { throw new MojoExecutionException( "Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." ); Modified: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java?rev=1050900&r1=1050899&r2=1050900&view=diff ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java (original) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java Sun Dec 19 17:10:52 2010 @@ -21,6 +21,7 @@ package org.apache.maven.plugin.ear; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.ear.util.JavaEEVersion; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.PlexusConfigurationException; import org.codehaus.plexus.util.FileUtils; @@ -118,18 +119,13 @@ public class GenerateApplicationXmlMojo } else { - // Check version - if ( !version.equals( VERSION_1_3 ) && !version.equals( VERSION_1_4 ) && !version.equals( VERSION_5 ) && - !version.equals( VERSION_6 ) ) - { - throw new MojoExecutionException( "Invalid version[" + version + "]" ); - } + final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version ); // Generate deployment descriptor and copy it to the build directory getLog().info( "Generating application.xml" ); try { - generateStandardDeploymentDescriptor(); + generateStandardDeploymentDescriptor( javaEEVersion ); } catch ( EarPluginException e ) { @@ -181,7 +177,7 @@ public class GenerateApplicationXmlMojo /** * Generates the deployment descriptor. */ - protected void generateStandardDeploymentDescriptor() + protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion ) throws EarPluginException { File outputDir = new File( generatedDescriptorLocation ); @@ -192,7 +188,7 @@ public class GenerateApplicationXmlMojo File descriptor = new File( outputDir, "application.xml" ); - final ApplicationXmlWriter writer = new ApplicationXmlWriter( version, encoding, generateModuleId ); + final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId ); final ApplicationXmlWriterContext context = new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), displayName, description, defaultLibBundleDir, applicationName, initializeInOrder ); Added: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/InvalidJavaEEVersion.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/InvalidJavaEEVersion.java?rev=1050900&view=auto ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/InvalidJavaEEVersion.java (added) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/InvalidJavaEEVersion.java Sun Dec 19 17:10:52 2010 @@ -0,0 +1,22 @@ +package org.apache.maven.plugin.ear.util; + +import org.apache.maven.plugin.MojoExecutionException; + +/** + * @author Stephane Nicoll + */ +public class InvalidJavaEEVersion extends MojoExecutionException { + + private final String invalidVersion; + + public InvalidJavaEEVersion( String message, String invalidVersion ) + { + super( message ); + this.invalidVersion = invalidVersion; + } + + public String getInvalidVersion() + { + return invalidVersion; + } +} Added: maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/JavaEEVersion.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/JavaEEVersion.java?rev=1050900&view=auto ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/JavaEEVersion.java (added) +++ maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugin/ear/util/JavaEEVersion.java Sun Dec 19 17:10:52 2010 @@ -0,0 +1,164 @@ +package org.apache.maven.plugin.ear.util; + +import java.util.HashMap; +import java.util.Map; + +/** + * Represents the supported JavaEE version. + * + * @author Stephane Nicoll + */ +public class JavaEEVersion + implements Comparable +{ + + private static final String VERSION_1_3 = "1.3"; + + private static final String VERSION_1_4 = "1.4"; + + private static final String VERSION_5 = "5"; + + private static final String VERSION_6 = "6"; + + private static final Map versionsMap = new HashMap(); + + + /** + * Represents the J2EE 1.3 version. + */ + public static final JavaEEVersion OneDotThree = new JavaEEVersion( new Integer( 0 ), VERSION_1_3 ); + + /** + * Represents the J2EE 1.4 version. + */ + public static final JavaEEVersion OneDotFour = new JavaEEVersion( new Integer( 1 ), VERSION_1_4 ); + + /** + * Represents the JavaEE 5 version. + */ + public static final JavaEEVersion Five = new JavaEEVersion( new Integer( 2 ), VERSION_5 ); + + /** + * Represents the JavaEE 7 version. + */ + public static final JavaEEVersion Six = new JavaEEVersion( new Integer( 3 ), VERSION_6 ); + + + private final Integer index; + + private final String version; + + private JavaEEVersion( Integer index, String version ) + { + this.index = index; + this.version = version; + versionsMap.put( version, this ); + } + + public static JavaEEVersion getJavaEEVersion( String version ) + throws InvalidJavaEEVersion + { + if ( !isValid( version ) ) + { + throw new InvalidJavaEEVersion( "Invalid version [" + version + "]", version ); + } + return (JavaEEVersion) versionsMap.get( version ); + } + + /** + * Returns the version as a string. + * + * @return the version string + */ + public String getVersion() + { + return version; + } + + /** + * Specifies if this version is greater or equal to the specified version. + * + * @param version the version to check + * @return true if this version is greater or equal to <tt>version</tt> + */ + public boolean ge( JavaEEVersion version ) + { + return this.compareTo( version ) >= 0; + } + + /** + * Specifies if this version is greater than the specified version. + * + * @param version the version to check + * @return true if this version is greater to <tt>version</tt> + */ + public boolean gt( JavaEEVersion version ) + { + return this.compareTo( version ) > 0; + } + + /** + * Specifies if this version is equal to the specified version. + * + * @param version the version to check + * @return true if this version is equal to <tt>version</tt> + */ + public boolean eq( JavaEEVersion version ) + { + return this.compareTo( version ) == 0; + } + + /** + * Specifies if this version is less or equal to the specified version. + * + * @param version the version to check + * @return true if this version is less or equal to <tt>version</tt> + */ + public boolean le( JavaEEVersion version ) + { + return this.compareTo( version ) <= 0; + } + + + /** + * Specifies if this version is less than the specified version. + * + * @param version the version to check + * @return true if this version is less or equal to <tt>version</tt> + */ + public boolean lt( JavaEEVersion version ) + { + return this.compareTo( version ) < 0; + } + + /** + * Checks if the specified version string is valid. + * + * @param version the version string to check + * @return <tt>true</tt> if the version is valid + */ + private static boolean isValid( String version ) + { + if ( version == null ) + { + throw new IllegalArgumentException( "version could not be null." ); + } + return VERSION_1_3.equals( version ) || VERSION_1_4.equals( version ) || VERSION_5.equals( version ) || + VERSION_6.equals( version ); + } + + public int compareTo( Object other ) + { + if ( other == null ) + { + throw new IllegalArgumentException( "other object to compare to could not be null." ); + } + if ( !( other instanceof JavaEEVersion ) ) + { + throw new IllegalArgumentException( + "other object to compare must be a JavaEEVersion but was [" + other.getClass().getName() + "]" ); + } + final JavaEEVersion otherVersion = (JavaEEVersion) other; + return index.compareTo( otherVersion.index ); + } +} Added: maven/plugins/trunk/maven-ear-plugin/src/test/java/org/apache/maven/plugin/ear/util/JavaEEVersionTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/test/java/org/apache/maven/plugin/ear/util/JavaEEVersionTest.java?rev=1050900&view=auto ============================================================================== --- maven/plugins/trunk/maven-ear-plugin/src/test/java/org/apache/maven/plugin/ear/util/JavaEEVersionTest.java (added) +++ maven/plugins/trunk/maven-ear-plugin/src/test/java/org/apache/maven/plugin/ear/util/JavaEEVersionTest.java Sun Dec 19 17:10:52 2010 @@ -0,0 +1,131 @@ +package org.apache.maven.plugin.ear.util; + +import junit.framework.TestCase; + +/** + * @author Stephane Nicoll + */ +public class JavaEEVersionTest + extends TestCase +{ + + public void testGtSameVersion() + { + assertFalse( JavaEEVersion.Five.gt( JavaEEVersion.Five ) ); + } + + public void testGtNextVersion() + { + assertFalse( JavaEEVersion.Five.gt( JavaEEVersion.Six ) ); + } + + public void testGtPreviousVersion() + { + assertTrue( JavaEEVersion.Five.gt( JavaEEVersion.OneDotFour ) ); + } + + public void testGeSameVersion() + { + assertTrue( JavaEEVersion.Five.ge( JavaEEVersion.Five ) ); + } + + public void testGePreviousVersion() + { + assertTrue( JavaEEVersion.Five.ge( JavaEEVersion.OneDotFour ) ); + } + + public void testGeNextVersion() + { + assertFalse( JavaEEVersion.Five.ge( JavaEEVersion.Six ) ); + } + + public void testLtSameVersion() + { + assertFalse( JavaEEVersion.Five.lt( JavaEEVersion.Five ) ); + } + + public void testLtPreviousVersion() + { + assertFalse( JavaEEVersion.Five.lt( JavaEEVersion.OneDotFour ) ); + } + + public void testLtNextVersion() + { + assertTrue( JavaEEVersion.Five.lt( JavaEEVersion.Six ) ); + } + + public void testLeSameVersion() + { + assertTrue( JavaEEVersion.Five.le( JavaEEVersion.Five ) ); + } + + public void testLePreviousVersion() + { + assertFalse( JavaEEVersion.Five.le( JavaEEVersion.OneDotFour ) ); + } + + public void testLeNextVersion() + { + assertTrue( JavaEEVersion.Five.le( JavaEEVersion.Six ) ); + } + + public void testEqSameVersion() + { + assertTrue( JavaEEVersion.Five.eq( JavaEEVersion.Five ) ); + } + + public void testEqAnotherVersion() + { + assertFalse( JavaEEVersion.Five.eq( JavaEEVersion.OneDotThree ) ); + } + + public void testGetVersion() + { + assertEquals( "5", JavaEEVersion.Five.getVersion() ); + } + + public void testGetJavaEEVersionValid() + { + try + { + assertEquals( JavaEEVersion.Six, JavaEEVersion.getJavaEEVersion( "6" ) ); + } + catch ( InvalidJavaEEVersion invalidJavaEEVersion ) + { + fail( "No exception should have been thrown but got [" + invalidJavaEEVersion.getMessage() + "]" ); + } + } + + public void testGetJavaEEVersionInvalid() + { + try + { + JavaEEVersion.getJavaEEVersion( "2.4" ); + fail( "Should have failed to get an invalid version." ); + } + catch ( InvalidJavaEEVersion e ) + { + //OK + } + } + + public void testGetJavaEEVersionNull() + { + try + { + JavaEEVersion.getJavaEEVersion( null ); + fail( "Should have failed to get a 'null' version." ); + } + catch ( InvalidJavaEEVersion e ) + { + fail( "Should have failed with an illegal argument exception instead." ); + } + catch ( IllegalArgumentException e ) + { + // OK + } + + } + + +}