1 package org.roller.presentation.velocity; 2 3 import org.apache.commons.collections.ExtendedProperties; 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.apache.velocity.exception.ResourceNotFoundException; 7 import org.apache.velocity.runtime.Runtime; 8 import org.apache.velocity.runtime.resource.Resource; 9 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 10 import org.roller.util.LRUCache2; 11 12 import java.io.ByteArrayInputStream ; 13 import java.io.InputStream ; 14 15 30 public class PreviewResourceLoader extends ResourceLoader 31 { 32 private static Log mLogger = 33 LogFactory.getFactory().getInstance(PreviewResourceLoader.class); 34 35 private static String cacheName = "PreviewCache"; 36 37 42 private static int maxObjects = 50; 43 44 50 private static int time = 60 * 60 * 1000; 51 52 private static LRUCache2 mCache = new LRUCache2(maxObjects, time); 53 54 public void init( ExtendedProperties configuration) 55 { 56 } 57 58 public boolean isSourceModified(Resource resource) 59 { 60 return true; 61 } 62 63 public long getLastModified(Resource resource) 64 { 65 return 0; 66 } 67 68 75 public InputStream getResourceStream( String name ) 76 throws ResourceNotFoundException 77 { 78 if (name == null || name.length() == 0) 79 { 80 throw new ResourceNotFoundException( 81 "Need to specify a template name!"); 82 } 83 84 if (mLogger.isDebugEnabled()) 85 { 86 mLogger.debug("PreviewResourceLoader.getResourceStream(" + name + ")"); 87 } 88 89 try 90 { 91 String template = PreviewResourceLoader.getTemplate(name); 92 if (template != null && mLogger.isDebugEnabled()) 93 { 94 mLogger.debug("PreviewResourceLoader found resource " + name); 95 } 96 return new ByteArrayInputStream ( template.getBytes("UTF-8") ); 97 } 98 catch (NullPointerException npe) 99 { 100 throw new ResourceNotFoundException("Resource not found in PreviewResourceLoader"); 102 } 103 catch (Exception e) 104 { 105 String msg = "PreviewResourceLoader Error: " + 106 "problem trying to load resource " + name + ": " + e.getMessage(); 107 if (mLogger.isDebugEnabled()) 108 { 109 mLogger.debug( msg); 110 } 111 Runtime.error(msg ); 112 throw new ResourceNotFoundException (msg); 113 } 114 } 115 116 119 public static void setTemplate(String id, String template, String username) 120 { 121 if (mLogger.isDebugEnabled()) 122 { 123 mLogger.debug("PreviewResourceLoader.setTemplate(" 124 + id + ", template)"); 125 126 } 127 mCache.put(id, template); 128 } 129 130 133 public static String getTemplate(String id) 134 { 135 if (mLogger.isDebugEnabled()) 136 { 137 mLogger.debug("PreviewResourceLoader.getTemplate(" 138 + id + ")"); 139 } 140 return (String ) mCache.get( id ); 141 } 142 143 146 public static void clearTemplate(String id) 147 { 148 if (mLogger.isDebugEnabled()) 149 { 150 mLogger.debug("PreviewResourceLoader.clearTemplate(" + id + ")"); 151 } 152 mCache.purge(new String [] {id}); 153 } 154 155 159 public static void clearAllTemplates(String username) 160 { 161 mCache.purge(); 163 } 164 } | Popular Tags |