View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.freemarker;
35  
36  import info.magnolia.freemarker.models.MagnoliaModelFactory;
37  import info.magnolia.freemarker.models.MagnoliaObjectWrapper;
38  
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import javax.inject.Singleton;
45  
46  import freemarker.cache.ClassTemplateLoader;
47  import freemarker.cache.MultiTemplateLoader;
48  import freemarker.cache.TemplateLoader;
49  import freemarker.core.TemplateClassResolver;
50  import freemarker.template.ObjectWrapper;
51  import freemarker.template.TemplateExceptionHandler;
52  import freemarker.template.TemplateModel;
53  import freemarker.template.TemplateModelException;
54  
55  /**
56   * Observed bean holding Freemarker configuration. Not to be confused with
57   * Freemarker's own {@link freemarker.template.Configuration}. This only exposes the few
58   * properties that Magnolia allows to configure and is able to handle properly.
59   * It also provides a few additional methods used internally.
60   *
61   * @see info.magnolia.freemarker.FreemarkerHelper
62   * @see info.magnolia.freemarker.models.MagnoliaObjectWrapper
63   * @see info.magnolia.freemarker.models.MagnoliaModelFactory
64   */
65  @Singleton
66  public class FreemarkerConfig {
67  
68      /**
69       * The MagnoliaModelFactory implementations explicitly registered by modules.
70       */
71      private List<MagnoliaModelFactory> registeredModelFactories = new ArrayList<MagnoliaModelFactory>();
72      private List<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>();
73      private Map<String, TemplateModel> sharedVariables = new HashMap<String, TemplateModel>();
74      private TemplateExceptionHandler templateExceptionHandler = TemplateExceptionHandler.IGNORE_HANDLER;
75      private TemplateClassResolver templateClassResolver = TemplateClassResolver.SAFER_RESOLVER;
76  
77      private ObjectWrapper objectWrapper;
78      private TemplateLoader multiTL;
79  
80      public FreemarkerConfig() {
81      }
82  
83      public ObjectWrapper getObjectWrapper() {
84          if (objectWrapper == null) {
85              objectWrapper = newObjectWrapper();
86          }
87          return objectWrapper;
88      }
89  
90      protected ObjectWrapper newObjectWrapper() {
91          return new MagnoliaObjectWrapper(this);
92      }
93  
94      public TemplateLoader getTemplateLoader() {
95          // multiTL will be null every time the config is changed, since node2bean instantiates a new FreemarkerConfig
96          if (multiTL == null) {
97              // using getTemplateLoaders() instead of the variable to make sure we go to the proxied object
98              final List<TemplateLoader> loaders = getTemplateLoaders();
99              // Fall back to ClassTemplateLoader
100             if (loaders.isEmpty()) {
101                 multiTL = new ClassTemplateLoader(getClass(), "/");
102             } else {
103                 final TemplateLoader[] tl = loaders.toArray(new TemplateLoader[loaders.size()]);
104                 multiTL = new MultiTemplateLoader(tl);
105             }
106         }
107         return multiTL;
108     }
109 
110     public List<MagnoliaModelFactory> getModelFactories() {
111         return registeredModelFactories;
112     }
113 
114     public void setModelFactories(List<MagnoliaModelFactory> registeredModelFactories) {
115         this.registeredModelFactories.addAll(registeredModelFactories);
116     }
117 
118     public List<TemplateLoader> getTemplateLoaders() {
119         return templateLoaders;
120     }
121 
122     public void setTemplateLoaders(List<TemplateLoader> templateLoaders) {
123         this.templateLoaders.addAll(templateLoaders);
124     }
125 
126     public Map<String, TemplateModel> getSharedVariables() {
127         return sharedVariables;
128     }
129 
130     public void setSharedVariables(Map<String, Object> configuredSharedVariables) throws TemplateModelException {
131         for (Map.Entry<String, Object> sv : configuredSharedVariables.entrySet()) {
132             sharedVariables.put(sv.getKey(), getObjectWrapper().wrap(sv.getValue()));
133         }
134     }
135 
136     // for tests
137     void addSharedVariable(String name, Object value) throws TemplateModelException {
138         sharedVariables.put(name, getObjectWrapper().wrap(value));
139     }
140 
141     public TemplateExceptionHandler getTemplateExceptionHandler() {
142         return templateExceptionHandler;
143     }
144 
145     public void setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler) {
146         this.templateExceptionHandler = templateExceptionHandler;
147     }
148 
149     public TemplateClassResolver getTemplateClassResolver() {
150         return templateClassResolver;
151     }
152 
153     public void setTemplateClassResolver(TemplateClassResolver templateClassResolver) {
154         this.templateClassResolver = templateClassResolver;
155     }
156 }