Discussion:
[FreeMarker-user] Checking if template exists
Leandro Saad
2006-02-21 02:42:53 UTC
Permalink
Hi all. I'd like to check if a template can be loaded, but I don' t wan't
and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.

Cheers
--
Leandro Rodrigo Saad Cruz
CTO - InterBusiness Technologies
sitedafesta.com.br
db.apache.org/ojb
guara-framework.sf.net
xingu.sf.net
Jonathan Revusky
2006-02-21 15:43:31 UTC
Permalink
Post by Leandro Saad
Hi all. I'd like to check if a template can be loaded, but I don' t
wan't and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.
You can get the actual TemplateLoader object from the Configuration, and
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.

OTOH, I'm not sure what the big deal about the exception getting thrown
is. For example, I don't know what practical objection there really is
to the following little routine.

boolean isTemplateThere(String templateName) {
try {
config.getTemplate(TemplateName);
}
catch (Exception e) {
return false;
}
return true;
}
Max Rydahl Andersen
2006-02-21 17:29:54 UTC
Permalink
Post by Jonathan Revusky
Post by Leandro Saad
Hi all. I'd like to check if a template can be loaded, but I don' t
wan't and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.
You can get the actual TemplateLoader object from the Configuration, and
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting thrown
is.
performance.

/max
Post by Jonathan Revusky
For example, I don't know what practical objection there really is to
the following little routine.
boolean isTemplateThere(String templateName) {
try {
config.getTemplate(TemplateName);
}
catch (Exception e) {
return false;
}
return true;
}
/max
Post by Jonathan Revusky
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log
files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
--
Max Rydahl Andersen
callto://max.rydahl.andersen

Hibernate
***@hibernate.org
http://hibernate.org

JBoss Inc
***@jboss.com
Jonathan Revusky
2006-02-21 17:50:12 UTC
Permalink
Post by Max Rydahl Andersen
Post by Jonathan Revusky
Post by Leandro Saad
Hi all. I'd like to check if a template can be loaded, but I don' t
wan't and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.
You can get the actual TemplateLoader object from the Configuration, and
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting thrown
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant. If you were going
to call this isTemplateThere() routine a thousand times within a loop
or something. But I couldnt' imagine a usage like that where any
performance considerations would be ever noticeable.

JR
Post by Max Rydahl Andersen
/max
Post by Jonathan Revusky
For example, I don't know what practical objection there really is to
the following little routine.
boolean isTemplateThere(String templateName) {
try {
config.getTemplate(TemplateName);
}
catch (Exception e) {
return false;
}
return true;
}
/max
Post by Jonathan Revusky
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log
files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
--
Max Rydahl Andersen
callto://max.rydahl.andersen
Hibernate
http://hibernate.org
JBoss Inc
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Max Rydahl Andersen
2006-02-21 21:36:28 UTC
Permalink
TemplateLoader object from the Configuration,
Post by Jonathan Revusky
Post by Leandro Saad
and
Post by Jonathan Revusky
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting
thrown
Post by Jonathan Revusky
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant.
it is for my unit tests and large scale generation from a UI ;)

Anyway as another poster stated, relying on exceptions for controlling flow
is bad style. Providing a method to check if something will
be available to avoid the overhead in code and runtime is in my book A
Good Thing.

/max
Post by Jonathan Revusky
If you were going
to call this isTemplateThere() routine a thousand times within a loop
or something. But I couldnt' imagine a usage like that where any
performance considerations would be ever noticeable.
JR
Post by Leandro Saad
/max
Post by Jonathan Revusky
For example, I don't know what practical objection there really is to
the following little routine.
boolean isTemplateThere(String templateName) {
try {
config.getTemplate(TemplateName);
}
catch (Exception e) {
return false;
}
return true;
}
/max
Post by Jonathan Revusky
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD
SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
Post by Jonathan Revusky
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
--
Max Rydahl Andersen
callto://max.rydahl.andersen
Hibernate
http://hibernate.org
JBoss Inc
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid3432&bid#0486&dat1642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
--
Max Rydahl Andersen
callto://max.rydahl.andersen

Hibernate
***@hibernate.org
http://hibernate.org

JBoss Inc
***@jboss.com
Daniel Dekany
2006-02-22 06:08:51 UTC
Permalink
Post by Jonathan Revusky
TemplateLoader object from the Configuration,
Post by Jonathan Revusky
Post by Leandro Saad
and
Post by Jonathan Revusky
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting
thrown
Post by Jonathan Revusky
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant.
it is for my unit tests and large scale generation from a UI ;)
Anyway as another poster stated, relying on exceptions for controlling flow
is bad style. Providing a method to check if something will
be available to avoid the overhead in code and runtime is in my book A
Good Thing.
If you look into the API docs, TemplateLoader.findTemplateSource
doesn't throw exception if the template is missing, but it return
null. This behavior is very clearly required by the API. The same goes
for TemplateCache.getTemplate(). Unless there are bugs around, it
should not throw exception if the template is missing.

