Coverage Report - org.jitr.container.TomcatContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
TomcatContainer
81%
26/32
N/A
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.lang.annotation.Annotation;
 20  
 
 21  
 import javax.servlet.ServletContext;
 22  
 
 23  
 import org.apache.catalina.Engine;
 24  
 import org.apache.catalina.Host;
 25  
 import org.apache.catalina.LifecycleException;
 26  
 import org.apache.catalina.Server;
 27  
 import org.apache.catalina.connector.Connector;
 28  
 import org.apache.catalina.core.StandardContext;
 29  
 import org.apache.catalina.startup.Embedded;
 30  
 import org.jitr.core.Container;
 31  
 import org.jitr.core.ContainerOperationException;
 32  
 import org.jitr.core.ConfigurationModel;
 33  
 
 34  
 /**
 35  
  * Tomcat specific container wrapper.
 36  
  * 
 37  
  * @author Josh Devins (info@joshdevins.net)
 38  
  */
 39  0
 public class TomcatContainer implements Container<Server> {
 40  
 
 41  
     private static final String NAME = "Tomcat";
 42  
 
 43  
     private static final String LOCALHOST = "localhost";
 44  
 
 45  
     private final Embedded embedded;
 46  
 
 47  
     private final Host host;
 48  
 
 49  1
     public TomcatContainer() {
 50  1
         embedded = new Embedded();
 51  1
         embedded.setName(NAME);
 52  1
         embedded.setCatalinaBase("");
 53  
 
 54  
         // create engine
 55  1
         Engine engine = embedded.createEngine();
 56  1
         engine.setDefaultHost(LOCALHOST);
 57  1
         embedded.addEngine(engine);
 58  
 
 59  
         // add host with default webapps path
 60  1
         host = embedded.createHost(LOCALHOST, "");
 61  1
         engine.addChild(host);
 62  1
     }
 63  
 
 64  
     public String getName() {
 65  3
         return NAME;
 66  
     }
 67  
 
 68  
     public Server getWrappedContainer() {
 69  0
         return embedded.getServer();
 70  
     }
 71  
 
 72  
     public ServletContext initialize(final ConfigurationModel configuration,
 73  
             final Annotation[] testClassAnnotations) {
 74  
 
 75  
         // add connector
 76  1
         Connector connector = embedded.createConnector(LOCALHOST, configuration.getPort(), false);
 77  1
         connector.setUseBodyEncodingForURI(true);
 78  1
         connector.setURIEncoding("UTF-8");
 79  1
         embedded.addConnector(connector);
 80  
 
 81  
         // add specific war context
 82  1
         StandardContext context =
 83  
                 (StandardContext) embedded.createContext(configuration.getContextPath(),
 84  
                         configuration.getWarPath());
 85  1
         context.setReloadable(false);
 86  1
         host.addChild(context);
 87  
 
 88  1
         context.setWorkDir(configuration.getContainerWorkPath());
 89  
 
 90  1
         return context.getServletContext();
 91  
     }
 92  
 
 93  
     public void start() throws ContainerOperationException {
 94  
         try {
 95  1
             embedded.start();
 96  0
         } catch (final LifecycleException le) {
 97  0
             throw new ContainerOperationException("Error starting Tomcat.", le, this);
 98  1
         }
 99  1
     }
 100  
 
 101  
     public void stop() throws ContainerOperationException {
 102  
         try {
 103  1
             embedded.stop();
 104  0
         } catch (final LifecycleException le) {
 105  0
             throw new ContainerOperationException("Error stopping Tomcat.", le, this);
 106  1
         }
 107  1
     }
 108  
 }