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      private Runnable callBeforeUse;
25      private static final Runnable doNothing = new Runnable()
26      {
27          public void run()
28          {
29          }
30      };
31  
32      /**
33       * Factory to lazily instantiate the destination.
34       */
35      public static interface DestinationFactory {
36          PrintWriter activateDestination() throws IOException;
37      }
38  
39      public RoutablePrintWriter(DestinationFactory factory, Runnable callBeforeUse) {
40          super(new NullWriter());
41          this.factory = factory;
42          this.callBeforeUse = callBeforeUse;
43      }
44  
45      public RoutablePrintWriter(DestinationFactory factory) {
46          this(factory, doNothing);
47      }
48  
49      private PrintWriter getDestination() {
50          if (destination == null) {
51              try {
52                  destination = factory.activateDestination();
53              } catch (IOException e) {
54                  setError();
55              }
56          }
57  
58          callBeforeUse.run();
59  
60          return destination;
61      }
62  
63      public void updateDestination(DestinationFactory factory) {
64          destination = null;
65          this.factory = factory;
66      }
67  
68      public void close() {
69          getDestination().close();
70      }
71  
72      public void println(Object x) {
73          getDestination().println(x);
74      }
75  
76      public void println(String x) {
77          getDestination().println(x);
78      }
79  
80      public void println(char x[]) {
81          getDestination().println(x);
82      }
83  
84      public void println(double x) {
85          getDestination().println(x);
86      }
87  
88      public void println(float x) {
89          getDestination().println(x);
90      }
91  
92      public void println(long x) {
93          getDestination().println(x);
94      }
95  
96      public void println(int x) {
97          getDestination().println(x);
98      }
99  
100     public void println(char x) {
101         getDestination().println(x);
102     }
103 
104     public void println(boolean x) {
105         getDestination().println(x);
106     }
107 
108     public void println() {
109         getDestination().println();
110     }
111 
112     public void print(Object obj) {
113         getDestination().print(obj);
114     }
115 
116     public void print(String s) {
117         getDestination().print(s);
118     }
119 
120     public void print(char s[]) {
121         getDestination().print(s);
122     }
123 
124     public void print(double d) {
125         getDestination().print(d);
126     }
127 
128     public void print(float f) {
129         getDestination().print(f);
130     }
131 
132     public void print(long l) {
133         getDestination().print(l);
134     }
135 
136     public void print(int i) {
137         getDestination().print(i);
138     }
139 
140     public void print(char c) {
141         getDestination().print(c);
142     }
143 
144     public void print(boolean b) {
145         getDestination().print(b);
146     }
147 
148     public void write(String s) {
149         getDestination().write(s);
150     }
151 
152     public void write(String s, int off, int len) {
153         getDestination().write(s, off, len);
154     }
155 
156     public void write(char buf[]) {
157         getDestination().write(buf);
158     }
159 
160     public void write(char buf[], int off, int len) {
161         getDestination().write(buf, off, len);
162     }
163 
164     public void write(int c) {
165         getDestination().write(c);
166     }
167 
168     public boolean checkError() {
169         return getDestination().checkError();
170     }
171 
172     public void flush() {
173         getDestination().flush();
174     }
175 
176     /**
177      * Just to keep super constructor for PrintWriter happy - it's never actually used.
178      */
179     private static class NullWriter extends Writer {
180 
181         protected NullWriter() {
182             super();
183         }
184 
185         public void write(char cbuf[], int off, int len) throws IOException {
186             throw new UnsupportedOperationException();
187         }
188 
189         public void flush() throws IOException {
190             throw new UnsupportedOperationException();
191         }
192 
193         public void close() throws IOException {
194             throw new UnsupportedOperationException();
195         }
196 
197     }
198 
199 }