| 1 | |
package org.webslinger.commons.vfs; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Writer; |
| 5 | |
import java.io.OutputStreamWriter; |
| 6 | |
|
| 7 | |
import org.apache.commons.vfs.FileObject; |
| 8 | |
|
| 9 | |
import org.webslinger.io.Charsets; |
| 10 | |
|
| 11 | |
import org.webslinger.io.RollingWriter; |
| 12 | |
import org.webslinger.util.RolloverLogic; |
| 13 | |
|
| 14 | |
public class CommonsVfsRollingWriter extends RollingWriter { |
| 15 | |
protected final FileObject dir; |
| 16 | |
protected final String name; |
| 17 | |
|
| 18 | |
public CommonsVfsRollingWriter(FileObject dir, String name, String pattern) { |
| 19 | 0 | this(dir, name, new RolloverLogic(pattern)); |
| 20 | 0 | } |
| 21 | |
|
| 22 | |
public CommonsVfsRollingWriter(FileObject dir, String name, RolloverLogic logic) { |
| 23 | 0 | super(logic); |
| 24 | 0 | this.dir = dir; |
| 25 | 0 | this.name = name; |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
protected Writer makeWriter(boolean rollover) throws IOException { |
| 29 | |
FileObject currentFile; |
| 30 | |
boolean append; |
| 31 | 0 | if (rollover) { |
| 32 | 0 | currentFile = dir.resolveFile(name); |
| 33 | 0 | if (currentFile.getContent().getSize() > 0) { |
| 34 | 0 | String suffix = logic.getCurrentFormat(); |
| 35 | 0 | currentFile.moveTo(dir.resolveFile(name + suffix)); |
| 36 | 0 | append = false; |
| 37 | 0 | } else { |
| 38 | 0 | append = true; |
| 39 | |
} |
| 40 | |
} else { |
| 41 | 0 | currentFile = dir.resolveFile(name); |
| 42 | 0 | append = true; |
| 43 | |
} |
| 44 | 0 | return new OutputStreamWriter(dir.resolveFile(name).getContent().getOutputStream(append), Charsets.UTF8); |
| 45 | |
} |
| 46 | |
} |