/* * #%L * synat-portal-webapp Maven Webapp * %% * Copyright (C) 2010 - 2013 ICM, Warsaw University * %% * Licensed 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. * #L% */ import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.webapp.WebAppContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.Lifecycle; import org.springframework.web.context.WebApplicationContext; public class RunPortal { public static final int DEFAULT_HTTP_PORT = 8080; public static final int DEFAULT_HTTPS_PORT = 8443; public static final String DEFAULT_HTTP_URL = "http://localhost:" + DEFAULT_HTTP_PORT + "/"; private int httpPort; private int httpsPort; private Server server; private WebApplicationContext webApplicationContext; private final String descriptor = "src/main/webapp/WEB-INF/web.xml"; private static final Logger logger = LoggerFactory.getLogger(RunPortal.class); public static void main(final String[] args) { final RunPortal starter = new RunPortal(); starter.defaultValues(); starter.valuesFromArguments(args); starter.printParameters(); starter.startWebContainer(); } private void defaultValues() { this.httpPort = DEFAULT_HTTP_PORT; this.httpsPort = DEFAULT_HTTPS_PORT; } private static void usage() { logger.error("[httpPort [httpsPort]] "); logger.error("additional container configuration is stored in containerConfiguration.xml. this can be changed by setting system varibale: containerConfiguration"); System.exit(-1); } private void valuesFromArguments(final String[] params) { final String[] args = params.clone(); try { if (args.length > 0) { this.httpPort = Integer.parseInt(args[0]); } if (args.length > 1) { this.httpsPort = Integer.parseInt(args[0]); } } catch (final Exception e) { logger.error("Exception reported", e); usage(); } } private void printParameters() { logger.info("httpPort: " + httpPort); logger.info("httpsPort: " + httpsPort); } public void stopWebContainer() { try { // server.destroy(); // server.get cleanWebApplicationContext(); // server.join(); server.stop(); } catch (final Exception e) { logger.error("Exception reported", e); } } protected void cleanWebApplicationContext() { if (webApplicationContext instanceof Lifecycle) { Lifecycle lifecycle = (Lifecycle) webApplicationContext; if (lifecycle.isRunning()) { lifecycle.stop(); } } if (webApplicationContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) webApplicationContext; ; if (configurableApplicationContext.isActive()) { configurableApplicationContext.close(); } } } public void startWebContainer() { server = new Server(); server.setStopAtShutdown(true); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(httpPort); SslContextFactory sslContextFactory = new SslContextFactory("src/main/resources/keystore"); sslContextFactory.setKeyStorePassword("password"); SslSelectChannelConnector sslConnector = new SslSelectChannelConnector(sslContextFactory); sslConnector.setPort(httpsPort); server.setConnectors(new Connector[] { connector, sslConnector }); final WebAppContext context = new WebAppContext(); context.setDescriptor(descriptor); context.setContextPath("/portal"); context.setParentLoaderPriority(true); context.setResourceBase("src/main/webapp"); server.setHandler(context); try { server.start(); webApplicationContext = (WebApplicationContext) context.getServletContext().getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (webApplicationContext instanceof ConfigurableApplicationContext) { ((ConfigurableApplicationContext) webApplicationContext).registerShutdownHook(); } } catch (final Exception e) { logger.error("Exception reported", e); } } public ApplicationContext getMainApplicationContext() { return webApplicationContext; } }