01: /*
02: * $Id: ClassLoaderResourceStreamLocator.java,v 1.4 2005/08/24 20:42:59
03: * jdonnerstag Exp $ $Revision: 461190 $ $Date: 2006-06-28 08:35:51 +0200 (Wed, 28 Jun 2006) $
04: *
05: * ==============================================================================
06: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
07: * use this file except in compliance with the License. You may obtain a copy of
08: * the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations under
16: * the License.
17: */
18: package wicket.util.resource.locator;
19:
20: import java.net.URL;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: import wicket.util.resource.IResourceStream;
26: import wicket.util.resource.UrlResourceStream;
27:
28: /**
29: * IResourceStreamLocator implementation that locates resources using a class
30: * loader.
31: *
32: * @author Juergen Donnerstag
33: * @author Jonathan Locke
34: */
35: public final class ClassLoaderResourceStreamLocator extends
36: AbstractResourceStreamLocator {
37: /** Logging */
38: private static final Log log = LogFactory
39: .getLog(ClassLoaderResourceStreamLocator.class);
40:
41: /**
42: * Constructor
43: */
44: public ClassLoaderResourceStreamLocator() {
45: }
46:
47: /**
48: * @see wicket.util.resource.locator.AbstractResourceStreamLocator#locate(Class,
49: * java.lang.String)
50: */
51: public IResourceStream locate(final Class clazz, final String path) {
52: ClassLoader classLoader = null;
53: if (clazz != null) {
54: classLoader = clazz.getClassLoader();
55: }
56:
57: if (classLoader == null) {
58: // use context classloader when no specific classloader is set
59: // (package resources for instance)
60: classLoader = Thread.currentThread()
61: .getContextClassLoader();
62: }
63:
64: if (classLoader == null) {
65: // use Wicket classloader when no specific classloader is set
66: classLoader = getClass().getClassLoader();
67: }
68:
69: // Log attempt
70: if (log.isDebugEnabled()) {
71: log.debug("Attempting to locate resource '" + path
72: + "' using classloader " + classLoader);
73: }
74:
75: // Try loading path using classloader
76: final URL url = classLoader.getResource(path);
77: if (url != null) {
78: return new UrlResourceStream(url);
79: }
80: return null;
81: }
82: }
|