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 import java.io.*;
00034
00035 import javax.servlet.http.HttpServletResponse;
00036
00037 import org.openmobileis.common.intl.IntlResourceManager;
00038 import org.openmobileis.common.util.PropertiesManager;
00039 import org.openmobileis.services.ServletTools;
00040 import org.openmobileis.common.util.log.*;
00041 import org.openmobileis.common.util.exception.ServerException;
00042
00058 public class TemplateManager implements CacheListener {
00059
00060
00061
00062 private static TemplateManager manager = null;
00063 private static String extension = "htm";
00064 private String rootTemplatePath;
00065
00066 private TemplateManager() {
00067 this.init();
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("templatesDir");
00091 rootTemplatePath = org.openmobileis.common.util.file.FileUtilities.convertFileNameToSystem(templatesDir);
00092 }
00093
00094
00095 public void cacheUnavailable(CacheEvent e) {
00096 System.out.println("Cache unavailable: " + e.getException().toString());
00097 }
00098
00099 public void elementUpdated(CacheEvent e) {
00100 System.out.println("Template updated: " + e.getElementName());
00101 }
00102
00103 public void elementUpdateFailed(CacheEvent e) {
00104 System.out.println("Update of template " + e.getElementName() +
00105 " failed: " + e.getException().toString());
00106 e.getException().printStackTrace();
00107 }
00108
00109 public void elementRemoved(CacheEvent e) {
00110 System.out.println("Template removed: " + e.getElementName());
00111 }
00112
00116 public TemplateModelRoot getTemplateModelRoot() {
00117 return new SimpleHash();
00118 }
00119
00127 public Template getTemplate(String relatifPath) {
00128
00129 try {
00130 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00131 String fileName = rootTemplatePath+relatifPath;
00132 fileName = resourceManager.getLocalizedFileName(fileName);
00133 File testfile = new File(fileName);
00134 if (testfile.exists())
00135 return new Template(fileName);
00136 } catch (IOException ex) {
00137 LogManager.trace(new ServerException("TemplateNotFound :"+rootTemplatePath+File.separator+relatifPath, ex));
00138 }
00139 return null;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 public void sendResponse (String templateName, TemplateModelRoot modelRoot, HttpServletResponse res) throws java.io.IOException, TemplateNotFoundException {
00154 if (templateName != null) {
00155 Template template = this.getTemplate(templateName);
00156 if (template != null) {
00157 res.setContentType("text/html" );
00158 PrintWriter print = new PrintWriter(res.getOutputStream());
00159 try {
00160 template.process(modelRoot, print);
00161 } finally {
00162 print.flush();
00163 }
00164 } else {
00165 LogManager.traceError(LogServices.WEBSERVICE, "Template non trouvé :"+templateName);
00166 ServletTools.sendErrorPage("Erreur Interne", "Fichier de visualisation manquant.","/index", res);
00167 }
00168 }
00169 }
00170
00171
00172 }
00173
00174
00175