1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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
81
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
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 }