01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.util;
16:
17: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
18:
19: import java.net.URL;
20:
21: import org.apache.tapestry.ioc.Resource;
22:
23: /**
24: * Implementation of {@link Resource} for files on the classpath (as defined by a
25: * {@link ClassLoader}).
26: */
27: public final class ClasspathResource extends AbstractResource {
28: private final ClassLoader _classLoader;
29:
30: private URL _url;
31:
32: public ClasspathResource(String path) {
33: this (Thread.currentThread().getContextClassLoader(), path);
34: }
35:
36: public ClasspathResource(ClassLoader classLoader, String path) {
37: super (path);
38:
39: notNull(classLoader, "classLoader");
40:
41: _classLoader = classLoader;
42: }
43:
44: @Override
45: protected Resource newResource(String path) {
46: return new ClasspathResource(_classLoader, path);
47: }
48:
49: public synchronized URL toURL() {
50: if (_url == null)
51: _url = _classLoader.getResource(getPath());
52:
53: return _url;
54: }
55:
56: @Override
57: public boolean equals(Object obj) {
58: if (obj == null)
59: return false;
60:
61: if (obj == this )
62: return true;
63:
64: if (obj.getClass() != getClass())
65: return false;
66:
67: ClasspathResource other = (ClasspathResource) obj;
68:
69: return other._classLoader == _classLoader
70: && other.getPath().equals(getPath());
71: }
72:
73: @Override
74: public int hashCode() {
75: return 227 ^ getPath().hashCode();
76: }
77:
78: @Override
79: public String toString() {
80: return "classpath:" + getPath();
81: }
82:
83: }
|