Java/Velocity/Resource Loader

Материал из Java эксперт
Перейти к: навигация, поиск

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.rumons.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;
  }
}





Resource Loader Demo

import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class ResourceLoaders {
  private static final String FILE_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader1.vm";
  private static final String JAR_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader2.vm";
  private static final String CLASSPATH_RESOURCE_LOADER_TEMPLATE = "./src/ResourceLoader3.vm";
  public static void main(String[] args) throws Exception {
    processTemplate(FILE_RESOURCE_LOADER_TEMPLATE);
    processTemplate(FILE_RESOURCE_LOADER_TEMPLATE);
    processTemplate(JAR_RESOURCE_LOADER_TEMPLATE);
    processTemplate(JAR_RESOURCE_LOADER_TEMPLATE);
    processTemplate(CLASSPATH_RESOURCE_LOADER_TEMPLATE);
    processTemplate(CLASSPATH_RESOURCE_LOADER_TEMPLATE);
  }
  private static void processTemplate(String templateName) throws Exception {
    Velocity.init();
    Template template = Velocity.getTemplate(templateName);
    VelocityContext context = new VelocityContext();
    Writer writer = new StringWriter();
    template.merge(context, writer);
    System.out.println(writer.toString());
  }
}
-------------------------------------------------------------------------------------
//File: ResourceLoader1.vm
ResourceLoader1 Template!
//File: ResourceLoader2.vm
ResourceLoader2 Template Modified!

//File: ResourceLoader3.vm
ResourceLoader3 Template 3