Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 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;
 
 
 
 
 /* ------------------------------------------------------------ */
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 class Server extends HandlerWrapper implements Attributes
 {
     private static final Logger LOG = Log.getLogger(Server.class);
 
     private static final String __version;
     static
     {
         if (Server.class.getPackage()!=null &&
             "Eclipse.org - Jetty".equals(Server.class.getPackage().getImplementationVendor()) &&
              Server.class.getPackage().getImplementationVersion()!=null)
             =Server.class.getPackage().getImplementationVersion();
         else
             =System.getProperty("jetty.version","8.y.z-SNAPSHOT");
     }
 
     private final Container _container=new Container();
     private final AttributesMap _attributes = new AttributesMap();
     private ThreadPool _threadPool;
     private Connector[] _connectors;
     private boolean _sendServerVersion = true//send Server: header
     private boolean _sendDateHeader = false//send Date: header
     private int _graceful=0;
     private boolean _stopAtShutdown;
     private boolean _dumpAfterStart=false;
     private boolean _dumpBeforeStop=false;
     private boolean _uncheckedPrintWriter=false;
 
 
     /* ------------------------------------------------------------ */
     public Server()
     {
         setServer(this);
     }
 
     /* ------------------------------------------------------------ */
    
Convenience constructor Creates server and a org.eclipse.jetty.server.nio.SelectChannelConnector at the passed port.
 
     public Server(int port)
     {
         setServer(this);
        Connector connector=new SelectChannelConnector();
        connector.setPort(port);
        setConnectors(new Connector[]{connector});
    }
    /* ------------------------------------------------------------ */
    
Convenience constructor Creates server and a org.eclipse.jetty.server.nio.SelectChannelConnector at the passed address.
    public Server(InetSocketAddress addr)
    {
        setServer(this);
        Connector connector=new SelectChannelConnector();
        connector.setHost(addr.getHostName());
        connector.setPort(addr.getPort());
        setConnectors(new Connector[]{connector});
    }
    /* ------------------------------------------------------------ */
    public static String getVersion()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Returns:
Returns the container.
    public Container getContainer()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public boolean getStopAtShutdown()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public void setStopAtShutdown(boolean stop)
    {
        //if we now want to stop
        if (stop)
        {
            //and we weren't stopping before
            if (!)
            {  
                //only register to stop if we're already started (otherwise we'll do it in doStart())
                if (isStarted()) 
                    ShutdownThread.register(this);
            }
        }
        else
            ShutdownThread.deregister(this);
        
        =stop;
    }
    /* ------------------------------------------------------------ */
    

Returns:
Returns the connectors.
    public Connector[] getConnectors()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public void addConnector(Connector connector)
    {
        setConnectors((Connector[])LazyList.addToArray(getConnectors(), connectorConnector.class));
    }
    /* ------------------------------------------------------------ */
    
Conveniance method which calls getConnectors() and setConnectors(org.eclipse.jetty.server.Connector[]) to remove a connector.

Parameters:
connector The connector to remove.
    public void removeConnector(Connector connector) {
        setConnectors((Connector[])LazyList.removeFromArray (getConnectors(), connector));
    }
    /* ------------------------------------------------------------ */
    
Set the connectors for this server. Each connector has this server set as it's ThreadPool and its Handler.

Parameters:
connectors The connectors to set.
    public void setConnectors(Connector[] connectors)
    {
        if (connectors!=null)
        {
            for (int i=0;i<connectors.length;i++)
                connectors[i].setServer(this);
        }
        .update(thisconnectors"connector");
         = connectors;
    }
    /* ------------------------------------------------------------ */
    

Returns:
Returns the threadPool.
    public ThreadPool getThreadPool()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Parameters:
threadPool The threadPool to set.
    public void setThreadPool(ThreadPool threadPool)
    {
        if (!=null)
            removeBean();
        .update(thisthreadPool"threadpool",false);
         = threadPool;
        if (!=null)
            addBean();
    }

    
    public boolean isDumpAfterStart()
    {
        return ;
    }

    

Parameters:
dumpAfterStart true if org.eclipse.jetty.util.component.AggregateLifeCycle.dumpStdErr() is called after starting
    public void setDumpAfterStart(boolean dumpAfterStart)
    {
         = dumpAfterStart;
    }

    

Returns:
true if org.eclipse.jetty.util.component.AggregateLifeCycle.dumpStdErr() is called before stopping
    public boolean isDumpBeforeStop()
    {
        return ;
    }

    

Parameters:
dumpBeforeStop true if org.eclipse.jetty.util.component.AggregateLifeCycle.dumpStdErr() is called before stopping
    public void setDumpBeforeStop(boolean dumpBeforeStop)
    {
         = dumpBeforeStop;
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStart() throws Exception
    {
        if (getStopAtShutdown())
            ShutdownThread.register(this);
        .info("jetty-"+);
        HttpGenerator.setServerVersion();
        MultiException mex=new MultiException();
        if (==null)
            setThreadPool(new QueuedThreadPool());
        try
        {
            super.doStart();
        }
        catch(Throwable e)
        {
            mex.add(e);
        }
        if (!=null && mex.size()==0)
        {
            for (int i=0;i<.;i++)
            {
                try{[i].start();}
                catch(Throwable e)
                {
                    mex.add(e);
                }
            }
        }
        if (isDumpAfterStart())
            dumpStdErr();
        mex.ifExceptionThrow();
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        if (isDumpBeforeStop())
            dumpStdErr();
        MultiException mex=new MultiException();
        if (>0)
        {
            if (!=null)
            {
                for (int i=.;i-->0;)
                {
                    .info("Graceful shutdown {}",[i]);
                    try{[i].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];
                .info("Graceful shutdown {}",context);
                context.setShutdown(true);
            }
            Thread.sleep();
        }
        if (!=null)
        {
            for (int i=.;i-->0;)
                try{[i].stop();}catch(Throwable e){mex.add(e);}
        }
        try {super.doStop(); } catch(Throwable e) { mex.add(e);}
        mex.ifExceptionThrow();
        if (getStopAtShutdown())
            ShutdownThread.deregister(this);
    }
    /* ------------------------------------------------------------ */
    /* 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 connectionthrows IOExceptionServletException
    {
        final String target=connection.getRequest().getPathInfo();
        final Request request=connection.getRequest();
        final Response response=connection.getResponse();
        if (.isDebugEnabled())
        {
            .debug("REQUEST "+target+" on "+connection);
            handle(targetrequestrequestresponse);
            .debug("RESPONSE "+target+"  "+connection.getResponse().getStatus()+" handled="+request.isHandled());
        }
        else
            handle(targetrequestrequestresponse);
    }
    /* ------------------------------------------------------------ */
    /* 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 handleAsync(AbstractHttpConnection connectionthrows IOExceptionServletException
    {
        final AsyncContinuation async = connection.getRequest().getAsyncContinuation();
        final AsyncContinuation.AsyncEventState state = async.getAsyncEventState();
        final Request baseRequest=connection.getRequest();
        final String path=state.getPath();
        if (path!=null)
        {
            // this is a dispatch with a path
            final String contextPath=state.getServletContext().getContextPath();
            HttpURI uri = new HttpURI(URIUtil.addPaths(contextPath,path));
            baseRequest.setUri(uri);
            baseRequest.setRequestURI(null);
            baseRequest.setPathInfo(baseRequest.getRequestURI());
            if (uri.getQuery()!=null)
                baseRequest.mergeQueryString(uri.getQuery()); //we have to assume dispatch path and query are UTF8
        }
        final String target=baseRequest.getPathInfo();
        final HttpServletRequest request=(HttpServletRequest)async.getRequest();
        final HttpServletResponse response=(HttpServletResponse)async.getResponse();
        if (.isDebugEnabled())
        {
            .debug("REQUEST "+target+" on "+connection);
            handle(targetbaseRequestrequestresponse);
            .debug("RESPONSE "+target+"  "+connection.getResponse().getStatus());
        }
        else
            handle(targetbaseRequestrequestresponse);
    }
    /* ------------------------------------------------------------ */
    public void join() throws InterruptedException
    {
        getThreadPool().join();
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    

Returns:
Returns the sessionIdManager.
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    

Parameters:
sessionIdManager The sessionIdManager to set.
    public void setSessionIdManager(SessionIdManager sessionIdManager)
    {
        if (!=null)
            removeBean();
        .update(thissessionIdManager"sessionIdManager",false);
         = sessionIdManager;
        if (!=null)
            addBean();
    }
    /* ------------------------------------------------------------ */
    public void setSendServerVersion (boolean sendServerVersion)
    {
         = sendServerVersion;
    }
    /* ------------------------------------------------------------ */
    public boolean getSendServerVersion()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    

Parameters:
sendDateHeader
    public void setSendDateHeader(boolean sendDateHeader)
    {
         = sendDateHeader;
    }
    /* ------------------------------------------------------------ */
    public boolean getSendDateHeader()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    
    @Deprecated
    public int getMaxCookieVersion()
    {
        return 1;
    }
    /* ------------------------------------------------------------ */
    
    @Deprecated
    public void setMaxCookieVersion(int maxCookieVersion)
    {
    }
    /* ------------------------------------------------------------ */
    
Add a LifeCycle object to be started/stopped along with the Server.

Deprecated:
Use addBean(java.lang.Object)
Parameters:
c
    @Deprecated
    public void addLifeCycle (LifeCycle c)
    {
        addBean(c);
    }
    /* ------------------------------------------------------------ */
    
Add an associated bean. The bean will be added to the servers org.eclipse.jetty.util.component.Container and if it is a org.eclipse.jetty.util.component.LifeCycle instance, it will be started/stopped along with the Server. Any beans that are also org.eclipse.jetty.util.component.Destroyable, will be destroyed with the server.

Parameters:
o the bean object to add
    @Override
    public boolean addBean(Object o)
    {
        if (super.addBean(o))
        {
            .addBean(o);
            return true;
        }
        return false;
    }

    
Remove a LifeCycle object to be started/stopped along with the Server

    @Deprecated
    public void removeLifeCycle (LifeCycle c)
    {
        removeBean(c);
    }
    /* ------------------------------------------------------------ */
    
Remove an associated bean.
    @Override
    public boolean removeBean (Object o)
    {
        if (super.removeBean(o))
        {
            .removeBean(o);
            return true;
        }
        return false;
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.util.AttributesMap#clearAttributes()
     */
    public void clearAttributes()
    {
        .clearAttributes();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.util.AttributesMap#getAttribute(java.lang.String)
     */
    public Object getAttribute(String name)
    {
        return .getAttribute(name);
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.util.AttributesMap#getAttributeNames()
     */
    {
        return AttributesMap.getAttributeNamesCopy();
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.util.AttributesMap#removeAttribute(java.lang.String)
     */
    public void removeAttribute(String name)
    {
        .removeAttribute(name);
    }
    /* ------------------------------------------------------------ */
    /*
     * @see org.eclipse.util.AttributesMap#setAttribute(java.lang.String, java.lang.Object)
     */
    public void setAttribute(String nameObject attribute)
    {
        .setAttribute(nameattribute);
    }
    /* ------------------------------------------------------------ */
    

Returns:
the graceful
    public int getGracefulShutdown()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    
Set graceful shutdown timeout. If set, the internal doStop() method will not immediately stop the server. Instead, all Connectors will be closed so that new connections will not be accepted and all handlers that implement Server.Graceful will be put into the shutdown mode so that no new requests will be accepted, but existing requests can complete. The server will then wait the configured timeout before stopping.

Parameters:
timeoutMS the milliseconds to wait for existing request to complete before stopping the server.
    public void setGracefulShutdown(int timeoutMS)
    {
        =timeoutMS;
    }
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        return this.getClass().getName()+"@"+Integer.toHexString(hashCode());
    }
    /* ------------------------------------------------------------ */
    @Override
    public void dump(Appendable out,String indentthrows IOException
    {
        dumpThis(out);
        dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(),TypeUtil.asList());
    }
    /* ------------------------------------------------------------ */
    public boolean isUncheckedPrintWriter()
    {
        return ;
    }
    /* ------------------------------------------------------------ */
    public void setUncheckedPrintWriter(boolean unchecked)
    {
        =unchecked;
    }
    /* ------------------------------------------------------------ */
    /* 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 static void main(String...argsthrows Exception
    {
        ..println(getVersion());
    }
New to GrepCode? Check out our FAQ X