This project has retired. For details please refer to its Attic page.
FreemarkerRendererBuilder xref
View Javadoc

1   /*
2    * $Id: FreemarkerRendererBuilder.java 1306435 2012-03-28 15:39:11Z nlebas $
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.tiles.request.freemarker.render;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.servlet.ServletException;
27  
28  import org.apache.tiles.request.ApplicationContext;
29  import org.apache.tiles.request.freemarker.FreemarkerRequestException;
30  
31  /**
32   * Builds instances of {@link FreemarkerRenderer}.
33   *
34   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
35   */
36  public final class FreemarkerRendererBuilder {
37  
38      /**
39       * The initialization parameters.
40       */
41      private Map<String, String> params = new HashMap<String, String>();
42  
43      /**
44       * The application context.
45       */
46      private ApplicationContext applicationContext;
47  
48      /**
49       * Constructor.
50       */
51      private FreemarkerRendererBuilder() {
52      }
53  
54      /**
55       * Creates a new instance of this class.
56       *
57       * @return A new instance of the builder.
58       */
59      public static FreemarkerRendererBuilder createInstance() {
60          return new FreemarkerRendererBuilder();
61      }
62  
63      /**
64       * Sets a parameter for the internal servlet.
65       *
66       * @param key The name of the parameter.
67       * @param value The value of the parameter.
68       * @return This object.
69       */
70      public FreemarkerRendererBuilder setParameter(String key, String value) {
71          params.put(key, value);
72          return this;
73      }
74  
75      /**
76       * Sets the application context.
77       *
78       * @param applicationContext The application context.
79       * @return This object.
80       */
81      public FreemarkerRendererBuilder setApplicationContext(ApplicationContext applicationContext) {
82          this.applicationContext = applicationContext;
83          return this;
84      }
85  
86      /**
87       * Creates a new {@link FreemarkerRenderer} with the given configuration.
88       *
89       * @return A new Freemarker renderer.
90       */
91      public FreemarkerRenderer build() {
92          AttributeValueFreemarkerServlet servlet = new AttributeValueFreemarkerServlet();
93          try {
94              servlet.init(new InitParamsServletConfig(params, applicationContext));
95              return new FreemarkerRenderer(servlet);
96          } catch (ServletException e) {
97              throw new FreemarkerRequestException(
98                      "Cannot initialize internal servlet", e);
99          }
100 
101     }
102 
103 }