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 private String defaultTemplateContentType = "text/html";
00060
00061 public Freemarkerv1TemplateDelegate() {
00062 templateRetreiverList = new Array();
00063 }
00064
00065
00066
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
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
00132
00133
00134
00135
00136
00137
00138
00139
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