RequestThread w = ctx.tg.respond(req);
w.start();
}
+
+ protected void shutdown() {
+ synchronized(contexts) {
+ for(Iterator<Map.Entry<File, DSContext>> i = contexts.entrySet().iterator(); i.hasNext();) {
+ Map.Entry<File, DSContext> e = i.next();
+ DSContext ctx = e.getValue();
+ i.remove();
+ ctx.tg.shutdown();
+ }
+ }
+ }
private static void usage(PrintStream out) {
out.println("usage: dolda.jsvc.scgi.DirServer [-h] [-e CHARSET] [-d DATADIR] PORT");
public abstract class Server implements Runnable {
private final ServerSocket sk;
private final Logger logger = Logger.getLogger("dolda.jsvc.scgi");
+ private boolean running = false;
public String headcs = "UTF-8";
public Server(ServerSocket sk) {
public void run() {
try {
try {
- while(true) {
+ synchronized(this) {
+ if(running)
+ throw(new IllegalStateException("SCGI server is already running"));
+ running = true;
+ }
+ while(running) {
Socket nsk = sk.accept();
serve(nsk);
}
sk.close();
}
} catch(IOException e) {
- logger.log(Level.SEVERE, "SCGI server encountered I/O error", e);
+ if((e instanceof SocketException) && !running) {
+ /* Assume that stop() has closed the socket. */
+ } else {
+ logger.log(Level.SEVERE, "SCGI server encountered I/O error", e);
+ }
+ } finally {
+ shutdown();
+ running = false;
}
}
+
+ public void stop() {
+ try {
+ running = false;
+ sk.close();
+ } catch(IOException e) {
+ throw(new RuntimeException(e));
+ }
+ }
+
+ protected void shutdown() {
+ }
}