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 942855 2010-05-10 19:13:52Z krosenvold $
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( "Invalid artifact type mapping, mapping[" + mapping +
91                      "] must be a standard Ear artifact type[" + 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 typeMapping = (List) 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( "Artifact type[" + standardType +
123                 "] is not a standard Ear artifact type[" + EarModuleFactory.getStandardArtifactTypes() + "]" );
124         }
125         final List typeMappings = (List) this.typeMappings.get( standardType );
126         return typeMappings.contains( customType );
127 
128     }
129 
130     /**
131      * Returns the standard type for the specified <tt>type</tt>. If
132      * the specified type is already a standard type, the orignal type
133      * is returned.
134      *
135      * @param type a type
136      * @return the standard type (ejb, jar, war, ...) for this type
137      */
138     public String getStandardType( final String type )
139         throws UnknownArtifactTypeException
140     {
141         if ( type == null )
142         {
143             throw new IllegalStateException( "custom type could not be null." );
144         }
145         else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
146         {
147             return type;
148         }
149         else if ( !customMappings.containsKey( type ) )
150         {
151             throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
152         }
153         else
154         {
155             return (String) customMappings.get( type );
156         }
157     }
158 
159     private void init()
160     {
161         // Initialize the typeMappings
162         typeMappings.clear();
163 
164         // Clear the customMappings
165         customMappings.clear();
166 
167         // Initialize the mapping with the standard artifact types
168         final Iterator it = EarModuleFactory.getStandardArtifactTypes().iterator();
169         while ( it.hasNext() )
170         {
171             String type = (String) it.next();
172             List typeMapping = new ArrayList();
173             typeMapping.add( type );
174             this.typeMappings.put( type, typeMapping );
175         }
176 
177     }
178 }