1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.io.ByteArrayInputStream ; 59 import java.io.InputStream ; 60 import java.util.Map ; 61 62 import org.apache.commons.collections.ExtendedProperties; 63 import org.apache.commons.collections.map.LRUMap; 64 import org.apache.velocity.Template; 65 import org.apache.velocity.exception.ParseErrorException; 66 import org.apache.velocity.exception.ResourceNotFoundException; 67 import org.apache.velocity.runtime.RuntimeServices; 68 import org.apache.velocity.runtime.resource.Resource; 69 import org.apache.velocity.runtime.resource.ResourceManager; 70 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 71 72 79 public class SQLTemplateResourceManager 81 extends ResourceLoader 82 implements ResourceManager { 83 84 protected Map templateCache; 85 86 public void initialize(RuntimeServices rs) throws Exception { 87 super.rsvc = rs; 88 this.templateCache = new LRUMap(100); 89 } 90 91 public void clearCache() { 92 templateCache.clear(); 93 } 94 95 98 public Resource getResource(String resourceName, int resourceType, String encoding) 99 throws ResourceNotFoundException, ParseErrorException, Exception { 100 101 synchronized (templateCache) { 102 Template resource = (Template) templateCache.get(resourceName); 103 104 if (resource == null) { 105 resource = new Template(); 106 resource.setRuntimeServices(rsvc); 107 resource.setResourceLoader(this); 108 resource.setName(resourceName); 109 resource.setEncoding(encoding); 110 resource.process(); 111 112 templateCache.put(resourceName, resource); 113 } 114 115 return resource; 116 } 117 } 118 119 public String getLoaderNameForResource(String resourceName) { 120 return getClass().getName(); 121 } 122 123 public long getLastModified(Resource resource) { 124 return -1; 125 } 126 127 public InputStream getResourceStream(String source) 128 throws ResourceNotFoundException { 129 return new ByteArrayInputStream (source.getBytes()); 130 } 131 132 public void init(ExtendedProperties configuration) { 133 134 } 135 136 public boolean isSourceModified(Resource resource) { 137 return false; 138 } 139 } 140 | Popular Tags |