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