X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fscgi%2FDirServer.java;h=1ac452c8139bfcc010ccebc368ca4d65d5c0e897;hb=48d0e295cb79948858eb5aa99cc08cdf9f9e4546;hp=7bf064d2078c47888648da07e99ee02e6bd8c906;hpb=13e578b10b388cc0dea88e05b79265c21416e3a0;p=jsvc.git diff --git a/src/dolda/jsvc/scgi/DirServer.java b/src/dolda/jsvc/scgi/DirServer.java index 7bf064d..1ac452c 100644 --- a/src/dolda/jsvc/scgi/DirServer.java +++ b/src/dolda/jsvc/scgi/DirServer.java @@ -3,32 +3,38 @@ package dolda.jsvc.scgi; import java.io.*; import java.net.*; import java.util.*; +import java.util.logging.*; import dolda.jsvc.*; import dolda.jsvc.util.*; import dolda.jsvc.j2ee.PosixArgs; public class DirServer extends Server { private final Map contexts = new HashMap(); - private final File datroot; + private final Environment env; + private final Logger logger = Logger.getLogger("dolda.jsvc.scgi.dirserver"); + private Thread sdhook = null, main = null; - public DirServer(ServerSocket sk, File datroot) { + public DirServer(ServerSocket sk, Environment env) { super(sk); - this.datroot = datroot; + this.env = env; } private DSContext context(File file) throws ThreadContext.CreateException { synchronized(contexts) { DSContext ctx = contexts.get(file); + String act = "loaded %s as %s"; if(ctx != null) { if(ctx.mtime < file.lastModified()) { - ctx.tg.destroy(); + ctx.tg.shutdown(); contexts.remove(file); ctx = null; + act = "reloaded %s as %s"; } } if(ctx == null) { - ctx = new DSContext(file, datroot); + ctx = new DSContext(file, env); contexts.put(file, ctx); + logger.config(String.format(act, file, ctx.name())); } return(ctx); } @@ -46,6 +52,32 @@ public class DirServer extends Server { RequestThread w = ctx.tg.respond(req); w.start(); } + + private class ShutdownHandler extends Thread { + public void run() { + sdhook = null; + DirServer.this.stop(); + try { + main.join(); + } catch(InterruptedException e) {} + } + } + + protected void shutdown() { + try { + if(sdhook != null) + Runtime.getRuntime().removeShutdownHook(sdhook); + } catch(Exception e) {} + synchronized(contexts) { + for(Iterator> i = contexts.entrySet().iterator(); i.hasNext();) { + Map.Entry e = i.next(); + DSContext ctx = e.getValue(); + i.remove(); + ctx.tg.shutdown(); + } + } + super.shutdown(); + } private static void usage(PrintStream out) { out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-e CHARSET] [-d DATADIR] PORT"); @@ -80,11 +112,8 @@ public class DirServer extends Server { usage(System.err); System.exit(1); } - if(datroot == null) { - datroot = new File(System.getProperty("user.home"), ".jsvc"); - if(!datroot.exists() || !datroot.isDirectory()) - datroot = null; - } + Environment env = (datroot == null)?new Environment():new Environment(datroot); + env.initvm(); int port = Integer.parseInt(opt.rest[0]); ServerSocket sk; try { @@ -94,10 +123,12 @@ public class DirServer extends Server { System.exit(1); return; /* Because javac is stupid. :-/ */ } - DirServer s = new DirServer(sk, datroot); + DirServer s = new DirServer(sk, env); if(charset != null) s.headcs = charset; - new Thread(s, "SCGI server thread").start(); + Runtime.getRuntime().addShutdownHook(s.sdhook = s.new ShutdownHandler()); + s.main = new Thread(s, "SCGI server thread"); + s.main.start(); } }