001: /* This software is published under the terms of the OpenSymphony Software
002: * License version 1.1, of which a copy has been included with this
003: * distribution in the LICENSE.txt file. */
004: package com.opensymphony.module.sitemesh.filter;
005:
006: import javax.servlet.ServletOutputStream;
007: import java.io.IOException;
008:
009: /**
010: * Provides a ServletOutputStream that routes through to another ServletOutputStream, however the destination
011: * can be changed at any point. The destination can be passed in using a factory, so it will not be created
012: * until it's actually needed.
013: *
014: * @author Joe Walnes
015: * @version $Revision: 1.1 $
016: */
017: public class RoutableServletOutputStream extends ServletOutputStream {
018:
019: private ServletOutputStream destination;
020: private DestinationFactory factory;
021:
022: /**
023: * Factory to lazily instantiate the destination.
024: */
025: public static interface DestinationFactory {
026: ServletOutputStream create() throws IOException;
027: }
028:
029: public RoutableServletOutputStream(DestinationFactory factory) {
030: this .factory = factory;
031: }
032:
033: private ServletOutputStream getDestination() throws IOException {
034: if (destination == null) {
035: destination = factory.create();
036: }
037: return destination;
038: }
039:
040: public void updateDestination(DestinationFactory factory) {
041: destination = null;
042: this .factory = factory;
043: }
044:
045: public void close() throws IOException {
046: getDestination().close();
047: }
048:
049: public void write(int b) throws IOException {
050: getDestination().write(b);
051: }
052:
053: public void print(String s) throws IOException {
054: getDestination().print(s);
055: }
056:
057: public void print(boolean b) throws IOException {
058: getDestination().print(b);
059: }
060:
061: public void print(char c) throws IOException {
062: getDestination().print(c);
063: }
064:
065: public void print(int i) throws IOException {
066: getDestination().print(i);
067: }
068:
069: public void print(long l) throws IOException {
070: getDestination().print(l);
071: }
072:
073: public void print(float v) throws IOException {
074: getDestination().print(v);
075: }
076:
077: public void print(double v) throws IOException {
078: getDestination().print(v);
079: }
080:
081: public void println() throws IOException {
082: getDestination().println();
083: }
084:
085: public void println(String s) throws IOException {
086: getDestination().println(s);
087: }
088:
089: public void println(boolean b) throws IOException {
090: getDestination().println(b);
091: }
092:
093: public void println(char c) throws IOException {
094: getDestination().println(c);
095: }
096:
097: public void println(int i) throws IOException {
098: getDestination().println(i);
099: }
100:
101: public void println(long l) throws IOException {
102: getDestination().println(l);
103: }
104:
105: public void println(float v) throws IOException {
106: getDestination().println(v);
107: }
108:
109: public void println(double v) throws IOException {
110: getDestination().println(v);
111: }
112:
113: public void write(byte b[]) throws IOException {
114: getDestination().write(b);
115: }
116:
117: public void write(byte b[], int off, int len) throws IOException {
118: getDestination().write(b, off, len);
119: }
120:
121: public void flush() throws IOException {
122: getDestination().flush();
123: }
124: }
|