Regarding the checking function VS exception topic... sure, doing it
with exceptions is ugly. However, relying on a separate checker call
and a separate getter call is not necessary better. First of all, the
atomicity of the operation is lost then. I mean, just because at the
time of checker call the template was present, what ensures that it
will be still there when later you call the getter method? Some short
of transactional storage is needed to ensure thing like this, which
probably means a lot of overhead if we are talking about performance.
Also, if some errors hazards are OK (that the user of API must
realize), then calling the checking and the getting method may means
more I/O operations, as you access the file twice. So usually the good
solution no checker method, but getter method must indicate if the
resource is missing in its return value, or maybe by throwing an
exception.
--
Best regards,
Daniel Dekany
Jonathan Revusky
2006-02-22 12:58:48 UTC
Permalink
Post by Daniel Dekany
Post by Jonathan Revusky
TemplateLoader object from the Configuration,
Post by Jonathan Revusky
Post by Leandro Saad
and
Post by Jonathan Revusky
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting
thrown
Post by Jonathan Revusky
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant.
it is for my unit tests and large scale generation from a UI ;)
Anyway as another poster stated, relying on exceptions for controlling flow
is bad style. Providing a method to check if something will
be available to avoid the overhead in code and runtime is in my book A
Good Thing.
If you look into the API docs, TemplateLoader.findTemplateSource
doesn't throw exception if the template is missing, but it return
null. This behavior is very clearly required by the API. The same goes
for TemplateCache.getTemplate(). Unless there are bugs around, it
should not throw exception if the template is missing.
Yes, but the top-level API method, Configuration.getTemplate() throws an
exception if these lower-level methods return null.

But this is the correct semantics IMO for the top-level public API that
most people will use. In typical usages of FreeMarker, I don't think
that a template fails to load. Or, I mean, rather, that if loading a
template does fail, it's indicative of an extremely critical problem in
your app and you have little recourse but just to abort or shut down.

Really, a FreeMarker template is application *code*. In fact, I would
imagine that many, if not most, deployed apps would load FTL files via
the same ClassLoader mechanism that is used to load a .class file.
Certainly, if an app fails to load a class it needs, this is a severe
error that causes an exception, actually a java.lang.Error, I guess.

Now, you still can, for special situations, check whether a class is
avaialble to be loaded, but the way you do that would be precisely
analogous to what I proposed for isTemplateThere().

boolean isClassThere(String className) {
try {
Class.forName(className);
} catch (Exception e) {
return false;
}
return true;
}
Post by Daniel Dekany
Regarding the checking function VS exception topic... sure, doing it
with exceptions is ugly. However, relying on a separate checker call
and a separate getter call is not necessary better. First of all, the
atomicity of the operation is lost then. I mean, just because at the
time of checker call the template was present, what ensures that it
will be still there when later you call the getter method? Some short
of transactional storage is needed to ensure thing like this, which
probably means a lot of overhead if we are talking about performance.
Also, if some errors hazards are OK (that the user of API must
realize), then calling the checking and the getting method may means
more I/O operations, as you access the file twice. So usually the good
solution no checker method, but getter method must indicate if the
resource is missing in its return value, or maybe by throwing an
exception.
The way our top-level API works currently seems right to me. You can get
at the underlying TemplateLoader object via
Configuration.getTemplateLoader() and then the underlying TemplateLoader
will return null rather than throw an exception. But I am pretty sure
this is a special case usage.

If somebody writes:

template = config.getTemplate("purchase_form.ftl");

and the template can't be found, this is almost always going to be a
severe error condition and is not part of normal flow of execution. It's
the same as if a class fails to load. It means that some .jar file that
is suppsoed to be present is not there or whatever, which is a pretty
severe problem.

