View Javadoc
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  public class URLResourceLoader
42      extends AbstractResourceLoader
43  {
44  
45      public static final String ID = "url";
46  
47      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          if ( name == null || name.length() == 0 )
60          {
61              throw new ResourceNotFoundException( "URLResourceLoader : No template name provided" );
62          }
63  
64          for ( Iterator i = paths.iterator(); i.hasNext(); )
65          {
66              String path = (String) i.next();
67  
68              try
69              {
70                  URL u = new URL( path + name );
71  
72                  final InputStream inputStream = u.openStream();
73  
74                  if ( inputStream != null )
75                  {
76                      if ( getLogger().isDebugEnabled() )
77                      {
78                          getLogger().debug( "URLResourceLoader: Found '" + name + "' at '" + path + "'" );
79                      }
80  
81                      // save this root for later re-use
82                      templateRoots.put( name, path );
83  
84                      return new URLPlexusResource( u )
85                      {
86                          private boolean useSuper;
87  
88                          public synchronized InputStream getInputStream()
89                              throws IOException
90                          {
91                              if ( !useSuper )
92                              {
93                                  useSuper = true;
94                                  return inputStream;
95                              }
96                              return super.getInputStream();
97                          }
98                      };
99                  }
100             }
101             catch( MalformedURLException mue )
102             {
103                 if ( getLogger().isDebugEnabled() )
104                 {
105                     getLogger().debug( "URLResourceLoader: No valid URL '" + path + name + '\'' );
106                 }
107             }
108             catch ( IOException ioe )
109             {
110                 if ( getLogger().isDebugEnabled() )
111                 {
112                     getLogger().debug(
113                                        "URLResourceLoader: Exception when looking for '" + name + "' at '" + path + "'",
114                                        ioe );
115                 }
116             }
117         }
118         
119         // here we try to download without any path just the name which can be an url
120         try
121         {
122             URL u = new URL( name );
123 
124             final InputStream inputStream = u.openStream();
125 
126             if ( inputStream != null )
127             {
128                 return new URLPlexusResource( u )
129                 {
130                     private boolean useSuper;
131 
132                     public synchronized InputStream getInputStream()
133                         throws IOException
134                     {
135                         if ( !useSuper )
136                         {
137                             useSuper = true;
138                             return inputStream;
139                         }
140                         return super.getInputStream();
141                     }
142                 };
143             }
144         }
145         catch( MalformedURLException mue )
146         {
147             if ( getLogger().isDebugEnabled() )
148             {
149                 getLogger().debug( "URLResourceLoader: No valid URL '" + name + '\'' );
150             }
151         }
152         catch ( IOException ioe )
153         {
154             if ( getLogger().isDebugEnabled() )
155             {
156                 getLogger().debug( "URLResourceLoader: Exception when looking for '" + name + '\'', ioe );
157             }
158        }
159 
160         // convert to a general Velocity ResourceNotFoundException
161         throw new ResourceNotFoundException( name );
162     }
163 }