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

Generated on Mon Jan 14 17:29:46 2008 for OpenMobileIS by  doxygen 1.5.4