00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00065
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
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
00131
00132
00133
00134
00135
00136
00137
00138
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