You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2017/03/01 14:21:12 UTC

[14/50] [abbrv] incubator-freemarker git commit: Some minor code cleanup in the TemplateLoader implementations.

Some minor code cleanup in the TemplateLoader implementations.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/b1e28b0f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/b1e28b0f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/b1e28b0f

Branch: refs/heads/2.3
Commit: b1e28b0fee57d99206fb3ed363c634b92a162a0d
Parents: 6837e4c
Author: ddekany <dd...@apache.org>
Authored: Wed Feb 8 02:00:51 2017 +0100
Committer: ddekany <dd...@apache.org>
Committed: Wed Feb 8 02:00:51 2017 +0100

----------------------------------------------------------------------
 .../cache/ByteArrayTemplateLoader.java          | 20 +++---
 .../freemarker/cache/ClassTemplateLoader.java   | 12 ++--
 .../freemarker/cache/MultiTemplateLoader.java   | 67 +++++++++++---------
 .../freemarker/cache/StringTemplateLoader.java  | 22 +++----
 .../freemarker/cache/URLTemplateSource.java     |  3 +-
 .../freemarker/cache/WebappTemplateLoader.java  |  9 +--
 6 files changed, 68 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/ByteArrayTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/ByteArrayTemplateLoader.java b/src/main/java/freemarker/cache/ByteArrayTemplateLoader.java
