Created
May 28, 2012 10:53
-
-
Save vshank77/2818487 to your computer and use it in GitHub Desktop.
Tomcat7 Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <bean id="neonto.AppServer" class="com.vshank77.neonto.application.TomcatWrapper" | |
| init-method="runServer" destroy-method="stopServer"> | |
| <constructor-arg index="0" value="${neonto.appserver.port:9999}" /> | |
| <property name="appInfos"> | |
| <list> | |
| <bean class="com.vshank77.neonto.application.TomcatWrapper.AppInfo" p:context="/neontoweb" | |
| p:contextBase="neonto-web" p:active="true" /> | |
| </list> | |
| </property> | |
| </bean> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| <parent> | |
| <groupId>com.vshank77</groupId> | |
| <artifactId>neonto</artifactId> | |
| <version>1.0.0-SNAPSHOT</version> | |
| </parent> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.vshank77.neonto</groupId> | |
| <artifactId>neonto-server</artifactId> | |
| <name>Neonto application server</name> | |
| <dependencies> | |
| <dependency> | |
| <groupId>com.google.guava</groupId> | |
| <artifactId>guava</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-core</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-beans</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-context</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-web</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.tomcat</groupId> | |
| <artifactId>tomcat-catalina</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.tomcat.embed</groupId> | |
| <artifactId>tomcat-embed-core</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.tomcat</groupId> | |
| <artifactId>tomcat-jasper</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.slf4j</groupId> | |
| <artifactId>jcl-over-slf4j</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>ch.qos.logback</groupId> | |
| <artifactId>logback-classic</artifactId> | |
| </dependency> | |
| </dependencies> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-jar-plugin</artifactId> | |
| <configuration> | |
| <excludes> | |
| <exclude>**/logback.xml</exclude> | |
| </excludes> | |
| </configuration> | |
| </plugin> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-assembly-plugin</artifactId> | |
| <configuration> | |
| <descriptors> | |
| <descriptor>src/main/assembly/deploy.xml</descriptor> | |
| </descriptors> | |
| </configuration> | |
| <executions> | |
| <execution> | |
| <id>app-assembly</id> | |
| <phase>package</phase> | |
| <goals> | |
| <goal>single</goal> | |
| </goals> | |
| </execution> | |
| </executions> | |
| </plugin> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-dependency-plugin</artifactId> | |
| <executions> | |
| <execution> | |
| <id>unpack-webapps</id> | |
| <phase>generate-resources</phase> | |
| <goals> | |
| <goal>unpack</goal> | |
| </goals> | |
| <configuration> | |
| <artifactItems> | |
| <artifactItem> | |
| <groupId>com.vshank77.neonto</groupId> | |
| <artifactId>neonto-webapp</artifactId> | |
| <version>${project.version}</version> | |
| <type>war</type> | |
| <overWrite>true</overWrite> | |
| <outputDirectory>${project.build.directory}/webapps/neonto-web</outputDirectory> | |
| </artifactItem> | |
| </artifactItems> | |
| </configuration> | |
| </execution> | |
| </executions> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.vshank77.neonto.application; | |
| import static com.google.common.base.Preconditions.checkNotNull; | |
| import java.util.List; | |
| import javax.servlet.ServletException; | |
| import org.apache.catalina.Context; | |
| import org.apache.catalina.LifecycleEvent; | |
| import org.apache.catalina.LifecycleException; | |
| import org.apache.catalina.LifecycleListener; | |
| import org.apache.catalina.LifecycleState; | |
| import org.apache.catalina.core.AprLifecycleListener; | |
| import org.apache.catalina.session.StandardManager; | |
| import org.apache.catalina.startup.Tomcat; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| public class TomcatWrapper { | |
| private static final Logger logger = LoggerFactory | |
| .getLogger(TomcatWrapper.class); | |
| private final int port; | |
| private final Tomcat tomcat = new Tomcat(); | |
| private boolean initialised = false; | |
| private List<AppInfo> appInfos = null; | |
| public TomcatWrapper(int _port) { | |
| this.port = _port; | |
| tomcat.setPort(port); | |
| tomcat.setBaseDir("."); | |
| tomcat.getHost().setCreateDirs(false); | |
| tomcat.getHost().setDeployOnStartup(false); | |
| tomcat.getHost().setAutoDeploy(true); | |
| tomcat.getServer().addLifecycleListener(new AprLifecycleListener()); | |
| } | |
| public synchronized void runServer() throws LifecycleException, | |
| ServletException { | |
| checkNotNull(appInfos); | |
| for (AppInfo info : appInfos) { | |
| if(info.active) { | |
| addWebappFrom(info); | |
| } | |
| } | |
| tomcat.start(); | |
| tomcat.getServer().await(); | |
| initialised = true; | |
| } | |
| private void addWebappFrom(AppInfo info) throws ServletException { | |
| logger.info("deploying webapp at " + info.contextBase + " to " | |
| + info.context); | |
| Context ctx = tomcat.addWebapp(info.context, info.contextBase); | |
| ctx.setSessionTimeout(30); | |
| ctx.setManager(new StandardManager()); | |
| ctx.addLifecycleListener(new LifecycleListener() { | |
| @Override | |
| public void lifecycleEvent(LifecycleEvent evt) { | |
| if(LifecycleState.FAILED == evt.getLifecycle().getState()) { | |
| logger.error("SEVERE error: context initialization failed, aborting server startup.."); | |
| System.exit(-2); | |
| } | |
| } | |
| }); | |
| } | |
| public void stopServer() { | |
| if (initialised) { | |
| try { | |
| tomcat.stop(); | |
| } catch (LifecycleException ex) { | |
| logger.error(ex.getMessage()); | |
| } | |
| } | |
| } | |
| public void setAppInfos(List<AppInfo> appInfos) { | |
| this.appInfos = appInfos; | |
| } | |
| public static class AppInfo { | |
| private String context; | |
| private String contextBase; | |
| private boolean active = true; | |
| public void setContext(String context) { | |
| this.context = context; | |
| } | |
| public void setContextBase(String contextBase) { | |
| this.contextBase = contextBase; | |
| } | |
| public void setActive(boolean active) { | |
| this.active = active; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment