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