You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/08/26 22:38:19 UTC

svn commit: r689212 - in /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template: DefaultTemplateEncoder.java DefaultTemplateLoader.java

Author: lu4242
Date: Tue Aug 26 13:38:19 2008
New Revision: 689212

URL: http://svn.apache.org/viewvc?rev=689212&view=rev
Log:
TOMAHAWK-1319 DefaultTemplateEncoder does not cache FreeMarker templates

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java   (with props)
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateEncoder.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateEncoder.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateEncoder.java?rev=689212&r1=689211&r2=689212&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateEncoder.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateEncoder.java Tue Aug 26 13:38:19 2008
@@ -27,7 +27,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import freemarker.cache.ClassTemplateLoader;
 import freemarker.cache.TemplateLoader;
 import freemarker.template.Configuration;
 import freemarker.template.DefaultObjectWrapper;
@@ -37,19 +36,24 @@
 /**
  * @author Martin Marinschek
  */
-public class DefaultTemplateEncoder implements TemplateEncoder {
-
+public class DefaultTemplateEncoder implements TemplateEncoder
+{
     private static final Log log = LogFactory.getLog(DefaultTemplateEncoder.class);
+    private static final String TEMPLATE_CACHE = "org.apache.myfaces.tomahawk.template.DefaultTemplateEncoder.CACHE";
     private static final String TEMPLATE_DIRECTORY = "template";
 
-    public void encodeTemplate(FacesContext context, UIComponent component, Renderer renderer, String template, Object dataModel) throws IOException {
-            Configuration cfg = new Configuration();
+    public void encodeTemplate(FacesContext context, UIComponent component, Renderer renderer, String template, Object dataModel)
+        throws IOException
+    {
         if(log.isDebugEnabled())
+        {
             log.debug("Encoding template : " + renderer.getClass().getResource(TEMPLATE_DIRECTORY+"/"+template));
-        TemplateLoader templateLoader = new ClassTemplateLoader(renderer.getClass(), TEMPLATE_DIRECTORY);
-        cfg.setTemplateLoader(templateLoader);
-        cfg.setObjectWrapper(new DefaultObjectWrapper());
-        Template temp = cfg.getTemplate(template);
+        }
+        Configuration cfg = getConfig(context, TEMPLATE_CACHE);
+        //Get the template using absolute path
+        Template temp = cfg.getTemplate('/'
+                +renderer.getClass().getPackage().getName().replace('.','/')
+                +'/'+TEMPLATE_DIRECTORY+'/'+template);
         try
         {
             temp.process(dataModel, context.getResponseWriter());
@@ -59,4 +63,32 @@
             throw new IOException(e.getMessage());
         }
     }
+    
+    /**
+     * Retrieve the current configuration or if no instance exists create a new one. 
+     * 
+     * @param context The current FacesContext
+     * @param cacheParamName the variable used to save and retrieve on application scope the current template configuration
+     * @return
+     */
+    protected Configuration getConfig(FacesContext context, String cacheParamName)
+    {
+        Configuration config = 
+            (Configuration) context.getExternalContext().getApplicationMap().get(cacheParamName);
+        if(config == null)
+        {
+            config = createConfig(context);
+            context.getExternalContext().getApplicationMap().put(cacheParamName, config);
+        }
+        return config;
+    }
+    
+    protected Configuration createConfig(FacesContext context)
+    {
+        Configuration config = new Configuration();
+        TemplateLoader templateLoader = new DefaultTemplateLoader();
+        config.setObjectWrapper(new DefaultObjectWrapper());
+        config.setTemplateLoader(templateLoader);
+        return config;
+    }
 }

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java?rev=689212&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java Tue Aug 26 13:38:19 2008
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.renderkit.template;
+
+import java.net.URL;
+
+import freemarker.cache.URLTemplateLoader;
+
+public class DefaultTemplateLoader extends URLTemplateLoader
+{
+
+    protected URL getURL(String s)
+    {
+        return getCurrentLoader(this).getResource(s);
+    }
+    
+    /**
+     * Gets the ClassLoader associated with the current thread.  Returns the class loader associated with
+     * the specified default object if no context loader is associated with the current thread.
+     *
+     * @param defaultObject The default object to use to determine the class loader (if none associated with current thread.)
+     * @return ClassLoader
+     */
+    protected static ClassLoader getCurrentLoader(Object defaultObject)
+    {
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        if(loader == null)
+        {
+            loader = defaultObject.getClass().getClassLoader();
+        }
+        return loader;
+    }
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/renderkit/template/DefaultTemplateLoader.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL