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.gwt;
018
019import java.io.IOException;
020import java.net.URISyntaxException;
021import java.net.URL;
022
023import java.io.File;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.Environment;
028import org.nuxeo.runtime.api.Framework;
029import org.osgi.framework.Bundle;
030import org.osgi.framework.BundleActivator;
031import org.osgi.framework.BundleContext;
032import org.osgi.framework.FrameworkEvent;
033import org.osgi.framework.FrameworkListener;
034
035/**
036 * This activator must be used as an activator by bundles that wants to deploy GWT resources in a nuxeo server.
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class GwtBundleActivator implements BundleActivator, FrameworkListener {
041
042    protected static final Log log = LogFactory.getLog(GwtBundleActivator.class);
043
044    public static final String GWT_DEV_MODE_PROP = "nuxeo.gwt_dev_mode";
045
046    public static final File GWT_ROOT = new File(Environment.getDefault().getWeb(), "root.war/gwt");
047
048    public static final boolean GWT_DEV_MODE = "true".equals(Framework.getProperty(GWT_DEV_MODE_PROP, "false"));
049
050    protected BundleContext context;
051
052    @Override
053    public void start(BundleContext context) {
054        this.context = context;
055        if (GWT_DEV_MODE) {
056            return;
057        }
058        context.addFrameworkListener(this);
059    }
060
061    @Override
062    public void stop(BundleContext context) {
063        this.context = null;
064    }
065
066    protected void installGwtApp(Bundle bundle) throws IOException, URISyntaxException {
067        URL location = bundle.getEntry("gwt-war");
068        if (location == null) {
069            throw new IOException("Cannot locate gwt-war in " + bundle.getSymbolicName());
070        }
071        Framework.getService(GwtResolver.class).install(location.toURI());
072    }
073
074    @Override
075    public void frameworkEvent(FrameworkEvent event) {
076        if (event.getType() == FrameworkEvent.STARTED) {
077            try {
078                installGwtApp(context.getBundle());
079            } catch (IOException | URISyntaxException cause) {
080                throw new RuntimeException("Cannot start GWT", cause);
081            }
082        }
083    }
084
085}