View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.reporting.web.template;
18  
19  import org.apache.commons.monitoring.reporting.web.plugin.PluginRepository;
20  import org.apache.velocity.Template;
21  import org.apache.velocity.VelocityContext;
22  import org.apache.velocity.app.Velocity;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  import org.apache.velocity.runtime.log.JdkLogChute;
25  import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
26  
27  import java.io.PrintWriter;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  public final class Templates {
32      private static String mapping;
33  
34      public static void init(final String context, final String filterMapping) {
35          final Properties velocityConfiguration = new Properties();
36          velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, JdkLogChute.class.getName());
37          velocityConfiguration.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
38          velocityConfiguration.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
39          velocityConfiguration.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
40          velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE.toString());
41          velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, Boolean.TRUE.toString());
42          velocityConfiguration.setProperty(RuntimeConstants.RESOURCE_LOADER, "monitoring");
43          velocityConfiguration.setProperty(RuntimeConstants.VM_LIBRARY, "/templates/macro.vm");
44          velocityConfiguration.setProperty("monitoring." + RuntimeConstants.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class.getName());
45          Velocity.init(velocityConfiguration);
46  
47          if (filterMapping.isEmpty()) {
48              mapping = context;
49          } else {
50              mapping = context + filterMapping;
51          }
52      }
53  
54      public static void htmlRender(final PrintWriter writer, final String template, final Map<String, ?> variables) {
55          final VelocityContext context = newVelocityContext(variables);
56          context.put("mapping", mapping);
57          context.put("currentTemplate", template);
58          context.put("plugins", PluginRepository.PLUGIN_INFO);
59          if (context.get("templateId") == null) {
60              context.put("templateId", template.replace(".vm", ""));
61          }
62  
63          final Template velocityTemplate = Velocity.getTemplate("/templates/page.vm", "UTF-8");
64          velocityTemplate.merge(context, writer);
65      }
66  
67      public static void render(final PrintWriter writer, final String template, final Map<String, ?> variables) {
68          final VelocityContext context = newVelocityContext(variables);
69          context.put("mapping", mapping);
70          final Template velocityTemplate = Velocity.getTemplate(template, "UTF-8");
71          velocityTemplate.merge(context, writer);
72      }
73  
74      private static VelocityContext newVelocityContext(final Map<String, ?> variables) {
75          final VelocityContext context;
76          if (variables.isEmpty()) {
77              context = new VelocityContext();
78          } else {
79              context = new VelocityContext(variables);
80          }
81          return context;
82      }
83  
84      private Templates() {
85          // no-op
86      }
87  }