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.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  public class JettyContainer implements Container<Server> {
38  
39      private final Server server;
40  
41      public JettyContainer() {
42          server = new Server();
43      }
44  
45      public String getName() {
46          return "Jetty";
47      }
48  
49      public Server getWrappedContainer() {
50          return server;
51      }
52  
53      public ServletContext initialize(final ConfigurationModel configuration,
54              final Annotation[] testClassAnnotations) {
55  
56          final Connector connector = new SelectChannelConnector();
57          connector.setPort(configuration.getPort());
58          server.setConnectors(new Connector[] { connector });
59  
60          final WebAppContext webapp = new WebAppContext();
61          webapp.setContextPath(configuration.getContextPath());
62          webapp.setWar(configuration.getWarPath());
63          webapp.setClassLoader(Thread.currentThread().getContextClassLoader());
64  
65          server.setHandler(webapp);
66  
67          final File tempDirectory = new File(configuration.getContainerWorkPath());
68          webapp.setTempDirectory(tempDirectory);
69  
70          return webapp.getServletContext().getContext(configuration.getContextPath());
71      }
72  
73      public void start() throws ContainerOperationException {
74          try {
75              server.start();
76          } catch (final Exception e) {
77              throw new ContainerOperationException("Error starting Jetty.", e, this);
78          }
79  
80          // TODO: Is this a valid/useful assertion?
81          // make sure Jetty is running
82          if (!server.isRunning()) {
83              throw new ContainerOperationException("Container could not be started.", this);
84          }
85      }
86  
87      public void stop() throws ContainerOperationException {
88  
89          // TODO: Could log something in this case I suspect.
90          if (server.isStopped() || server.isStopping()) {
91              return;
92          }
93  
94          try {
95              server.stop();
96          } catch (final Exception e) {
97              throw new ContainerOperationException("Error stopping Jetty.", e, this);
98          }
99      }
100 }