Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
   * 
   * Licensed 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 freemarker.cache;
 
 import java.io.File;
 import java.io.Reader;
 import java.net.URL;
 
 import  javax.servlet.ServletContext;
 
A TemplateLoader that uses streams reachable through ServletContext.getResource(String) as its source of templates.
 
 public class WebappTemplateLoader implements TemplateLoader
 {
 
     private static final Logger LOG = Logger.getLogger("freemarker.cache");
 
     private final ServletContext servletContext;
     private final String subdirPath;
 
     private Boolean urlConnectionUsesCaches;
 
     private boolean attemptFileAccess = true;

    
Creates a resource template cache that will use the specified servlet context to load the resources. It will use the base path of "/" meaning templates will be resolved relative to the servlet context root location.

Parameters:
servletContext the servlet context whose ServletContext.getResource(String) will be used to load the templates.
 
     public WebappTemplateLoader(ServletContext servletContext) {
         this(servletContext"/");
     }

    
Creates a template loader that will use the specified servlet context to load the resources. It will use the specified base path, which is interpreted relatively to the context root (does not mater if you start it with "/" or not). Path components should be separated by forward slashes independently of the separator character used by the underlying operating system.

Parameters:
servletContext the servlet context whose ServletContext.getResource(String) will be used to load the templates.
subdirPath the base path to template resources.
 
     public WebappTemplateLoader(ServletContext servletContextString subdirPath) {
         if (servletContext == null) {
             throw new IllegalArgumentException("servletContext == null");
         }
         if (subdirPath == null) {
             throw new IllegalArgumentException("path == null");
         }
 
         subdirPath = subdirPath.replace('\\''/');
         if (!subdirPath.endsWith("/")) {
             subdirPath += "/";
         }
         if (!subdirPath.startsWith("/")) {
             subdirPath = "/" + subdirPath;
         }
         this. = subdirPath;
         this. = servletContext;
     }
 
     public Object findTemplateSource(String namethrows IOException {
         String fullPath =  + name;
 
         if () {
             // First try to open as plain file (to bypass servlet container resource caches).
            try {
                String realPath = .getRealPath(fullPath);
                if (realPath != null) {
                    File file = new File(realPath);
                    if (file.canRead() && file.isFile()) {
                        return file;
                    }
                }
            } catch (SecurityException e) {
                ;// ignore
            }
        }
        // If it fails, try to open it with servletContext.getResource.
        URL url = null;
        try {
            url = .getResource(fullPath);
        } catch (MalformedURLException e) {
            .warn("Could not retrieve resource " + StringUtil.jQuoteNoXSS(fullPath),
                    e);
            return null;
        }
        return url == null ? null : new URLTemplateSource(urlgetURLConnectionUsesCaches());
    }
    public long getLastModified(Object templateSource) {
        if (templateSource instanceof File) {
            return ((FiletemplateSource).lastModified();
        } else {
            return ((URLTemplateSourcetemplateSource).lastModified();
        }
    }
    public Reader getReader(Object templateSourceString encoding)
            throws IOException {
        if (templateSource instanceof File) {
            return new InputStreamReader(
                    new FileInputStream((FiletemplateSource),
                    encoding);
        } else {
            return new InputStreamReader(
                    ((URLTemplateSourcetemplateSource).getInputStream(),
                    encoding);
        }
    }
    public void closeTemplateSource(Object templateSourcethrows IOException {
        if (templateSource instanceof File) {
            // Do nothing.
        } else {
            ((URLTemplateSourcetemplateSource).close();
        }
    }

    
Getter pair of setURLConnectionUsesCaches(Boolean).

Since:
2.3.21
        return ;
    }

    
It does the same as URLTemplateLoader.setURLConnectionUsesCaches(Boolean); see there.

Since:
2.3.21
    public void setURLConnectionUsesCaches(Boolean urlConnectionUsesCaches) {
        this. = urlConnectionUsesCaches;
    }

    
Show class name and some details that are useful in template-not-found errors.

Since:
2.3.21
    public String toString() {
        return TemplateLoaderUtils.getClassNameForToString(this)
                + "(subdirPath=" + StringUtil.jQuote()
                + ", servletContext={contextPath=" + StringUtil.jQuote(getContextPath())
                + ", displayName=" + StringUtil.jQuote(.getServletContextName()) + "})";
    }

    
Gets the context path if we are on Servlet 2.5+, or else returns failure description string.
    private String getContextPath() {
        try {
            Method m = .getClass().getMethod("getContextPath".);
            return (Stringm.invoke(.);
        } catch (Throwable e) {
            return "[can't query before Serlvet 2.5]";
        }
    }

    
Getter pair of setAttemptFileAccess(boolean).

Since:
2.3.23
    public boolean getAttemptFileAccess() {
        return ;
    }

    
Specifies that before loading templates with ServletContext.getResource(String), it should try to load the template as File; default is true, though it's not always recommended anymore. This is a workaround for the case when the servlet container doesn't show template modifications after the template was already loaded earlier. But it's certainly better to counter this problem by disabling the URL connection cache with setURLConnectionUsesCaches(Boolean), which is also the default behavior with incompatible_improvements 2.3.21 and later.

Since:
2.3.23
    public void setAttemptFileAccess(boolean attemptLoadingFromFile) {
        this. = attemptLoadingFromFile;
    }
New to GrepCode? Check out our FAQ X