]> WPIA git - gigi.git/blobdiff - lib/jetty/org/eclipse/jetty/server/Server.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / server / Server.java
index b1437d6d98db29451bc23caa67fa80a8d7c4baa7..ba0655e5ef8782839f6dc644f3f24107c8a0ca2a 100644 (file)
@@ -1,6 +1,6 @@
 //
 //  ========================================================================
-//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
+//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
 //  ------------------------------------------------------------------------
 //  All rights reserved. This program and the accompanying materials
 //  are made available under the terms of the Eclipse Public License v1.0
@@ -19,7 +19,6 @@
 package org.eclipse.jetty.server;
 
 import java.io.IOException;
-import java.lang.management.ManagementFactory;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.URI;
@@ -32,6 +31,7 @@ import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -46,11 +46,13 @@ import org.eclipse.jetty.http.HttpStatus;
 import org.eclipse.jetty.http.HttpURI;
 import org.eclipse.jetty.server.handler.ContextHandler;
 import org.eclipse.jetty.server.handler.HandlerWrapper;
+import org.eclipse.jetty.server.handler.StatisticsHandler;
 import org.eclipse.jetty.util.Attributes;
 import org.eclipse.jetty.util.AttributesMap;
 import org.eclipse.jetty.util.Jetty;
 import org.eclipse.jetty.util.MultiException;
 import org.eclipse.jetty.util.URIUtil;
+import org.eclipse.jetty.util.Uptime;
 import org.eclipse.jetty.util.annotation.ManagedAttribute;
 import org.eclipse.jetty.util.annotation.ManagedObject;
 import org.eclipse.jetty.util.annotation.Name;
@@ -141,8 +143,28 @@ public class Server extends HandlerWrapper implements Attributes
     {
         return _stopAtShutdown;
     }
+   
+    
+    /* ------------------------------------------------------------ */
+    /**
+     * Set a graceful stop time.
+     * The {@link StatisticsHandler} must be configured so that open connections can
+     * be tracked for a graceful shutdown.
+     * @see org.eclipse.jetty.util.component.ContainerLifeCycle#setStopTimeout(long)
+     */
+    @Override
+    public void setStopTimeout(long stopTimeout)
+    {
+        super.setStopTimeout(stopTimeout);
+    }
 
     /* ------------------------------------------------------------ */
+    /** Set stop server at shutdown behaviour.
+     * @param stop If true, this server instance will be explicitly stopped when the
+     * JVM is shutdown. Otherwise the JVM is stopped with the server running.
+     * @see Runtime#addShutdownHook(Thread)
+     * @see ShutdownThread
+     */
     public void setStopAtShutdown(boolean stop)
     {
         //if we now want to stop
@@ -290,11 +312,16 @@ public class Server extends HandlerWrapper implements Attributes
     @Override
     protected void doStart() throws Exception
     {
+        //If the Server should be stopped when the jvm exits, register
+        //with the shutdown handler thread.
         if (getStopAtShutdown())
-        {
             ShutdownThread.register(this);
-        }
 
+        //Register the Server with the handler thread for receiving
+        //remote stop commands
+        ShutdownMonitor.register(this);
+        
+        //Start a thread waiting to receive "stop" commands.
         ShutdownMonitor.getInstance().start(); // initialize
 
         LOG.info("jetty-" + getVersion());
@@ -304,20 +331,23 @@ public class Server extends HandlerWrapper implements Attributes
         // check size of thread pool
         SizedThreadPool pool = getBean(SizedThreadPool.class);
         int max=pool==null?-1:pool.getMaxThreads();
-        int needed=1;
+        int selectors=0;
+        int acceptors=0;
         if (mex.size()==0)
         {
             for (Connector connector : _connectors)
             {
                 if (connector instanceof AbstractConnector)
-                    needed+=((AbstractConnector)connector).getAcceptors();
+                    acceptors+=((AbstractConnector)connector).getAcceptors();
+                    
                 if (connector instanceof ServerConnector)
-                    needed+=((ServerConnector)connector).getSelectorManager().getSelectorCount();
+                    selectors+=((ServerConnector)connector).getSelectorManager().getSelectorCount();
             }
         }
 
+        int needed=1+selectors+acceptors;
         if (max>0 && needed>max)
-            throw new IllegalStateException("Insufficient max threads in ThreadPool: max="+max+" < needed="+needed);
+            throw new IllegalStateException(String.format("Insufficient threads: max=%d < needed(acceptors=%d + selectors=%d + request=1)",max,acceptors,selectors));
         
         try
         {
@@ -346,7 +376,7 @@ public class Server extends HandlerWrapper implements Attributes
 
         mex.ifExceptionThrow();
 
-        LOG.info(String.format("Started @%dms",ManagementFactory.getRuntimeMXBean().getUptime()));
+        LOG.info(String.format("Started @%dms",Uptime.getUptime()));
     }
 
     @Override
@@ -384,7 +414,8 @@ public class Server extends HandlerWrapper implements Attributes
         if (stopTimeout>0)
         {
             long stop_by=System.currentTimeMillis()+stopTimeout;
-            LOG.debug("Graceful shutdown {} by ",this,new Date(stop_by));
+            if (LOG.isDebugEnabled())
+                LOG.debug("Graceful shutdown {} by ",this,new Date(stop_by));
 
             // Wait for shutdowns
             for (Future<Void> future: futures)
@@ -396,7 +427,7 @@ public class Server extends HandlerWrapper implements Attributes
                 }
                 catch (Exception e)
                 {
-                    mex.add(e.getCause());
+                    mex.add(e);
                 }
             }
         }
