Coverage Report - org.jitr.container.JettyContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
JettyContainer
74%
23/31
50%
3/6
0
 
 1  
 /*
 2  
  * Copyright 2009, Josh Devins, Jitr.org.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.jitr.container;
 18  
 
 19  
 import java.io.File;
 20  
 import java.lang.annotation.Annotation;
 21  
 
 22  
 import javax.servlet.ServletContext;
 23  
 
 24  
 import org.jitr.core.Container;
 25  
 import org.jitr.core.ContainerOperationException;
 26  
 import org.jitr.core.ConfigurationModel;
 27  
 import org.mortbay.jetty.Connector;
 28  
 import org.mortbay.jetty.Server;
 29  
 import org.mortbay.jetty.nio.SelectChannelConnector;
 30  
 import org.mortbay.jetty.webapp.WebAppContext;
 31  
 
 32  
 /**
 33  
  * Jetty specific container wrapper.
 34  
  * 
 35  
  * @author Josh Devins (info@joshdevins.net)
 36  
  */
 37  0
 public class JettyContainer implements Container<Server> {
 38  
 
 39  
     private final Server server;
 40  
 
 41  4
     public JettyContainer() {
 42  4
         server = new Server();
 43  4
     }
 44  
 
 45  
     public String getName() {
 46  12
         return "Jetty";
 47  
     }
 48  
 
 49  
     public Server getWrappedContainer() {
 50  0
         return server;
 51  
     }
 52  
 
 53  
     public ServletContext initialize(final ConfigurationModel configuration,
 54  
             final Annotation[] testClassAnnotations) {
 55  
 
 56  4
         final Connector connector = new SelectChannelConnector();
 57  4
         connector.setPort(configuration.getPort());
 58  4
         server.setConnectors(new Connector[] { connector });
 59  
 
 60  4
         final WebAppContext webapp = new WebAppContext();
 61  4
         webapp.setContextPath(configuration.getContextPath());
 62  4
         webapp.setWar(configuration.getWarPath());
 63  4
         webapp.setClassLoader(Thread.currentThread().getContextClassLoader());
 64  
 
 65  4
         server.setHandler(webapp);
 66  
 
 67  4
         final File tempDirectory = new File(configuration.getContainerWorkPath());
 68  4
         webapp.setTempDirectory(tempDirectory);
 69  
 
 70  4
         return webapp.getServletContext().getContext(configuration.getContextPath());
 71  
     }
 72  
 
 73  
     public void start() throws ContainerOperationException {
 74  
         try {
 75  4
             server.start();
 76  0
         } catch (final Exception e) {
 77  0
             throw new ContainerOperationException("Error starting Jetty.", e, this);
 78  4
         }
 79  
 
 80  
         // TODO: Is this a valid/useful assertion?
 81  
         // make sure Jetty is running
 82  4
         if (!server.isRunning()) {
 83  0
             throw new ContainerOperationException("Container could not be started.", this);
 84  
         }
 85  4
     }
 86  
 
 87  
     public void stop() throws ContainerOperationException {
 88  
 
 89  
         // TODO: Could log something in this case I suspect.
 90  4
         if (server.isStopped() || server.isStopping()) {
 91  0
             return;
 92  
         }
 93  
 
 94  
         try {
 95  4
             server.stop();
 96  0
         } catch (final Exception e) {
 97  0
             throw new ContainerOperationException("Error stopping Jetty.", e, this);
 98  4
         }
 99  4
     }
 100  
 }