001: package vicazh.hyperpool.stream.net.http;
002:
003: import java.io.*;
004: import java.util.*;
005: import vicazh.hyperpool.Writer;
006: import vicazh.hyperpool.stream.*;
007:
008: /**
009: * The explorer service
010: *
011: * @author Victor Zhigunov
012: * @version 0.4.0
013: */
014: public class ExplorerService extends FileService implements
015: ExplorerServiceMBean, Serializable {
016: public ExplorerService() {
017: }
018:
019: Writer writer;
020:
021: private String cache;
022:
023: private String base;
024:
025: /**
026: * @param cachesize
027: * cache size
028: * @param writer
029: * xsl result transformer
030: * @param cache
031: * cache folder name
032: * @param base
033: * base folder name
034: */
035: public ExplorerService(int cachesize, Writer writer, String cache,
036: String base) {
037: super (cachesize);
038: this .writer = writer;
039: this .cache = cache;
040: this .base = base;
041: }
042:
043: private long size;
044:
045: public void setSize(long size) {
046: if (getDir() != null && this .size != size) {
047: this .size = size;
048: pack();
049: } else
050: this .size = size;
051: }
052:
053: public long getSize() {
054: return size;
055: }
056:
057: private long max;
058:
059: public void setMax(long max) {
060: this .max = max;
061: }
062:
063: public long getMax() {
064: return max;
065: }
066:
067: public boolean checkPut(File file) {
068: return max == Long.MAX_VALUE || file.length() <= max * 1048576L;
069: }
070:
071: private long receive;
072:
073: public boolean checkRemove(File file) {
074: return (receive > size * 1048576L);
075: }
076:
077: protected synchronized void delete(File file) {
078: super .delete(getBase(file));
079: super .delete(file);
080: }
081:
082: File getBase(File file) {
083: return new File(new File(super .dir2path(), base),
084: file.getAbsolutePath()
085: .substring(
086: new File(dir2path()).getAbsolutePath()
087: .length() + 1));
088: }
089:
090: synchronized protected void add(File file) {
091: receive += file.length();
092: }
093:
094: protected void put(File file) {
095: super .put(file);
096: add(file);
097: }
098:
099: synchronized public void remove(File file) {
100: super .remove(file);
101: receive -= file.length();
102: }
103:
104: public Connection getConnection() {
105: return new ExplorerConnection(this );
106: }
107:
108: synchronized protected void fill() {
109: if (size == Long.MAX_VALUE)
110: return;
111: receive = 0;
112: super .fill();
113: }
114:
115: synchronized protected void clear() {
116: super .clear();
117: receive = 0;
118: }
119:
120: protected void clear(File file) {
121: if (file.equals(new File(dir2path())))
122: super .clear(new File(super .dir2path(), base));
123: super .clear(file);
124: }
125:
126: public String dir2path() {
127: return super .dir2path() + File.separatorChar + cache;
128: }
129:
130: public void setAttribute(String name, Object value)
131: throws Exception {
132: if (name.equals(FileServiceMBean.OPTIONS)) {
133: setSize(((ExplorerService) value).getSize());
134: setMax(((ExplorerService) value).getMax());
135: }
136: super .setAttribute(name, value);
137: }
138:
139: private boolean sel(File file, PeriodControl pc) {
140: File[] l = file.listFiles();
141: if (l == null)
142: return false;
143: for (File f : l) {
144: if (c)
145: return false;
146: Thread.yield();
147: if (f.isFile()) {
148: if (pc.check(new Date(f.lastModified())))
149: return true;
150: } else if (sel(f, pc))
151: return true;
152: }
153: return false;
154: }
155:
156: public File[] select(File file, PeriodControl pc) {
157: List<File> r = new ArrayList<File>();
158: File[] l = file.listFiles(new FileFilter() {
159: public boolean accept(File pathname) {
160: return pathname.isDirectory();
161: }
162: });
163: if (l == null)
164: return null;
165: for (File f : l) {
166: if (c)
167: return null;
168: Thread.yield();
169: if (sel(f, pc))
170: r.add(f);
171: }
172: return r.toArray(new File[] {});
173: }
174:
175: public long lastModified(File file) {
176: return file.lastModified();
177: }
178:
179: public File[] listFiles(File file, final PeriodControl pc) {
180: return file.listFiles(new FileFilter() {
181: public boolean accept(File pathname) {
182: return pathname.isFile()
183: && pc.check(new Date(pathname.lastModified()));
184: }
185: });
186: }
187: }
|