Discussion:
[FreeMarker-user] Loading templates from classpath ( ClassTemplateLoader problem or /dev/hands ? )
Alebu
2007-02-08 14:12:28 UTC
Permalink
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.
Leandro Saad
2007-02-08 16:20:44 UTC
Permalink
Funny. I implemented support for loading template from the classpath
yesterday :-)
This is what I'm using for xingu-template<http://black-beans.com.br:8088/xingu>
:
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
Post by 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
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 ).
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.
Attila Szegedi
2007-02-08 20:09:31 UTC
Permalink
Post by 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
TemplateLoader templateLoader =3D new ClassTemplateLoader(=
Main.class, "/templates" );
Try "templates" instead of "/templates".

Attila.
akuntamukkala
2013-02-21 18:32:01 UTC
Permalink
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



--
View this message in context: http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-ClassTemplateLoader-problem-or-dev-hands-tp625098p4654453.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-02-22 00:19:44 UTC
Permalink
Post by 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);
}
}
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
Post by akuntamukkala
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
akuntamukkala
2013-02-22 05:47:51 UTC
Permalink
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]"
<ml-node+***@n4.nabble.com>
Date: Thursday, February 21, 2013 6:20 PM
To: Ashwini Kuntamukkala <***@scispike.com>
Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem
or /dev/hands ? )
Post by 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);
}
}
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
Post by akuntamukkala
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] </user/SendEmail.jtp?type=node&node=4654454&i=0>
https://lists.sourceforge.net/lists/listinfo/freemarker-user



If you reply to this email, your message will be added to the discussion
below:
http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-Clas
sTemplateLoader-problem-or-dev-hands-tp625098p4654454.html
To unsubscribe from Loading templates from classpath ( ClassTemplateLoader
problem or /dev/hands ? ), click here
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=unsub
scribe_by_code&node=625098&code=YXNod2luLmt1bnRhbXVra2FsYUBzY2lzcGlrZS5jb218
NjI1MDk4fC0xMTYyMDA0NzAz> .
NAML
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=macro
_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Ba
sicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.templa
te.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instan
t_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>






--
View this message in context: http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-ClassTemplateLoader-problem-or-dev-hands-tp625098p4654455.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-02-23 09:12:11 UTC
Permalink
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
Post by Daniel Dekany
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 ? )
Post by 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);
}
}
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.
akuntamukkala
2013-02-22 06:36:44 UTC
Permalink
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]"
<ml-node+***@n4.nabble.com>
Date: Thursday, February 21, 2013 6:20 PM
To: Ashwini Kuntamukkala <***@scispike.com>
Subject: Re: Loading templates from classpath ( ClassTemplateLoader problem
or /dev/hands ? )
Post by 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);
}
}
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
Post by akuntamukkala
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] </user/SendEmail.jtp?type=node&node=4654454&i=0>
https://lists.sourceforge.net/lists/listinfo/freemarker-user



If you reply to this email, your message will be added to the discussion
below:
http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-Clas
sTemplateLoader-problem-or-dev-hands-tp625098p4654454.html
To unsubscribe from Loading templates from classpath ( ClassTemplateLoader
problem or /dev/hands ? ), click here
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=unsub
scribe_by_code&node=625098&code=YXNod2luLmt1bnRhbXVra2FsYUBzY2lzcGlrZS5jb218
NjI1MDk4fC0xMTYyMDA0NzAz> .
NAML
<http://freemarker.624813.n4.nabble.com/template/NamlServlet.jtp?macro=macro
_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Ba
sicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.templa
te.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instan
t_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>






--
View this message in context: http://freemarker.624813.n4.nabble.com/Loading-templates-from-classpath-ClassTemplateLoader-problem-or-dev-hands-tp625098p4654456.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Loading...