001/*
002 * (C) Copyright 2006-2010 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
018 */
019package org.nuxeo.runtime.tomcat;
020
021import java.io.File;
022import java.io.IOException;
023import java.lang.reflect.Method;
024import java.net.URL;
025import java.net.URLClassLoader;
026import java.util.ArrayList;
027
028import org.apache.catalina.Lifecycle;
029import org.apache.catalina.LifecycleEvent;
030import org.apache.catalina.LifecycleListener;
031import org.apache.catalina.core.ContainerBase;
032import org.nuxeo.osgi.application.FrameworkBootstrap;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class NuxeoDeployer implements LifecycleListener {
038
039    protected String home = "nxserver";
040
041    protected FrameworkBootstrap bootstrap;
042
043    public void setHome(String home) {
044        this.home = home;
045    }
046
047    public String getHome() {
048        return home;
049    }
050
051    @Override
052    public void lifecycleEvent(LifecycleEvent event) {
053        Lifecycle lf = event.getLifecycle();
054        if (lf instanceof ContainerBase) {
055            ContainerBase container = (ContainerBase) lf;
056            handleEvent(container, event);
057        }
058    }
059
060    protected void handleEvent(ContainerBase container, LifecycleEvent event) {
061        try {
062            ClassLoader parentCl = container.getParentClassLoader();
063            String type = event.getType();
064            if (type == Lifecycle.BEFORE_START_EVENT) {
065                File homeDir = resolveHomeDirectory();
066                File bundles = new File(homeDir, "bundles");
067                File lib = new File(homeDir, "lib");
068                File deployerJar = FrameworkBootstrap.findFileStartingWidth(bundles, "nuxeo-runtime-deploy");
069                File commonJar = FrameworkBootstrap.findFileStartingWidth(bundles, "nuxeo-common");
070                if (deployerJar == null || commonJar == null) {
071                    System.out.println("Deployer and/or common JAR (nuxeo-runtime-deploy* | nuxeo-common*) not found in "
072                            + bundles);
073                    return;
074                }
075                ArrayList<URL> urls = new ArrayList<URL>();
076                File[] files = lib.listFiles();
077                if (files != null) {
078                    for (File f : files) {
079                        if (f.getPath().endsWith(".jar")) {
080                            urls.add(f.toURI().toURL());
081                        }
082                    }
083                }
084                files = bundles.listFiles();
085                if (files != null) {
086                    for (File f : files) {
087                        if (f.getPath().endsWith(".jar")) {
088                            urls.add(f.toURI().toURL());
089                        }
090                    }
091                }
092                urls.add(homeDir.toURI().toURL());
093                urls.add(new File(homeDir, "config").toURI().toURL());
094                try (URLClassLoader cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), parentCl)) {
095                    System.out.println("# Running Nuxeo Preprocessor ...");
096                    Class<?> klass = cl.loadClass("org.nuxeo.runtime.deployment.preprocessor.DeploymentPreprocessor");
097                    Method main = klass.getMethod("main", String[].class);
098                    main.invoke(null, new Object[] { new String[] { homeDir.getAbsolutePath() } });
099                    System.out.println("# Preprocessing done.");
100                }
101            }
102        } catch (IOException | ReflectiveOperationException e) {
103            throw new RuntimeException("Failed to handle event", e);
104        }
105    }
106
107    protected File resolveHomeDirectory() {
108        String path;
109        if (home.startsWith("/")) {
110            path = home;
111        } else {
112            path = getTomcatHome() + "/" + home;
113        }
114        return new File(path);
115    }
116
117    public String getTomcatHome() {
118        String tomcatHome = System.getProperty("catalina.base");
119        if (tomcatHome == null) {
120            tomcatHome = System.getProperty("catalina.home");
121        }
122        return tomcatHome;
123    }
124
125}