001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.ecm.webengine.loader;
018
019import java.io.File;
020import java.io.IOException;
021import java.net.URL;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.webengine.WebEngine;
026import org.nuxeo.ecm.webengine.loader.store.FileResourceStore;
027import org.nuxeo.ecm.webengine.scripting.GroovyScripting;
028import org.osgi.framework.Bundle;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class WebLoader {
034
035    private static final Log log = LogFactory.getLog(WebLoader.class);
036
037    protected final WebEngine engine;
038
039    protected final ReloadingClassLoader classLoader;
040
041    protected final GroovyScripting gScripting; // TODO refactor groovy loading
042
043    public WebLoader(WebEngine engine) {
044        this.engine = engine;
045        File root = engine.getRootDirectory();
046        classLoader = new ReloadingClassLoader(getParentLoader());
047        gScripting = new GroovyScripting(classLoader);
048        addClassPathElement(new File(root, "WEB-INF/classes"));
049    }
050
051    public WebEngine getEngine() {
052        return engine;
053    }
054
055    /**
056     * Adds a class or resource container to the reloading class loader. The container is either a jar or a directory.
057     */
058    public void addClassPathElement(File container) {
059        try {
060            classLoader.addResourceStore(new FileResourceStore(container));
061            gScripting.getGroovyClassLoader().addURL(container.toURI().toURL());
062        } catch (IOException e) {
063            log.error("Failed to create file store: " + container, e);
064        }
065    }
066
067    public URL getResource(String name) {
068        return classLoader.getResource(name);
069    }
070
071    public Class<?> loadClass(String name) throws ClassNotFoundException {
072        return classLoader.loadClass(name);
073    }
074
075    public ReloadingClassLoader getClassLoader() {
076        return classLoader;
077    }
078
079    public GroovyScripting getGroovyScripting() {
080        return gScripting;
081    }
082
083    public void flushCache() {
084        log.info("Flushing loader cache");
085        classLoader.reload();
086        gScripting.clearCache();
087    }
088
089    public ClassProxy getGroovyClassProxy(String className) throws ClassNotFoundException {
090        return new StaticClassProxy(gScripting.loadClass(className));
091    }
092
093    public ClassProxy getClassProxy(String className) throws ClassNotFoundException {
094        return new StaticClassProxy(classLoader.loadClass(className));
095    }
096
097    public ClassProxy getClassProxy(Bundle bundle, String className) throws ClassNotFoundException {
098        return new StaticClassProxy(bundle.loadClass(className));
099    }
100
101    public static ClassLoader getParentLoader() {
102        ClassLoader cl = Thread.currentThread().getContextClassLoader();
103        return cl == null ? GroovyScripting.class.getClassLoader() : cl;
104    }
105
106}