View Javadoc

1   /*
2    * $Id: FreemarkerTemplateEngine.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components.template;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.struts2.ServletActionContext;
33  import org.apache.struts2.views.freemarker.FreemarkerManager;
34  
35  import com.opensymphony.xwork2.util.ClassLoaderUtil;
36  import com.opensymphony.xwork2.ActionContext;
37  import com.opensymphony.xwork2.ActionInvocation;
38  import com.opensymphony.xwork2.util.ValueStack;
39  
40  import freemarker.template.Configuration;
41  import freemarker.template.SimpleHash;
42  
43  /***
44   * Freemarker based template engine.
45   */
46  public class FreemarkerTemplateEngine extends BaseTemplateEngine {
47      static Class bodyContent = null;
48  
49      static {
50          try {
51              bodyContent = ClassLoaderUtil.loadClass("javax.servlet.jsp.tagext.BodyContent",
52                      FreemarkerTemplateEngine.class);
53          } catch (ClassNotFoundException e) {
54              // this is OK -- this just means JSP isn't even being used here, which is perfectly fine.
55              // we need this class in environments that use JSP to know when to wrap the writer
56              // and ignore flush() calls. In JSP, it is illegal for a BodyContent writer to be flushed(),
57              // so we have to take caution here.
58          }
59      }
60  
61      private static final Log LOG = LogFactory.getLog(FreemarkerTemplateEngine.class);
62  
63      public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
64          // get the various items required from the stack
65          ValueStack stack = templateContext.getStack();
66          Map context = stack.getContext();
67          ServletContext servletContext = (ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT);
68          HttpServletRequest req = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
69          HttpServletResponse res = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
70  
71          // prepare freemarker
72          FreemarkerManager freemarkerManager = FreemarkerManager.getInstance();
73          Configuration config = freemarkerManager.getConfiguration(servletContext);
74  
75          // get the list of templates we can use
76          List templates = templateContext.getTemplate().getPossibleTemplates(this);
77  
78          // find the right template
79          freemarker.template.Template template = null;
80          String templateName = null;
81          Exception exception = null;
82          for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
83              Template t = (Template) iterator.next();
84              templateName = getFinalTemplateName(t);
85              try {
86                  // try to load, and if it works, stop at the first one
87                  template = config.getTemplate(templateName);
88                  break;
89              } catch (IOException e) {
90                  if (exception == null) {
91                      exception = e;
92                  }
93              }
94          }
95  
96          if (template == null) {
97              LOG.error("Could not load template " + templateContext.getTemplate());
98              if (exception != null) {
99                  throw exception;
100             } else {
101                 return;
102             }
103         }
104 
105         if (LOG.isDebugEnabled()) {
106             LOG.debug("Rendering template " + templateName);
107         }
108 
109         ActionInvocation ai = ActionContext.getContext().getActionInvocation();
110 
111         Object action = (ai == null) ? null : ai.getAction();
112         SimpleHash model = freemarkerManager.buildTemplateModel(stack, action, servletContext, req, res, config.getObjectWrapper());
113 
114         model.put("tag", templateContext.getTag());
115         model.put("themeProperties", getThemeProps(templateContext.getTemplate()));
116 
117         // the BodyContent JSP writer doesn't like it when FM flushes automatically --
118         // so let's just not do it (it will be flushed eventually anyway)
119         Writer writer = templateContext.getWriter();
120         if (bodyContent != null && bodyContent.isAssignableFrom(writer.getClass())) {
121             final Writer wrapped = writer;
122             writer = new Writer() {
123                 public void write(char cbuf[], int off, int len) throws IOException {
124                     wrapped.write(cbuf, off, len);
125                 }
126 
127                 public void flush() throws IOException {
128                     // nothing!
129                 }
130 
131                 public void close() throws IOException {
132                     wrapped.close();
133                 }
134             };
135         }
136 
137         try {
138             stack.push(templateContext.getTag());
139             template.process(model, writer);
140         } finally {
141             stack.pop();
142         }
143     }
144 
145     protected String getSuffix() {
146         return "ftl";
147     }
148 }