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  		this.rootDir = new File(new File("").getAbsolutePath());
49  		this.logger = new NullLogger();
50  	}
51  
52  	/**
53  	 * Constructor
54  	 *
55  	 * @param rootDir the root directory to start the search
56  	 */
57  	public InputStreamLocator(File rootDir) {
58  		this(rootDir, new NullLogger());
59  	}
60  
61  	/**
62  	 * Constructor
63  	 *
64  	 * @param rootDir the root directory to start the search
65  	 * @param logger  the logger to be used
66  	 */
67  	public InputStreamLocator(File rootDir, Logger logger) {
68  		this.rootDir = rootDir;
69  		this.logger = logger;
70  	}
71  
72  	/**
73  	 * Locate the file with the given position using the following steps
74  	 *
75  	 * @param location the location of the source to be loaded
76  	 * @return input stream of the source
77  	 * @throws IOException if source is not found
78  	 */
79  	public InputStream locate(String location) throws IOException {
80  		if (location == null || location.length() == 0) {
81  			return null;
82  		}
83  
84  		String baseName = null;
85  		File file = null;
86  		InputStream is = null;
87  
88  		// try to load a relative location with the given root dir
89  		// e.g. "componentRoles.xml" located in the current working directory
90  		if (is == null) {
91  			file = new File(this.rootDir, location);
92  
93  			this.logger.debug("Looking for " + location + " in the root directory");
94  
95  			if (file.exists()) {
96  				is = new FileInputStream(file);
97  				this.logger.debug("Found " + location + " as " + file.getAbsolutePath());
98  			}
99  		}
100 
101 		// try to load an absolute location as file
102 		// e.g. "/foo/componentRoles.xml" from the root of the file system
103 		if (is == null) {
104 			file = new File(location);
105 
106 			this.logger.debug("Looking for " + location + " as absolute file location");
107 
108 			if (file.isAbsolute() && file.exists()) {
109 				is = new FileInputStream(file);
110 				this.logger.debug("Found " + location + " as " + file.getAbsolutePath());
111 			}
112 		}
113 
114 		// try to load an absolute location through the classpath
115 		// e.g. "/componentRoles.xml" located in the classpath
116 		if (is == null && location.startsWith("/") == true) {
117 			this.logger.debug("Looking for " + location + " using the class loader");
118 			is = getClass().getResourceAsStream(location);
119 
120 			if (is != null) {
121 				this.logger.debug("Successfully located " + location);
122 			}
123 		}
124 
125 		// try to load the last part of the file name using the classloader
126 		// e.g. "conf/componentRoles.xml" as "/componentRoles.xml" located in
127 		// the classpath.
128 
129 		if (is == null && location.startsWith("/") == false) {
130 			baseName = '/' + new File(location).getName();
131 			this.logger.debug("Looking for " + baseName + " using the class loader");
132 			is = getClass().getResourceAsStream(baseName);
133 			if (is != null) {
134 				this.logger.debug("Successfully located " + baseName);
135 			}
136 		}
137 
138 		if (is == null) {
139 			this.logger.debug("Unable to find any resource with the name '" + location + "'");
140 		}
141 
142 		return is;
143 	}
144 
145 	/**
146 	 * @return Returns the rootDir.
147 	 */
148 	protected File getRootDir() {
149 		return rootDir;
150 	}
151 
152 }