/
GrailsRoutablePrintWriter.java
202 lines (156 loc) · 4.36 KB
/
GrailsRoutablePrintWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.codehaus.groovy.grails.web.sitemesh;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import org.codehaus.groovy.grails.web.util.GrailsPrintWriter;
public class GrailsRoutablePrintWriter extends GrailsPrintWriter {
private PrintWriter destination;
private DestinationFactory factory;
/**
* Factory to lazily instantiate the destination.
*/
public static interface DestinationFactory {
PrintWriter activateDestination() throws IOException;
}
public GrailsRoutablePrintWriter(DestinationFactory factory) {
super(new NullWriter());
this.factory = factory;
}
public Writer getOut() {
return getDestination();
}
@Override
public Writer getFinalTarget() {
if(getDestination() instanceof GrailsPrintWriter) {
return ((GrailsPrintWriter)getDestination()).getOut();
} else {
return getOut();
}
}
private PrintWriter getDestination() {
if (destination == null) {
try {
destination = factory.activateDestination();
} catch (IOException e) {
setError();
}
super.out = destination;
}
return destination;
}
public void updateDestination(DestinationFactory factory) {
destination = null;
super.out = destination;
this.factory = factory;
}
public void close() {
getDestination().close();
}
public void println(Object x) {
getDestination().println(x);
}
public void println(String x) {
getDestination().println(x);
}
public void println(char x[]) {
getDestination().println(x);
}
public void println(double x) {
getDestination().println(x);
}
public void println(float x) {
getDestination().println(x);
}
public void println(long x) {
getDestination().println(x);
}
public void println(int x) {
getDestination().println(x);
}
public void println(char x) {
getDestination().println(x);
}
public void println(boolean x) {
getDestination().println(x);
}
public void println() {
getDestination().println();
}
public void print(Object obj) {
getDestination().print(obj);
}
public void print(String s) {
getDestination().print(s);
}
public void print(char s[]) {
getDestination().print(s);
}
public void print(double d) {
getDestination().print(d);
}
public void print(float f) {
getDestination().print(f);
}
public void print(long l) {
getDestination().print(l);
}
public void print(int i) {
getDestination().print(i);
}
public void print(char c) {
getDestination().print(c);
}
public void print(boolean b) {
getDestination().print(b);
}
public void write(String s) {
getDestination().write(s);
}
public void write(String s, int off, int len) {
getDestination().write(s, off, len);
}
public void write(char buf[]) {
getDestination().write(buf);
}
public void write(char buf[], int off, int len) {
getDestination().write(buf, off, len);
}
public void write(int c) {
getDestination().write(c);
}
public boolean checkError() {
return getDestination().checkError();
}
public void flush() {
getDestination().flush();
}
@Override
public PrintWriter append(char c) {
return getDestination().append(c);
}
@Override
public PrintWriter append(CharSequence csq, int start, int end) {
return getDestination().append(csq, start, end);
}
@Override
public PrintWriter append(CharSequence csq) {
return getDestination().append(csq);
}
/**
* Just to keep super constructor for PrintWriter happy - it's never actually used.
*/
private static class NullWriter extends Writer {
protected NullWriter() {
super();
}
public void write(char cbuf[], int off, int len) throws IOException {
throw new UnsupportedOperationException();
}
public void flush() throws IOException {
throw new UnsupportedOperationException();
}
public void close() throws IOException {
throw new UnsupportedOperationException();
}
}
}