Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

alebu
Hi list,
I got problem with loading templates from classpath ( from jar file ).
I have "/templates" directory in my class path and code which
initializes freemarker template loader:
            TemplateLoader templateLoader = new ClassTemplateLoader(
Main.class, "/templates" );
            cfg.setTemplateLoader( templateLoader );

Here Main class is my class where main() method located. This class is
my application entry point ( obviously ).
I tested resource from my code and it was founded:
                    InputStream is = null;
                    is =
Main.class.getClassLoader().getResourceAsStream( "templates/const.tpl" );
                    if( is != null ){
                        log.info( "resource 1 found" );
                        is.close();
                    }else{
                        log.warn( "resource 1 not found" );
                    }

but when I requesting "const.tpl" template, I got IOExceoption - file
not found.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Leandro Cruz
Funny. I implemented support for loading template from the classpath yesterday :-)
This is what I'm using for xingu-template:
loader = new ClassTemplateLoader(getClass(), "/");

If you want to retrieve resources from your classpath try this:
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);

--
Leandro Rodrigo Saad Cruz
software developer - certified scrum master
:: scrum.com.br
:: db.apache.org/ojb
:: guara-framework.sf.net
:: xingu.sf.net

On 2/8/07, Alebu <[hidden email]> wrote:
Hi list,
I got problem with loading templates from classpath ( from jar file ).
I have "/templates" directory in my class path and code which
initializes freemarker template loader:
            TemplateLoader templateLoader = new ClassTemplateLoader(
Main.class, "/templates" );
            cfg.setTemplateLoader( templateLoader );

Here Main class is my class where main() method located. This class is
my application entry point ( obviously ).
I tested resource from my code and it was founded:
                    InputStream is = null;
                    is =
Main.class.getClassLoader().getResourceAsStream( "templates/const.tpl" );
                    if( is != null ){
                        log.info( "resource 1 found" );
                        is.close();
                    }else{
                        log.warn( "resource 1 not found" );
                    }

but when I requesting "const.tpl" template, I got IOExceoption - file
not found.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Attila Szegedi
In reply to this post by alebu
On Thu, 08 Feb 2007 15:12:28 +0100, Alebu <[hidden email]> wrote:

> Hi list,
> I got problem with loading templates from classpath ( from jar file ).
> I have "/templates" directory in my class path and code which
> initializes freemarker template loader:
>             TemplateLoader templateLoader = new ClassTemplateLoader(
> Main.class, "/templates" );

Try "templates" instead of "/templates".

Attila.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

akuntamukkala
Here is what I did to get around this problem with ClassTemplateLoader

The issue is that the proper class loader isn't being used. I got around this by subclassing ClassTemplateLoader

public class FixedClassTemplateLoader extends ClassTemplateLoader {

        private ClassLoader classLoader;
        public FixedClassTemplateLoader(Class clazz, String string) {
                super(clazz, string);
                classLoader = clazz.getClassLoader();
        }
        @Override
        protected URL getURL(String name) {
                // this now loads the template file from the jar
                return classLoader.getResource(name);
        }
}

then here is how I used it

ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(), "templates");
TemplateLoader[] loaders = new TemplateLoader[] { ctl };
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);
Template template = cfg.getTemplate("templates/testEmailTemplate.ftl");

----

Now I am able to load the template and generate my email template.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Daniel Dekany
Thursday, February 21, 2013, 7:32:01 PM, akuntamukkala wrote:

> Here is what I did to get around this problem with ClassTemplateLoader
>
> The issue is that the proper class loader isn't being used. I got around
> this by subclassing ClassTemplateLoader
>
> public class FixedClassTemplateLoader extends ClassTemplateLoader {
>
>         private ClassLoader classLoader;
>         public FixedClassTemplateLoader(Class clazz, String string) {
>                 super(clazz, string);
>                 classLoader = clazz.getClassLoader();
>         }
>         @Override
>         protected URL getURL(String name) {
>                 // this now loads the template file from the jar
>                 return classLoader.getResource(name);
>         }      
> }

So, what exactly does this fix and how? As far as I see,
ClassTemplateLoader already uses the defining class-loader of the
`clazz` constructor argument, so there's no change here. Or is there?

BTW, the other change is that you ignore the 2nd constructor parameter
(the base path) in the getURL method for some reason, although if you
really don't want a base path, you could just use "" for it as the
constructor argument.

--
Best regards,
 Daniel Dekany


