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