/* * #%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 java.io.IOException; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; 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.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.context.WebApplicationContext; import pl.edu.icm.synat.security.SSLCertDisabler; public class RunPortal { public static final int DEFAULT_HTTP_PORT = 8280; public static final int DEFAULT_HTTPS_PORT = 8443; public static final int DEFAULT_AJP_PORT = 8010; public static final String DEFAULT_HTTP_URL = "http://localhost:" + DEFAULT_HTTP_PORT + "/"; private int httpPort; private int httpsPort; private int ajpPort; 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) throws IOException { 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; this.ajpPort = DEFAULT_AJP_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[1]); } if (args.length > 2) { this.ajpPort = Integer.parseInt(args[2]); } } catch (final Exception e) { logger.error("Exception reported", e); usage(); } } private void printParameters() { logger.info("httpPort: " + httpPort); logger.info("httpsPort: " + httpsPort); logger.info("ajpPort: " + ajpPort); } 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() throws IOException { disableSsl(); server = new Server(); server.setStopAtShutdown(true); ServerConnector connector = new ServerConnector(server); connector.setPort(httpPort); SslContextFactory sslContextFactory = new SslContextFactory("src/main/resources/keystore"); sslContextFactory.setKeyStorePassword("password"); ServerConnector sslConnector = new ServerConnector(server, 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"); Resource resource = new ClassPathResource("/override-web.xml"); context.addOverrideDescriptor(resource.getFile().getAbsolutePath()); resource = new ClassPathResource("/override-web-custom.xml"); if(resource.exists()){ context.addOverrideDescriptor(resource.getFile().getAbsolutePath()); } 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; } void disableSsl() { SSLCertDisabler sslCertDisabler = new SSLCertDisabler(); sslCertDisabler.setTrustAll(true); sslCertDisabler.init(); //disable SSL for Apache HTTP Client try { org.apache.commons.httpclient.protocol.ProtocolSocketFactory socketFactory = new org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory(); org.apache.commons.httpclient.protocol.Protocol https = new org.apache.commons.httpclient.protocol.Protocol( "https", socketFactory, 443); org.apache.commons.httpclient.protocol.Protocol.registerProtocol( "https", https); } catch (Exception e1) { e1.printStackTrace(); } } }