View Javadoc
1   package org.apache.maven.plugin.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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService;
24  import org.apache.maven.plugin.ear.util.JavaEEVersion;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  
30  /**
31   * Builds an {@link EarModule} based on an <tt>Artifact</tt>.
32   * 
33   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
34   * @version $Id: EarModuleFactory.java 1645331 2014-12-13 17:31:09Z khmarbaise $
35   */
36  public final class EarModuleFactory
37  {
38      /**
39       * The list of artifact types.
40       */
41      public static final List<String> STANDARD_ARTIFACT_TYPE;
42  
43      static
44      {
45          List<String> temp = new ArrayList<String>();
46          temp.add( "jar" );
47          temp.add( "ejb" );
48          temp.add( "ejb3" );
49          temp.add( "par" );
50          temp.add( "ejb-client" );
51          temp.add( "app-client" );
52          temp.add( "rar" );
53          temp.add( "war" );
54          temp.add( "sar" );
55          temp.add( "wsr" );
56          temp.add( "har" );
57          STANDARD_ARTIFACT_TYPE = Collections.unmodifiableList( temp );
58      }
59  
60      /**
61       * Creates a new {@link EarModule} based on the specified {@link Artifact} and the specified execution
62       * configuration.
63       * 
64       * @param artifact the artifact
65       * @param javaEEVersion the javaEE version to use
66       * @param defaultLibBundleDir the default bundle dir for {@link org.apache.maven.plugin.ear.JarModule}
67       * @param includeInApplicationXml should {@link org.apache.maven.plugin.ear.JarModule} be included in application
68       *            Xml
69       * @param typeMappingService The artifact type mapping service
70       * @return an ear module for this artifact
71       * @throws UnknownArtifactTypeException if the artifact is not handled
72       */
73      @SuppressWarnings( "deprecation" )
74      // CHECKSTYLE_OFF: LineLength
75      public static EarModule newEarModule( Artifact artifact, JavaEEVersion javaEEVersion, String defaultLibBundleDir,
76                                            Boolean includeInApplicationXml, ArtifactTypeMappingService typeMappingService )
77      // CHECKSTYLE_ON: LineLength
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 ( "jar".equals( artifactType ) )
92          {
93              return new JarModule( artifact, defaultLibBundleDir, includeInApplicationXml );
94          }
95          else if ( "ejb".equals( artifactType ) )
96          {
97              return new EjbModule( artifact );
98          }
99          else if ( "ejb3".equals( artifactType ) )
100         {
101             return new Ejb3Module( artifact );
102         }
103         else if ( "par".equals( artifactType ) )
104         {
105             return new ParModule( artifact );
106         }
107         else if ( "ejb-client".equals( artifactType ) )
108         {
109             // Somewhat weird way to tackle the problem described in MEAR-85
110             if ( javaEEVersion.le( JavaEEVersion.ONE_DOT_FOUR ) )
111             {
112                 return new EjbClientModule( artifact, null );
113             }
114             else
115             {
116                 return new EjbClientModule( artifact, defaultLibBundleDir );
117             }
118         }
119         else if ( "app-client".equals( artifactType ) )
120         {
121             return new AppClientModule( artifact );
122         }
123         else if ( "rar".equals( artifactType ) )
124         {
125             return new RarModule( artifact );
126         }
127         else if ( "war".equals( artifactType ) )
128         {
129             return new WebModule( artifact );
130         }
131         else if ( "sar".equals( artifactType ) )
132         {
133             return new SarModule( artifact );
134         }
135         else if ( "wsr".equals( artifactType ) )
136         {
137             return new WsrModule( artifact );
138         }
139         else if ( "har".equals( artifactType ) )
140         {
141             return new HarModule( artifact );
142         }
143         else
144         {
145             throw new IllegalStateException( "Could not handle artifact type[" + artifactType + "]" );
146         }
147     }
148 
149     /**
150      * Returns a list of standard artifact types.
151      * 
152      * @return the standard artifact types
153      */
154     public static List<String> getStandardArtifactTypes()
155     {
156         return STANDARD_ARTIFACT_TYPE;
157     }
158 
159     /**
160      * Specify whether the specified type is standard artifact type.
161      * 
162      * @param type the type to check
163      * @return true if the specified type is a standard artifact type
164      */
165     public static boolean isStandardArtifactType( final String type )
166     {
167         return STANDARD_ARTIFACT_TYPE.contains( type );
168     }
169 
170 }