view src/org/eclipse/jetty/server/Server.java @ 1054:87275900646e

remove JBuffer.toString()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 01:03:02 -0700
parents 3fa54d9d19cd
children
line wrap: on
line source

//
//  ========================================================================
//  Copyright (c) 1995-2014 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
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;

import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.nio.BlockingChannelConnector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.component.Destroyable;
import org.eclipse.jetty.util.component.LifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* ------------------------------------------------------------ */
/** Jetty HTTP Servlet Server.
 * This class is the main class for the Jetty HTTP Servlet server.
 * It aggregates Connectors (HTTP request receivers) and request Handlers.
 * The server is itself a handler and a ThreadPool.  Connectors use the ThreadPool methods
 * to run jobs that will eventually call the handle method.
 *
 *  @org.apache.xbean.XBean  description="Creates an embedded Jetty web server"
 */
public final class Server extends HandlerWrapper
{
	private static final Logger LOG = LoggerFactory.getLogger(Server.class);

	public static final String version = "8";

	public final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(256, 256, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
	final List<Connector> connectors = new ArrayList<Connector>();


	public Server() {
		setServer(this);
	}

	public Server(int port)
	{
		setServer(this);
//		new SelectChannelConnector(this,port);
		new BlockingChannelConnector(this,port);
	}

	@Override
	protected void doStart() throws Exception
	{
		LOG.info("jetty-"+version);
		
		MultiException mex=new MultiException();

		try
		{
			super.doStart();
		}
		catch(Throwable e)
		{
			mex.add(e);
		}

		if (mex.size()==0) {
			for( Connector connector : connectors ) {
				try{connector.start();}
				catch(Throwable e)
				{
					mex.add(e);
				}
			}
		}
		mex.ifExceptionThrow();
	}

	@Override
	protected void doStop() throws Exception
	{
		MultiException mex=new MultiException();
/*
		if (_graceful>0)
		{
			LOG.info("Graceful shutdown {}",connector);
			try{connector.close();}catch(Throwable e){mex.add(e);}

			Handler[] contexts = getChildHandlersByClass(Graceful.class);
			for (int c=0;c<contexts.length;c++)
			{
				Graceful context=(Graceful)contexts[c];
				LOG.info("Graceful shutdown {}",context);
				context.setShutdown(true);
			}
			Thread.sleep(_graceful);
		}
*/
		for( Connector connector : connectors ) {
			try{connector.stop();}catch(Throwable e){mex.add(e);}
		}

		threadPool.shutdownNow();

		try {super.doStop(); } catch(Throwable e) { mex.add(e);}

		mex.ifExceptionThrow();
	}

	/* ------------------------------------------------------------ */
	/* Handle a request from a connection.
	 * Called to handle a request on the connection when either the header has been received,
	 * or after the entire request has been received (for short requests of known length), or
	 * on the dispatch of an async request.
	 */
	public void handle(AbstractHttpConnection connection) throws IOException, ServletException
	{
		final String target = connection.getRequest().getPathInfo();
		final Request request = connection.getRequest();
		final Response response = connection.getResponse();
		handle(target, request, response);
	}
/*
	public void join() throws InterruptedException
	{
		threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
	}
*/

	@Override
	public void dump(Appendable out,String indent) throws IOException
	{
		dumpThis(out);
		dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(),connectors);
	}


	/* ------------------------------------------------------------ */
	/* A handler that can be gracefully shutdown.
	 * Called by doStop if a {@link #setGracefulShutdown} period is set.
	 * TODO move this somewhere better
	 */
/*
	public interface Graceful extends Handler
	{
		public void setShutdown(boolean shutdown);
	}
*/

	public final boolean isLowOnThreads()
	{
		// getActiveCount() locks the thread pool, so execute it last
		return threadPool.getPoolSize() == threadPool.getMaximumPoolSize() &&
				threadPool.getQueue().size() >= threadPool.getPoolSize() - threadPool.getActiveCount();
	}


	public static void main(String...args) throws Exception
	{
		System.err.println(version);
	}
}