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.File;
28  import java.io.IOException;
29  import java.util.Iterator;
30  
31  import org.codehaus.plexus.resource.PlexusResource;
32  import org.codehaus.plexus.resource.loader.AbstractResourceLoader;
33  import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
34  import org.codehaus.plexus.util.FileUtils;
35  
36  /**
37   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
38   * @author Jason van Zyl
39   * @version $Id$
40   * @plexus.component role-hint="file" instantiation-strategy="per-lookup"
41   */
42  public class FileResourceLoader
43      extends AbstractResourceLoader
44  {
45      public static final String ID = "file";
46  
47      // ----------------------------------------------------------------------
48      // ResourceLoader Implementation
49      // ----------------------------------------------------------------------
50  
51      public PlexusResource getResource( String name )
52          throws ResourceNotFoundException
53      {
54          for ( Iterator it = paths.iterator(); it.hasNext(); )
55          {
56              String path = (String) it.next();
57  
58              final File file = new File( path, name );
59  
60              if ( file.canRead() )
61              {
62                  return new FilePlexusResource( file );
63              }
64          }
65          File file = new File( name );
66          if ( file.isAbsolute() && file.canRead() )
67          {
68              return new FilePlexusResource( file );
69          }
70          throw new ResourceNotFoundException( name );
71      }
72  
73      /**
74       * @deprecated Use {@link org.codehaus.plexus.resource.ResourceManager#getResourceAsFile(PlexusResource )}.
75       */
76      public static File getResourceAsFile( String name, String outputPath, File outputDirectory )
77          throws FileResourceCreationException
78  
79      {
80          File f = new File( name );
81  
82          if ( f.exists() )
83          {
84              if ( outputPath == null )
85              {
86                  return f;
87              }
88              else
89              {
90                  try
91                  {
92                      File outputFile;
93  
94                      if ( outputDirectory != null )
95                      {
96                          outputFile = new File( outputDirectory, outputPath );
97                      }
98                      else
99                      {
100                         outputFile = new File( outputPath );
101                     }
102 
103                     if ( !outputFile.getParentFile().exists() )
104                     {
105                         outputFile.getParentFile().mkdirs();
106                     }
107 
108                     FileUtils.copyFile( f, outputFile );
109 
110                     return outputFile;
111                 }
112                 catch ( IOException e )
113                 {
114                     throw new FileResourceCreationException( "Cannot create file-based resource.", e );
115                 }
116             }
117         }
118 
119         return null;
120     }
121 }