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

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 import java.io.*;
00034 
00035 import javax.servlet.http.HttpServletResponse;
00036 
00037 import org.openmobileis.common.intl.IntlResourceManager;
00038 import org.openmobileis.common.util.PropertiesManager;
00039 import org.openmobileis.services.ServletTools;
00040 import org.openmobileis.common.util.log.*;
00041 import org.openmobileis.common.util.exception.ServerException;
00042 
00058 public class TemplateManager  implements CacheListener {
00059 
00060  // private static FileTemplateCache templateCache;
00061 
00062   private static TemplateManager manager = null;
00063   private static String extension = "htm";
00064   private String rootTemplatePath;
00065 
00066   private TemplateManager() {
00067     this.init();
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("templatesDir");
00091     rootTemplatePath = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(templatesDir);
00092   }
00093 
00094   // cache listener methods
00095   public void cacheUnavailable(CacheEvent e) {
00096     System.out.println("Cache unavailable: " + e.getException().toString());
00097   }
00098 
00099   public void elementUpdated(CacheEvent e) {
00100     System.out.println("Template updated: " + e.getElementName());
00101   }
00102 
00103   public void elementUpdateFailed(CacheEvent e) {
00104     System.out.println("Update of template " + e.getElementName() +
00105             " failed: " + e.getException().toString());
00106     e.getException().printStackTrace();
00107   }
00108 
00109   public void elementRemoved(CacheEvent e) {
00110     System.out.println("Template removed: " + e.getElementName());
00111   }
00112 
00116   public TemplateModelRoot getTemplateModelRoot()  {
00117     return new SimpleHash();
00118   }
00119 
00127   public Template getTemplate(String relatifPath) {
00128 //    return templateCache.getTemplate(relatifPath);
00129     try {
00130       IntlResourceManager resourceManager = IntlResourceManager.getManager();
00131       String fileName = rootTemplatePath+relatifPath;
00132       fileName = resourceManager.getLocalizedFileName(fileName);
00133       File testfile = new File(fileName);
00134       if (testfile.exists())  
00135       return new Template(fileName);
00136     } catch (IOException ex)  {
00137        LogManager.trace(new ServerException("TemplateNotFound :"+rootTemplatePath+File.separator+relatifPath, ex));
00138     }
00139     return null;
00140   }
00141 
00142 /*
00143   * Get the template called templateName (load it if it is not already done),
00144   * replace all variables tree of TemplateModelRoot in the template.
00145   * Write the HTTP response in the output stream with a basic header (mime type + status)
00146   * @param: String templateName the name of the template to use
00147   * @param: Hashtable variables (varName -> variable value) pairs
00148   * @param: HttpServletResponse response
00149   * @return: none
00150   * @OpenMSPException : IOEXception if an error occurs during sending data
00151   * @OpenMSPException : TemplateNotFoundException if the template is not found
00152   */
00153   public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00154     if (templateName != null) {
00155       Template template = this.getTemplate(templateName);
00156       if (template != null) {
00157         res.setContentType("text/html" );
00158         PrintWriter print = new PrintWriter(res.getOutputStream());
00159         try {
00160           template.process(modelRoot, print);
00161         } finally {
00162           print.flush();
00163         }
00164       } else  {
00165         LogManager.traceError(LogServices.WEBSERVICE, "Template non trouvé :"+templateName);
00166         ServletTools.sendErrorPage("Erreur Interne", "Fichier de visualisation manquant.","/index", res);
00167       }
00168     }
00169   }
00170 
00171 
00172 }
00173 
00174 
00175 

Generated on Thu Oct 6 10:06:34 2005 for OpenMobileIS by  doxygen 1.4.3