1 5 8 package com.opensymphony.webwork.views.freemarker; 9 10 import com.opensymphony.util.FileManager; 11 import com.opensymphony.webwork.config.Configuration; 12 import com.opensymphony.webwork.views.JspSupportServlet; 13 import com.opensymphony.webwork.views.freemarker.tags.WebWorkModels; 14 import com.opensymphony.webwork.views.util.ContextUtil; 15 import com.opensymphony.xwork.ActionContext; 16 import com.opensymphony.xwork.ObjectFactory; 17 import com.opensymphony.xwork.util.OgnlValueStack; 18 import freemarker.cache.*; 19 import freemarker.ext.beans.BeansWrapper; 20 import freemarker.ext.jsp.TaglibFactory; 21 import freemarker.ext.servlet.HttpRequestHashModel; 22 import freemarker.ext.servlet.HttpSessionHashModel; 23 import freemarker.ext.servlet.ServletContextHashModel; 24 import freemarker.template.*; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import javax.servlet.GenericServlet ; 29 import javax.servlet.ServletContext ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 39 40 45 public class FreemarkerManager { 46 48 private static final Log log = LogFactory.getLog(FreemarkerManager.class); 49 public static final String CONFIG_SERVLET_CONTEXT_KEY = "freemarker.Configuration"; 50 public static final String KEY_EXCEPTION = "exception"; 51 52 private static final String ATTR_APPLICATION_MODEL = ".freemarker.Application"; 54 private static final String ATTR_JSP_TAGLIBS_MODEL = ".freemarker.JspTaglibs"; 55 private static final String ATTR_SESSION_MODEL = ".freemarker.Session"; 56 private static final String ATTR_REQUEST_MODEL = ".freemarker.Request"; 57 58 public static final String KEY_APPLICATION = "Application"; 60 public static final String KEY_REQUEST_MODEL = "Request"; 61 public static final String KEY_SESSION_MODEL = "Session"; 62 public static final String KEY_JSP_TAGLIBS = "JspTaglibs"; 63 private static FreemarkerManager instance = null; 64 65 67 75 public final static synchronized FreemarkerManager getInstance() { 76 if (instance == null) { 77 String classname = FreemarkerManager.class.getName(); 78 79 if (Configuration.isSet("webwork.freemarker.manager.classname")) { 80 classname = Configuration.getString("webwork.freemarker.manager.classname").trim(); 81 } 82 83 try { 84 log.info("Instantiating Freemarker ConfigManager!, " + classname); 85 instance = (FreemarkerManager) ObjectFactory.getObjectFactory().buildBean(Class.forName(classname)); 86 } catch (Exception e) { 87 log.fatal("Fatal exception occurred while trying to instantiate a Freemarker ConfigManager instance, " + classname, e); 88 } 89 } 90 91 if (instance == null) { 93 instance = new FreemarkerManager(); 94 } 95 96 return instance; 97 } 98 99 public final synchronized freemarker.template.Configuration getConfigruation(ServletContext servletContext) throws TemplateException { 100 freemarker.template.Configuration config = (freemarker.template.Configuration) servletContext.getAttribute(CONFIG_SERVLET_CONTEXT_KEY); 101 102 if (config == null) { 103 config = createConfiguration(servletContext); 104 105 servletContext.setAttribute(CONFIG_SERVLET_CONTEXT_KEY, config); 107 } 108 109 config.setWhitespaceStripping(true); 110 111 return config; 112 } 113 114 public ScopesHashModel buildScopesHashModel(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, ObjectWrapper wrapper, OgnlValueStack stack) { 115 ScopesHashModel model = new ScopesHashModel(wrapper, servletContext, request, stack); 116 117 synchronized (servletContext) { 120 ServletContextHashModel servletContextModel = (ServletContextHashModel) servletContext.getAttribute(ATTR_APPLICATION_MODEL); 121 122 if (servletContextModel == null) { 123 124 GenericServlet servlet = JspSupportServlet.jspSupportServlet; 125 if (servlet != null) { 128 servletContextModel = new ServletContextHashModel(servlet, wrapper); 129 servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel); 130 TaglibFactory taglibs = new TaglibFactory(servletContext); 131 servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs); 132 } 133 134 } 135 136 model.put(KEY_APPLICATION, servletContextModel); 137 model.put(KEY_JSP_TAGLIBS, (TemplateModel) servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL)); 138 } 139 140 TemplateHashModel sessionModel; 142 143 HttpSession session = request.getSession(false); 144 if (session != null) { 145 model.put(KEY_SESSION_MODEL, new HttpSessionHashModel(session, wrapper)); 146 } else { 147 } 150 151 HttpRequestHashModel requestModel = (HttpRequestHashModel) request.getAttribute(ATTR_REQUEST_MODEL); 153 154 if ((requestModel == null) || (requestModel.getRequest() != request)) { 155 requestModel = new HttpRequestHashModel(request, response, wrapper); 156 request.setAttribute(ATTR_REQUEST_MODEL, requestModel); 157 } 158 159 model.put(KEY_REQUEST_MODEL, requestModel); 160 161 return model; 162 } 163 164 167 public ScopesHashModel buildScopesHashModel(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, ObjectWrapper wrapper) { 168 return buildScopesHashModel(servletContext, request, response, wrapper, ActionContext.getContext().getValueStack()); 169 } 170 171 public void populateContext(ScopesHashModel model, OgnlValueStack stack, Object action, HttpServletRequest request, HttpServletResponse response) { 172 Map standard = ContextUtil.getStandardContext(stack, request, response); 174 model.putAll(standard); 175 176 Throwable exception = (Throwable ) request.getAttribute("javax.servlet.error.exception"); 178 179 if (exception == null) { 180 exception = (Throwable ) request.getAttribute("javax.servlet.error.JspException"); 181 } 182 183 if (exception != null) { 184 model.put(KEY_EXCEPTION, exception); 185 } 186 } 187 188 protected BeansWrapper getObjectWrapper() { 189 BeansWrapper beansWrapper = new BeansWrapper(); 190 beansWrapper.setSimpleMapWrapper(true); 191 192 return beansWrapper; 193 } 194 195 205 protected TemplateLoader getTemplateLoader(ServletContext servletContext) { 206 FileTemplateLoader templatePathLoader = null; 208 209 String templatePath = servletContext.getInitParameter("TemplatePath"); 210 if (templatePath == null) { 211 templatePath = servletContext.getInitParameter("templatePath"); 212 } 213 214 if (templatePath != null) { 215 try { 216 templatePathLoader = new FileTemplateLoader(new File (templatePath)); 217 } catch (IOException e) { 218 log.error("Invalid template path specified: " + e.getMessage(), e); 219 } 220 } 221 222 return templatePathLoader != null ? 225 new MultiTemplateLoader(new TemplateLoader[]{ 226 templatePathLoader, 227 new WebappTemplateLoader(servletContext), 228 new WebWorkClassTemplateLoader() 229 }) 230 : new MultiTemplateLoader(new TemplateLoader[]{ 231 new WebappTemplateLoader(servletContext), 232 new WebWorkClassTemplateLoader() 233 }); 234 } 235 236 249 protected freemarker.template.Configuration createConfiguration(ServletContext servletContext) throws TemplateException { 250 freemarker.template.Configuration configuration = freemarker.template.Configuration.getDefaultConfiguration(); 251 252 configuration.setTemplateLoader(getTemplateLoader(servletContext)); 253 254 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); 255 256 configuration.setObjectWrapper(getObjectWrapper()); 257 258 loadSettings(servletContext, configuration); 259 260 return configuration; 261 } 262 263 268 protected void loadSettings(ServletContext servletContext, freemarker.template.Configuration configuration) { 269 try { 270 InputStream in = FileManager.loadFile("freemarker.properties", FreemarkerManager.class); 271 272 if (in != null) { 273 Properties p = new Properties (); 274 p.load(in); 275 configuration.setSettings(p); 276 } 277 } catch (IOException e) { 278 log.error("Error while loading freemarker settings from /freemarker.properties", e); 279 } catch (TemplateException e) { 280 log.error("Error while loading freemarker settings from /freemarker.properties", e); 281 } 282 } 283 284 public SimpleHash buildTemplateModel(OgnlValueStack stack, Object action, ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, ObjectWrapper wrapper) { 285 ScopesHashModel model = buildScopesHashModel(servletContext, request, response, wrapper); 286 populateContext(model, stack, action, request, response); 287 model.put("ww", new WebWorkModels(stack, request, response)); 288 return model; 289 } 290 } 291 | Popular Tags |