> then here is how I used it
>
> ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(),
> "templates");
> TemplateLoader[] loaders = new TemplateLoader[] { ctl };
> MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
> cfg.setTemplateLoader(mtl);
> Template template =
> cfg.getTemplate("templates/testEmailTemplate.ftl");
>
> ----
>
> Now I am able to load the template and generate my email template.
>
> Thanks


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

akuntamukkala
Daniel
Thanks for taking time to respond.

The problem I was having is that the "templates/testEmailTemplate.ftl" which is in a jar file wasn't getting loaded when I used ClassTemplateLoader.  

log4j:WARN No appenders could be found for logger (freemarker.cache).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.io.FileNotFoundException: Template testEmailTemplate.ftl not found.
        at freemarker.template.Configuration.getTemplate(Configuration.java:489)
        at freemarker.template.Configuration.getTemplate(Configuration.java:452)

Upon digging up ClassTemplateLoader {free marker v2.3.9} source, I found getUrl() to be defined as follows. 

    protected URL getURL(String name)

    {

        return loaderClass.getResource(path + name);

    }


It seems valid. However this wasn't loading the .ftl file from templates folder. I explicitly initialized ClassLoader as shown the code in the previous email and it worked. 

I understand the intent of this class but wasn't able to make it work for me so I used that class loader to load the resource.


Thank you,
Ashwin 




*****This e-mail and replies and forwards are for the sole use of the above individual(s) or entities and may contain proprietary, privileged and/or highly confidential information. Unauthorized use is strictly prohibited.*****


From: "Daniel Dekany [via FreeMarker]" <[hidden email]>
Date: Thursday, February 21, 2013 6:20 PM
To: Ashwini Kuntamukkala <[hidden email]>
Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Thursday, February 21, 2013, 7:32:01 PM, akuntamukkala wrote:

> Here is what I did to get around this problem with ClassTemplateLoader
>
> The issue is that the proper class loader isn't being used. I got around
> this by subclassing ClassTemplateLoader
>
> public class FixedClassTemplateLoader extends ClassTemplateLoader {
>
>         private ClassLoader classLoader;
>         public FixedClassTemplateLoader(Class clazz, String string) {
>                 super(clazz, string);
>                 classLoader = clazz.getClassLoader();
>         }
>         @Override
>         protected URL getURL(String name) {
>                 // this now loads the template file from the jar
>                 return classLoader.getResource(name);
>         }      
> }
So, what exactly does this fix and how? As far as I see,
ClassTemplateLoader already uses the defining class-loader of the
`clazz` constructor argument, so there's no change here. Or is there?

BTW, the other change is that you ignore the 2nd constructor parameter
(the base path) in the getURL method for some reason, although if you
really don't want a base path, you could just use "" for it as the
constructor argument.

--
Best regards,
 Daniel Dekany


> then here is how I used it
>
> ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(),
> "templates");
> TemplateLoader[] loaders = new TemplateLoader[] { ctl };
> MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
> cfg.setTemplateLoader(mtl);
> Template template =
> cfg.getTemplate("templates/testEmailTemplate.ftl");
>
> ----
>
> Now I am able to load the template and generate my email template.
>
> Thanks

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user



To unsubscribe from Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? ), click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

akuntamukkala
In reply to this post by Daniel Dekany
I think the issue stems from the difference between class.getResouce() and classLoader.getResource() which is why the file wasn't found when I used the former. 


*****This e-mail and replies and forwards are for the sole use of the above individual(s) or entities and may contain proprietary, privileged and/or highly confidential information. Unauthorized use is strictly prohibited.*****


From: "Daniel Dekany [via FreeMarker]" <[hidden email]>
Date: Thursday, February 21, 2013 6:20 PM
To: Ashwini Kuntamukkala <[hidden email]>
Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Thursday, February 21, 2013, 7:32:01 PM, akuntamukkala wrote:

> Here is what I did to get around this problem with ClassTemplateLoader
>
> The issue is that the proper class loader isn't being used. I got around
> this by subclassing ClassTemplateLoader
>
> public class FixedClassTemplateLoader extends ClassTemplateLoader {
>
>         private ClassLoader classLoader;
>         public FixedClassTemplateLoader(Class clazz, String string) {
>                 super(clazz, string);
>                 classLoader = clazz.getClassLoader();
>         }
>         @Override
>         protected URL getURL(String name) {
>                 // this now loads the template file from the jar
>                 return classLoader.getResource(name);
>         }      
> }
So, what exactly does this fix and how? As far as I see,
ClassTemplateLoader already uses the defining class-loader of the
`clazz` constructor argument, so there's no change here. Or is there?

