Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

TemplateManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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.services.common.ServletTools;
00041 import org.openmobileis.common.util.log.*;
00042 import org.openmobileis.common.util.exception.ServerException;
00043 
00059 public class TemplateManager  implements CacheListener {
00060 
00061  // private static FileTemplateCache templateCache;
00062 
00063   private static TemplateManager manager = null;
00064   private String rootTemplatePath;
00065   FileTemplateCache templateCache = null;
00066 
00067   private TemplateManager() {
00068     this.init();
00069     
00070   }
00071 
00072   public static TemplateManager getManager() {
00073     if (manager == null)  {
00074       synchronized(TemplateManager.class)  {
00075         if (manager == null)  {
00076           manager = new  TemplateManager();
00077         }
00078       }
00079     }
00080     return manager;
00081     }
00082     
00083     public void setTemplateRootPath(String rootPath)    {
00084                         rootTemplatePath = rootPath;
00085     }
00086 
00091   protected void init() {
00092     String templatesDir = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00093     rootTemplatePath = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(templatesDir);
00094     templateCache = new FileTemplateCache(rootTemplatePath);
00095     templateCache.setFilenameSuffix("htm");
00096     
00097   }
00098 
00099   // cache listener methods
00100   public void cacheUnavailable(CacheEvent e) {
00101     System.out.println("Cache unavailable: " + e.getException().toString());
00102   }
00103 
00104   public void elementUpdated(CacheEvent e) {
00105     System.out.println("Template updated: " + e.getElementName());
00106   }
00107 
00108   public void elementUpdateFailed(CacheEvent e) {
00109     System.out.println("Update of template " + e.getElementName() +
00110             " failed: " + e.getException().toString());
00111     e.getException().printStackTrace();
00112   }
00113 
00114   public void elementRemoved(CacheEvent e) {
00115     System.out.println("Template removed: " + e.getElementName());
00116   }
00117 
00121   public TemplateModelRoot getTemplateModelRoot()  {
00122     return new SimpleHash();
00123   }
00124 
00132   public Template getTemplate(String relatifPath) {
00133     //use template cache to allow template include.
00134     IntlResourceManager resourceManager = IntlResourceManager.getManager();
00135     relatifPath = resourceManager.getLocalizedFileName(relatifPath);
00136     return (Template)templateCache.getTemplate(relatifPath);
00137  /*   try {
00138        String fileName = rootTemplatePath+relatifPath;
00139       fileName = resourceManager.getLocalizedFileName(fileName);
00140       File testfile = new File(fileName);
00141       if (testfile.exists())  
00142         return new Template(fileName);
00143     } catch (Throwable ex)  {
00144        LogManager.trace(new ServerException("TemplateNotFound :"+rootTemplatePath+File.separator+relatifPath, ex));
00145     }
00146     return null; */
00147   }
00148 
00149 /*
00150   * Get the template called templateName (load it if it is not already done),
00151   * replace all variables tree of TemplateModelRoot in the template.
00152   * Write the HTTP response in the output stream with a basic header (mime type + status)
00153   * @param: String templateName the name of the template to use
00154   * @param: Hashtable variables (varName -> variable value) pairs
00155   * @param: HttpServletResponse response
00156   * @return: none
00157   * @OpenMSPException : IOEXception if an error occurs during sending data
00158   * @OpenMSPException : TemplateNotFoundException if the template is not found
00159   */
00160   public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00161     if (templateName != null) {
00162       Template template = this.getTemplate(templateName);
00163       if (template != null) {
00164         res.setContentType("text/html" );
00165         PrintWriter print = new PrintWriter(res.getOutputStream());
00166         try {
00167           template.process(modelRoot, print);
00168         } finally {
00169           print.flush();
00170         }
00171       } else  {
00172         LogManager.traceError(LogServices.WEBSERVICE, "Template non trouvé :"+templateName);
00173         ServletTools.sendErrorPage("Erreur Interne", "Fichier de visualisation manquant.","/index", res);
00174       }
00175     }
00176   }
00177 
00178 
00179 }
00180 
00181 
00182 

Generated on Wed Dec 14 21:05:35 2005 for OpenMobileIS by  doxygen 1.4.4