TemplateManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.embedded.webserver.templates;
00031 
00032 import freemarker.template.*;
00033 
00034 import java.io.*;
00035 
00036 import javax.servlet.http.HttpServletResponse;
00037 
00038 import org.openmobileis.common.intl.IntlResourceManager;
00039 import org.openmobileis.common.util.PropertiesManager;
00040 import org.openmobileis.common.util.log.*;
00041 
00057 public class TemplateManager  implements CacheListener {
00058 
00059  // private static FileTemplateCache templateCache;
00060 
00061   private static TemplateManager manager = null;
00062   private String rootTemplatePath;
00063   FileTemplateCache templateCache = null;
00064 
00065   private TemplateManager() {
00066     this.init();
00067     
00068   }
00069 
00070   public static TemplateManager getManager() {
00071     if (manager == null)  {
00072       synchronized(TemplateManager.class)  {
00073         if (manager == null)  {
00074           manager = new  TemplateManager();
00075         }
00076       }
00077     }
00078     return manager;
00079     }
00080     
00081     public void setTemplateRootPath(String rootPath)    {
00082                         rootTemplatePath = rootPath;
00083     }
00084 
00089   protected void init() {
00090     String templatesDir = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00091     rootTemplatePath = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(templatesDir);
00092     templateCache = new FileTemplateCache(rootTemplatePath);
00093     templateCache.setFilenameSuffix("htm");
00094     
00095   }
00096 
00097   // cache listener methods
00098   public void cacheUnavailable(CacheEvent e) {
00099     System.out.println("Cache unavailable: " + e.getException().toString());
00100   }
00101 
00102   public void elementUpdated(CacheEvent e) {
00103     System.out.println("Template updated: " + e.getElementName());
00104   }
00105 
00106   public void elementUpdateFailed(CacheEvent e) {
00107     System.out.println("Update of template " + e.getElementName() +
00108             " failed: " + e.getException().toString());
00109     e.getException().printStackTrace();
00110   }
00111 
00112   public void elementRemoved(CacheEvent e) {
00113     System.out.println("Template removed: " + e.getElementName());
00114   }
00115 
00119   public TemplateModelRoot getTemplateModelRoot()  {
00120     return new SimpleHash();
00121   }
00122 
00130   public Template getTemplate(String relatifPath) {
00131     //use template cache to allow template include.
00132     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00133     String newrelatifPath = resourceManager.getLocalizedFileName(relatifPath, false);
00134     //test if the file exist. If not use not localized version
00135     File file = new File(rootTemplatePath+File.separator+newrelatifPath);
00136     if (!file.exists()) newrelatifPath = relatifPath;
00137     return (Template)templateCache.getTemplate(newrelatifPath);
00138   }
00139 
00140 /*
00141   * Get the template called templateName (load it if it is not already done),
00142   * replace all variables tree of TemplateModelRoot in the template.
00143   * Write the HTTP response in the output stream with a basic header (mime type + status)
00144   * @param: String templateName the name of the template to use
00145   * @param: Hashtable variables (varName -> variable value) pairs
00146   * @param: HttpServletResponse response
00147   * @return: none
00148   * @OpenMSPException : IOEXception if an error occurs during sending data
00149   * @OpenMSPException : TemplateNotFoundException if the template is not found
00150   */
00151   public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00152     if (templateName != null) {
00153       Template template = this.getTemplate(templateName);
00154       if (template != null) {
00155         res.setContentType("text/html" );
00156         PrintWriter print = new PrintWriter(res.getOutputStream());
00157         try {
00158           template.process(modelRoot, print);
00159         } finally {
00160           print.flush();
00161         }
00162       } else  {
00163         LogManager.traceError(LogServices.WEBSERVICE, "Template non trouvé :"+templateName);
00164         throw new TemplateNotFoundException("Template file not found :"+templateName);
00165       }
00166     }
00167   }
00168 
00169 
00170 }
00171 
00172 
00173 

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1