1 44 package org.jpublish.template; 45 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.io.InputStreamReader ; 49 import java.io.Reader ; 50 51 import com.anthonyeden.lib.config.Configuration; 52 import com.anthonyeden.lib.config.ConfigurationException; 53 import com.anthonyeden.lib.util.ClassUtilities; 54 import com.anthonyeden.lib.util.IOUtilities; 55 import com.anthonyeden.lib.util.MessageUtilities; 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.apache.commons.vfs.FileContent; 59 import org.apache.commons.vfs.FileName; 60 import org.apache.commons.vfs.FileObject; 61 import org.apache.commons.vfs.FileSystemManager; 62 import org.jpublish.JPublishEngine; 63 import org.jpublish.JPublishRuntimeException; 64 import org.jpublish.RequestContext; 65 import org.jpublish.SiteContext; 66 import org.jpublish.cache.CacheEntry; 67 import org.jpublish.cache.CacheStorage; 68 import org.jpublish.cache.HashMapCacheStorage; 69 import org.jpublish.util.PathUtilities; 70 import org.jpublish.util.encoding.CharacterEncodingManager; 71 72 78 public class DefaultTemplateManager extends AbstractTemplateManager { 79 80 private final static String ATTRIBUTE_CLASSNAME = "classname"; 81 82 private Log log = LogFactory.getLog(DefaultTemplateManager.class); 83 private CacheStorage templateCache = null; 84 85 90 public synchronized CacheStorage getTemplateCache() { 91 if (templateCache == null) { 92 templateCache = new HashMapCacheStorage(); 93 } 94 return templateCache; 95 } 96 97 102 public void setTemplateCache(CacheStorage templateCache) { 103 this.templateCache = templateCache; 104 } 105 106 113 public synchronized FileSystemManager getFileSystemManager() 114 throws IOException { 115 if (fileSystemManager == null) { 116 configureDefaultFileSystemManager(SiteContext.DEFAULT_TEMPLATE_ROOT); 117 } 118 return fileSystemManager; 119 } 120 121 130 public synchronized Template getTemplate(String path) throws IOException , 131 TemplateNotFoundException { 132 if (log.isDebugEnabled()) { 133 log.debug("getTemplate(" + path + ")"); 134 } 135 136 FileSystemManager fileSystemManager = getFileSystemManager(); 137 FileObject baseFile = fileSystemManager.getBaseFile(); 138 FileObject templateFile = fileSystemManager.resolveFile(baseFile, 139 PathUtilities.toRelativePath(path)); 140 if (!templateFile.exists()) { 141 Object [] args = {path}; 142 throw new TemplateNotFoundException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, "templateNotFound", 143 args)); 144 } 145 146 FileContent templateContent = templateFile.getContent(); 147 long lastModified = templateContent.getLastModifiedTime(); 148 149 CacheStorage templateCache = getTemplateCache(); 151 CacheEntry cacheEntry = (CacheEntry) templateCache.get(path); 152 Template template = null; 153 154 if (cacheEntry == null) { 155 if (log.isDebugEnabled()) { 156 log.debug("Template (" + path + ") not found in cache."); 157 } 158 template = new Template(siteContext, path, getName()); 159 160 loadTemplate(template, templateFile); 162 163 configureTemplate(template, templateFile); 165 166 templateCache.put(path, new CacheEntry(template, lastModified)); 167 template.setLastModified(lastModified); 168 } else { 169 if (log.isDebugEnabled()) { 170 log.debug("Template (" + path + ") found in cache."); 171 } 172 template = (Template) cacheEntry.getObject(); 173 if (cacheEntry.getLastModified() != 174 templateContent.getLastModifiedTime()) { 175 log.debug("Template modification dates do not match."); 176 log.debug("Reloading template."); 177 178 loadTemplate(template, templateFile); 180 181 configureTemplate(template, templateFile); 183 184 cacheEntry.setLastModified(lastModified); 185 template.setLastModified(lastModified); 186 } 187 } 188 189 return template; 190 } 191 192 200 201 public long getLastModified(String path) throws IOException { 202 FileSystemManager fileSystemManager = getFileSystemManager(); 203 FileObject baseFile = fileSystemManager.getBaseFile(); 204 FileObject file = fileSystemManager.resolveFile(baseFile, 205 PathUtilities.toRelativePath(path)); 206 FileContent content = file.getContent(); 207 return content.getLastModifiedTime(); 208 } 209 210 216 217 public void loadConfiguration(Configuration configuration) 218 throws ConfigurationException { 219 super.loadConfiguration(configuration); 220 221 String className = null; 223 try { 224 Configuration cacheStorageElement = configuration.getChild("cache-storage"); 225 if (cacheStorageElement != null) { 226 className = cacheStorageElement.getAttribute(ATTRIBUTE_CLASSNAME, HashMapCacheStorage.class.getName()); 227 CacheStorage cache = (CacheStorage) ClassUtilities.loadClass(className).newInstance(); 228 cache.loadConfiguration(cacheStorageElement); 229 setTemplateCache(cache); 230 } 231 } catch (ClassNotFoundException e) { 232 Object [] args = {className}; 233 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 234 "cacheStorageClassNotFound", args), e, configuration); 235 } catch (Exception e) { 236 throw new ConfigurationException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 237 "cacheStorageConfigurationError", null), e, configuration); 238 } 239 } 240 241 252 253 protected synchronized void loadTemplate(Template template, FileObject file) 254 throws IOException { 255 257 RequestContext context = RequestContext.getRequestContext(); 258 String path = PathUtilities.getRealPath(siteContext, 259 context.getRequest().getPathInfo()); 260 CharacterEncodingManager encManager = 261 siteContext.getCharacterEncodingManager(); 262 String templateEncoding = 263 encManager.getMap(path).getTemplateEncoding(); 264 265 if (log.isDebugEnabled()) { 266 log.debug("Template encoding: " + templateEncoding); 267 } 268 269 FileContent content = file.getContent(); 270 271 Reader in = null; 272 try { 273 in = new InputStreamReader (content.getInputStream(), 274 templateEncoding); 275 StringBuffer buffer = new StringBuffer (); 276 int c = -1; 277 while ((c = in.read()) != -1) { 278 buffer.append((char) c); 279 } 280 281 template.setText(buffer.toString()); 282 283 } finally { 284 IOUtilities.close(in); 285 } 286 } 287 288 295 296 protected void configureTemplate(Template template, FileObject file) 297 throws IOException { 298 FileName name = file.getName(); 299 InputStream in = null; 300 try { 301 303 306 FileObject parentFile = file.getParent(); 307 String baseName = name.getBaseName(); 308 String namePart = baseName.substring(0, baseName.lastIndexOf(".")); 309 FileObject configFile = parentFile.resolveFile(namePart + 310 getActualConfigurationSuffix()); 311 if (configFile.exists()) { 312 in = configFile.getContent().getInputStream(); 313 template.loadConfiguration(in); 314 } 315 } catch (ConfigurationException e) { 316 Object [] args = {name.getPath(), e.getMessage()}; 317 throw new JPublishRuntimeException(MessageUtilities.getMessage(getClass(), JPublishEngine.MESSAGE_PACKAGE, 318 "templateConfigurationError", args), e); 319 } finally { 320 IOUtilities.close(in); 321 } 322 } 323 324 } 325 326 | Popular Tags |