1 17 package org.alfresco.repo.template; 18 19 import java.io.StringWriter ; 20 import java.io.Writer ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.alfresco.error.AlfrescoRuntimeException; 25 import org.alfresco.service.cmr.repository.TemplateException; 26 import org.alfresco.service.cmr.repository.TemplateProcessor; 27 import org.alfresco.service.cmr.repository.TemplateService; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.springframework.beans.BeansException; 31 import org.springframework.context.ApplicationContext; 32 import org.springframework.context.ApplicationContextAware; 33 34 37 public class TemplateServiceImpl implements TemplateService, ApplicationContextAware 38 { 39 private static Log logger = LogFactory.getLog(TemplateService.class); 40 41 42 private ApplicationContext applicationContext; 43 44 45 private String defaultTemplateEngine; 46 47 48 private Map <String , String > templateEngines; 49 50 51 private static ThreadLocal <Map <String , TemplateProcessor>> processors = new ThreadLocal (); 52 53 58 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 59 { 60 this.applicationContext = applicationContext; 61 } 62 63 66 public void setDefaultTemplateEngine(String defaultTemplateEngine) 67 { 68 this.defaultTemplateEngine = defaultTemplateEngine; 69 } 70 71 74 public void setTemplateEngines(Map <String , String > templateEngines) 75 { 76 this.templateEngines = templateEngines; 77 } 78 79 82 public TemplateProcessor getTemplateProcessor(String engine) 83 { 84 try 85 { 86 return getTemplateProcessorImpl(engine); 87 } 88 catch (Throwable err) 89 { 90 if (logger.isDebugEnabled()) 91 logger.debug("Unable to load template processor.", err); 92 93 return null; 94 } 95 } 96 97 100 public void processTemplate(String engine, String template, Object model, Writer out) 101 throws TemplateException 102 { 103 try 104 { 105 TemplateProcessor processor = getTemplateProcessorImpl(engine); 107 processor.process(template, model, out); 108 } 109 catch (TemplateException terr) 110 { 111 throw terr; 112 } 113 catch (Throwable err) 114 { 115 throw new TemplateException(err.getMessage(), err); 116 } 117 } 118 119 122 public String processTemplate(String engine, String template, Object model) 123 throws TemplateException 124 { 125 Writer out = new StringWriter (1024); 126 processTemplate(engine, template, model, out); 127 return out.toString(); 128 } 129 130 137 private TemplateProcessor getTemplateProcessorImpl(String name) 138 { 139 Map <String , TemplateProcessor> procMap = processors.get(); 142 if (procMap == null) 143 { 144 procMap = new HashMap <String , TemplateProcessor>(7, 1.0f); 145 processors.set(procMap); 146 } 147 148 if (name == null) 149 { 150 name = defaultTemplateEngine; 151 } 152 153 TemplateProcessor processor = procMap.get(name); 155 if (processor == null) 156 { 157 String className = templateEngines.get(name); 158 if (className == null) 159 { 160 throw new AlfrescoRuntimeException("Unable to find configured ClassName for template engine: " + name); 161 } 162 try 163 { 164 Object obj; 165 try 166 { 167 obj = this.applicationContext.getBean(className); 168 } 169 catch (BeansException err) 170 { 171 obj = Class.forName(className).newInstance(); 173 } 174 175 if (obj instanceof TemplateProcessor) 176 { 177 processor = (TemplateProcessor)obj; 178 } 179 else 180 { 181 throw new AlfrescoRuntimeException("Supplied template processors does not implement TemplateProcessor: " + className); 182 } 183 } 184 catch (ClassNotFoundException err1) 185 { 186 throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err1); 188 } 189 catch (IllegalAccessException err2) 190 { 191 throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err2); 192 } 193 catch (InstantiationException err3) 194 { 195 throw new AlfrescoRuntimeException("Unable to instantiate class for supplied template processors: " + className, err3); 196 } 197 198 procMap.put(name, processor); 200 } 201 202 return processor; 203 } 204 } 205 | Popular Tags |