View Javadoc

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