View Javadoc

1   package org.apache.fulcrum.yaafi.framework.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import org.apache.avalon.framework.logger.Logger;
28  import org.apache.avalon.framework.logger.NullLogger;
29  
30  /**
31   * Helper for locating a file name and returning an input stream.
32   *
33   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
34   */
35  
36  public class InputStreamLocator
37  {
38      /** the root directory of our search */
39      private File rootDir;
40  
41      /** the logger to be used */
42      private Logger logger;
43  
44      /**
45       * Constructor
46       */
47      public InputStreamLocator()
48      {
49          this.rootDir = new File( new File("").getAbsolutePath() );
50          this.logger = new NullLogger();
51      }
52  
53      /**
54       * Constructor
55       *
56       * @param rootDir the root directory to start the search     */
57      public InputStreamLocator( File rootDir )
58      {
59          this( rootDir, new NullLogger() );
60      }
61  
62      /**
63       * Constructor
64       *
65       * @param rootDir the root directory to start the search
66       * @param logger the logger to be used
67       */
68      public InputStreamLocator( File rootDir, Logger logger )
69      {
70          this.rootDir    = rootDir;
71          this.logger     = logger;
72      }
73  
74      /**
75       * Locate the file with the given position using the following steps
76       *
77       * @param location the location of the source to be loaded
78       */
79      public InputStream locate( String location ) throws IOException
80      {
81          if( ( location == null ) || ( location.length() == 0 ) )
82          {
83              return null;
84          }
85  
86          String baseName = null;
87          File file = null;
88          InputStream is = null;
89  
90          // try to load a relative location with the given root dir
91          // e.g. "componentRoles.xml" located in the current working directory
92  
93          if( is == null )
94          {
95              file = new File( this.rootDir, location );
96  
97              this.getLogger().debug("Looking for " + location + " in the root directory");
98  
99              if( file.exists() )
100             {
101                 is = new FileInputStream( file );
102                 this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
103             }
104         }
105 
106         // try to load an absolute location as file
107         // e.g. "/foo/componentRoles.xml" from the root of the file system
108 
109         if( is == null )
110         {
111             file = new File( location );
112 
113             this.getLogger().debug("Looking for " + location + " as absolute file location");
114 
115             if( file.isAbsolute() && file.exists() )
116             {
117                 is = new FileInputStream( file );
118                 this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
119             }
120         }
121 
122         // try to load an absolute location through the classpath
123         // e.g. "/componentRoles.xml" located in the classpath
124 
125         if( ( is == null ) && ( location.startsWith( "/" ) == true ) )
126         {
127             this.getLogger().debug("Looking for " + location + " using the class loader");
128             is =  getClass().getResourceAsStream( location );
129         }
130 
131         if( is == null )
132         {
133             this.getLogger().warn("Unable to locate " + location);
134         }
135         else
136         {
137             this.getLogger().debug("Successfully located " + location);
138         }
139 
140         // try to load the last part of the file name using the classloader
141         // e.g. "conf/componentRoles.xml" as "/componentRoles.xml" located in
142         // the classpath.
143 
144         if( ( is == null ) && ( location.startsWith( "/" ) == false ) )
145         {
146             baseName = '/' + new File(location).getName();
147             this.getLogger().debug("Looking for " + baseName + " using the class loader");
148             is =  getClass().getResourceAsStream( baseName );
149         }
150 
151         if( is == null )
152         {
153             this.getLogger().warn("Unable to locate " + baseName);
154         }
155         else
156         {
157             this.getLogger().debug("Successfully located " + baseName);
158         }
159 
160         return is;
161     }
162 
163     /**
164      * @return Returns the logger.
165      */
166     protected Logger getLogger()
167     {
168         return logger;
169     }
170 
171     /**
172      * @return Returns the rootDir.
173      */
174     protected File getRootDir()
175     {
176         return rootDir;
177     }
178 
179 
180 }