View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.javagen.agile.core.util;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.Properties;
25  
26  /***
27   * Convenience methods for loading Spring-like resources.  
28   * For example: classpath:email.properties or file:user.properties
29   *  
30   * @author <a href="mailto:reaster@AutoTradeCenter.com">Richard Easterling</a>
31   */
32  public class ResourceLoader {
33  
34  	private static ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
35  
36  	/*** Pseudo URL prefix for loading from the class path: "classpath:" */
37  	public static final String CLASSPATH_URL_PREFIX = "classpath:";
38  
39  	/*** URL prefix for loading from the file system: "file:" */
40  	public static final String FILE_URL_PREFIX = "file:";
41  
42  	public static boolean exists(String resourceURL) {
43  		return existsOrCanOpen(resourceURL.getClass(), resourceURL);
44  	}
45  
46  	public static boolean exists(Class<?> clazz, String resourceURL) {
47  		return existsOrCanOpen(clazz, resourceURL);
48  	}
49  
50  	public static InputStream inputStream(String resourceURL) throws IOException {
51  		try {
52  			return getInputStream(resourceURL.getClass(), resourceURL);
53  		} catch (Exception e) {
54  			throw new FileNotFoundException(resourceURL);
55  		}
56  	}
57  	
58      public static InputStream inputStream(Class<?> clazz, String resourceURL) throws IOException {
59  		try {
60  			return getInputStream(clazz, resourceURL);
61  		} catch (Exception e) {
62  			throw new FileNotFoundException(resourceURL);
63  		}
64  	}
65  	
66  	public static Properties properties(String resourceURL) throws IOException {
67  		return properties(resourceURL, new Properties());
68  	}
69  	
70  	public static Properties properties(String resourceURL, Properties props) throws IOException {
71  		return properties(props.getClass(), resourceURL, props);
72  	}
73  	
74      public static Properties properties(Class<?> clazz, String resourceURL, Properties props) throws IOException {
75  		props.load(getInputStream(clazz, resourceURL));
76  		return props;
77  	}
78  	
79      private static InputStream getInputStream(Class<?> clazz, String resourceURL) throws IOException {
80  		if (resourceURL==null)
81  			throw new FileNotFoundException("resourceURL is null");
82  		if (resourceURL.indexOf('//')>=0)
83  			resourceURL = resourceURL.replace('//','/');
84  		if (resourceURL.startsWith(CLASSPATH_URL_PREFIX)) {
85  			String path = resourceURL.substring(CLASSPATH_URL_PREFIX.length());
86  			if ( ! path.startsWith("/") )
87  				path = "/"+path;
88  			return getResourceInputStream(clazz, path);
89  		} else {
90  			URL url = tryMakingUrl(resourceURL);
91  			if (url!=null) {
92  				return url.openStream();
93  			} else {
94  				throw new FileNotFoundException(resourceURL  + " cannot be opened because it does not exist"); 
95  			}
96  		}
97  	}
98  
99      private static InputStream getResourceInputStream(Class<?> clazz, String path) throws IOException {
100 		InputStream is = null;
101 		if (clazz != null) {
102 			is = clazz.getResourceAsStream(path);
103 		} else {
104 			is = classLoader.getResourceAsStream(path);
105 		}
106 		if (is == null) {
107 			throw new FileNotFoundException(path  + " cannot be opened because it does not exist");
108 		}
109 		return is;
110 	}
111 
112 	private static URL tryMakingUrl(String resourceLocation) {
113 		if (resourceLocation==null)
114 			return null;
115 		try {
116 			return new URL(resourceLocation);
117 		} catch (MalformedURLException ex) {
118 			return null;
119 		}
120 	}
121 
122     private static boolean existsOrCanOpen(Class<?> clazz, String resourceURL) {
123 		if (resourceURL==null)
124 			return false;
125 		if (resourceURL.indexOf('//')>=0)
126 			resourceURL = resourceURL.replace('//','/');
127 		// Try file existence: can we find the file in the file system?
128 		if (resourceURL.startsWith(FILE_URL_PREFIX)) {
129 			String path = resourceURL.substring(FILE_URL_PREFIX.length());
130 			File file = new File(path);
131 			return file.exists();
132 		} else {
133 			// Fall back to stream existence: can we open the stream?
134 			try {
135 				InputStream is = getInputStream(clazz, resourceURL);
136 				is.close();
137 				return true;
138 			} catch (IOException ex2) {
139 				return false;
140 			}
141 		}
142 	}
143 }