View Javadoc

1   /*
2    * $Id$
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  
22  package org.apache.struts2.osgi;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import javax.servlet.ServletContext;
28  
29  import org.apache.struts2.osgi.loaders.FreeMarkerBundleResourceLoader;
30  import org.apache.struts2.views.freemarker.FreemarkerManager;
31  import org.apache.struts2.views.freemarker.StrutsClassTemplateLoader;
32  
33  import com.opensymphony.xwork2.util.logging.Logger;
34  import com.opensymphony.xwork2.util.logging.LoggerFactory;
35  
36  import freemarker.cache.FileTemplateLoader;
37  import freemarker.cache.MultiTemplateLoader;
38  import freemarker.cache.TemplateLoader;
39  import freemarker.cache.WebappTemplateLoader;
40  
41  /***
42   * This class extends FreemarkerManager in core to add a template loader
43   * (that finds resources inside bundles) to MultiTemplateLoader
44   */
45  public class BundleFreemarkerManager extends FreemarkerManager {
46      private static final Logger LOG = LoggerFactory.getLogger(BundleFreemarkerManager.class);
47  
48      @Override
49      protected TemplateLoader getTemplateLoader(ServletContext servletContext) {
50          // construct a FileTemplateLoader for the init-param 'TemplatePath'
51          FileTemplateLoader templatePathLoader = null;
52  
53          String templatePath = servletContext.getInitParameter("TemplatePath");
54          if (templatePath == null) {
55              templatePath = servletContext.getInitParameter("templatePath");
56          }
57  
58          if (templatePath != null) {
59              try {
60                  templatePathLoader = new FileTemplateLoader(new File(templatePath));
61              } catch (IOException e) {
62                  if (LOG.isErrorEnabled())
63                      LOG.error("Invalid template path specified: [#0]", e, e.getMessage());
64              }
65          }
66  
67          // presume that most apps will require the class and webapp template loader
68          // if people wish to
69          return templatePathLoader != null ?
70                  new MultiTemplateLoader(new TemplateLoader[]{
71                          templatePathLoader,
72                          new WebappTemplateLoader(servletContext),
73                          new StrutsClassTemplateLoader(),
74                          new FreeMarkerBundleResourceLoader()
75                  })
76                  : new MultiTemplateLoader(new TemplateLoader[]{
77                  new WebappTemplateLoader(servletContext),
78                  new StrutsClassTemplateLoader(),
79                  new FreeMarkerBundleResourceLoader()
80          });
81      }
82  
83  }