01: /*
02: * $Id: CallbackOutputStream.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.model.streaming;
12:
13: import java.io.IOException;
14: import java.io.OutputStream;
15:
16: import org.apache.commons.logging.Log;
17: import org.apache.commons.logging.LogFactory;
18:
19: public class CallbackOutputStream extends OutputStream {
20:
21: protected final Log logger = LogFactory
22: .getLog(CallbackOutputStream.class);
23:
24: public static interface Callback {
25:
26: public void onClose() throws Exception;
27:
28: }
29:
30: private OutputStream delegate;
31: private Callback callback;
32:
33: public CallbackOutputStream(OutputStream delegate, Callback callback) {
34: this .delegate = delegate;
35: this .callback = callback;
36: }
37:
38: public void write(int b) throws IOException {
39: delegate.write(b);
40: }
41:
42: public void write(byte b[]) throws IOException {
43: delegate.write(b);
44: }
45:
46: public void write(byte b[], int off, int len) throws IOException {
47: delegate.write(b, off, len);
48: }
49:
50: public void close() throws IOException {
51: try {
52: delegate.close();
53: } finally {
54: closeCallback();
55: }
56: }
57:
58: private void closeCallback() {
59: if (null != callback) {
60: try {
61: callback.onClose();
62: } catch (Exception e) {
63: logger
64: .debug("Suppressing exception while releasing resources: "
65: + e.getMessage());
66: }
67: }
68:
69: }
70:
71: }
|