Freemarkerv1TemplateDelegate.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.util.PropertiesManager;
00039 import org.openmobileis.common.util.collection.Array;
00040 import org.openmobileis.common.util.log.*;
00041 
00057 public class Freemarkerv1TemplateDelegate  implements TemplateManagerDelegate, TemplateRetrieverManagerService {
00058   private Array templateRetreiverList;
00059   private String defaultTemplateContentType = "text/html";
00060 
00061   public Freemarkerv1TemplateDelegate() {
00062         templateRetreiverList = new Array();
00063   }
00064 
00065  /*   public void setTemplateRootPath(String rootPath)  {
00066                         rootTemplatePath = rootPath;
00067     } */
00068 
00073   public void init() {
00074         String rootTemplatePath = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00075         File file = new File (rootTemplatePath);
00076         if (file.exists()) templateRetreiverList.add(new FileTemplateRetrieverService());
00077   }
00078 
00079   public void updateTemplateWithNavigationBarData(Object modelRoot, String data)        {
00080         ((TemplateModelRoot)modelRoot).put("navbarNOSY", new SimpleScalar(data));
00081   }
00082 
00083   // cache listener methods
00084   public void cacheUnavailable(CacheEvent e) {
00085     System.out.println("Cache unavailable: " + e.getException().toString());
00086   }
00087 
00088   public void elementUpdated(CacheEvent e) {
00089     System.out.println("Template updated: " + e.getElementName());
00090   }
00091 
00092   public void elementUpdateFailed(CacheEvent e) {
00093     System.out.println("Update of template " + e.getElementName() +
00094             " failed: " + e.getException().toString());
00095     e.getException().printStackTrace();
00096   }
00097 
00098   public void elementRemoved(CacheEvent e) {
00099     System.out.println("Template removed: " + e.getElementName());
00100   }
00101 
00105   public TemplateModelRoot getTemplateModelRoot()  {
00106     return new SimpleHash();
00107   }
00108 
00116   public Template getTemplate(String relatifPath) {
00117     TemplateRetrieverService templateService = null;
00118     Template template = null;
00119     int size = templateRetreiverList.size();
00120     for (int i=0; i<size; i++)  {
00121         templateService = (TemplateRetrieverService)templateRetreiverList.get(i);
00122         template = templateService.getServiceTemplate(relatifPath);
00123         if (template != null)   {
00124                 break;
00125         }
00126     }
00127     return template;
00128   }
00129 
00130 /*
00131   * Get the template called templateName (load it if it is not already done),
00132   * replace all variables tree of TemplateModelRoot in the template.
00133   * Write the HTTP response in the output stream with a basic header (mime type + status)
00134   * @param: String templateName the name of the template to use
00135   * @param: Hashtable variables (varName -> variable value) pairs
00136   * @param: HttpServletResponse response
00137   * @return: none
00138   * @OpenMSPException : IOEXception if an error occurs during sending data
00139   * @OpenMSPException : TemplateNotFoundException if the template is not found
00140   */
00141   public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00142     if (templateName != null) {
00143       Template template = this.getTemplate(templateName);
00144       if (template != null) {
00145         res.setContentType(this.getDefaultTemplateContentType());
00146         PrintWriter print = new PrintWriter(res.getOutputStream());
00147         try {
00148           template.process((TemplateModelRoot)modelRoot, print);
00149         } finally {
00150           print.flush();
00151         }
00152       } else  {
00153         LogManager.traceError(LogServices.WEBSERVICE, "Template not found :"+templateName);
00154         throw new TemplateNotFoundException("Template file not found :"+templateName);
00155       }
00156     }
00157   }
00158 
00159         public void registerTemplateRetriever(TemplateRetrieverService retriever)       {
00160                 templateRetreiverList.add(retriever);
00161         }
00162 
00163         public String getDefaultTemplateContentType() {
00164                 return defaultTemplateContentType;
00165         }
00166 
00167         public void setDefaultTemplateContentType(String defaultTemplateContentType) {
00168                 this.defaultTemplateContentType = defaultTemplateContentType;
00169         }
00170 
00171 }
00172 

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4