Source Code : Caching Class path Resource Loader

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Caching Class path Resource Loader


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

public class CachingClasspathResourceLoader extends ResourceLoader {
  private Map urlMap = new HashMap();

  public void init(ExtendedProperties configuration) {
  }

  public synchronized InputStream getResourceStream(String resourceName)
      throws ResourceNotFoundException {
    try {
      URL url = getURL(resourceName);

      if (url == null) {
        throw new ResourceNotFoundException("Can not find resource: "
            + resourceName);
      }
      return url.openStream();
    } catch (IOException e) {
      throw new ResourceNotFoundException("Can not find resource: "
          + resourceName + " - Reason: " + e.getMessage());
    }
  }

  public long getLastModified(Resource res) {
    try {
      URL url = getURL(res.getName());
      long lm = url.openConnection().getLastModified();
      return lm;
    } catch (Exception e) {
      rsvc.error(e);
      return 0;
    }
  }

  public boolean isSourceModified(Resource res) {
    long lastModified = getLastModified(res);
    return (lastModified != res.getLastModified());
  }

  private URL getURL(String rn) {
    if (urlMap.containsKey(rn)) {
      return (URL) urlMap.get(rn);
    }

    ClassLoader cl = this.getClass().getClassLoader();
    URL url = cl.getResource(rn);

    if (url != null) {
      urlMap.put(rn, url);
    }

    return url;
  }
}
           
       

Thank with us