| 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.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 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 55 | 1 | Engine engine = embedded.createEngine(); |
| 56 | 1 | engine.setDefaultHost(LOCALHOST); |
| 57 | 1 | embedded.addEngine(engine); |
| 58 | |
|
| 59 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |