View Javadoc
1   package org.apache.maven.plugin.ear.util;
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.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.plugin.ear.EarModuleFactory;
28  import org.apache.maven.plugin.ear.EarPluginException;
29  import org.apache.maven.plugin.ear.UnknownArtifactTypeException;
30  import org.codehaus.plexus.configuration.PlexusConfiguration;
31  import org.codehaus.plexus.configuration.PlexusConfigurationException;
32  
33  /**
34   * Allows to map custom artifact type to standard type.
35   * 
36   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
37   * @version $Id: ArtifactTypeMappingService.java 1645331 2014-12-13 17:31:09Z khmarbaise $
38   */
39  public class ArtifactTypeMappingService
40  {
41      static final String ARTIFACT_TYPE_MAPPING_ELEMENT = "artifactTypeMapping";
42  
43      static final String TYPE_ATTRIBUTE = "type";
44  
45      static final String MAPPING_ATTRIBUTE = "mapping";
46  
47      // A standard type to a list of customType
48      private final Map<String, List<String>> typeMappings;
49  
50      // The user-defined mapping for direct access
51      private final Map<String, String> customMappings;
52  
53      /**
54       * Create an instance.
55       */
56      public ArtifactTypeMappingService()
57      {
58          this.typeMappings = new HashMap<String, List<String>>();
59          this.customMappings = new HashMap<String, String>();
60          init();
61      }
62  
63      /**
64       * @param plexusConfiguration {@link PlexusConfiguration}
65       * @throws EarPluginException {@link EarPluginException}
66       * @throws PlexusConfigurationException {@link PlexusConfigurationException}
67       */
68      public void configure( final PlexusConfiguration plexusConfiguration )
69          throws EarPluginException, PlexusConfigurationException
70      {
71  
72          // No user defined configuration
73          if ( plexusConfiguration == null )
74          {
75              return;
76          }
77  
78          // Inject users configuration
79          final PlexusConfiguration[] artifactTypeMappings =
80              plexusConfiguration.getChildren( ARTIFACT_TYPE_MAPPING_ELEMENT );
81          for ( PlexusConfiguration artifactTypeMapping : artifactTypeMappings )
82          {
83              final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE );
84              final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE );
85  
86              if ( customType == null )
87              {
88                  throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." );
89              }
90              else if ( mapping == null )
91              {
92                  throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." );
93              }
94              else if ( !EarModuleFactory.isStandardArtifactType( mapping ) )
95              {
96                  throw new EarPluginException( "Invalid artifact type mapping, mapping[" + mapping
97                      + "] must be a standard Ear artifact type[" + EarModuleFactory.getStandardArtifactTypes() + "]" );
98              }
99              else if ( customMappings.containsKey( customType ) )
100             {
101                 throw new EarPluginException( "Invalid artifact type mapping, type[" + customType
102                     + "] is already registered." );
103             }
104             else
105             {
106                 // Add the custom mapping
107                 customMappings.put( customType, mapping );
108 
109                 // Register the custom mapping to its standard type
110                 List<String> typeMapping = typeMappings.get( mapping );
111                 typeMapping.add( customType );
112             }
113         }
114     }
115 
116     /**
117      * Specify whether the <tt>customType</tt> could be mapped to the <tt>standardType</tt>.
118      * 
119      * @param standardType the standard type (ejb, jar, war, ...)
120      * @param customType a user-defined type
121      * @return true if the customType could be mapped to the standard type
122      */
123     public boolean isMappedToType( final String standardType, final String customType )
124     {
125         if ( !EarModuleFactory.isStandardArtifactType( standardType ) )
126         {
127             throw new IllegalStateException( "Artifact type[" + standardType + "] is not a standard Ear artifact type["
128                 + EarModuleFactory.getStandardArtifactTypes() + "]" );
129         }
130         return this.typeMappings.get( standardType ).contains( customType );
131 
132     }
133 
134     /**
135      * Returns the standard type for the specified <tt>type</tt>. If the specified type is already a standard type, the
136      * orignal type is returned.
137      * 
138      * @param type a type
139      * @return the standard type (ejb, jar, war, ...) for this type
140      * @throws UnknownArtifactTypeException In case of missing mappings types.
141      */
142     public String getStandardType( final String type )
143         throws UnknownArtifactTypeException
144     {
145         if ( type == null )
146         {
147             throw new IllegalStateException( "custom type could not be null." );
148         }
149         else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
150         {
151             return type;
152         }
153         else if ( !customMappings.containsKey( type ) )
154         {
155             throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
156         }
157         else
158         {
159             return customMappings.get( type );
160         }
161     }
162 
163     private void init()
164     {
165         // Initialize the typeMappings
166         typeMappings.clear();
167 
168         // Clear the customMappings
169         customMappings.clear();
170 
171         // Initialize the mapping with the standard artifact types
172         for ( String type : EarModuleFactory.getStandardArtifactTypes() )
173         {
174             List<String> typeMapping = new ArrayList<String>();
175             typeMapping.add( type );
176             this.typeMappings.put( type, typeMapping );
177         }
178     }
179 }