Added initial SCGI server and a handler for serving JARs from the filesystem.
[jsvc.git] / src / dolda / jsvc / scgi / DSContext.java
1 package dolda.jsvc.scgi;
2
3 import java.io.*;
4 import dolda.jsvc.*;
5 import dolda.jsvc.util.*;
6
7 public class DSContext extends JarContext {
8     public final long mtime;
9     private final File datroot;
10     public final ThreadContext tg;
11
12     public DSContext(File jar, File datroot) throws ThreadContext.CreateException {
13         super(jar);
14         this.mtime = jar.lastModified();
15         this.datroot = datroot;
16         loadconfig();
17         this.tg = ThreadContext.create(this, loader);
18     }
19     
20     private void loadconfig() {
21         if(datroot != null) {
22             File sroot = new File(new File(datroot, "store"), name());
23             sysconfig.put("jsvc.storage", "file:" + sroot.getPath());
24             File conf = new File(datroot, "jsvc.properties");
25             if(conf.exists()) {
26                 try {
27                     InputStream in = new FileInputStream(conf);
28                     try {
29                         sysconfig.load(in);
30                     } finally {
31                         in.close();
32                     }
33                 } catch(IOException e) {
34                     throw(new RuntimeException(e));
35                 }
36             }
37         }
38     }
39     
40     public RequestThread worker(Responder root, Request req, ThreadGroup tg, String name) {
41         java.net.Socket sk = ((ScgiRequest)req).sk;
42         if(req.path().equals("")) {
43             return(new ScgiReqThread(new RootRedirect(""), req, tg, name, sk));
44         } else {
45             return(new ScgiReqThread(root, RequestWrap.chpath(req, req.path().substring(1)), tg, name, sk));
46         }
47     }
48 }