1 package dolda.jsvc.scgi;
7 import dolda.jsvc.util.*;
8 import dolda.jsvc.j2ee.PosixArgs;
10 public class DirServer extends Server {
11 private final Map<File, DSContext> contexts = new HashMap<File, DSContext>();
12 private final File datroot;
14 public DirServer(ServerSocket sk, File datroot) {
16 this.datroot = datroot;
19 private DSContext context(File file) throws ThreadContext.CreateException {
20 synchronized(contexts) {
21 DSContext ctx = contexts.get(file);
23 if(ctx.mtime < file.lastModified()) {
25 contexts.remove(file);
30 ctx = new DSContext(file, datroot);
31 contexts.put(file, ctx);
37 public void handle(Map<String, String> head, Socket sk) throws Exception {
38 String filename = head.get("SCRIPT_FILENAME");
40 throw(new Exception("Request for DirServer must contain SCRIPT_FILENAME"));
41 File file = new File(filename);
42 if(!file.exists() || !file.canRead())
43 throw(new Exception("Cannot access the requested JSvc file " + file.toString()));
44 DSContext ctx = context(file);
45 Request req = new ScgiRequest(sk, head);
46 RequestThread w = ctx.tg.respond(req);
50 private static void usage(PrintStream out) {
51 out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-e CHARSET] [-d DATADIR] PORT");
54 public static void main(String[] args) {
55 PosixArgs opt = PosixArgs.getopt(args, "h");
60 String charset = null;
62 for(char c : opt.parsed()) {
68 datroot = new File(opt.arg);
69 if(!datroot.exists() || !datroot.isDirectory()) {
70 System.err.println(opt.arg + ": no such directory");
79 if(opt.rest.length < 1) {
84 datroot = new File(System.getProperty("user.home"), ".jsvc");
85 if(!datroot.exists() || !datroot.isDirectory())
88 int port = Integer.parseInt(opt.rest[0]);
91 sk = new ServerSocket(port);
92 } catch(IOException e) {
93 System.err.println("could not bind to port " + port + ": " + e.getMessage());
95 return; /* Because javac is stupid. :-/ */
97 DirServer s = new DirServer(sk, datroot);
101 new Thread(s, "SCGI server thread").start();