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