001 // Copyright 2006, 2007, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc.internal.util;
016
017 import java.net.URL;
018
019 import org.apache.tapestry5.ioc.Resource;
020
021 /**
022 * Implementation of {@link Resource} for files on the classpath (as defined by a {@link ClassLoader}).
023 */
024 public final class ClasspathResource extends AbstractResource
025 {
026 private final ClassLoader classLoader;
027
028 // Guarded by this
029 private URL url;
030
031 // Guarded by this
032 private boolean urlResolved;
033
034 public ClasspathResource(String path)
035 {
036 this(Thread.currentThread().getContextClassLoader(), path);
037 }
038
039 public ClasspathResource(ClassLoader classLoader, String path)
040 {
041 super(path);
042 assert classLoader != null;
043
044 this.classLoader = classLoader;
045 }
046
047 @Override
048 protected Resource newResource(String path)
049 {
050 return new ClasspathResource(classLoader, path);
051 }
052
053 public synchronized URL toURL()
054 {
055 if (!urlResolved)
056 {
057 url = classLoader.getResource(getPath());
058 urlResolved = true;
059 }
060
061 return url;
062 }
063
064 @Override
065 public boolean equals(Object obj)
066 {
067 if (obj == null) return false;
068
069 if (obj == this) return true;
070
071 if (obj.getClass() != getClass()) return false;
072
073 ClasspathResource other = (ClasspathResource) obj;
074
075 return other.classLoader == classLoader && other.getPath().equals(getPath());
076 }
077
078 @Override
079 public int hashCode()
080 {
081 return 227 ^ getPath().hashCode();
082 }
083
084 @Override
085 public String toString()
086 {
087 return "classpath:" + getPath();
088 }
089
090 }