View Javadoc

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.atlassian.gzipfilter;
5   
6   import java.io.PrintWriter;
7   import java.io.IOException;
8   import java.io.Writer;
9   
10  /**
11   * Provides a PrintWriter that routes through to another PrintWriter, however the destination
12   * can be changed at any point. The destination can be passed in using a factory, so it will not be created
13   * until it's actually needed.
14   * <p>
15   * Borrowed from sitemesh.
16   *
17   * @author Joe Walnes
18   * @version $Revision: 1.1 $
19   */
20  public class RoutablePrintWriter extends PrintWriter {
21  
22      private PrintWriter destination;
23      private DestinationFactory factory;
24  
25      /**
26       * Factory to lazily instantiate the destination.
27       */
28      public static interface DestinationFactory {
29          PrintWriter activateDestination() throws IOException;
30      }
31  
32      public RoutablePrintWriter(DestinationFactory factory) {
33          super(new NullWriter());
34          this.factory = factory;
35      }
36  
37      private PrintWriter getDestination() {
38          if (destination == null) {
39              try {
40                  destination = factory.activateDestination();
41              } catch (IOException e) {
42                  setError();
43              }
44          }
45          return destination;
46      }
47  
48      public void updateDestination(DestinationFactory factory) {
49          destination = null;
50          this.factory = factory;
51      }
52  
53      public void close() {
54          getDestination().close();
55      }
56  
57      public void println(Object x) {
58          getDestination().println(x);
59      }
60  
61      public void println(String x) {
62          getDestination().println(x);
63      }
64  
65      public void println(char x[]) {
66          getDestination().println(x);
67      }
68  
69      public void println(double x) {
70          getDestination().println(x);
71      }
72  
73      public void println(float x) {
74          getDestination().println(x);
75      }
76  
77      public void println(long x) {
78          getDestination().println(x);
79      }
80  
81      public void println(int x) {
82          getDestination().println(x);
83      }
84  
85      public void println(char x) {
86          getDestination().println(x);
87      }
88  
89      public void println(boolean x) {
90          getDestination().println(x);
91      }
92  
93      public void println() {
94          getDestination().println();
95      }
96  
97      public void print(Object obj) {
98          getDestination().print(obj);
99      }
100 
101     public void print(String s) {
102         getDestination().print(s);
103     }
104 
105     public void print(char s[]) {
106         getDestination().print(s);
107     }
108 
109     public void print(double d) {
110         getDestination().print(d);
111     }
112 
113     public void print(float f) {
114         getDestination().print(f);
115     }
116 
117     public void print(long l) {
118         getDestination().print(l);
119     }
120 
121     public void print(int i) {
122         getDestination().print(i);
123     }
124 
125     public void print(char c) {
126         getDestination().print(c);
127     }
128 
129     public void print(boolean b) {
130         getDestination().print(b);
131     }
132 
133     public void write(String s) {
134         getDestination().write(s);
135     }
136 
137     public void write(String s, int off, int len) {
138         getDestination().write(s, off, len);
139     }
140 
141     public void write(char buf[]) {
142         getDestination().write(buf);
143     }
144 
145     public void write(char buf[], int off, int len) {
146         getDestination().write(buf, off, len);
147     }
148 
149     public void write(int c) {
150         getDestination().write(c);
151     }
152 
153     public boolean checkError() {
154         return getDestination().checkError();
155     }
156 
157     public void flush() {
158         getDestination().flush();
159     }
160 
161     /**
162      * Just to keep super constructor for PrintWriter happy - it's never actually used.
163      */
164     private static class NullWriter extends Writer {
165 
166         protected NullWriter() {
167             super();
168         }
169 
170         public void write(char cbuf[], int off, int len) throws IOException {
171             throw new UnsupportedOperationException();
172         }
173 
174         public void flush() throws IOException {
175             throw new UnsupportedOperationException();
176         }
177 
178         public void close() throws IOException {
179             throw new UnsupportedOperationException();
180         }
181 
182     }
183 
184 }