This is an automated email from the ASF dual-hosted git repository. bhaisaab pushed a commit to branch CLOUDSTACK-10012 in repository https://gitbox.apache.org/repos/asf/cloudstack.git
commit 8abc75f2f50a02d0b7c2a8279d349c8da86391fd Author: Marc-Aurèle Brothier <[email protected]> AuthorDate: Fri Jul 28 08:17:56 2017 +0200 Inception for backport jetty embedded with shaded jar --- client/pom.xml | 233 ++++++++++++++++----- client/src/org/apache/cloudstack/ServerDaemon.java | 205 ++++++++++++++++++ 2 files changed, 381 insertions(+), 57 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index 9b8fa38..9b0df82 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -13,7 +13,7 @@ <modelVersion>4.0.0</modelVersion> <artifactId>cloud-client-ui</artifactId> <name>Apache CloudStack Client UI</name> - <packaging>war</packaging> + <packaging>jar</packaging> <parent> <groupId>org.apache.cloudstack</groupId> <artifactId>cloudstack</artifactId> @@ -29,184 +29,257 @@ <dependencies> <dependency> + <groupId>javax.servlet</groupId> + <artifactId>javax.servlet-api</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-server</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-servlet</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-servlets</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-jmx</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-util</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-webapp</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-annotations</artifactId> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-spring-module</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-spring-lifecycle</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-volume-solidfire</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-volume-cloudbyte</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-server</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-acl-static-role-based</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-acl-dynamic-role-based</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-dedicated-resources</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-api-limit-account-based</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-api-discovery</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-ldap</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-md5</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-pbkdf2</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-plaintext</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-saml2</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-user-authenticator-sha256salted</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-metrics</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-nvp</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-contrail</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-palo-alto</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-netscaler</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> - <groupId>org.apache.cloudstack</groupId> + <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-ovs</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-elb</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-bigswitch</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-ssp</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-internallb</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-vxlan</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-opendaylight</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-vcs</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-vsp</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-xenserver</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-baremetal</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-ucs</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-ovm</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-ovm3</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-kvm</artifactId> <version>${project.version}</version> + <scope>runtime</scope> <exclusions> <exclusion> <groupId>org.mortbay.jetty</groupId> @@ -218,186 +291,223 @@ <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-hypervisor-hyperv</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-allocator-random</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-planner-user-dispersing</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-planner-skip-heurestics</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-planner-user-concentrated-pod</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-planner-implicit-dedication</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-explicit-dedication</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-host-allocator-random</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-outofbandmanagement-driver-ipmitool</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-mom-rabbitmq</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-mom-inmemory</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-mom-kafka</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-ipc</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-quota</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-framework-rest</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-api</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-components-api</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-network</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-orchestration</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-schema</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage-cache</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-controller-secondary-storage</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage-image</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage-datamotion</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage-snapshot</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-engine-storage-volume</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-volume-default</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-image-default</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-image-s3</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-storage-image-swift</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-syslog-alerts</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-snmp-alerts</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-host-anti-affinity</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-api-solidfire-intg-test</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-network-globodns</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.cloudstack</groupId> <artifactId>cloud-plugin-database-quota</artifactId> <version>${project.version}</version> + <scope>runtime</scope> </dependency> </dependencies> <build> @@ -417,34 +527,17 @@ </executions> </plugin> <plugin> - <groupId>com.googlecode.todomap</groupId> - <artifactId>maven-jettygzip-plugin</artifactId> - <version>0.0.4</version> - <configuration> - <webappDirectory>${project.build.directory}/generated-webapp</webappDirectory> - <outputDirectory>${project.build.directory}/generated-webapp</outputDirectory> - </configuration> - <executions> - <execution> - <phase>prepare-package</phase> - <goals> - <goal>process</goal> - </goals> - </execution> - </executions> - </plugin> - <plugin> <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-war-plugin</artifactId> - <version>2.5</version> + <artifactId>maven-jar-plugin</artifactId> <configuration> - <webXml>./target/generated-webapp/WEB-INF/web.xml</webXml> - <warSourceDirectory>./target/generated-webapp</warSourceDirectory> <archive> <manifest> - <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + <mainClass>org.apache.cloudstack.ServerDaemon</mainClass> </manifest> <manifestEntries> + <X-Git-Branch>${git.branch}</X-Git-Branch> + <X-Git-Tag>${git.tag}</X-Git-Tag> + <X-Git-Revision>${git.revision}</X-Git-Revision> <Implementation-Revision>${git.revision}</Implementation-Revision> <Implementation-Branch>${git.branch}</Implementation-Branch> </manifestEntries> @@ -489,15 +582,6 @@ </configuration> </plugin> <plugin> - <groupId>org.apache.tomcat.maven</groupId> - <artifactId>tomcat7-maven-plugin</artifactId> - <configuration> - <path>/client</path> - <tomcatWebXml>${basedir}/WEB-INF/web.xml</tomcatWebXml> - <warSourceDirectory>${project.build.directory}/generated-webapp/</warSourceDirectory> - </configuration> - </plugin> - <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> @@ -508,36 +592,20 @@ </goals> <configuration> <target> - <copy todir="${basedir}/target/generated-webapp/WEB-INF/classes/scripts"> + <copy todir="${project.build.directory}/classes/scripts"> <fileset dir="${basedir}/../scripts"/> </copy> <!-- CLOUDSTACK-1304 --> <chmod perm="755" - file="${basedir}/target/generated-webapp/WEB-INF/classes/scripts/**" type="both"/> - <copy todir="${basedir}/target/generated-webapp/WEB-INF/"> + file="${project.build.directory}/classes/scripts/**" type="both"/> + <copy todir="${project.build.directory}/classes/META-INF/webapp/WEB-INF/"> <fileset dir="${basedir}/WEB-INF/"> <include name="web.xml"/> </fileset> </copy> - <copy todir="${basedir}/target/generated-webapp"> + <copy todir="${project.build.directory}/classes/META-INF/webapp"> <fileset dir="${basedir}/../ui"/> </copy> - <copy overwrite="true" todir="${basedir}/target/generated-webapp/WEB-INF/classes"> - <fileset dir="${basedir}/tomcatconf"> - <include name="*.in"/> - </fileset> - <globmapper from="*.in" to="*"/> - <filterchain> - <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"> - <param type="propertiesfile" value="${cs.replace.properties}"/> - </filterreader> - </filterchain> - </copy> - <copy overwrite="true" todir="${basedir}/target/generated-webapp/WEB-INF/classes"> - <fileset dir="${basedir}/tomcatconf"> - <exclude name="*.in"/> - </fileset> - </copy> <copy overwrite="true" todir="${basedir}/target/utilities/bin"> <fileset dir="${basedir}/../setup/bindir"> <include name="*.in"/> @@ -649,11 +717,10 @@ <outputDirectory>${project.build.directory}/pythonlibs</outputDirectory> </artifactItem> <artifactItem> - <groupId>org.jasypt</groupId> - <artifactId>jasypt</artifactId> - <version>1.9.2</version> + <groupId>org.bouncycastle</groupId> + <artifactId>bcprov-jdk15on</artifactId> <overWrite>false</overWrite> - <outputDirectory>${project.build.directory}/pythonlibs</outputDirectory> + <outputDirectory>${project.build.directory}</outputDirectory> </artifactItem> </artifactItems> </configuration> @@ -671,6 +738,58 @@ </execution> </executions> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.0.0</version> + <executions> + <execution> + <id>rebuild-war</id> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <createDependencyReducedPom>false</createDependencyReducedPom> + <artifactSet> + <includes> + <include>*:*</include> + </includes> + <excludes> + <exclude>junit:junit</exclude> + <exclude>org.bouncycastle:bcprov-jdk15on</exclude> + </excludes> + </artifactSet> + <transformers> + <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" /> + <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> + <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <mainClass>org.apache.cloudstack.ServerDaemon</mainClass> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> + <resource>META-INF/spring.handlers</resource> + </transformer> + <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> + <resource>META-INF/spring.schemas</resource> + </transformer> + </transformers> + <filters> + <filter> + <artifact>*:*</artifact> + <excludes> + <exclude>META-INF/VERSION.txt</exclude> + <exclude>META-INF/LICENSE.txt</exclude> + <exclude>META-INF/*.SF</exclude> + <exclude>META-INF/*.DSA</exclude> + <exclude>META-INF/*.RSA</exclude> + <exclude>META-INF/MANIFEST.MF</exclude> + </excludes> + </filter> + </filters> + </configuration> + </execution> + </executions> + </plugin> </plugins> </build> <profiles> diff --git a/client/src/org/apache/cloudstack/ServerDaemon.java b/client/src/org/apache/cloudstack/ServerDaemon.java new file mode 100644 index 0000000..a1ddbd3 --- /dev/null +++ b/client/src/org/apache/cloudstack/ServerDaemon.java @@ -0,0 +1,205 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// +package org.apache.cloudstack; + +import org.apache.commons.daemon.Daemon; +import org.apache.commons.daemon.DaemonContext; +import org.eclipse.jetty.jmx.MBeanContainer; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.NCSARequestLog; +import org.eclipse.jetty.server.NetworkTrafficServerConnector; +import org.eclipse.jetty.server.RequestLog; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.RequestLogHandler; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.eclipse.jetty.util.thread.ThreadPool; +import org.eclipse.jetty.webapp.Configuration; +import org.eclipse.jetty.webapp.WebAppContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.lang.management.ManagementFactory; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +/*** + * Daemon server class to start the embedded server, either through JSVC or directly inside a JAR. + * Parameter to configure the jetty server are: + * - jetty.port: to start jetty on the specific port (default: 8080) + * - jetty.host: to bind to specific interface (default: null = all) + * - jetty.requestlog: path to log file for requests (default: request.log) + */ +public class ServerDaemon implements Daemon { + private static final Logger logger = LoggerFactory.getLogger(ServerDaemon.class); + private static final String WEB_XML = "META-INF/webapp/WEB-INF/web.xml"; + private static final String PROJECT_RELATIVE_PATH_TO_WEBAPP = "src/main/webapp"; + private static final String REQUEST_LOG = "request.log"; + + private Server jettyServer; + private int port; + private String bindInterface; + private String requestLogFile; + + public static void main(String... anArgs) throws Exception { + ServerDaemon csServer = new ServerDaemon(); + csServer.init(null); + csServer.start(); + csServer.join(); + } + + @Override + public void init(DaemonContext context) { + Properties props = System.getProperties(); + setPort(Integer.parseInt(props.getProperty("jetty.port", "8080"))); + setBindInterface(props.getProperty("jetty.host")); + setRequestLogFile(props.getProperty("jetty.requestlog", REQUEST_LOG)); + StringBuilder sb = new StringBuilder("Initializing server daemon on "); + sb.append(bindInterface == null ? "*" : bindInterface); + sb.append(":"); + sb.append(port); + logger.info(sb.toString()); + } + + @Override + public void start() throws Exception { + jettyServer = new Server(createThreadPool()); + + // Setup JMX + MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); + jettyServer.addBean(mbeanContainer); + + NetworkTrafficServerConnector connector = createConnector(); + jettyServer.addConnector(connector); + + // This webapp will use jsps and jstl. We need to enable the + // AnnotationConfiguration in order to correctly + // set up the jsp container + Configuration.ClassList classlist = Configuration.ClassList + .setServerDefault( jettyServer ); + classlist.addBefore( + "org.eclipse.jetty.webapp.JettyWebXmlConfiguration", + "org.eclipse.jetty.annotations.AnnotationConfiguration" ); + + + + jettyServer.setHandler(createHandlers()); + jettyServer.setStopAtShutdown(true); + + jettyServer.start(); + } + + public void join() throws InterruptedException { + jettyServer.join(); + } + + @Override + public void stop() throws Exception { + jettyServer.stop(); + } + + @Override + public void destroy() { + jettyServer.destroy(); + } + + public void setPort(int port) { + this.port = port; + } + + public void setBindInterface(String bindInterface) { + this.bindInterface = bindInterface; + } + + public void setRequestLogFile(String requestLogFile) { + this.requestLogFile = requestLogFile; + } + + private ThreadPool createThreadPool() { + QueuedThreadPool threadPool = new QueuedThreadPool(); + threadPool.setMinThreads(10); + threadPool.setMaxThreads(100); + return threadPool; + } + + private NetworkTrafficServerConnector createConnector() { + NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(jettyServer); + connector.setPort(port); + connector.setHost(bindInterface); + return connector; + } + + private HandlerCollection createHandlers() { + WebAppContext webapp = new WebAppContext(); + webapp.setContextPath("/client"); + + if (isRunningInShadedJar()) { + webapp.setWar(getShadedWarUrl()); + } else { + webapp.setWar(PROJECT_RELATIVE_PATH_TO_WEBAPP); + } + + List<Handler> handlers = new ArrayList<>(); + handlers.add(webapp); + + HandlerList contexts = new HandlerList(); + contexts.setHandlers(handlers.toArray(new Handler[0])); + + RequestLogHandler log = new RequestLogHandler(); + log.setRequestLog(createRequestLog()); + + HandlerCollection result = new HandlerCollection(); + result.setHandlers(new Handler[]{log, contexts}); + + return result; + } + + private RequestLog createRequestLog() { + NCSARequestLog log = new NCSARequestLog(); + File logPath = new File(requestLogFile); + File parentFile = logPath.getParentFile(); + if (parentFile != null) { + parentFile.mkdirs(); + } + + log.setFilename(logPath.getPath()); + log.setAppend(true); + log.setLogTimeZone("GMT"); + log.setLogLatency(true); + return log; + } + + + private boolean isRunningInShadedJar() { + return true; + } + + private URL getResource(String aResource) { + return Thread.currentThread().getContextClassLoader().getResource(aResource); + } + + private String getShadedWarUrl() { + String urlStr = getResource(WEB_XML).toString(); + return urlStr.substring(0, urlStr.length() - 15); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
