001: package vicazh.hyperpool.stream;
002:
003: import java.io.*;
004: import java.text.*;
005: import java.util.*;
006: import java.util.logging.*;
007: import vicazh.hyperpool.Start;
008:
009: /**
010: * The report service
011: *
012: * @author Victor Zhigunov
013: * @version 0.4.0
014: */
015: abstract public class ReportService extends FileService implements
016: ReportServiceMBean {
017: public ReportService() {
018: }
019:
020: /**
021: * @param cachesize
022: * cache size
023: */
024: public ReportService(int cachesize) {
025: super (cachesize);
026: }
027:
028: private int date;
029:
030: public void setDate(int date) {
031: this .date = date;
032: if (getDir() != null && this .date != date)
033: pack();
034: }
035:
036: public int getDate() {
037: return date;
038: }
039:
040: private String delimiter;
041:
042: public String getDelimiter() {
043: return delimiter;
044: }
045:
046: public void setDelimiter(String delimiter) {
047: this .delimiter = delimiter;
048: }
049:
050: public boolean checkPut(File file) {
051: return !checkRemove(file);
052: }
053:
054: public boolean checkRemove(File file) {
055: return new Date(file.lastModified() + getDate() * 86400000L)
056: .before(new Date());
057: }
058:
059: protected PrintStream ps;
060:
061: protected synchronized void clear() {
062: if (ps != null) {
063: ps.close();
064: ps = null;
065: }
066: super .clear();
067: }
068:
069: public synchronized void stop() throws Exception {
070: super .stop();
071: if (ps != null)
072: ps.close();
073: }
074:
075: public void setAttribute(String name, Object value)
076: throws Exception {
077: if (name.equals(FileServiceMBean.OPTIONS)) {
078: setDate(((ReportService) value).getDate());
079: setDelimiter(((ReportService) value).getDelimiter());
080: }
081: super .setAttribute(name, value);
082: }
083:
084: private File file;
085:
086: private void open(File file) throws FileNotFoundException {
087: this .file = file;
088: file.getParentFile().mkdirs();
089: ps = new PrintStream(new BufferedOutputStream(
090: new FileOutputStream(file, true)));
091: }
092:
093: private static final SimpleDateFormat df = new SimpleDateFormat(
094: "yyyyMMdd");
095:
096: protected void store(Date date) throws FileNotFoundException {
097: pack();
098: File f = new File(dir2path() + File.separatorChar
099: + df.format(date));
100: if (ps == null)
101: open(f);
102: else if (!file.equals(f)) {
103: ps.close();
104: open(f);
105: }
106: }
107:
108: public File[] listFiles(final PeriodControl pc) {
109: return new File(dir2path()).listFiles(new FileFilter() {
110: public boolean accept(File pathname) {
111: try {
112: return pc.check(PeriodControl.lastTime(df
113: .parse(pathname.getName())));
114: } catch (ParseException e) {
115: Start.logger.log(Level.SEVERE, e.getMessage(), e);
116: }
117: return false;
118: }
119: });
120: }
121:
122: public Locale getLocale() {
123: return Locale.getDefault();
124: }
125: }
|