@@ -431,6 +462,11 @@ public class Server extends HandlerWrapper implements Attributes
 
         if (getStopAtShutdown())
             ShutdownThread.deregister(this);
+        
+        //Unregister the Server with the handler thread for receiving
+        //remote stop commands as we are stopped already
+        ShutdownMonitor.deregister(this);
+        
 
         mex.ifExceptionThrow();
 
@@ -449,10 +485,12 @@ public class Server extends HandlerWrapper implements Attributes
         final Response response=connection.getResponse();
 
         if (LOG.isDebugEnabled())
-            LOG.debug(request.getDispatcherType()+" "+target+" on "+connection);
+            LOG.debug(request.getDispatcherType()+" "+request.getMethod()+" "+target+" on "+connection);
 
-        if ("*".equals(target))
+        if (HttpMethod.OPTIONS.is(request.getMethod()) || "*".equals(target))
         {
+            if (!HttpMethod.OPTIONS.is(request.getMethod()))
+                response.sendError(HttpStatus.BAD_REQUEST_400);
             handleOptions(request,response);
             if (!request.isHandled())
                 handle(target, request, request, response);
@@ -469,13 +507,6 @@ public class Server extends HandlerWrapper implements Attributes
      */
     protected void handleOptions(Request request,Response response) throws IOException
     {
-        if (!HttpMethod.OPTIONS.is(request.getMethod()))
-            response.sendError(HttpStatus.BAD_REQUEST_400);
-        request.setHandled(true);
-        response.setStatus(200);
-        response.getHttpFields().put(HttpHeader.ALLOW,"GET,POST,HEAD,OPTIONS");
-        response.setContentLength(0);
-        response.closeOutput();
     }
 
     /* ------------------------------------------------------------ */
@@ -491,15 +522,15 @@ public class Server extends HandlerWrapper implements Attributes
 
         final Request baseRequest=connection.getRequest();
         final String path=event.getPath();
-
+        
         if (path!=null)
         {
             // this is a dispatch with a path
             ServletContext context=event.getServletContext();
-            HttpURI uri = new HttpURI(context==null?path:URIUtil.addPaths(context.getContextPath(),path));
+            HttpURI uri = new HttpURI(URIUtil.addPaths(context==null?null:context.getContextPath(), path));            
             baseRequest.setUri(uri);
             baseRequest.setRequestURI(null);
-            baseRequest.setPathInfo(baseRequest.getRequestURI());
+            baseRequest.setPathInfo(uri.getDecodedPath());
             if (uri.getQuery()!=null)
                 baseRequest.mergeQueryParameters(uri.getQuery(), true); //we have to assume dispatch path and query are UTF8
         }
@@ -510,7 +541,7 @@ public class Server extends HandlerWrapper implements Attributes
 
         if (LOG.isDebugEnabled())
         {
-            LOG.debug(request.getDispatcherType()+" "+target+" on "+connection);
+            LOG.debug(request.getDispatcherType()+" "+request.getMethod()+" "+target+" on "+connection);
             handle(target, baseRequest, request, response);
             LOG.debug("RESPONSE "+target+"  "+connection.getResponse().getStatus());
         }
@@ -605,6 +636,7 @@ public class Server extends HandlerWrapper implements Attributes
     /**
      * @return The URI of the first {@link NetworkConnector} and first {@link ContextHandler}, or null
      */
+    @SuppressWarnings("resource")
     public URI getURI()
     {
         NetworkConnector connector=null;
@@ -651,6 +683,7 @@ public class Server extends HandlerWrapper implements Attributes
         return this.getClass().getName()+"@"+Integer.toHexString(hashCode());
     }
 
+    /* ------------------------------------------------------------ */
     @Override
     public void dump(Appendable out,String indent) throws IOException
     {