Class FreeMarkerTemplateLoader

  • All Implemented Interfaces:
    freemarker.cache.TemplateLoader

    public class FreeMarkerTemplateLoader
    extends Object
    implements freemarker.cache.TemplateLoader
    The FreeMarkerTemplateLoader class implements the Apache FreeMarker template loader.
    Author:
    Marcus Portmann
    • Constructor Detail

      • FreeMarkerTemplateLoader

        public FreeMarkerTemplateLoader​(IMailService mailService)
        Constructs a new FreeMarkerTemplateLoader.
        Parameters:
        mailService - the Mail Service
    • Method Detail

      • closeTemplateSource

        public void closeTemplateSource​(Object templateSource)
                                 throws IOException
        Closes the template source, releasing any resources held that are only required for reading the template and/or its metadata. This is the last method that is called by the TemplateCache for a template source, except that Object.equals(Object) is might called later too. TemplateCache ensures that this method will be called on every object that is returned from findTemplateSource(String).
        Specified by:
        closeTemplateSource in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - the template source that should be closed.
        Throws:
        IOException
      • findTemplateSource

        public Object findTemplateSource​(String name)
                                  throws IOException
        Finds the template in the backing storage and returns an object that identifies the storage location where the template can be loaded from. See the return value for more information.
        Specified by:
        findTemplateSource in interface freemarker.cache.TemplateLoader
        Parameters:
        name - The name of the template, already localized and normalized by the cache.
        Returns:
        An object representing the template source, which can be supplied in subsequent calls to getLastModified(Object) and getReader(Object, String), when those are called on the same TemplateLoader. null must be returned if the source for the template doesn't exist
        Throws:
        IOException
      • getLastModified

        public long getLastModified​(Object templateSource)
        Returns the time of last modification of the specified template source. This method is called after findTemplateSource().
        Specified by:
        getLastModified in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - an object representing a template source, obtained through a prior call to findTemplateSource(String)
        Returns:
        the time of last modification of the specified template source, or -1 if the time is not known.
      • getReader

        public Reader getReader​(Object templateSource,
                                String encoding)
                         throws IOException
        Returns the character stream of a template represented by the specified template source. This method is possibly called for multiple times for the same template source object, and it must always return a Reader that reads the template from its beginning. Before this method is called for the second time (or later), its caller must close the previously returned Reader, and it must not use it anymore. That is, this method is not required to support multiple concurrent readers for the same source templateSource object.

        Typically, this method is called if the template is missing from the cache, or if after calling findTemplateSource(String) and getLastModified(Object) it was determined that the cached copy of the template is stale. Then, if it turns out that the encoding parameter used doesn't match the actual template content (based on the #ftl encoding=... header), this method will be called for a second time with the correct encoding parameter value.

        Specified by:
        getReader in interface freemarker.cache.TemplateLoader
        Parameters:
        templateSource - an object representing a template source, obtained through a prior call to findTemplateSource(String). This must be an object on which TemplateLoader.closeTemplateSource(Object) wasn't applied yet.
        encoding - the character encoding used to translate source bytes to characters. Some loaders may not have access to the byte representation of the template stream, and instead directly obtain a character stream. These loaders should ignore the encoding parameter.
        Returns:
        A Reader representing the template character stream. It's the responsibility of the caller (which is TemplateCache usually) to close() it. The Reader is not required to work after the templateSource was closed (closeTemplateSource(Object)).
        Throws:
        IOException