View Javadoc
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  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          PlexusResource resource = getResource( name );
68          try
69          {
70              return resource.getInputStream();
71          }
72          catch ( IOException e )
73          {
74              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          return getResourceAsFile( getResource( name ) );
82      }
83  
84      public File getResourceAsFile( String name, String outputPath )
85          throws ResourceNotFoundException, FileResourceCreationException
86      {
87          if ( outputPath == null )
88          {
89              return getResourceAsFile( name );
90          }
91          PlexusResource resource = getResource( name );
92          File outputFile;
93          if ( outputDirectory != null )
94          {
95              outputFile = new File( outputDirectory, outputPath );
96          }
97          else
98          {
99              outputFile = new File( outputPath );
100         }
101         createResourceAsFile( resource, outputFile );
102         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             return getResourceAsFile( name, outputPath );
112         }
113         catch ( Exception e )
114         {
115             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             return getResourceAsFile( name );
126         }
127         catch ( Exception e )
128         {
129             return null;
130         }
131     }
132 
133     public void setOutputDirectory( File outputDirectory )
134     {
135         this.outputDirectory = outputDirectory;
136     }
137 
138     public void addSearchPath( String id, String path )
139     {
140         ResourceLoader loader = (ResourceLoader) resourceLoaders.get( id );
141 
142         if ( loader == null )
143         {
144             throw new IllegalArgumentException( "unknown resource loader: " + id );
145         }
146 
147         loader.addSearchPath( path );
148     }
149 
150     public PlexusResource getResource( String name )
151         throws ResourceNotFoundException
152     {
153         for ( Iterator i = resourceLoaders.values().iterator(); i.hasNext(); )
154         {
155             ResourceLoader resourceLoader = (ResourceLoader) i.next();
156 
157             try
158             {
159                 PlexusResource resource = resourceLoader.getResource( name );
160 
161                 getLogger().debug( "The resource " + "'" + name + "'" + " was found as " + resource.getName() + "." );
162 
163                 return resource;
164             }
165             catch ( ResourceNotFoundException e )
166             {
167                 getLogger().debug(
168                                    "The resource " + "'" + name + "'" + " was not found with resourceLoader "
169                                        + resourceLoader.getClass().getName() + "." );
170             }
171         }
172 
173         throw new ResourceNotFoundException( name );
174     }
175 
176     public File getResourceAsFile( PlexusResource resource )
177         throws FileResourceCreationException
178     {
179         try
180         {
181             File f = resource.getFile();
182             if ( f != null )
183             {
184                 return f;
185             }
186         }
187         catch ( IOException e )
188         {
189             // Ignore this, try to make use of resource.getInputStream().
190         }
191 
192         final File outputFile = FileUtils.createTempFile( "plexus-resources", "tmp", outputDirectory );
193         outputFile.deleteOnExit();
194         createResourceAsFile( resource, outputFile );
195         return outputFile;
196     }
197 
198     public void createResourceAsFile( PlexusResource resource, File outputFile )
199         throws FileResourceCreationException
200     {
201         InputStream is = null;
202         OutputStream os = null;
203         try
204         {
205             is = resource.getInputStream();
206             File dir = outputFile.getParentFile();
207             if ( !dir.isDirectory() && !dir.mkdirs() )
208             {
209                 throw new FileResourceCreationException( "Failed to create directory " + dir.getPath() );
210             }
211             os = new FileOutputStream( outputFile );
212             IOUtil.copy( is, os );
213             is.close();
214             is = null;
215             os.close();
216             os = null;
217         }
218         catch ( IOException e )
219         {
220             throw new FileResourceCreationException( "Cannot create file-based resource:" + e.getMessage(), e );
221         }
222         finally
223         {
224             IOUtil.close( is );
225             IOUtil.close( os );
226         }
227     }
228 
229 }