| 1 | |
package org.webslinger.io; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
|
| 6 | |
import org.webslinger.util.RolloverLogic; |
| 7 | |
|
| 8 | |
public abstract class RollingWriter { |
| 9 | |
protected final RolloverLogic logic; |
| 10 | |
|
| 11 | |
protected Writer writer; |
| 12 | |
|
| 13 | 0 | protected RollingWriter(RolloverLogic logic) { |
| 14 | 0 | this.logic = logic; |
| 15 | 0 | } |
| 16 | |
|
| 17 | |
|
| 18 | |
public Writer getWriter() throws IOException { |
| 19 | 0 | synchronized (this) { |
| 20 | 0 | if (writer != null) { |
| 21 | 0 | if (!logic.checkRollover()) return writer; |
| 22 | 0 | writer.flush(); |
| 23 | 0 | writer.close(); |
| 24 | 0 | writer = makeWriter(true); |
| 25 | |
} else { |
| 26 | 0 | writer = makeWriter(false); |
| 27 | |
} |
| 28 | 0 | return writer; |
| 29 | 0 | } |
| 30 | |
} |
| 31 | |
|
| 32 | |
protected abstract Writer makeWriter(boolean rollover) throws IOException; |
| 33 | |
} |