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 1542508 2013-11-16 13:21:35Z 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 ( PlexusConfiguration artifactTypeMapping : artifactTypeMappings )
74          {
75              final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE );
76              final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE );
77  
78              if ( customType == null )
79              {
80                  throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." );
81              }
82              else if ( mapping == null )
83              {
84                  throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." );
85              }
86              else if ( !EarModuleFactory.isStandardArtifactType( mapping ) )
87              {
88                  throw new EarPluginException( "Invalid artifact type mapping, mapping[" + mapping
89                      + "] must be a standard Ear artifact type[" + EarModuleFactory.getStandardArtifactTypes() + "]" );
90              }
91              else if ( customMappings.containsKey( customType ) )
92              {
93                  throw new EarPluginException( "Invalid artifact type mapping, type[" + customType
94                      + "] is already registered." );
95              }
96              else
97              {
98                  // Add the custom mapping
99                  customMappings.put( customType, mapping );
100 
101                 // Register the custom mapping to its standard type
102                 List<String> typeMapping = typeMappings.get( mapping );
103                 typeMapping.add( customType );
104             }
105         }
106     }
107 
108     /**
109      * Specify whether the <tt>customType</tt> could be mapped to the <tt>standardType</tt>.
110      * 
111      * @param standardType the standard type (ejb, jar, war, ...)
112      * @param customType a user-defined type
113      * @return true if the customType could be mapped to the standard type
114      */
115     public boolean isMappedToType( final String standardType, final String customType )
116     {
117         if ( !EarModuleFactory.isStandardArtifactType( standardType ) )
118         {
119             throw new IllegalStateException( "Artifact type[" + standardType + "] is not a standard Ear artifact type["
120                 + EarModuleFactory.getStandardArtifactTypes() + "]" );
121         }
122         final List<String> typeMappings = this.typeMappings.get( standardType );
123         return typeMappings.contains( customType );
124 
125     }
126 
127     /**
128      * Returns the standard type for the specified <tt>type</tt>. If the specified type is already a standard type, the
129      * orignal type is returned.
130      * 
131      * @param type a type
132      * @return the standard type (ejb, jar, war, ...) for this type
133      */
134     public String getStandardType( final String type )
135         throws UnknownArtifactTypeException
136     {
137         if ( type == null )
138         {
139             throw new IllegalStateException( "custom type could not be null." );
140         }
141         else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
142         {
143             return type;
144         }
145         else if ( !customMappings.containsKey( type ) )
146         {
147             throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
148         }
149         else
150         {
151             return customMappings.get( type );
152         }
153     }
154 
155     private void init()
156     {
157         // Initialize the typeMappings
158         typeMappings.clear();
159 
160         // Clear the customMappings
161         customMappings.clear();
162 
163         // Initialize the mapping with the standard artifact types
164         for ( String type : EarModuleFactory.getStandardArtifactTypes() )
165         {
166             List<String> typeMapping = new ArrayList<String>();
167             typeMapping.add( type );
168             this.typeMappings.put( type, typeMapping );
169         }
170     }
171 }