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