01: package example;
02:
03: import javax.annotation.*;
04: import java.net.*;
05: import java.io.*;
06:
07: import com.caucho.vfs.*;
08:
09: /**
10: * AppConfig is a singleton <bean> service containing configuration.
11: */
12: public class AppConfig {
13: ConfigFilesLocation _cfl = null;
14:
15: /**
16: * Set the base for subsequent call's to openConfigFileRead()
17: * and openConfigFileWrite()
18: *
19: * @param location a file path or url
20: */
21: public void setConfigFilesLocation(String location)
22: throws Exception {
23: _cfl = new ConfigFilesLocation();
24: _cfl.setLocation(location);
25: }
26:
27: @PostConstruct
28: public void init() throws Exception {
29: if (_cfl == null)
30: throw new Exception("'config-files-location' must be set");
31: }
32:
33: /**
34: * Create and return a ReadStream for a configuration file, with
35: * the file being relative to the base previously set with
36: * setConfigFilesLocation()
37: *
38: * @return a WriteStream, which can be treated as a
39: * java.io.InputStream if desired
40: *
41: * @see java.io.InputStream
42: */
43: public ReadStream openConfigFileRead(String file)
44: throws IOException {
45: return _cfl.openRead(file);
46: }
47:
48: /**
49: * Create and return an WriteStream for a configuration file, with
50: * the file being relative to the base previously set with
51: * setConfigFilesLocation().
52: *
53: * @return a WriteStream, which can be treated as a
54: * java.io.OutputStream if desired
55: *
56: * @see java.io.OutputStream
57: */
58: public WriteStream openConfigFileWrite(String file)
59: throws IOException {
60: return _cfl.openWrite(file);
61: }
62:
63: public static class ConfigFilesLocation {
64: Path _path; // com.caucho.vfs.Path
65:
66: public void setLocation(String location) {
67: _path = Vfs.lookup().lookup(location);
68: }
69:
70: public ReadStream openRead(String file) throws IOException {
71: Path p = _path.lookup(file);
72:
73: if (!p.getFullPath().startsWith(_path.getFullPath()))
74: throw new IllegalStateException();
75:
76: return p.openRead();
77: }
78:
79: public WriteStream openWrite(String file) throws IOException {
80: Path p = _path.lookup(file);
81:
82: if (!p.getFullPath().startsWith(_path.getFullPath()))
83: throw new IllegalStateException();
84:
85: return p.openWrite();
86: }
87: }
88: }
|