Coverage Report - org.apache.maven.plugin.ear.util.ArtifactTypeMappingService
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactTypeMappingService
88%
38/43
77%
17/22
5
 
 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  14
     {
 55  14
         this.typeMappings = new HashMap<String, List<String>>();
 56  14
         this.customMappings = new HashMap<String, String>();
 57  14
         init();
 58  14
     }
 59  
 
 60  
     public void configure( final PlexusConfiguration plexusConfiguration )
 61  
         throws EarPluginException, PlexusConfigurationException
 62  
     {
 63  
 
 64  
         // No user defined configuration
 65  9
         if ( plexusConfiguration == null )
 66  
         {
 67  0
             return;
 68  
         }
 69  
 
 70  
         // Inject users configuration
 71  9
         final PlexusConfiguration[] artifactTypeMappings =
 72  
             plexusConfiguration.getChildren( ARTIFACT_TYPE_MAPPING_ELEMENT );
 73  12
         for ( int i = 0; i < artifactTypeMappings.length; i++ )
 74  
         {
 75  7
             PlexusConfiguration artifactTypeMapping = artifactTypeMappings[i];
 76  7
             final String customType = artifactTypeMapping.getAttribute( TYPE_ATTRIBUTE );
 77  7
             final String mapping = artifactTypeMapping.getAttribute( MAPPING_ATTRIBUTE );
 78  
 
 79  7
             if ( customType == null )
 80  
             {
 81  2
                 throw new EarPluginException( "Invalid artifact type mapping, type attribute should be set." );
 82  
             }
 83  5
             else if ( mapping == null )
 84  
             {
 85  1
                 throw new EarPluginException( "Invalid artifact type mapping, mapping attribute should be set." );
 86  
             }
 87  4
             else if ( !EarModuleFactory.isStandardArtifactType( mapping ) )
 88  
             {
 89  1
                 throw new EarPluginException(
 90  
                     "Invalid artifact type mapping, mapping[" + mapping + "] must be a standard Ear artifact type["
 91  
                         + EarModuleFactory.getStandardArtifactTypes() + "]" );
 92  
             }
 93  3
             else if ( customMappings.containsKey( customType ) )
 94  
             {
 95  0
                 throw new EarPluginException(
 96  
                     "Invalid artifact type mapping, type[" + customType + "] is already registered." );
 97  
             }
 98  
             else
 99  
             {
 100  
                 // Add the custom mapping
 101  3
                 customMappings.put( customType, mapping );
 102  
 
 103  
                 // Register the custom mapping to its standard type
 104  3
                 List<String> typeMapping = typeMappings.get( mapping );
 105  3
                 typeMapping.add( customType );
 106  
             }
 107  
         }
 108  5
     }
 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  48
         if ( !EarModuleFactory.isStandardArtifactType( standardType ) )
 121  
         {
 122  0
             throw new IllegalStateException(
 123  
                 "Artifact type[" + standardType + "] is not a standard Ear artifact type["
 124  
                     + EarModuleFactory.getStandardArtifactTypes() + "]" );
 125  
         }
 126  48
         final List<String> typeMappings = this.typeMappings.get( standardType );
 127  48
         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  2
         if ( type == null )
 143  
         {
 144  0
             throw new IllegalStateException( "custom type could not be null." );
 145  
         }
 146  2
         else if ( EarModuleFactory.getStandardArtifactTypes().contains( type ) )
 147  
         {
 148  0
             return type;
 149  
         }
 150  2
         else if ( !customMappings.containsKey( type ) )
 151  
         {
 152  1
             throw new UnknownArtifactTypeException( "Unknown artifact type[" + type + "]" );
 153  
         }
 154  
         else
 155  
         {
 156  1
             return (String) customMappings.get( type );
 157  
         }
 158  
     }
 159  
 
 160  
     private void init()
 161  
     {
 162  
         // Initialize the typeMappings
 163  14
         typeMappings.clear();
 164  
 
 165  
         // Clear the customMappings
 166  14
         customMappings.clear();
 167  
 
 168  
         // Initialize the mapping with the standard artifact types
 169  14
         for ( String type : EarModuleFactory.getStandardArtifactTypes() )
 170  
         {
 171  154
             List<String> typeMapping = new ArrayList<String>();
 172  154
             typeMapping.add( type );
 173  154
             this.typeMappings.put( type, typeMapping );
 174  154
         }
 175  14
     }
 176  
 }