dev@jsr311.java.net

Re: StreamingOutput.cleanUp() ?

From: Bill Burke <bburke_at_redhat.com>
Date: Fri, 14 Mar 2008 09:11:29 -0400

I like the one-method interface better. Will start to look real nice
when closures get in Java.

Stephan Koops wrote:
> Hi @ all,
>
> what about a method in the StreamingOutput, that is called, also if
> write throws an Exception? That means, the new method does the same as a
> finally block?
> If a cleanup is needed, we need a try finally block now:
>
> @GET
> public Object get() throws WebAppException
> {
> final Connection conn = ...; //
> ...
> return new StreamingOutput() {
> void write(OutputStream output) throws IOException {
> try {
> outputStream.write(...)
> outputStream.write(...)
> outputStream.write(...)
> } finally {
> conn.close(); // or whatever cleanup
> }
> }
> }
> }
>
> Sometimes we forget to put the cleanup in a finally block. With the new
> method, the app developer do not need to "pollute" the write method with
> try and finally:
>
> @GET
> public Object get() throws WebAppException
> {
> final Connection conn = //
> ...
> return new StreamingOutput() {
> public void write(OutputStream output) throws IOException {
> outputStream.write(...)
> outputStream.write(...)
> outputStream.write(...)
> }
>
> public void cleanUp() {
> conn.close(); // or whatever cleanup
> }
> }
> }
>
> What do you think about this?
>
> To not coerce app developers to implement the new method, the interface
> StreamingOutput could be changed to an abstract class. The method
> write(OutputStream) stays abstract, and the new method cleanUp() has a
> default implementation, that does nothing.
>
> best regards
> Stephan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jsr311.dev.java.net
> For additional commands, e-mail: users-help_at_jsr311.dev.java.net
>

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com