Added some possibilities for local configuration.
[jsvc.git] / src / dolda / jsvc / j2ee / J2eeContext.java
... / ...
CommitLineData
1package dolda.jsvc.j2ee;
2
3import dolda.jsvc.*;
4import dolda.jsvc.util.*;
5import javax.servlet.*;
6import javax.servlet.http.*;
7import java.util.*;
8import java.io.*;
9
10public class J2eeContext implements ServerContext {
11 private final ServletConfig sc;
12 private final long ctime;
13 private final Properties config;
14
15 J2eeContext(ServletConfig sc) {
16 this.sc = sc;
17 this.ctime = System.currentTimeMillis();
18 config = readconfig(sc);
19 }
20
21 private static void loadprops(Properties props, File pfile) {
22 if(!pfile.exists())
23 return;
24 try {
25 InputStream in = new FileInputStream(pfile);
26 try {
27 props.load(in);
28 } finally {
29 in.close();
30 }
31 } catch(IOException e) {
32 throw(new RuntimeException(e));
33 }
34 }
35
36 private static Properties readconfig(ServletConfig sc) {
37 /* This only works on Tomcat now, but that's because I've no
38 * idea how other J2EE servers work. I don't even know if they
39 * _have_ any reasonable place to put configuration and
40 * storage. */
41 Properties cfg = new Properties();
42 String basename = System.getProperty("catalina.base");
43 if(basename != null) {
44 File base = new File(basename);
45 cfg.put("jsvc.storage", new File(new File(base, "work"), "jsvc").getPath());
46 File cdir = new File(base, "conf");
47 loadprops(cfg, new File(cdir, "jsvc.properties"));
48 }
49 return(cfg);
50 }
51
52 public long starttime() {
53 return(ctime);
54 }
55
56 public Properties config() {
57 return(config);
58 }
59
60 public ServletConfig j2eeconfig() {
61 return(sc);
62 }
63}