Coverage Report - org.apache.maven.shared.model.fileset.mappers.MapperUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
MapperUtil
0%
0/37
0%
0/16
7.333
 
 1  
 package org.apache.maven.shared.model.fileset.mappers;
 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.shared.model.fileset.Mapper;
 23  
 import org.codehaus.plexus.util.IOUtil;
 24  
 
 25  
 import java.io.IOException;
 26  
 import java.io.InputStream;
 27  
 import java.util.Properties;
 28  
 
 29  
 /**
 30  
  * Element to define a FileNameMapper.
 31  
  *
 32  
  * @version $Id: org.apache.maven.shared.model.fileset.mappers.MapperUtil.html 886882 2013-11-16 21:55:43Z hboutemy $
 33  
  */
 34  
 public final class MapperUtil
 35  
 {
 36  
     private static final String MAPPER_PROPERTIES = "mapper.properties";
 37  
 
 38  
     private static Properties implementations;
 39  
 
 40  
     private MapperUtil()
 41  0
     {
 42  
         // nop
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Initializes a properties object to store the built-in classnames.
 47  
      */
 48  
     private static void initializeBuiltIns()
 49  
     {
 50  0
         if ( implementations == null )
 51  
         {
 52  0
             Properties props = new Properties();
 53  
 
 54  0
             ClassLoader cloader = Thread.currentThread().getContextClassLoader();
 55  
 
 56  0
             InputStream stream = null;
 57  
 
 58  
             try
 59  
             {
 60  0
                 stream = cloader.getResourceAsStream( MAPPER_PROPERTIES );
 61  
 
 62  0
                 if ( stream == null )
 63  
                 {
 64  0
                     throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
 65  
                 }
 66  
 
 67  
                 try
 68  
                 {
 69  0
                     props.load( stream );
 70  
                 }
 71  0
                 catch ( IOException e )
 72  
                 {
 73  0
                     throw new IllegalStateException( "Cannot find classpath resource: " + MAPPER_PROPERTIES );
 74  0
                 }
 75  
             }
 76  
             finally
 77  
             {
 78  0
                 IOUtil.close( stream );
 79  0
             }
 80  
         }
 81  0
     }
 82  
 
 83  
     /**
 84  
      * Returns a fully configured FileNameMapper implementation.
 85  
      *
 86  
      * @param mapper
 87  
      * @return
 88  
      * @throws MapperException
 89  
      */
 90  
     public static FileNameMapper getFileNameMapper( Mapper mapper )
 91  
         throws MapperException
 92  
     {
 93  0
         if ( mapper == null )
 94  
         {
 95  0
             return null;
 96  
         }
 97  
 
 98  0
         initializeBuiltIns();
 99  
 
 100  0
         String type = mapper.getType();
 101  0
         String classname = mapper.getClassname();
 102  
 
 103  0
         if ( type == null && classname == null )
 104  
         {
 105  0
             throw new MapperException( "nested mapper or " + "one of the attributes type or classname is required" );
 106  
         }
 107  
 
 108  0
         if ( type != null && classname != null )
 109  
         {
 110  0
             throw new MapperException( "must not specify both type and classname attribute" );
 111  
         }
 112  0
         if ( type != null )
 113  
         {
 114  0
             classname = implementations.getProperty( type );
 115  
         }
 116  
 
 117  
         try
 118  
         {
 119  0
             FileNameMapper m = (FileNameMapper) Class.forName( classname ).newInstance();
 120  
 
 121  0
             m.setFrom( mapper.getFrom() );
 122  0
             m.setTo( mapper.getTo() );
 123  
 
 124  0
             return m;
 125  
         }
 126  0
         catch ( ClassNotFoundException e )
 127  
         {
 128  0
             throw new MapperException( "Cannot find mapper implementation: " + classname, e );
 129  
         }
 130  0
         catch ( InstantiationException e )
 131  
         {
 132  0
             throw new MapperException( "Cannot load mapper implementation: " + classname, e );
 133  
         }
 134  0
         catch ( IllegalAccessException e )
 135  
         {
 136  0
             throw new MapperException( "Cannot load mapper implementation: " + classname, e );
 137  
         }
 138  
     }
 139  
 }