1 package dolda.jsvc.scgi;
6 import java.util.logging.*;
8 import dolda.jsvc.util.*;
9 import dolda.jsvc.j2ee.PosixArgs;
11 public class DirServer extends Server {
12 private final Map<File, DSContext> contexts = new HashMap<File, DSContext>();
13 private final Environment env;
14 private final Logger logger = Logger.getLogger("dolda.jsvc.scgi.dirserver");
16 public DirServer(ServerSocket sk, Environment env) {
21 private DSContext context(File file) throws ThreadContext.CreateException {
22 synchronized(contexts) {
23 DSContext ctx = contexts.get(file);
24 String act = "loaded %s as %s";
26 if(ctx.mtime < file.lastModified()) {
28 contexts.remove(file);
30 act = "reloaded %s as %s";
34 ctx = new DSContext(file, env);
35 contexts.put(file, ctx);
36 logger.config(String.format(act, file, ctx.name()));
42 public void handle(Map<String, String> head, Socket sk) throws Exception {
43 String filename = head.get("SCRIPT_FILENAME");
45 throw(new Exception("Request for DirServer must contain SCRIPT_FILENAME"));
46 File file = new File(filename);
47 if(!file.exists() || !file.canRead())
48 throw(new Exception("Cannot access the requested JSvc file " + file.toString()));
49 DSContext ctx = context(file);
50 Request req = new ScgiRequest(sk, head);
51 RequestThread w = ctx.tg.respond(req);
55 private static void usage(PrintStream out) {
56 out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-e CHARSET] [-d DATADIR] PORT");
59 public static void main(String[] args) {
60 PosixArgs opt = PosixArgs.getopt(args, "h");
65 String charset = null;
67 for(char c : opt.parsed()) {
73 datroot = new File(opt.arg);
74 if(!datroot.exists() || !datroot.isDirectory()) {
75 System.err.println(opt.arg + ": no such directory");
84 if(opt.rest.length < 1) {
88 Environment env = (datroot == null)?new Environment():new Environment(datroot);
90 int port = Integer.parseInt(opt.rest[0]);
93 sk = new ServerSocket(port);
94 } catch(IOException e) {
95 System.err.println("could not bind to port " + port + ": " + e.getMessage());
97 return; /* Because javac is stupid. :-/ */
99 DirServer s = new DirServer(sk, env);
103 new Thread(s, "SCGI server thread").start();