View Javadoc
1   package org.apache.maven.plugins.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugins.ear.util.ArtifactTypeMappingService;
28  import org.apache.maven.plugins.ear.util.JavaEEVersion;
29  
30  /**
31   * Builds an {@link EarModule} based on an {@code Artifact}.
32   * 
33   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
34   */
35  public final class EarModuleFactory
36  {
37      private static final String TEST_JAR_ARTIFACT_TYPE = "test-jar";
38      private static final String JBOSS_PAR_ARTIFACT_TYPE = "jboss-par";
39      private static final String JBOSS_SAR_ARTIFACT_TYPE = "jboss-sar";
40      private static final String JBOSS_HAR_ARTIFACT_TYPE = "jboss-har";
41  
42      /**
43       * The list of artifact types.
44       */
45      private static final List<String> STANDARD_ARTIFACT_TYPES =
46          Collections.unmodifiableList( Arrays.asList(
47              JarModule.DEFAULT_ARTIFACT_TYPE,
48              EjbModule.DEFAULT_ARTIFACT_TYPE,
49              ParModule.DEFAULT_ARTIFACT_TYPE,
50              EjbClientModule.DEFAULT_ARTIFACT_TYPE,
51              AppClientModule.DEFAULT_ARTIFACT_TYPE,
52              RarModule.DEFAULT_ARTIFACT_TYPE,
53              WebModule.DEFAULT_ARTIFACT_TYPE,
54              SarModule.DEFAULT_ARTIFACT_TYPE,
55              WsrModule.DEFAULT_ARTIFACT_TYPE,
56              HarModule.DEFAULT_ARTIFACT_TYPE,
57              TEST_JAR_ARTIFACT_TYPE,
58              JBOSS_PAR_ARTIFACT_TYPE,
59              JBOSS_SAR_ARTIFACT_TYPE,
60              JBOSS_HAR_ARTIFACT_TYPE ) );
61  
62      /**
63       * Creates a new {@link EarModule} based on the specified {@link Artifact} and the specified execution
64       * configuration.
65       * 
66       * @param artifact the artifact
67       * @param javaEEVersion the javaEE version to use
68       * @param defaultLibBundleDir the default bundle dir for {@link org.apache.maven.plugins.ear.JarModule}
69       * @param includeInApplicationXml should {@link org.apache.maven.plugins.ear.JarModule} be included in application
70       *            Xml
71       * @param typeMappingService The artifact type mapping service
72       * @return an ear module for this artifact
73       * @throws UnknownArtifactTypeException if the artifact is not handled
74       */
75      public static EarModule newEarModule( Artifact artifact, JavaEEVersion javaEEVersion, String defaultLibBundleDir,
76                                            Boolean includeInApplicationXml,
77                                            ArtifactTypeMappingService typeMappingService )
78          throws UnknownArtifactTypeException
79      {
80          // Get the standard artifact type based on default config and user-defined mapping(s)
81          final String artifactType;
82          try
83          {
84              artifactType = typeMappingService.getStandardType( artifact.getType() );
85          }
86          catch ( UnknownArtifactTypeException e )
87          {
88              throw new UnknownArtifactTypeException( e.getMessage() + " for " + artifact.getArtifactId() );
89          }
90  
91          if ( JarModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) || TEST_JAR_ARTIFACT_TYPE.equals( artifactType ) )
92          {
93              return new JarModule( artifact, defaultLibBundleDir, includeInApplicationXml );
94          }
95          else if ( EjbModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
96          {
97              return new EjbModule( artifact );
98          }
99          else if ( ParModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType )
100             || JBOSS_PAR_ARTIFACT_TYPE.equals( artifactType ) )
101         {
102             return new ParModule( artifact );
103         }
104         else if ( EjbClientModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
105         {
106             // Somewhat weird way to tackle the problem described in MEAR-85
107             if ( javaEEVersion.le( JavaEEVersion.ONE_DOT_FOUR ) )
108             {
109                 return new EjbClientModule( artifact, null );
110             }
111             else
112             {
113                 return new EjbClientModule( artifact, defaultLibBundleDir );
114             }
115         }
116         else if ( AppClientModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
117         {
118             return new AppClientModule( artifact );
119         }
120         else if ( RarModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
121         {
122             return new RarModule( artifact );
123         }
124         else if ( WebModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
125         {
126             return new WebModule( artifact );
127         }
128         else if ( SarModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType )
129             || JBOSS_SAR_ARTIFACT_TYPE.equals( artifactType ) )
130         {
131             return new SarModule( artifact );
132         }
133         else if ( WsrModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType ) )
134         {
135             return new WsrModule( artifact );
136         }
137         else if ( HarModule.DEFAULT_ARTIFACT_TYPE.equals( artifactType )
138             || JBOSS_HAR_ARTIFACT_TYPE.equals( artifactType ) )
139         {
140             return new HarModule( artifact );
141         }
142         else
143         {
144             throw new IllegalStateException( "Could not handle artifact type[" + artifactType + "]" );
145         }
146     }
147 
148     /**
149      * Returns a list of standard artifact types.
150      * 
151      * @return the standard artifact types
152      */
153     public static List<String> getStandardArtifactTypes()
154     {
155         return STANDARD_ARTIFACT_TYPES;
156     }
157 
158     /**
159      * Specify whether the specified type is standard artifact type.
160      * 
161      * @param type the type to check
162      * @return true if the specified type is a standard artifact type
163      */
164     public static boolean isStandardArtifactType( final String type )
165     {
166         return STANDARD_ARTIFACT_TYPES.contains( type );
167     }
168 
169 }