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