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