KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > filter > RoutableServletOutputStream


1 /* This software is published under the terms of the OpenSymphony Software
2  * License version 1.1, of which a copy has been included with this
3  * distribution in the LICENSE.txt file. */

4 package com.opensymphony.module.sitemesh.filter;
5
6 import javax.servlet.ServletOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8
9 /**
10  * Provides a ServletOutputStream that routes through to another ServletOutputStream, however the destination
11  * can be changed at any point. The destination can be passed in using a factory, so it will not be created
12  * until it's actually needed.
13  *
14  * @author Joe Walnes
15  * @version $Revision: 1.1 $
16  */

17 public class RoutableServletOutputStream extends ServletOutputStream JavaDoc {
18
19     private ServletOutputStream JavaDoc destination;
20     private DestinationFactory factory;
21
22     /**
23      * Factory to lazily instantiate the destination.
24      */

25     public static interface DestinationFactory {
26         ServletOutputStream JavaDoc create() throws IOException JavaDoc;
27     }
28
29     public RoutableServletOutputStream(DestinationFactory factory) {
30         this.factory = factory;
31     }
32
33     private ServletOutputStream JavaDoc getDestination() throws IOException JavaDoc {
34         if (destination == null) {
35             destination = factory.create();
36         }
37         return destination;
38     }
39
40     public void updateDestination(DestinationFactory factory) {
41         destination = null;
42         this.factory = factory;
43     }
44
45     public void close() throws IOException JavaDoc {
46         getDestination().close();
47     }
48
49     public void write(int b) throws IOException JavaDoc {
50         getDestination().write(b);
51     }
52
53     public void print(String JavaDoc s) throws IOException JavaDoc {
54         getDestination().print(s);
55     }
56
57     public void print(boolean b) throws IOException JavaDoc {
58         getDestination().print(b);
59     }
60
61     public void print(char c) throws IOException JavaDoc {
62         getDestination().print(c);
63     }
64
65     public void print(int i) throws IOException JavaDoc {
66         getDestination().print(i);
67     }
68
69     public void print(long l) throws IOException JavaDoc {
70         getDestination().print(l);
71     }
72
73     public void print(float v) throws IOException JavaDoc {
74         getDestination().print(v);
75     }
76
77     public void print(double v) throws IOException JavaDoc {
78         getDestination().print(v);
79     }
80
81     public void println() throws IOException JavaDoc {
82         getDestination().println();
83     }
84
85     public void println(String JavaDoc s) throws IOException JavaDoc {
86         getDestination().println(s);
87     }
88
89     public void println(boolean b) throws IOException JavaDoc {
90         getDestination().println(b);
91     }
92
93     public void println(char c) throws IOException JavaDoc {
94         getDestination().println(c);
95     }
96
97     public void println(int i) throws IOException JavaDoc {
98         getDestination().println(i);
99     }
100
101     public void println(long l) throws IOException JavaDoc {
102         getDestination().println(l);
103     }
104
105     public void println(float v) throws IOException JavaDoc {
106         getDestination().println(v);
107     }
108
109     public void println(double v) throws IOException JavaDoc {
110         getDestination().println(v);
111     }
112
113     public void write(byte b[]) throws IOException JavaDoc {
114         getDestination().write(b);
115     }
116
117     public void write(byte b[], int off, int len) throws IOException JavaDoc {
118         getDestination().write(b, off, len);
119     }
120
121     public void flush() throws IOException JavaDoc {
122         getDestination().flush();
123     }
124 }
125
Popular Tags