Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]
    1   // Copyright 2006, 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // 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.tapestry5.ioc.internal.util;
   16   
   17   import org.apache.tapestry5.ioc.Resource;
   18   import static org.apache.tapestry5.ioc.internal.util.Defense.notNull;
   19   
   20   import java.net.URL;
   21   
   22   /**
   23    * Implementation of {@link Resource} for files on the classpath (as defined by a {@link ClassLoader}).
   24    */
   25   public final class ClasspathResource extends AbstractResource
   26   {
   27       private final ClassLoader classLoader;
   28   
   29       // Guarded by this
   30       private URL url;
   31   
   32       // Guarded by this
   33       private boolean urlResolved;
   34   
   35       public ClasspathResource(String path)
   36       {
   37           this(Thread.currentThread().getContextClassLoader(), path);
   38       }
   39   
   40       public ClasspathResource(ClassLoader classLoader, String path)
   41       {
   42           super(path);
   43   
   44           notNull(classLoader, "classLoader");
   45   
   46           this.classLoader = classLoader;
   47       }
   48   
   49       @Override
   50       protected Resource newResource(String path)
   51       {
   52           return new ClasspathResource(classLoader, path);
   53       }
   54   
   55       public synchronized URL toURL()
   56       {
   57           if (!urlResolved)
   58           {
   59               url = classLoader.getResource(getPath());
   60               urlResolved = true;
   61           }
   62   
   63           return url;
   64       }
   65   
   66       @Override
   67       public boolean equals(Object obj)
   68       {
   69           if (obj == null) return false;
   70   
   71           if (obj == this) return true;
   72   
   73           if (obj.getClass() != getClass()) return false;
   74   
   75           ClasspathResource other = (ClasspathResource) obj;
   76   
   77           return other.classLoader == classLoader && other.getPath().equals(getPath());
   78       }
   79   
   80       @Override
   81       public int hashCode()
   82       {
   83           return 227 ^ getPath().hashCode();
   84       }
   85   
   86       @Override
   87       public String toString()
   88       {
   89           return "classpath:" + getPath();
   90       }
   91   
   92   }

Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.ioc.internal.util » [javadoc | source]