1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.InputStream ; 24 import java.util.Map ; 25 26 import org.apache.commons.collections.ExtendedProperties; 27 import org.apache.commons.collections.map.LRUMap; 28 import org.apache.velocity.Template; 29 import org.apache.velocity.exception.ParseErrorException; 30 import org.apache.velocity.exception.ResourceNotFoundException; 31 import org.apache.velocity.runtime.RuntimeServices; 32 import org.apache.velocity.runtime.resource.Resource; 33 import org.apache.velocity.runtime.resource.ResourceManager; 34 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 35 36 43 public class SQLTemplateResourceManager 45 extends ResourceLoader 46 implements ResourceManager { 47 48 protected Map templateCache; 49 50 public void initialize(RuntimeServices rs) throws Exception { 51 super.rsvc = rs; 52 this.templateCache = new LRUMap(100); 53 } 54 55 public void clearCache() { 56 templateCache.clear(); 57 } 58 59 62 public Resource getResource(String resourceName, int resourceType, String encoding) 63 throws ResourceNotFoundException, ParseErrorException, Exception { 64 65 synchronized (templateCache) { 66 Template resource = (Template) templateCache.get(resourceName); 67 68 if (resource == null) { 69 resource = new Template(); 70 resource.setRuntimeServices(rsvc); 71 resource.setResourceLoader(this); 72 resource.setName(resourceName); 73 resource.setEncoding(encoding); 74 resource.process(); 75 76 templateCache.put(resourceName, resource); 77 } 78 79 return resource; 80 } 81 } 82 83 public String getLoaderNameForResource(String resourceName) { 84 return getClass().getName(); 85 } 86 87 public long getLastModified(Resource resource) { 88 return -1; 89 } 90 91 public InputStream getResourceStream(String source) 92 throws ResourceNotFoundException { 93 return new ByteArrayInputStream (source.getBytes()); 94 } 95 96 public void init(ExtendedProperties configuration) { 97 98 } 99 100 public boolean isSourceModified(Resource resource) { 101 return false; 102 } 103 } 104 | Popular Tags |