01: package vicazh.hyperpool.stream.net.http;
02:
03: import java.io.*;
04: import vicazh.hyperpool.Reader;
05:
06: /**
07: * The cache service
08: *
09: * @author Victor Zhigunov
10: * @version 0.4.0
11: */
12: public class CacheService extends Service implements CacheServiceMBean {
13: public CacheService() {
14: }
15:
16: Reader reader;
17:
18: String cache;
19:
20: private String base;
21:
22: /**
23: * @param reader
24: * xsl source transformer
25: * @param cache
26: * cache folder name
27: * @param base
28: * base folder name
29: */
30: public CacheService(Reader reader, String cache, String base) {
31: this .reader = reader;
32: this .cache = cache;
33: this .base = base;
34: }
35:
36: private String dir;
37:
38: /**
39: * Get the files directory
40: */
41: public String getDir() {
42: return dir;
43: }
44:
45: /**
46: * Set the files directory
47: */
48: public void setDir(String dir) {
49: this .dir = dir;
50: }
51:
52: String dir2path() {
53: return dir.replace('/', File.separatorChar);
54: }
55:
56: private void path2dir(String dir) {
57: setDir(dir.replace(File.separatorChar, '/'));
58: }
59:
60: public Connection getConnection() {
61: return new CacheConnection(this );
62: }
63:
64: private long lExt = 1;
65:
66: private long lInt = 0;
67:
68: synchronized void add(long e, long i) {
69: lExt += e;
70: lInt += i;
71: sendAttribute(CacheServiceMBean.ACTION,
72: new Long[] { lInt, lExt });
73: }
74:
75: File getBase(File file) {
76: return new File(new File(dir2path(), base), file
77: .getAbsolutePath().substring(
78: new File(dir2path(), cache).getAbsolutePath()
79: .length() + 1));
80: }
81:
82: public void setAttribute(String name, Object value)
83: throws Exception {
84: if (name.equals(CacheServiceMBean.DIR)) {
85: path2dir((String) value);
86: super .setAttribute(name, value);
87: } else {
88: if (name.equals(CacheServiceMBean.ACTION)) {
89: lInt = 0;
90: lExt = 1;
91: }
92: super .setAttribute(name, new Long[] { lInt, lExt });
93: }
94: }
95:
96: public char getSeparator() {
97: return File.separatorChar;
98: }
99: }
|