View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.ear.util;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.plugins.ear.EarModuleFactory;
27  import org.apache.maven.plugins.ear.EarPluginException;
28  import org.apache.maven.plugins.ear.UnknownArtifactTypeException;
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.codehaus.plexus.configuration.PlexusConfigurationException;
31  
32  /**
33   * Allows to map custom artifact type to standard type.
34   *
35   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
36   */
37  public class ArtifactTypeMappingService {
38      static final String ARTIFACT_TYPE_MAPPING_ELEMENT = "artifactTypeMapping";
39  
40      static final String TYPE_ATTRIBUTE = "type";
41  
42      static final String MAPPING_ATTRIBUTE = "mapping";
43  
44      // A standard type to a list of customType
45      private final Map<String, List<String>> typeMappings;
46  
47      // The user-defined mapping for direct access
48      private final Map<String, String> customMappings;
49  
50      /**
51       * Create an instance.
52       */
53      public ArtifactTypeMappingService() {
54          this.typeMappings = new HashMap<>();
55          this.customMappings = new HashMap<>();
56          init();
57      }
58  
59      /**
60       * @param plexusConfiguration {@link PlexusConfiguration}
61       * @throws EarPluginException {@link EarPluginException}
62       * @throws PlexusConfigurationException {@link PlexusConfigurationException}
63       */
64      public void configure(final PlexusConfiguration plexusConfiguration)
65              throws EarPluginException, PlexusConfigurationException {
66  
67          // No user defined configuration
68          if (plexusConfiguration == null) {
69              return;
70          }
71  
72          // Inject users configuration
73          final PlexusConfiguration[] artifactTypeMappings =
74                  plexusConfiguration.getChildren(ARTIFACT_TYPE_MAPPING_ELEMENT);
75          for (PlexusConfiguration artifactTypeMapping : artifactTypeMappings) {
76              final String customType = artifactTypeMapping.getAttribute(TYPE_ATTRIBUTE);
77              final String mapping = artifactTypeMapping.getAttribute(MAPPING_ATTRIBUTE);
78  
79              if (customType == null) {
80                  throw new EarPluginException("Invalid artifact type mapping, type attribute should be set.");
81              } else if (mapping == null) {
82                  throw new EarPluginException("Invalid artifact type mapping, mapping attribute should be set.");
83              } else if (!EarModuleFactory.isStandardArtifactType(mapping)) {
84                  throw new EarPluginException(
85                          "Invalid artifact type mapping, mapping[" + mapping + "] must be a standard Ear artifact type["
86                                  + EarModuleFactory.getStandardArtifactTypes() + "]");
87              } else if (customMappings.containsKey(customType)) {
88                  throw new EarPluginException(
89                          "Invalid artifact type mapping, type[" + customType + "] is already registered.");
90              } else {
91                  // Add the custom mapping
92                  customMappings.put(customType, mapping);
93  
94                  // Register the custom mapping to its standard type
95                  List<String> typeMapping = typeMappings.get(mapping);
96                  typeMapping.add(customType);
97              }
98          }
99      }
100 
101     /**
102      * Specify whether the {@code customType} could be mapped to the {@code standardType}.
103      *
104      * @param standardType the standard type (ejb, jar, war, ...)
105      * @param customType a user-defined type
106      * @return true if the customType could be mapped to the standard type
107      */
108     public boolean isMappedToType(final String standardType, final String customType) {
109         if (!EarModuleFactory.isStandardArtifactType(standardType)) {
110             throw new IllegalStateException("Artifact type[" + standardType + "] is not a standard Ear artifact type["
111                     + EarModuleFactory.getStandardArtifactTypes() + "]");
112         }
113         return this.typeMappings.get(standardType).contains(customType);
114     }
115 
116     /**
117      * Returns the standard type for the specified {@code type}. If the specified type is already a standard type, the
118      * orignal type is returned.
119      *
120      * @param type a type
121      * @return the standard type (ejb, jar, war, ...) for this type
122      * @throws UnknownArtifactTypeException In case of missing mappings types.
123      */
124     public String getStandardType(final String type) throws UnknownArtifactTypeException {
125         if (type == null) {
126             throw new IllegalStateException("custom type could not be null.");
127         } else if (EarModuleFactory.getStandardArtifactTypes().contains(type)) {
128             return type;
129         } else if (!customMappings.containsKey(type)) {
130             throw new UnknownArtifactTypeException("Unknown artifact type[" + type + "]");
131         } else {
132             return customMappings.get(type);
133         }
134     }
135 
136     private void init() {
137         // Initialize the typeMappings
138         typeMappings.clear();
139 
140         // Clear the customMappings
141         customMappings.clear();
142 
143         // Initialize the mapping with the standard artifact types
144         for (String type : EarModuleFactory.getStandardArtifactTypes()) {
145             List<String> typeMapping = new ArrayList<>();
146             typeMapping.add(type);
147             this.typeMappings.put(type, typeMapping);
148         }
149     }
150 }