001/*
002 * (C) Copyright 2006-2011 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.jaxrs;
020
021import javax.servlet.ServletException;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.webengine.jaxrs.servlet.config.ServletRegistry;
026import org.osgi.framework.BundleActivator;
027import org.osgi.framework.BundleContext;
028import org.osgi.framework.Constants;
029import org.osgi.framework.ServiceReference;
030import org.osgi.service.http.HttpService;
031import org.osgi.service.http.NamespaceException;
032import org.osgi.service.packageadmin.PackageAdmin;
033import org.osgi.util.tracker.ServiceTracker;
034import org.osgi.util.tracker.ServiceTrackerCustomizer;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class Activator implements BundleActivator, ServiceTrackerCustomizer {
040
041    private static final Log log = LogFactory.getLog(Activator.class);
042
043    private static Activator instance;
044
045    public static Activator getInstance() {
046        return instance;
047    }
048
049    protected ServiceTracker httpServiceTracker;
050
051    protected BundleContext context;
052
053    protected ServiceReference pkgAdm;
054
055    @Override
056    public void start(BundleContext context) {
057        instance = this;
058        this.context = context;
059        pkgAdm = context.getServiceReference(PackageAdmin.class.getName());
060        // TODO workaround to disable service tracker on regular Nuxeo distribs until finding a better solution
061        if (!"Nuxeo".equals(context.getProperty(Constants.FRAMEWORK_VENDOR))) {
062            httpServiceTracker = new ServiceTracker(context, HttpService.class.getName(), this);
063            httpServiceTracker.open();
064        }
065
066        ApplicationManager.getInstance().start(context);
067    }
068
069    @Override
070    public void stop(BundleContext context) {
071        ApplicationManager.getInstance().stop(context);
072
073        if (httpServiceTracker != null) {
074            httpServiceTracker.close();
075            httpServiceTracker = null;
076        }
077        ServletRegistry.dispose();
078        instance = null;
079        context.ungetService(pkgAdm);
080        pkgAdm = null;
081        this.context = null;
082    }
083
084    public BundleContext getContext() {
085        return context;
086    }
087
088    public PackageAdmin getPackageAdmin() {
089        return (PackageAdmin) context.getService(pkgAdm);
090    }
091
092    @Override
093    public Object addingService(ServiceReference reference) {
094        Object service = context.getService(reference);
095        try {
096            if (service instanceof HttpService) {
097                ServletRegistry.getInstance().initHttpService((HttpService) service);
098            }
099        } catch (ServletException | NamespaceException e) {
100            throw new RuntimeException("Failed to initialize http service", e);
101        }
102        return service;
103    }
104
105    @Override
106    public void removedService(ServiceReference reference, Object service) {
107        try {
108            if (ServletRegistry.getInstance().getHttpService() == service) {
109                ServletRegistry.getInstance().initHttpService(null);
110            }
111        } catch (ServletException | NamespaceException e) {
112            log.error("Failed to remove http service", e);
113        } finally {
114            context.ungetService(reference);
115        }
116    }
117
118    @Override
119    public void modifiedService(ServiceReference reference, Object service) {
120        try {
121            if (ServletRegistry.getInstance().getHttpService() == service) {
122                ServletRegistry.getInstance().initHttpService(null);
123                ServletRegistry.getInstance().initHttpService((HttpService) service);
124            }
125        } catch (ServletException | NamespaceException e) {
126            log.error("Failed to update http service", e);
127        }
128    }
129}