Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.sonatype.sisu.jetty;
  
  import java.io.File;
  import java.util.Map;
  
  import javax.inject.Named;
 
A simple component managing embedded Jetty life cycle, configuring Jetty using passed in jetty.xml File, interpolating it with passed in context maps, if any. Usable in cases like UT/ITs, when you can lookup this component and control the Jetty instance with it.

Author(s):
cstamas
 
 public class Jetty8
 {
     private final Server server;
 
     private final File jettyXml;
 
     private final ClassLoader classloader;
 
     private final Map<?, ?>[] contexts;
 
     private final AppContext appContext;
 
     private volatile JettyWrapperThread serverThread;
 
     public Jetty8final File jettyXmlfinal Map<?, ?>... contexts )
         throws IOException
     {
         thisjettyXml, Thread.currentThread().getContextClassLoader(), contexts );
     }
 
     public Jetty8final File jettyXmlfinal ClassLoader classloaderfinal Map<?, ?>... contexts )
         throws IOException
     {
         this. = jettyXml;
         this. = classloader;
         this. = contexts;
         this. = new Server();
         this. = JettyUtils.configureServerthis.this.this. );
     }
 
     public AppContext getAppContext()
     {
         return ;
     }
 
     public synchronized boolean isStarted()
     {
         return  != null;
     }
 
     public synchronized boolean isRunning()
     {
         return  != null && .isAlive() && .isStarted();
     }
 
     public synchronized void startJetty()
         throws Exception
     {
         if ( isStarted() )
         {
             throw new IllegalStateException"Jetty7 already started, stop if first before starting again!" );
         }
 
          = new JettyWrapperThread );
         .startJetty();
     }
 
     public synchronized void stopJetty()
         throws Exception
     {
         if ( !isStarted() )
         {
             throw new IllegalStateException"Jetty7 not started, start if first before stopping!" );
         }
 
         .stopJetty();
          = null;
     }
 
     public synchronized <T> T mangleServerfinal ServerMangler<T> mangler )
     {
        return mangler.mangle );
    }
    // ==
    public static class JettyWrapperThread
        extends Thread
    {
        private static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger( 1 );
        private final Server server;
        private final CountDownLatch started;
        private final CountDownLatch stopped;
        private volatile Exception exception;
        public JettyWrapperThreadfinal Server server )
        {
            super"jetty-main-thread-" + .getAndIncrement() );
            this. = server;
            this. = new CountDownLatch( 1 );
            this. = new CountDownLatch( 1 );
        }
        @Override
        public void run()
        {
            try
            {
                try
                {
                    .start();
                }
                finally
                {
                    .countDown();
                }
                try
                {
                    .join();
                }
                finally
                {
                    .countDown();
                }
            }
            catch ( InterruptedException e )
            {
                // nothing
            }
            catch ( Exception e )
            {
                 = e;
            }
        }
        public void startJetty()
            throws Exception
        {
            start();
            .await();
            if (  != null )
            {
                throw ;
            }
        }
        public void stopJetty()
            throws Exception
        {
            final ClassLoader original = Thread.currentThread().getContextClassLoader();
            try
            {
                Thread.currentThread().setContextClassLoadergetContextClassLoader() );
                .stop();
                .await();
                if (  != null )
                {
                    throw ;
                }
            }
            finally
            {
                Thread.currentThread().setContextClassLoaderoriginal );
            }
        }
    }
New to GrepCode? Check out our FAQ X