| 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 | 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 | |
|
| 81 | |
|
| 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 | |
|
| 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 | |
} |