Re: FileTemplateLoader doesnt close reader ?

Albert Kam <[email protected]> Wed, 13 Mar 2013 09:42:36 +0700
Newsgroups gmane.comp.web.freemarker.user
Message-ID <CAMT20g9dVFhxEqKHYn+DDkTFMFR52V8kBdzrOP4wX5qBTk7CDQ@mail.gmail.com>
> The Reader is closed by whoever has called getReader() by calling
> java.io.Reader.close(), not with a TemplateLoader method. The
> closeTemplateSource() just closes the "template source", which in the
> case of FileTemplateLoader is a File, and hence there's nothing to
> close in it.
>
> That sounds impossible. By the time IOUtils.closeQuietly() is called,
> the StringReader and the String it reads is already done. Maybe
> IOUtils.closeQuietly() throws an exception that's improperly handled,
> gibing an empty page?
>
> I tried closing the reader and catching and logging the exception instead
of just using IOTools.closeQuietly(),
and notices that there's no exception happening, and still gave me the
empty page.
Commenting the close reader section gave me back the output.


> You should not close the Reader that you return to the caller. But in
> this case you have a Reader that you don't return to the caller or to
> anybody, so of course you have to close it yourself, but that you
> should do right where it's done on that blog.
>
> Aha, maybe that's the reason it gave me the empty output.
According to
http://docs.oracle.com/javase/7/docs/api/java/io/StringReader.html#close()
"Closes the stream and releases any system resources associated with it.
Once the stream has been closed, further read(), ready(), mark(), or
reset() invocations will throw an IOException. Closing a previously closed
stream has no effect."
Maybe the caller of the closed-reader got an IOException somewhere and
hence, the empty output ?


>
> That implementation is not very good, because:
>
> - It can't handle the case when the template already has an #ftl
>   header
>
> - That it pre-loads the template, instead of creating a wrapping
>   Reader that appends and prepends what's needed right in the
>   character stream. Although it's not a big deal in practice, it's not
>   very nice.
>
> You may look into the source code of FMPP, and there
> Engine.wrapReader. (I was substantially younger when I wrote that, but
> let's hope it's OK... :) )
>
> I actually have switched back to the normal FileTemplateLoader without
wrapping the escape directive for several reasons :

- I actually make use of functions for URLs generation.
  And it's ugly to see the noescape tags wrapping every link calls, some
thing like :
  <#noescape>${url.home("Return to main page")}</#noescape>
  I prefer using ?html as needed

- Because i am using JBoss Tools for the freemarker editor (which does help
me in parsing error detection, highlighting, etc), it errors the line of
every noescape because it's not inside an escape directive, which gives me
the tension i dont need.

 And thank you for the wrapReader sharing, i will add it to my library in
case i find a use of it in the future.

Regards from Jakarta,
Albert Kam

-- 
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

------------------------------------------------------------------------------
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_mar

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user