index 84251fb..950b7c6 100644
--- a/src/main/java/freemarker/cache/ByteArrayTemplateLoader.java
+++ b/src/main/java/freemarker/cache/ByteArrayTemplateLoader.java
@@ -41,16 +41,16 @@ public class ByteArrayTemplateLoader implements TemplateLoader {
     /**
      * Adds a template to this template loader; see {@link StringTemplateLoader#putTemplate(String, String)} for more.
      */
-    public void putTemplate(String name, byte[] templateSource) {
-        putTemplate(name, templateSource, System.currentTimeMillis());
+    public void putTemplate(String name, byte[] templateContent) {
+        putTemplate(name, templateContent, System.currentTimeMillis());
     }
     
     /**
      * Adds a template to this template loader; see {@link StringTemplateLoader#putTemplate(String, String, long)} for
      * more.
      */
-    public void putTemplate(String name, byte[] templateSource, long lastModified) {
-        templates.put(name, new ByteArrayTemplateSource(name, templateSource, lastModified));
+    public void putTemplate(String name, byte[] templateContent, long lastModified) {
+        templates.put(name, new ByteArrayTemplateSource(name, templateContent, lastModified));
     }
     
     /**
@@ -76,27 +76,27 @@ public class ByteArrayTemplateLoader implements TemplateLoader {
     
     public Reader getReader(Object templateSource, String encoding) throws UnsupportedEncodingException {
         return new InputStreamReader(
-                new ByteArrayInputStream(((ByteArrayTemplateSource) templateSource).source),
+                new ByteArrayInputStream(((ByteArrayTemplateSource) templateSource).templateContent),
                 encoding);
     }
     
     private static class ByteArrayTemplateSource {
         private final String name;
-        private final byte[] source;
+        private final byte[] templateContent;
         private final long lastModified;
         
-        ByteArrayTemplateSource(String name, byte[] source, long lastModified) {
+        ByteArrayTemplateSource(String name, byte[] templateContent, long lastModified) {
             if (name == null) {
                 throw new IllegalArgumentException("name == null");
             }
-            if (source == null) {
-                throw new IllegalArgumentException("source == null");
+            if (templateContent == null) {
+                throw new IllegalArgumentException("templateContent == null");
             }
             if (lastModified < -1L) {
                 throw new IllegalArgumentException("lastModified < -1L");
             }
             this.name = name;
-            this.source = source;
+            this.templateContent = templateContent;
             this.lastModified = lastModified;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/ClassTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/ClassTemplateLoader.java b/src/main/java/freemarker/cache/ClassTemplateLoader.java
index 1bfbec8..9860355 100644
--- a/src/main/java/freemarker/cache/ClassTemplateLoader.java
+++ b/src/main/java/freemarker/cache/ClassTemplateLoader.java
@@ -31,7 +31,7 @@ import freemarker.template.utility.StringUtil;
  */
 public class ClassTemplateLoader extends URLTemplateLoader {
     
-    private final Class resourceLoaderClass;
+    private final Class<?> resourceLoaderClass;
     private final ClassLoader classLoader;
     private final String basePackagePath;
 
@@ -66,7 +66,7 @@ public class ClassTemplateLoader extends URLTemplateLoader {
      *             instead.
      */
     @Deprecated
-    public ClassTemplateLoader(Class resourceLoaderClass) {
+    public ClassTemplateLoader(Class<?> resourceLoaderClass) {
         this(resourceLoaderClass, "");
     }
 
@@ -96,7 +96,7 @@ public class ClassTemplateLoader extends URLTemplateLoader {
      * 
      * @see #ClassTemplateLoader(ClassLoader, String)
      */
-    public ClassTemplateLoader(Class resourceLoaderClass, String basePackagePath) {
+    public ClassTemplateLoader(Class<?> resourceLoaderClass, String basePackagePath) {
         this(resourceLoaderClass, false, null, basePackagePath);
     }
 
@@ -112,9 +112,9 @@ public class ClassTemplateLoader extends URLTemplateLoader {
         this(null, true, classLoader, basePackagePath);
     }
 
-    private ClassTemplateLoader(Class resourceLoaderClass, boolean allowNullBaseClass, ClassLoader classLoader,
-            String basePackagePath) {
-        if (!allowNullBaseClass) {
+    private ClassTemplateLoader(Class<?> resourceLoaderClass, boolean allowNullResourceLoaderClass,
+            ClassLoader classLoader, String basePackagePath) {
+        if (!allowNullResourceLoaderClass) {
             NullArgumentException.check("resourceLoaderClass", resourceLoaderClass);
         }
         NullArgumentException.check("basePackagePath", basePackagePath);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/MultiTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/MultiTemplateLoader.java b/src/main/java/freemarker/cache/MultiTemplateLoader.java
index 5457a88..bb8322d 100644
--- a/src/main/java/freemarker/cache/MultiTemplateLoader.java
+++ b/src/main/java/freemarker/cache/MultiTemplateLoader.java
@@ -24,6 +24,8 @@ import java.io.Reader;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import freemarker.template.utility.NullArgumentException;
+
 /**
  * A {@link TemplateLoader} that uses a set of other loaders to load the templates. On every request, loaders are
  * queried in the order of their appearance in the array of loaders provided to the constructor. However, by default, if
@@ -35,32 +37,35 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class MultiTemplateLoader implements StatefulTemplateLoader {
 
-    private final TemplateLoader[] loaders;
-    private final Map<String, TemplateLoader> lastLoaderForName = new ConcurrentHashMap<String, TemplateLoader>();
+    private final TemplateLoader[] templateLoaders;
+    private final Map<String, TemplateLoader> lastTemplateLoaderForName
+            = new ConcurrentHashMap<String, TemplateLoader>();
     
     private boolean sticky = true;
 
     /**
-     * Creates a new multi template Loader that will use the specified loaders.
+     * Creates a new instance that will use the specified template loaders.
      * 
-     * @param loaders
-     *            the loaders that are used to load templates.
+     * @param templateLoaders
+     *            the template loaders that are used to load templates, in the order as they will be searched
+     *            (except where {@linkplain #setSticky(boolean) stickiness} says otherwise).
      */
-    public MultiTemplateLoader(TemplateLoader[] loaders) {
-        this.loaders = loaders.clone();
+    public MultiTemplateLoader(TemplateLoader[] templateLoaders) {
+        NullArgumentException.check("templateLoaders", templateLoaders);
+        this.templateLoaders = templateLoaders.clone();
     }
 
     public Object findTemplateSource(String name)
             throws IOException {
-        TemplateLoader lastLoader = null;
+        TemplateLoader lastTemplateLoader = null;
         if (sticky) {
             // Use soft affinity - give the loader that last found this
             // resource a chance to find it again first.
-            lastLoader = lastLoaderForName.get(name);
-            if (lastLoader != null) {
-                Object source = lastLoader.findTemplateSource(name);
+            lastTemplateLoader = lastTemplateLoaderForName.get(name);
+            if (lastTemplateLoader != null) {
+                Object source = lastTemplateLoader.findTemplateSource(name);
                 if (source != null) {
-                    return new MultiSource(source, lastLoader);
+                    return new MultiSource(source, lastTemplateLoader);
                 }
             }
         }
@@ -69,30 +74,25 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
         // again, try all loaders in order of appearance. If any manages
         // to find the resource, then associate it as the new affine loader
         // for this resource.
-        for (TemplateLoader loader : loaders) {
-            if (lastLoader != loader) {
-                Object source = loader.findTemplateSource(name);
+        for (TemplateLoader templateLoader : templateLoaders) {
+            if (lastTemplateLoader != templateLoader) {
+                Object source = templateLoader.findTemplateSource(name);
                 if (source != null) {
                     if (sticky) {
-                        lastLoaderForName.put(name, loader);
+                        lastTemplateLoaderForName.put(name, templateLoader);
                     }
-                    return new MultiSource(source, loader);
+                    return new MultiSource(source, templateLoader);
                 }
             }
         }
 
         if (sticky) {
-            lastLoaderForName.remove(name);
+            lastTemplateLoaderForName.remove(name);
         }
         // Resource not found
         return null;
     }
 
-    private Object modifyForIcI(Object source) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     public long getLastModified(Object templateSource) {
         return ((MultiSource) templateSource).getLastModified();
     }
@@ -108,12 +108,11 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
     }
 
     /**
-     * Clears the soft affinity memory, also resets all enclosed {@link StatefulTemplateLoader}-s.
+     * Clears the sickiness memory, also resets the state of all enclosed {@link StatefulTemplateLoader}-s.
      */
     public void resetState() {
-        lastLoaderForName.clear();
-        for (int i = 0; i < loaders.length; i++) {
-            TemplateLoader loader = loaders[i];
+        lastTemplateLoaderForName.clear();
+        for (TemplateLoader loader : templateLoaders) {
             if (loader instanceof StatefulTemplateLoader) {
                 ((StatefulTemplateLoader) loader).resetState();
             }
@@ -181,11 +180,11 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
     public String toString() {
         StringBuilder sb = new StringBuilder();
         sb.append("MultiTemplateLoader(");
-        for (int i = 0; i < loaders.length; i++) {
+        for (int i = 0; i < templateLoaders.length; i++) {
             if (i != 0) {
                 sb.append(", ");
             }
-            sb.append("loader").append(i + 1).append(" = ").append(loaders[i]);
+            sb.append("loader").append(i + 1).append(" = ").append(templateLoaders[i]);
         }
         sb.append(")");
         return sb.toString();
@@ -197,7 +196,7 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
      * @since 2.3.23
      */
     public int getTemplateLoaderCount() {
-        return loaders.length;
+        return templateLoaders.length;
     }
 
     /**
@@ -207,10 +206,12 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
      *            Must be below {@link #getTemplateLoaderCount()}.
      */
     public TemplateLoader getTemplateLoader(int index) {
-        return loaders[index];
+        return templateLoaders[index];
     }
 
     /**
+     * Getter pair of {@link #setSticky(boolean)}.
+     * 
      * @since 2.3.24
      */
     public boolean isSticky() {
@@ -218,6 +219,10 @@ public class MultiTemplateLoader implements StatefulTemplateLoader {
     }
 
     /**
+     * Sets if for a name that was already loaded earlier the same {@link TemplateLoader} will be tried first, or
+     * we always try the {@link TemplateLoader}-s strictly in the order as it was specified in the constructor.
+     * The default is {@code true}.
+     * 
      * @since 2.3.24
      */
     public void setSticky(boolean sticky) {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/StringTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/StringTemplateLoader.java b/src/main/java/freemarker/cache/StringTemplateLoader.java
index 94e1af3..13b7548 100644
--- a/src/main/java/freemarker/cache/StringTemplateLoader.java
+++ b/src/main/java/freemarker/cache/StringTemplateLoader.java
@@ -72,10 +72,10 @@ public class StringTemplateLoader implements TemplateLoader {
      * loader.
      * 
      * @param name the name of the template.
-     * @param templateSource the source code of the template.
+     * @param templateContent the source code of the template.
      */
-    public void putTemplate(String name, String templateSource) {
-        putTemplate(name, templateSource, System.currentTimeMillis());
+    public void putTemplate(String name, String templateContent) {
+        putTemplate(name, templateContent, System.currentTimeMillis());
     }
     
     /**
@@ -93,12 +93,12 @@ public class StringTemplateLoader implements TemplateLoader {
      * loader.
      * 
      * @param name the name of the template.
-     * @param templateSource the source code of the template.
+     * @param templateContent the source code of the template.
      * @param lastModified the time of last modification of the template in 
      * terms of <tt>System.currentTimeMillis()</tt>
      */
-    public void putTemplate(String name, String templateSource, long lastModified) {
-        templates.put(name, new StringTemplateSource(name, templateSource, lastModified));
+    public void putTemplate(String name, String templateContent, long lastModified) {
+        templates.put(name, new StringTemplateSource(name, templateContent, lastModified));
     }
     
     /**
@@ -129,26 +129,26 @@ public class StringTemplateLoader implements TemplateLoader {
     }
     
     public Reader getReader(Object templateSource, String encoding) {
-        return new StringReader(((StringTemplateSource) templateSource).source);
+        return new StringReader(((StringTemplateSource) templateSource).templateContent);
     }
     
     private static class StringTemplateSource {
         private final String name;
-        private final String source;
+        private final String templateContent;
         private final long lastModified;
         
-        StringTemplateSource(String name, String source, long lastModified) {
+        StringTemplateSource(String name, String templateContent, long lastModified) {
             if (name == null) {
                 throw new IllegalArgumentException("name == null");
             }
-            if (source == null) {
+            if (templateContent == null) {
                 throw new IllegalArgumentException("source == null");
             }
             if (lastModified < -1L) {
                 throw new IllegalArgumentException("lastModified < -1L");
             }
             this.name = name;
-            this.source = source;
+            this.templateContent = templateContent;
             this.lastModified = lastModified;
         }
         

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/URLTemplateSource.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/URLTemplateSource.java b/src/main/java/freemarker/cache/URLTemplateSource.java
index 716b642..0bfd49f 100644
--- a/src/main/java/freemarker/cache/URLTemplateSource.java
+++ b/src/main/java/freemarker/cache/URLTemplateSource.java
@@ -27,7 +27,7 @@ import java.net.URL;
 import java.net.URLConnection;
 
 /**
- * Wraps a <code>java.net.URL</code>, and implements methods required for a typical template source.
+ * Wraps a {@link URL}, and implements methods required for a typical template source.
  */
 class URLTemplateSource {
     private final URL url;
@@ -69,6 +69,7 @@ class URLTemplateSource {
     long lastModified() {
         if (conn instanceof JarURLConnection) {
           // There is a bug in sun's jar url connection that causes file handle leaks when calling getLastModified()
+          // (see https://bugs.openjdk.java.net/browse/JDK-6956385).
           // Since the time stamps of jar file contents can't vary independent from the jar file timestamp, just use
           // the jar file timestamp
           URL jarURL = ((JarURLConnection) conn).getJarFileURL();

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/b1e28b0f/src/main/java/freemarker/cache/WebappTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/cache/WebappTemplateLoader.java b/src/main/java/freemarker/cache/WebappTemplateLoader.java
index 44c40df..24e64fa 100644
--- a/src/main/java/freemarker/cache/WebappTemplateLoader.java
+++ b/src/main/java/freemarker/cache/WebappTemplateLoader.java
@@ -33,6 +33,7 @@ import javax.servlet.ServletContext;
 import freemarker.log.Logger;
 import freemarker.template.Configuration;
 import freemarker.template.utility.CollectionUtils;
+import freemarker.template.utility.NullArgumentException;
 import freemarker.template.utility.StringUtil;
 
 /**
@@ -76,12 +77,8 @@ public class WebappTemplateLoader implements TemplateLoader {
      *            the base path to template resources.
      */
     public WebappTemplateLoader(ServletContext servletContext, String subdirPath) {
-        if (servletContext == null) {
-            throw new IllegalArgumentException("servletContext == null");
-        }
-        if (subdirPath == null) {
-            throw new IllegalArgumentException("path == null");
-        }
+        NullArgumentException.check("servletContext", servletContext);
+        NullArgumentException.check("subdirPath", subdirPath);
 
         subdirPath = subdirPath.replace('\\', '/');
         if (!subdirPath.endsWith("/")) {