5049d266d5e5d36c6221e0bd5d9f35ab71fad054
[jsvc.git] / src / dolda / jsvc / j2ee / J2eeContext.java
1 package dolda.jsvc.j2ee;
2
3 import dolda.jsvc.*;
4 import dolda.jsvc.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import java.util.*;
8 import java.io.*;
9
10 public 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 String config(String key) {
57         return((String)config.get(key));
58     }
59     
60     public ServletConfig j2eeconfig() {
61         return(sc);
62     }
63 }