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