View Javadoc

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  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      public TomcatContainer() {
50          embedded = new Embedded();
51          embedded.setName(NAME);
52          embedded.setCatalinaBase("");
53  
54          // create engine
55          Engine engine = embedded.createEngine();
56          engine.setDefaultHost(LOCALHOST);
57          embedded.addEngine(engine);
58  
59          // add host with default webapps path
60          host = embedded.createHost(LOCALHOST, "");
61          engine.addChild(host);
62      }
63  
64      public String getName() {
65          return NAME;
66      }
67  
68      public Server getWrappedContainer() {
69          return embedded.getServer();
70      }
71  
72      public ServletContext initialize(final ConfigurationModel configuration,
73              final Annotation[] testClassAnnotations) {
74  
75          // add connector
76          Connector connector = embedded.createConnector(LOCALHOST, configuration.getPort(), false);
77          connector.setUseBodyEncodingForURI(true);
78          connector.setURIEncoding("UTF-8");
79          embedded.addConnector(connector);
80  
81          // add specific war context
82          StandardContext context =
83                  (StandardContext) embedded.createContext(configuration.getContextPath(),
84                          configuration.getWarPath());
85          context.setReloadable(false);
86          host.addChild(context);
87  
88          context.setWorkDir(configuration.getContainerWorkPath());
89  
90          return context.getServletContext();
91      }
92  
93      public void start() throws ContainerOperationException {
94          try {
95              embedded.start();
96          } catch (final LifecycleException le) {
97              throw new ContainerOperationException("Error starting Tomcat.", le, this);
98          }
99      }
100 
101     public void stop() throws ContainerOperationException {
102         try {
103             embedded.stop();
104         } catch (final LifecycleException le) {
105             throw new ContainerOperationException("Error stopping Tomcat.", le, this);
106         }
107     }
108 }