BTW, the other change is that you ignore the 2nd constructor parameter
(the base path) in the getURL method for some reason, although if you
really don't want a base path, you could just use "" for it as the
constructor argument.

--
Best regards,
 Daniel Dekany


> then here is how I used it
>
> ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(),
> "templates");
> TemplateLoader[] loaders = new TemplateLoader[] { ctl };
> MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
> cfg.setTemplateLoader(mtl);
> Template template =
> cfg.getTemplate("templates/testEmailTemplate.ftl");
>
> ----
>
> Now I am able to load the template and generate my email template.
>
> Thanks

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user



To unsubscribe from Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? ), click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )

Daniel Dekany
In reply to this post by akuntamukkala
Since people use ClassTemplateLoader for ages to load templates from jar-s,
I suspect the problem is elsewhere. Looking at your example:


   ClassTemplateLoader ctl = new FixedClassTemplateLoader(getClass(), "templates");
   ...
   Template template = cfg.getTemplate("templates/testEmailTemplate.ftl");

In the first line you establish that the template root directory is
"templates", yet in the last line you write
"templates/testEmailTemplate.ftl", not "testEmailTemplate.ftl". With
ClassTemplateLoader that would mean that the final classpath-resource
path is "templates/templates/testEmailTemplate.ftl". But your template
loader ignores the base path, so I suppose, that's why it works. What
you had to do is either using cfg.getTemplate("testEmailTemplate.ftl")
or using new ClassTemplateLoader(getClass(), ""). Is possible that
this is the solution?

--
Best regards,
 Daniel Dekany


Friday, February 22, 2013, 6:47:51 AM, akuntamukkala wrote:

> Daniel
> Thanks for taking time to respond.
>
> The problem I was having is that the
> "templates/testEmailTemplate.ftl" which is in a jar file wasn't
> getting loaded when I used ClassTemplateLoader.  
>
> log4j:WARN No appenders could be found for logger (freemarker.cache).
> log4j:WARN Please initialize the log4j system properly.
> Exception in thread "main" java.io.FileNotFoundException: Template testEmailTemplate.ftl not found.
>         at
> freemarker.template.Configuration.getTemplate(Configuration.java:489)
>         at
> freemarker.template.Configuration.getTemplate(Configuration.java:452)
>
> Upon digging up ClassTemplateLoader {free marker v2.3.9} source, I
> found getUrl() to be defined as follows.
>     protected URL getURL(String name)
>     {
>         return loaderClass.getResource(path + name);
>     }
>
> It seems valid. However this wasn't loading the .ftl file from
> templates folder. I explicitly initialized ClassLoader as shown the
> code in the previous email and it worked.
>
> I understand the intent of this class but wasn't able to make it
> work for me so I used that class loader to load the resource.
>
>
> Thank you,
> Ashwin
>
>
>
>
> *****This e-mail and replies and forwards are for the sole use of
> the above individual(s) or entities and may contain proprietary,
> privileged and/or highly confidential information. Unauthorized use is strictly prohibited.*****
>
>
> From: "Daniel Dekany [via FreeMarker]" <[hidden email]>
> Date: Thursday, February 21, 2013 6:20 PM
> To: Ashwini Kuntamukkala <[hidden email]>
> Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )
>
> Thursday, February 21, 2013, 7:32:01 PM, akuntamukkala wrote:
>
>> Here is what I did to get around this problem with ClassTemplateLoader
>>
>> The issue is that the proper class loader isn't being used. I got around
>> this by subclassing ClassTemplateLoader
>>
>> public class FixedClassTemplateLoader extends ClassTemplateLoader {
>>
>>         private ClassLoader classLoader;
>>         public FixedClassTemplateLoader(Class clazz, String string) {
>>                 super(clazz, string);
>>                 classLoader = clazz.getClassLoader();
>>         }
>>         @Override
>>         protected URL getURL(String name) {
>>                 // this now loads the template file from the jar
>>                 return classLoader.getResource(name);
>>         }      
>> }
> So, what exactly does this fix and how? As far as I see,
> ClassTemplateLoader already uses the defining class-loader of the
> `clazz` constructor argument, so there's no change here. Or is there?
>
> BTW, the other change is that you ignore the 2nd constructor parameter
> (the base path) in the getURL method for some reason, although if you
> really don't want a base path, you could just use "" for it as the
> constructor argument.


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user