Coverage Report - org.apache.maven.index.artifact.DefaultArtifactPackagingMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArtifactPackagingMapper
0 %
0/51
0 %
0/16
3,2
 
 1  
 package org.apache.maven.index.artifact;
 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.io.File;
 23  
 import java.io.FileInputStream;
 24  
 import java.io.IOException;
 25  
 import java.util.HashMap;
 26  
 import java.util.Map;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.codehaus.plexus.component.annotations.Component;
 30  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 31  
 import org.codehaus.plexus.util.IOUtil;
 32  
 
 33  
 /**
 34  
  * A very simple artifact packaging mapper, that has everything for quick-start wired in this class. Also, it takes into
 35  
  * account the "${nexus-work}/conf/packaging2extension-mapping.properties" file into account if found. To override the
 36  
  * "defaults" in this class, simply add lines to properties file with same keys.
 37  
  *
 38  
  * @author cstamas
 39  
  */
 40  
 @Component( role = ArtifactPackagingMapper.class )
 41  0
 public class DefaultArtifactPackagingMapper
 42  
     extends AbstractLogEnabled
 43  
     implements ArtifactPackagingMapper
 44  
 {
 45  
 
 46  
     public static final String MAPPING_PROPERTIES_FILE = "packaging2extension-mapping.properties";
 47  
 
 48  
     private File propertiesFile;
 49  
 
 50  
     private volatile Map<String, String> packaging2extensionMapping;
 51  
 
 52  
     private final static Map<String, String> defaults;
 53  
 
 54  
     static
 55  
     {
 56  0
         defaults = new HashMap<String, String>();
 57  0
         defaults.put( "ejb-client", "jar" );
 58  0
         defaults.put( "ejb", "jar" );
 59  0
         defaults.put( "rar", "jar" );
 60  0
         defaults.put( "par", "jar" );
 61  0
         defaults.put( "maven-plugin", "jar" );
 62  0
         defaults.put( "maven-archetype", "jar" );
 63  0
         defaults.put( "plexus-application", "jar" );
 64  0
         defaults.put( "eclipse-plugin", "jar" );
 65  0
         defaults.put( "eclipse-feature", "jar" );
 66  0
         defaults.put( "eclipse-application", "zip" );
 67  0
         defaults.put( "nexus-plugin", "jar" );
 68  0
         defaults.put( "java-source", "jar" );
 69  0
         defaults.put( "javadoc", "jar" );
 70  0
         defaults.put( "test-jar", "jar" );
 71  0
     }
 72  
 
 73  
     public void setPropertiesFile( File propertiesFile )
 74  
     {
 75  0
         this.propertiesFile = propertiesFile;
 76  0
         this.packaging2extensionMapping = null;
 77  0
     }
 78  
 
 79  
     public Map<String, String> getPackaging2extensionMapping()
 80  
     {
 81  0
         if ( packaging2extensionMapping == null )
 82  
         {
 83  0
             synchronized ( this )
 84  
             {
 85  0
                 if ( packaging2extensionMapping == null )
 86  
                 {
 87  0
                     packaging2extensionMapping = new HashMap<String, String>();
 88  
 
 89  
                     // merge defaults
 90  0
                     packaging2extensionMapping.putAll( defaults );
 91  
 
 92  0
                     if ( propertiesFile != null && propertiesFile.exists() )
 93  
                     {
 94  0
                         getLogger().info( "Found user artifact packaging mapping file, applying it..." );
 95  
 
 96  0
                         Properties userMappings = new Properties();
 97  
 
 98  0
                         FileInputStream fis = null;
 99  
 
 100  
                         try
 101  
                         {
 102  0
                             fis = new FileInputStream( propertiesFile );
 103  
 
 104  0
                             userMappings.load( fis );
 105  
 
 106  0
                             if ( userMappings.keySet().size() > 0 )
 107  
                             {
 108  0
                                 for ( Object key : userMappings.keySet() )
 109  
                                 {
 110  0
                                     packaging2extensionMapping.put( key.toString(),
 111  
                                                                     userMappings.getProperty( key.toString() ) );
 112  
                                 }
 113  
 
 114  0
                                 getLogger().info(
 115  
                                     propertiesFile.getAbsolutePath()
 116  
                                         + " user artifact packaging mapping file contained "
 117  
                                         + userMappings.keySet().size() + " mappings, applied them all succesfully." );
 118  
                             }
 119  
                         }
 120  0
                         catch ( IOException e )
 121  
                         {
 122  0
                             getLogger().warn(
 123  
                                 "Got IO exception during read of file: " + propertiesFile.getAbsolutePath() );
 124  
                         }
 125  
                         finally
 126  
                         {
 127  0
                             IOUtil.close( fis );
 128  0
                         }
 129  
 
 130  0
                     }
 131  
                     else
 132  
                     {
 133  
                         // make it silent if using defaults
 134  0
                         getLogger().debug(
 135  
                             "User artifact packaging mappings file not found, will work with defaults..." );
 136  
                     }
 137  
                 }
 138  0
             }
 139  
         }
 140  
 
 141  0
         return packaging2extensionMapping;
 142  
     }
 143  
 
 144  
     public void setPackaging2extensionMapping( Map<String, String> packaging2extensionMapping )
 145  
     {
 146  0
         this.packaging2extensionMapping = packaging2extensionMapping;
 147  0
     }
 148  
 
 149  
     public Map<String, String> getDefaults()
 150  
     {
 151  0
         return defaults;
 152  
     }
 153  
 
 154  
     public String getExtensionForPackaging( String packaging )
 155  
     {
 156  0
         if ( packaging == null )
 157  
         {
 158  0
             return "jar";
 159  
         }
 160  
 
 161  0
         if ( getPackaging2extensionMapping().containsKey( packaging ) )
 162  
         {
 163  0
             return getPackaging2extensionMapping().get( packaging );
 164  
         }
 165  
         else
 166  
         {
 167  
             // default's to packaging name, ie. "jar", "war", "pom", etc.
 168  0
             return packaging;
 169  
         }
 170  
     }
 171  
 }