001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu, jcarsique 018 */ 019package org.nuxeo.runtime.tomcat; 020 021import java.io.File; 022import java.io.IOException; 023import javax.management.JMException; 024import org.apache.catalina.Container; 025import org.apache.catalina.Lifecycle; 026import org.apache.catalina.LifecycleEvent; 027import org.apache.catalina.LifecycleListener; 028import org.apache.catalina.Loader; 029import org.apache.catalina.core.ContainerBase; 030import org.apache.catalina.util.ServerInfo; 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.osgi.application.FrameworkBootstrap; 034import org.nuxeo.osgi.application.MutableClassLoader; 035import org.nuxeo.runtime.tomcat.dev.DevFrameworkBootstrap; 036import org.nuxeo.runtime.tomcat.dev.NuxeoDevWebappClassLoader; 037 038/** 039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 040 */ 041public class NuxeoLauncher implements LifecycleListener { 042 043 static final Log log = LogFactory.getLog(NuxeoLauncher.class); 044 045 protected boolean shared; // TODO 046 047 protected String home = "nxserver"; 048 049 protected boolean automaticReload = true; 050 051 protected FrameworkBootstrap bootstrap; 052 053 public void setShared(boolean shared) { 054 this.shared = shared; 055 } 056 057 public boolean isShared() { 058 return shared; 059 } 060 061 public void setHome(String home) { 062 this.home = home; 063 } 064 065 public String getHome() { 066 return home; 067 } 068 069 public void setAutomaticReload(boolean value) { 070 automaticReload = value; 071 } 072 073 public boolean getAutomaticReload() { 074 return automaticReload; 075 } 076 077 @Override 078 public void lifecycleEvent(LifecycleEvent event) { 079 Lifecycle lf = event.getLifecycle(); 080 if (lf instanceof ContainerBase) { 081 Loader loader = ((Container) lf).getLoader(); 082 if (loader instanceof NuxeoWebappLoader) { 083 handleEvent((NuxeoWebappLoader) loader, event); 084 } 085 } 086 } 087 088 protected void handleEvent(NuxeoWebappLoader loader, LifecycleEvent event) { 089 String type = event.getType(); 090 try { 091 MutableClassLoader cl = (MutableClassLoader) loader.getClassLoader(); 092 boolean devMode = cl instanceof NuxeoDevWebappClassLoader; 093 if (type == Lifecycle.CONFIGURE_START_EVENT) { 094 File homeDir = resolveHomeDirectory(loader); 095 if (devMode) { 096 bootstrap = new DevFrameworkBootstrap(cl, homeDir); 097 ((NuxeoDevWebappClassLoader) cl).setBootstrap((DevFrameworkBootstrap) bootstrap); 098 } else { 099 bootstrap = new FrameworkBootstrap(cl, homeDir); 100 } 101 bootstrap.setHostName("Tomcat"); 102 bootstrap.setHostVersion(ServerInfo.getServerNumber()); 103 bootstrap.initialize(); 104 } else if (type == Lifecycle.START_EVENT) { 105 bootstrap.start(cl); 106 } else if (type == Lifecycle.STOP_EVENT) { 107 bootstrap.stop(cl); 108 } 109 } catch (IOException | JMException | ReflectiveOperationException e) { 110 log.error("Failed to handle event: " + type, e); 111 } 112 } 113 114 protected File resolveHomeDirectory(NuxeoWebappLoader loader) { 115 String path = null; 116 if (home.startsWith("/") || home.startsWith("\\") || home.contains(":/") || home.contains(":\\")) { 117 // absolute 118 path = home; 119 } else if (home.startsWith("${catalina.base}")) { 120 path = getTomcatHome() + home.substring("${catalina.base}".length()); 121 } else { 122 try { 123 File baseDir = loader.getBaseDir(); 124 return new File(baseDir, home); 125 } catch (ReflectiveOperationException e) { 126 return null; 127 } 128 } 129 return new File(path); 130 } 131 132 public String getTomcatHome() { 133 String tomcatHome = System.getProperty("catalina.base"); 134 if (tomcatHome == null) { 135 tomcatHome = System.getProperty("catalina.home"); 136 } 137 return tomcatHome; 138 } 139 140}