JR
Daniel Dekany
2006-02-22 17:04:34 UTC
Permalink
Post by Jonathan Revusky
Post by Daniel Dekany
Post by Jonathan Revusky
TemplateLoader object from the Configuration,
Post by Jonathan Revusky
Post by Leandro Saad
and
Post by Jonathan Revusky
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting
thrown
Post by Jonathan Revusky
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant.
it is for my unit tests and large scale generation from a UI ;)
Anyway as another poster stated, relying on exceptions for controlling flow
is bad style. Providing a method to check if something will
be available to avoid the overhead in code and runtime is in my book A
Good Thing.
If you look into the API docs, TemplateLoader.findTemplateSource
doesn't throw exception if the template is missing, but it return
null. This behavior is very clearly required by the API. The same goes
for TemplateCache.getTemplate(). Unless there are bugs around, it
should not throw exception if the template is missing.
Yes, but the top-level API method, Configuration.getTemplate() throws an
exception if these lower-level methods return null.
Ops... Well, then we could have a Configuration.getTemplateIfExists()
that return null in that case. If this hassle in the API worts the
benefit.
Post by Jonathan Revusky
But this is the correct semantics IMO for the top-level public API that
most people will use. In typical usages of FreeMarker, I don't think
that a template fails to load. Or, I mean, rather, that if loading a
template does fail, it's indicative of an extremely critical problem in
your app and you have little recourse but just to abort or shut down.
Really, a FreeMarker template is application *code*. In fact, I would
imagine that many, if not most, deployed apps would load FTL files via
the same ClassLoader mechanism that is used to load a .class file.
Certainly, if an app fails to load a class it needs, this is a severe
error that causes an exception, actually a java.lang.Error, I guess.
Now, you still can, for special situations, check whether a class is
avaialble to be loaded, but the way you do that would be precisely
analogous to what I proposed for isTemplateThere().
boolean isClassThere(String className) {
try {
Class.forName(className);
} catch (Exception e) {
return false;
}
return true;
}
Note that this doesn't work well for templates, since it's assumed
that templates can be deleted or renamed without restarting the
application (similarly as they can be modified).
--
Best regards,
Daniel Dekany
Jonathan Revusky
2006-02-22 16:38:14 UTC
Permalink
Post by Daniel Dekany
Post by Jonathan Revusky
TemplateLoader object from the Configuration,
Post by Jonathan Revusky
Post by Leandro Saad
and
Post by Jonathan Revusky
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting
thrown
Post by Jonathan Revusky
is.
performance.
That's the reason (the only one) that occurred to me also. But I can't
imagine scenarios where this would be significant.
it is for my unit tests and large scale generation from a UI ;)
Anyway as another poster stated, relying on exceptions for controlling flow
is bad style. Providing a method to check if something will
be available to avoid the overhead in code and runtime is in my book A
Good Thing.
If you look into the API docs, TemplateLoader.findTemplateSource
doesn't throw exception if the template is missing, but it return
null. This behavior is very clearly required by the API. The same goes
for TemplateCache.getTemplate(). Unless there are bugs around, it
should not throw exception if the template is missing.
Yes, but the top-level API method, Configuration.getTemplate() throws an
exception if these lower-level methods return null.

But this is the correct semantics IMO for the top-level public API that
most people will use. In typical usages of FreeMarker, I don't think
that a template fails to load. Or, I mean, rather, that if loading a
template does fail, it's indicative of an extremely critical problem in
your app and you have little recourse but just to abort or shut down.

Really, a FreeMarker template is application *code*. In fact, I would
imagine that many, if not most, deployed apps would load FTL files via
the same ClassLoader mechanism that is used to load a .class file.
Certainly, if an app fails to load a class it needs, this is a severe
error that causes an exception, actually a java.lang.Error, I guess.

Now, you still can, for special situations, check whether a class is
avaialble to be loaded, but the way you do that would be precisely
analogous to what I proposed for isTemplateThere().

boolean isClassThere(String className) {
try {
Class.forName(className);
} catch (Exception e) {
return false;
}
return true;
}
Post by Daniel Dekany
Regarding the checking function VS exception topic... sure, doing it
with exceptions is ugly. However, relying on a separate checker call
and a separate getter call is not necessary better. First of all, the
atomicity of the operation is lost then. I mean, just because at the
time of checker call the template was present, what ensures that it
will be still there when later you call the getter method? Some short
of transactional storage is needed to ensure thing like this, which
probably means a lot of overhead if we are talking about performance.
Also, if some errors hazards are OK (that the user of API must
realize), then calling the checking and the getting method may means
more I/O operations, as you access the file twice. So usually the good
solution no checker method, but getter method must indicate if the
resource is missing in its return value, or maybe by throwing an
exception.
The way our top-level API works currently seems right to me. You can get
at the underlying TemplateLoader object via
Configuration.getTemplateLoader() and then the underlying TemplateLoader
will return null rather than throw an exception. But I am pretty sure
this is a special case usage.

If somebody writes:

template = config.getTemplate("purchase_form.ftl");

