Coverage Report - org.codehaus.plexus.resource.DefaultResourceManager
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultResourceManager
68 %
43/63
42 %
6/14
3,4
 
 1  
 package org.codehaus.plexus.resource;
 2  
 
 3  
 /*
 4  
  * The MIT License
 5  
  *
 6  
  * Copyright (c) 2004, The Codehaus
 7  
  *
 8  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 9  
  * this software and associated documentation files (the "Software"), to deal in
 10  
  * the Software without restriction, including without limitation the rights to
 11  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 12  
  * of the Software, and to permit persons to whom the Software is furnished to do
 13  
  * so, subject to the following conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be included in all
 16  
  * copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  * SOFTWARE.
 25  
  */
 26  
 
 27  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 28  
 import org.codehaus.plexus.resource.loader.FileResourceCreationException;
 29  
 import org.codehaus.plexus.resource.loader.ResourceIOException;
 30  
 import org.codehaus.plexus.resource.loader.ResourceLoader;
 31  
 import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
 32  
 import org.codehaus.plexus.util.FileUtils;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.FileOutputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.io.OutputStream;
 40  
 import java.util.Iterator;
 41  
 import java.util.Map;
 42  
 
 43  
 /**
 44  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 45  
  * @author Jason van Zyl
 46  
  * @version $Id$
 47  
  * @plexus.component instantiation-strategy="per-lookup"
 48  
  */
 49  4
 public class DefaultResourceManager
 50  
     extends AbstractLogEnabled
 51  
     implements ResourceManager
 52  
 {
 53  
 
 54  
     /** @plexus.requirement role="org.codehaus.plexus.resource.loader.ResourceLoader" */
 55  
     private Map resourceLoaders;
 56  
 
 57  
     /** @plexus.configuration */
 58  
     private File outputDirectory;
 59  
 
 60  
     // ----------------------------------------------------------------------
 61  
     // ResourceManager Implementation
 62  
     // ----------------------------------------------------------------------
 63  
 
 64  
     public InputStream getResourceAsInputStream( String name )
 65  
         throws ResourceNotFoundException
 66  
     {
 67  5
         PlexusResource resource = getResource( name );
 68  
         try
 69  
         {
 70  5
             return resource.getInputStream();
 71  
         }
 72  0
         catch ( IOException e )
 73  
         {
 74  0
             throw new ResourceIOException( "Failed to open resource " + resource.getName() + ": " + e.getMessage(), e );
 75  
         }
 76  
     }
 77  
 
 78  
     public File getResourceAsFile( String name )
 79  
         throws ResourceNotFoundException, FileResourceCreationException
 80  
     {
 81  5
         return getResourceAsFile( getResource( name ) );
 82  
     }
 83  
 
 84  
     public File getResourceAsFile( String name, String outputPath )
 85  
         throws ResourceNotFoundException, FileResourceCreationException
 86  
     {
 87  2
         if ( outputPath == null )
 88  
         {
 89  0
             return getResourceAsFile( name );
 90  
         }
 91  2
         PlexusResource resource = getResource( name );
 92  
         File outputFile;
 93  2
         if ( outputDirectory != null )
 94  
         {
 95  2
             outputFile = new File( outputDirectory, outputPath );
 96  
         }
 97  
         else
 98  
         {
 99  0
             outputFile = new File( outputPath );
 100  
         }
 101  2
         createResourceAsFile( resource, outputFile );
 102  2
         return outputFile;
 103  
     }
 104  
 
 105  
     public File resolveLocation( String name, String outputPath )
 106  
         throws IOException
 107  
     {
 108  
         // Honour what the original locator does and return null ...
 109  
         try
 110  
         {
 111  0
             return getResourceAsFile( name, outputPath );
 112  
         }
 113  0
         catch ( Exception e )
 114  
         {
 115  0
             return null;
 116  
         }
 117  
     }
 118  
 
 119  
     public File resolveLocation( String name )
 120  
         throws IOException
 121  
     {
 122  
         // Honour what the original locator does and return null ...
 123  
         try
 124  
         {
 125  0
             return getResourceAsFile( name );
 126  
         }
 127  0
         catch ( Exception e )
 128  
         {
 129  0
             return null;
 130  
         }
 131  
     }
 132  
 
 133  
     public void setOutputDirectory( File outputDirectory )
 134  
     {
 135  1
         this.outputDirectory = outputDirectory;
 136  1
     }
 137  
 
 138  
     public void addSearchPath( String id, String path )
 139  
     {
 140  0
         ResourceLoader loader = (ResourceLoader) resourceLoaders.get( id );
 141  
 
 142  0
         if ( loader == null )
 143  
         {
 144  0
             throw new IllegalArgumentException( "unknown resource loader: " + id );
 145  
         }
 146  
 
 147  0
         loader.addSearchPath( path );
 148  0
     }
 149  
 
 150  
     public PlexusResource getResource( String name )
 151  
         throws ResourceNotFoundException
 152  
     {
 153  12
         for ( Iterator i = resourceLoaders.values().iterator(); i.hasNext(); )
 154  
         {
 155  22
             ResourceLoader resourceLoader = (ResourceLoader) i.next();
 156  
 
 157  
             try
 158  
             {
 159  22
                 PlexusResource resource = resourceLoader.getResource( name );
 160  
 
 161  12
                 getLogger().debug( "The resource " + "'" + name + "'" + " was found as " + resource.getName() + "." );
 162  
 
 163  12
                 return resource;
 164  
             }
 165  10
             catch ( ResourceNotFoundException e )
 166  
             {
 167  10
                 getLogger().debug(
 168  
                                    "The resource " + "'" + name + "'" + " was not found with resourceLoader "
 169  
                                        + resourceLoader.getClass().getName() + "." );
 170  
             }
 171  10
         }
 172  
 
 173  0
         throw new ResourceNotFoundException( name );
 174  
     }
 175  
 
 176  
     public File getResourceAsFile( PlexusResource resource )
 177  
         throws FileResourceCreationException
 178  
     {
 179  
         try
 180  
         {
 181  5
             File f = resource.getFile();
 182  5
             if ( f != null )
 183  
             {
 184  3
                 return f;
 185  
             }
 186  
         }
 187  0
         catch ( IOException e )
 188  
         {
 189  
             // Ignore this, try to make use of resource.getInputStream().
 190  2
         }
 191  
 
 192  2
         final File outputFile = FileUtils.createTempFile( "plexus-resources", "tmp", outputDirectory );
 193  2
         outputFile.deleteOnExit();
 194  2
         createResourceAsFile( resource, outputFile );
 195  2
         return outputFile;
 196  
     }
 197  
 
 198  
     public void createResourceAsFile( PlexusResource resource, File outputFile )
 199  
         throws FileResourceCreationException
 200  
     {
 201  4
         InputStream is = null;
 202  4
         OutputStream os = null;
 203  
         try
 204  
         {
 205  4
             is = resource.getInputStream();
 206  4
             File dir = outputFile.getParentFile();
 207  4
             if ( !dir.isDirectory() && !dir.mkdirs() )
 208  
             {
 209  0
                 throw new FileResourceCreationException( "Failed to create directory " + dir.getPath() );
 210  
             }
 211  4
             os = new FileOutputStream( outputFile );
 212  4
             IOUtil.copy( is, os );
 213  4
             is.close();
 214  4
             is = null;
 215  4
             os.close();
 216  4
             os = null;
 217  
         }
 218  0
         catch ( IOException e )
 219  
         {
 220  0
             throw new FileResourceCreationException( "Cannot create file-based resource:" + e.getMessage(), e );
 221  
         }
 222  
         finally
 223  
         {
 224  4
             IOUtil.close( is );
 225  4
             IOUtil.close( os );
 226  4
         }
 227  4
     }
 228  
 
 229  
 }