KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > AppConfig


1 package example;
2
3 import java.net.*;
4 import java.io.*;
5
6 import com.caucho.vfs.*;
7
8 public class AppConfig {
9   ConfigFilesLocation _cfl = null;
10
11   /**
12    * Set the base for subsequent call's to openConfigFileRead()
13    * and openConfigFileWrite()
14    *
15    * @param location a file path or url
16    */

17   public void setConfigFilesLocation(String JavaDoc location)
18     throws Exception JavaDoc
19   {
20     _cfl = new ConfigFilesLocation();
21     _cfl.setLocation(location);
22   }
23
24   public void init()
25     throws Exception JavaDoc
26   {
27     if (_cfl == null)
28       throw new Exception JavaDoc("'config-files-location' must be set");
29   }
30
31   /**
32    * Create and return a ReadStream for a configuration file, with
33    * the file being relative to the base previously set with
34    * setConfigFilesLocation()
35    *
36    * @return a WriteStream, which can be treated as a
37    * java.io.InputStream if desired
38    *
39    * @see java.io.InputStream
40    */

41   public ReadStream openConfigFileRead(String JavaDoc file)
42     throws IOException
43   {
44     return _cfl.openRead(file);
45   }
46
47   /**
48    * Create and return an WriteStream for a configuration file, with
49    * the file being relative to the base previously set with
50    * setConfigFilesLocation().
51    *
52    * @return a WriteStream, which can be treated as a
53    * java.io.OutputStream if desired
54    *
55    * @see java.io.OutputStream
56    */

57   public WriteStream openConfigFileWrite(String JavaDoc file)
58     throws IOException
59   {
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 JavaDoc location)
67     {
68       _path = Vfs.lookup(location);
69     }
70
71     public ReadStream openRead(String JavaDoc file)
72       throws IOException
73     {
74       Path p = _path.lookup(file);
75       return p.openRead();
76     }
77
78     public WriteStream openWrite(String JavaDoc file)
79       throws IOException
80     {
81       Path p = _path.lookup(file);
82       return p.openWrite();
83     }
84   }
85 }
86
Popular Tags