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