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.intl.IntlResourceManager;
00039 import org.openmobileis.common.util.PropertiesManager;
00040 import org.openmobileis.services.common.ServletTools;
00041 import org.openmobileis.common.util.log.*;
00042 import org.openmobileis.common.util.exception.ServerException;
00043
00059 public class TemplateManager implements CacheListener {
00060
00061
00062
00063 private static TemplateManager manager = null;
00064 private String rootTemplatePath;
00065 FileTemplateCache templateCache = null;
00066
00067 private TemplateManager() {
00068 this.init();
00069
00070 }
00071
00072 public static TemplateManager getManager() {
00073 if (manager == null) {
00074 synchronized(TemplateManager.class) {
00075 if (manager == null) {
00076 manager = new TemplateManager();
00077 }
00078 }
00079 }
00080 return manager;
00081 }
00082
00083 public void setTemplateRootPath(String rootPath) {
00084 rootTemplatePath = rootPath;
00085 }
00086
00091 protected void init() {
00092 String templatesDir = PropertiesManager.getManager().getProperty("org.openmobileis.services.templatesDir");
00093 rootTemplatePath = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(templatesDir);
00094 templateCache = new FileTemplateCache(rootTemplatePath);
00095 templateCache.setFilenameSuffix("htm");
00096
00097 }
00098
00099
00100 public void cacheUnavailable(CacheEvent e) {
00101 System.out.println("Cache unavailable: " + e.getException().toString());
00102 }
00103
00104 public void elementUpdated(CacheEvent e) {
00105 System.out.println("Template updated: " + e.getElementName());
00106 }
00107
00108 public void elementUpdateFailed(CacheEvent e) {
00109 System.out.println("Update of template " + e.getElementName() +
00110 " failed: " + e.getException().toString());
00111 e.getException().printStackTrace();
00112 }
00113
00114 public void elementRemoved(CacheEvent e) {
00115 System.out.println("Template removed: " + e.getElementName());
00116 }
00117
00121 public TemplateModelRoot getTemplateModelRoot() {
00122 return new SimpleHash();
00123 }
00124
00132 public Template getTemplate(String relatifPath) {
00133
00134 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00135 relatifPath = resourceManager.getLocalizedFileName(relatifPath);
00136 return (Template)templateCache.getTemplate(relatifPath);
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00161 if (templateName != null) {
00162 Template template = this.getTemplate(templateName);
00163 if (template != null) {
00164 res.setContentType("text/html" );
00165 PrintWriter print = new PrintWriter(res.getOutputStream());
00166 try {
00167 template.process(modelRoot, print);
00168 } finally {
00169 print.flush();
00170 }
00171 } else {
00172 LogManager.traceError(LogServices.WEBSERVICE, "Template non trouvé :"+templateName);
00173 ServletTools.sendErrorPage("Erreur Interne", "Fichier de visualisation manquant.","/index", res);
00174 }
00175 }
00176 }
00177
00178
00179 }
00180
00181
00182