Coverage Report - org.codehaus.plexus.resource.loader.URLResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
URLResourceLoader
30 %
10/33
20 %
4/20
8,333
URLResourceLoader$1
0 %
0/5
0 %
0/2
8,333
URLResourceLoader$2
0 %
0/5
0 %
0/2
8,333
 
 1  
 package org.codehaus.plexus.resource.loader;
 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 java.io.IOException;
 28  
 import java.io.InputStream;
 29  
 import java.net.MalformedURLException;
 30  
 import java.net.URL;
 31  
 import java.util.HashMap;
 32  
 import java.util.Iterator;
 33  
 import java.util.Map;
 34  
 
 35  
 import org.codehaus.plexus.resource.PlexusResource;
 36  
 
 37  
 /**
 38  
  * @author Jason van Zyl
 39  
  * @plexus.component role-hint="url" instantiation-strategy="per-lookup"
 40  
  */
 41  6
 public class URLResourceLoader
 42  
     extends AbstractResourceLoader
 43  
 {
 44  
 
 45  
     public static final String ID = "url";
 46  
 
 47  6
     protected Map templateRoots = new HashMap();
 48  
 
 49  
     /**
 50  
      * Get an InputStream so that the Runtime can build a template with it.
 51  
      * 
 52  
      * @param name name of template to fetch bytestream of
 53  
      * @return InputStream containing the template
 54  
      * @throws ResourceNotFoundException if template not found in the file template path.
 55  
      */
 56  
     public PlexusResource getResource( String name )
 57  
         throws ResourceNotFoundException
 58  
     {
 59  1
         if ( name == null || name.length() == 0 )
 60  
         {
 61  0
             throw new ResourceNotFoundException( "URLResourceLoader : No template name provided" );
 62  
         }
 63  
 
 64  1
         for ( Iterator i = paths.iterator(); i.hasNext(); )
 65  
         {
 66  0
             String path = (String) i.next();
 67  
 
 68  
             try
 69  
             {
 70  0
                 URL u = new URL( path + name );
 71  
 
 72  0
                 final InputStream inputStream = u.openStream();
 73  
 
 74  0
                 if ( inputStream != null )
 75  
                 {
 76  0
                     if ( getLogger().isDebugEnabled() )
 77  
                     {
 78  0
                         getLogger().debug( "URLResourceLoader: Found '" + name + "' at '" + path + "'" );
 79  
                     }
 80  
 
 81  
                     // save this root for later re-use
 82  0
                     templateRoots.put( name, path );
 83  
 
 84  0
                     return new URLPlexusResource( u )
 85  0
                     {
 86  
                         private boolean useSuper;
 87  
 
 88  
                         public synchronized InputStream getInputStream()
 89  
                             throws IOException
 90  
                         {
 91  0
                             if ( !useSuper )
 92  
                             {
 93  0
                                 useSuper = true;
 94  0
                                 return inputStream;
 95  
                             }
 96  0
                             return super.getInputStream();
 97  
                         }
 98  
                     };
 99  
                 }
 100  
             }
 101  0
             catch( MalformedURLException mue )
 102  
             {
 103  0
                 if ( getLogger().isDebugEnabled() )
 104  
                 {
 105  0
                     getLogger().debug( "URLResourceLoader: No valid URL '" + path + name + '\'' );
 106  
                 }
 107  
             }
 108  0
             catch ( IOException ioe )
 109  
             {
 110  0
                 if ( getLogger().isDebugEnabled() )
 111  
                 {
 112  0
                     getLogger().debug(
 113  
                                        "URLResourceLoader: Exception when looking for '" + name + "' at '" + path + "'",
 114  
                                        ioe );
 115  
                 }
 116  0
             }
 117  0
         }
 118  
         
 119  
         // here we try to download without any path just the name which can be an url
 120  
         try
 121  
         {
 122  1
             URL u = new URL( name );
 123  
 
 124  0
             final InputStream inputStream = u.openStream();
 125  
 
 126  0
             if ( inputStream != null )
 127  
             {
 128  0
                 return new URLPlexusResource( u )
 129  0
                 {
 130  
                     private boolean useSuper;
 131  
 
 132  
                     public synchronized InputStream getInputStream()
 133  
                         throws IOException
 134  
                     {
 135  0
                         if ( !useSuper )
 136  
                         {
 137  0
                             useSuper = true;
 138  0
                             return inputStream;
 139  
                         }
 140  0
                         return super.getInputStream();
 141  
                     }
 142  
                 };
 143  
             }
 144  
         }
 145  1
         catch( MalformedURLException mue )
 146  
         {
 147  1
             if ( getLogger().isDebugEnabled() )
 148  
             {
 149  1
                 getLogger().debug( "URLResourceLoader: No valid URL '" + name + '\'' );
 150  
             }
 151  
         }
 152  0
         catch ( IOException ioe )
 153  
         {
 154  0
             if ( getLogger().isDebugEnabled() )
 155  
             {
 156  0
                 getLogger().debug( "URLResourceLoader: Exception when looking for '" + name + '\'', ioe );
 157  
             }
 158  1
        }
 159  
 
 160  
         // convert to a general Velocity ResourceNotFoundException
 161  1
         throw new ResourceNotFoundException( name );
 162  
     }
 163  
 }