Custom TemplateLoader

"Paul Rivera" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Hi!

I've created a custom TemplateLoader and I need your help with its design
and spotting potential issues.

Here's my situation:
I need to store my Configuration instance in the Application level of my
webapp.  Every time I use this Configuration instance, I want to be able to
change the 'base directory' to where it looks for the template.  All of my
templates are also stored inside a Jar.  So, I don't think the
setDirectoryForTemplateLoading() is applicable.

To make the situation clear to you, assume that I have two classes:
src/main/java/org/myorg/ClassA
src/main/java/org/myorg/utils/ClassB

Both classes will retrieve the Configuration instance stored in the
Application level of my webapp so that the cached templates are retained.
For each class, they have a folder named 'template' in their package
directory that contains their template file:

src/main/java/org/myorg/template/test.ftl
src/main/java/org/myorg/utils/template/foo.ftl

I'm looking for a solution wherein I can simply pass ClassA.class and the
path 'template' to be able to load test.ftl.  Same with foo.ftl.  I've tried
calling Configuration.setClassForTemplateLoading(Class, String).  The
problem here is that it clears the cache storage.  I want to be able to set
the Class several times without clearing the cache.  Is the Configuration
class intended to work with a single class only?

Here's my Custom TemplateLoader:
public class MyCustomTemplateLoader extends ClassTemplateLoader {
    private Class loaderClass;
    private String path;

    public void setLoaderClass(Class clazz, String path) {
        setFields(clazz, path);
    }

    protected URL getURL(String name) {
        return loaderClass.getResource(path + name);
    }

    private void setFields(Class loaderClass, String path) {
        if(loaderClass == null) {
            throw new IllegalArgumentException("loaderClass == null");
        }
        if(path == null) {
            throw new IllegalArgumentException("path == null");
        }
        this.loaderClass = loaderClass;
        this.path = canonicalizePrefix(path);
    }
}


So far, this has been working fine for me.  Everytime I need to change the
Class, I just call setLoaderClass().

I'd like to ask for your opinion on my design and for any potential issues
you can see with this implementation.  So far, I was only able to think of
problems with the TemplateKey.  Each cached template has a TemplateKey that
is identified by: encoding, locale and name.  I still have to check how my
custom TemplateLoader behaves if I have two files of the same name, locale
and encoding but belong to different folders.

Your comments and suggestions will be greatly appreciated.

Best Regards,
Paul Rivera

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.