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 java.lang.management.ManagementFactory; 024 025import javax.management.JMException; 026import javax.management.MBeanServer; 027import javax.management.ObjectName; 028 029import org.apache.catalina.Container; 030import org.apache.catalina.Lifecycle; 031import org.apache.catalina.LifecycleEvent; 032import org.apache.catalina.LifecycleListener; 033import org.apache.catalina.Loader; 034import org.apache.catalina.core.ContainerBase; 035import org.apache.catalina.util.ServerInfo; 036import org.apache.commons.logging.Log; 037import org.apache.commons.logging.LogFactory; 038import org.nuxeo.osgi.application.FrameworkBootstrap; 039import org.nuxeo.osgi.application.MutableClassLoader; 040import org.nuxeo.runtime.tomcat.dev.DevFrameworkBootstrap; 041import org.nuxeo.runtime.tomcat.dev.NuxeoDevWebappClassLoader; 042 043/** 044 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 045 */ 046public class NuxeoLauncher implements LifecycleListener { 047 048 public static final String DEV_BUNDLES_NAME = "org.nuxeo:type=sdk,name=dev-bundles"; 049 050 public static final String WEB_RESOURCES_NAME = "org.nuxeo:type=sdk,name=web-resources"; 051 052 static final Log log = LogFactory.getLog(NuxeoLauncher.class); 053 054 protected boolean shared; // TODO 055 056 protected String home = "nxserver"; 057 058 protected boolean automaticReload = true; 059 060 protected FrameworkBootstrap bootstrap; 061 062 public void setShared(boolean shared) { 063 this.shared = shared; 064 } 065 066 public boolean isShared() { 067 return shared; 068 } 069 070 public void setHome(String home) { 071 this.home = home; 072 } 073 074 public String getHome() { 075 return home; 076 } 077 078 public void setAutomaticReload(boolean value) { 079 automaticReload = value; 080 } 081 082 public boolean getAutomaticReload() { 083 return automaticReload; 084 } 085 086 @Override 087 public void lifecycleEvent(LifecycleEvent event) { 088 Lifecycle lf = event.getLifecycle(); 089 if (lf instanceof ContainerBase) { 090 Loader loader = ((Container) lf).getLoader(); 091 if (loader instanceof NuxeoWebappLoader) { 092 handleEvent((NuxeoWebappLoader) loader, event); 093 } 094 } 095 } 096 097 protected void handleEvent(NuxeoWebappLoader loader, LifecycleEvent event) { 098 String type = event.getType(); 099 try { 100 MutableClassLoader cl = (MutableClassLoader) loader.getClassLoader(); 101 boolean devMode = cl instanceof NuxeoDevWebappClassLoader; 102 if (type == Lifecycle.CONFIGURE_START_EVENT) { 103 File homeDir = resolveHomeDirectory(loader); 104 if (devMode) { 105 bootstrap = new DevFrameworkBootstrap(cl, homeDir); 106 ((NuxeoDevWebappClassLoader) cl).setBootstrap((DevFrameworkBootstrap) bootstrap); 107 } else { 108 bootstrap = new FrameworkBootstrap(cl, homeDir); 109 } 110 bootstrap.setHostName("Tomcat"); 111 bootstrap.setHostVersion(ServerInfo.getServerNumber()); 112 bootstrap.initialize(); 113 } else if (type == Lifecycle.START_EVENT) { 114 if (devMode) { 115 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 116 server.registerMBean(bootstrap, new ObjectName(DEV_BUNDLES_NAME)); 117 server.registerMBean(cl, new ObjectName(WEB_RESOURCES_NAME)); 118 } 119 } else if (type == Lifecycle.STOP_EVENT) { 120 if (devMode) { 121 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 122 server.unregisterMBean(new ObjectName(DEV_BUNDLES_NAME)); 123 server.unregisterMBean(new ObjectName(WEB_RESOURCES_NAME)); 124 } 125 } 126 } catch (IOException | JMException | ReflectiveOperationException e) { 127 log.error("Failed to handle event: " + type, e); 128 } 129 } 130 131 protected File resolveHomeDirectory(NuxeoWebappLoader loader) { 132 String path = null; 133 if (home.startsWith("/") || home.startsWith("\\") || home.contains(":/") || home.contains(":\\")) { 134 // absolute 135 path = home; 136 } else if (home.startsWith("${catalina.base}")) { 137 path = getTomcatHome() + home.substring("${catalina.base}".length()); 138 } else { 139 try { 140 File baseDir = loader.getBaseDir(); 141 return new File(baseDir, home); 142 } catch (ReflectiveOperationException e) { 143 return null; 144 } 145 } 146 return new File(path); 147 } 148 149 public String getTomcatHome() { 150 String tomcatHome = System.getProperty("catalina.base"); 151 if (tomcatHome == null) { 152 tomcatHome = System.getProperty("catalina.home"); 153 } 154 return tomcatHome; 155 } 156 157}