and the template can't be found, this is almost always going to be a
severe error condition and is not part of normal flow of execution. It's
the same as if a class fails to load. It means that some .jar file that
is suppsoed to be present is not there or whatever, which is a pretty
severe problem.

JR
Leandro Saad
2006-02-22 18:36:08 UTC
Permalink
Post by Jonathan Revusky
But this is the correct semantics IMO for the top-level public API that
most people will use. In typical usages of FreeMarker, I don't think
that a template fails to load. Or, I mean, rather, that if loading a
template does fail, it's indicative of an extremely critical problem in
your app and you have little recourse but just to abort or shut down.
I disagree. Suppose you want to parse a template only if it exists. This is
a common scenario for me.
Otherwise parse a default template.
Post by Jonathan Revusky
Post by Daniel Dekany
Regarding the checking function VS exception topic... sure, doing it
with exceptions is ugly. However, relying on a separate checker call
and a separate getter call is not necessary better. First of all, the
atomicity of the operation is lost then. I mean, just because at the
time of checker call the template was present, what ensures that it
will be still there when later you call the getter method?
I don't think this is a common scenario. Template may be deployed once or
generated on demand, but there is no need for atomic/synchronized access to
these files.


--
Leandro Rodrigo Saad Cruz
CTO - InterBusiness Technologies
sitedafesta.com.br
db.apache.org/ojb
guara-framework.sf.net
xingu.sf.net
Jonathan Revusky
2006-02-22 19:19:45 UTC
Permalink
Post by Leandro Saad
Post by Jonathan Revusky
But this is the correct semantics IMO for the top-level public API that
most people will use. In typical usages of FreeMarker, I don't think
that a template fails to load. Or, I mean, rather, that if loading a
template does fail, it's indicative of an extremely critical problem in
your app and you have little recourse but just to abort or shut down.
I disagree. Suppose you want to parse a template only if it exists. This is
a common scenario for me.
Otherwise parse a default template.
Well, you can get what you want by using Configuration.getTemplateLoader()
in conjunction with TemplateLoader.findTemplateSource(), as in:

public boolean isTemplateThere(String name) {
return config.getTemplateLoader().findTemplateSource(name) != null;
}


Another pattern (from within a template) that you might find useful is:

<#attempt>
<#include "someTemplate.ftl">
<#recover>
<#include "defaultTemplate.ftl">
</#recover>
Leandro Saad
2006-02-21 17:36:59 UTC
Permalink
There is no big deal. But relying on exceptions to control program flow is
not my favorite programming style.
I was just checking if cfg.getTemplate() is the only way to check if a
template exists or not.
This is a good feature to have, specially for framework integration. You may
still throw an exception if you can't find the template file, but you should
provide a method to check it first.
Post by Jonathan Revusky
Post by Leandro Saad
Hi all. I'd like to check if a template can be loaded, but I don' t
wan't and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.
You can get the actual TemplateLoader object from the Configuration, and
I *think* that will just return null if the template is not available.
Though I don't remember so well honestly.
OTOH, I'm not sure what the big deal about the exception getting thrown
is. For example, I don't know what practical objection there really is
to the following little routine.
boolean isTemplateThere(String templateName) {
try {
config.getTemplate(TemplateName);
}
catch (Exception e) {
return false;
}
return true;
}
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log
files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
Leandro Rodrigo Saad Cruz
CTO - InterBusiness Technologies
sitedafesta.com.br
db.apache.org/ojb
guara-framework.sf.net
xingu.sf.net
Attila Szegedi
2006-02-27 11:04:54 UTC
Permalink
boolean doesTemplateExist(String name)
{
TemplateLoader loader = cfg.getTemplateLoader();
Object templateSource = cfg.findTemplateSource(name);
if(templateSource != null)
{
loader.closeTemplateSource(templateSource);
return true;
}
else
{
return false;
}
}

Note that this will potentially involve an IO check on each invocation,
but otherwise has no side effects (i.e. the template won't get loaded,
thus no parsing exceptions will be thrown if it is syntactically incorrect
etc.)

Of course, we could provide a getTemplateOrNull() equivalents to the
current getTemplate() methods in Configuration - I don't see any problem
with that.
--
home: http://www.szegedi.org
weblog: http://constc.blogspot.com

Attila.
Post by Leandro Saad
Hi all. I'd like to check if a template can be loaded, but I don' t wan't
and exception to be thrown. Is that possible?
I do want to see exception if I tell freemarker to load the template.
Cheers
--
Leandro Rodrigo Saad Cruz
CTO - InterBusiness Technologies
sitedafesta.com.br
db.apache.org/ojb
guara-framework.sf.net
xingu.sf.net
Loading...