/*
 * Copyright (c) 2005 Henri Sivonen
 * Copyright (c) 2007-2008 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package nu.validator.servlet;


import nu.validator.servletfilter.InboundGzipFilter;
import nu.validator.servletfilter.InboundSizeLimitFilter;

import org.apache.log4j.PropertyConfigurator;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.ajp.Ajp13SocketConnector;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.servlet.GzipFilter;

/**
 * @version $Id$
 * @author hsivonen
 */
public class Main {

    private static final long SIZE_LIMIT = Integer.parseInt(System.getProperty(
            "nu.validator.servlet.max-file-size", "2097152"));
    
    public static void main(String[] args) throws Exception {
        PropertyConfigurator.configure(System.getProperty("nu.validator.servlet.log4j-properties", "log4j.properties"));
        Server server = new Server();
        Connector connector;
        if (args.length > 0 && "ajp".equals(args[0])) {
            connector = new Ajp13SocketConnector();
            int port = Integer.parseInt(args[1]);
            connector.setPort(port);
            connector.setHost("127.0.0.1");
        } else {
            connector = new SocketConnector();
            int port = Integer.parseInt(args[0]);
            connector.setPort(port);
        }
        server.addConnector(connector);

        Context context = new Context(server, "/");
        context.addFilter(new FilterHolder(new GzipFilter()), "/*", Handler.REQUEST);
        context.addFilter(new FilterHolder(new InboundSizeLimitFilter(SIZE_LIMIT)), "/*", Handler.REQUEST);
        context.addFilter(new FilterHolder(new InboundGzipFilter()), "/*", Handler.REQUEST);
        context.addFilter(new FilterHolder(new MultipartFormDataFilter()), "/*", Handler.REQUEST);
        context.addServlet(new ServletHolder(new VerifierServlet()), "/*");
        server.start();
                
        System.in.read(); // XXX do something smarter
        server.stop();
    }
}
@@ -1202,16 +1202,191 @@
 vlet;%0A%0A%0A
+import java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.net.ConnectException;%0Aimport java.net.InetAddress;%0Aimport java.net.ServerSocket;%0Aimport java.net.Socket;%0A%0A
 import n
@@ -2376,16 +2376,43 @@
 nector;%0A
+        int stopPort = -1;%0A
         
@@ -2638,24 +2638,127 @@
 27.0.0.1%22);%0A
+            if (args.length %3E 2) %7B%0A                stopPort = Integer.parseInt(args%5B2%5D);%0A            %7D%0A
         %7D el
@@ -2888,32 +2888,135 @@
 .setPort(port);%0A
+            if (args.length %3E 1) %7B%0A                stopPort = Integer.parseInt(args%5B1%5D);%0A            %7D%0A
         %7D%0A      
@@ -3564,32 +3564,446 @@
  %22/*%22);%0A        
+%0A        if (stopPort != -1) %7B%0A            try %7B%0A                Socket clientSocket = new Socket(%0A                        InetAddress.getByName(%22127.0.0.1%22), stopPort);%0A                InputStream in = clientSocket.getInputStream();%0A                in.read();%0A                in.close();%0A                clientSocket.close();%0A            %7D catch (ConnectException e) %7B%0A                %0A            %7D%0A%0A            
 server.start();%0A
@@ -4018,75 +4018,446 @@
     
+%0A 
     
-%0A
        
- System.in.read(); // XXX do something smarter%0A
+ServerSocket serverSocket = new ServerSocket(stopPort, 0, InetAddress.getByName(%22127.0.0.1%22));%0A            Socket s = serverSocket.accept();%0A            %0A            server.stop();%0A            %0A            OutputStream out = s.getOutputStream();%0A            out.close();%0A            s.close();%0A            serverSocket.close();%0A        %7D else %7B%0A            server.start();%0A            System.in.read();            %0A  
         
 serv
@@ -4444,32 +4444,34 @@
      %0A          
+  
 server.stop();%0A 
@@ -4465,19 +4465,29 @@
 ver.stop();%0A
+        %7D%0A
     %7D%0A%7D

Make the server able to stop its previous self