001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.webengine.jaxrs; 013 014import javax.servlet.ServletException; 015 016import org.apache.commons.logging.Log; 017import org.apache.commons.logging.LogFactory; 018import org.nuxeo.ecm.webengine.jaxrs.servlet.config.ServletRegistry; 019import org.osgi.framework.BundleActivator; 020import org.osgi.framework.BundleContext; 021import org.osgi.framework.Constants; 022import org.osgi.framework.ServiceReference; 023import org.osgi.service.http.HttpService; 024import org.osgi.service.http.NamespaceException; 025import org.osgi.service.packageadmin.PackageAdmin; 026import org.osgi.util.tracker.ServiceTracker; 027import org.osgi.util.tracker.ServiceTrackerCustomizer; 028 029/** 030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 031 */ 032public class Activator implements BundleActivator, ServiceTrackerCustomizer { 033 034 private static final Log log = LogFactory.getLog(Activator.class); 035 036 private static Activator instance; 037 038 public static Activator getInstance() { 039 return instance; 040 } 041 042 protected ServiceTracker httpServiceTracker; 043 044 protected BundleContext context; 045 046 protected ServiceReference pkgAdm; 047 048 @Override 049 public void start(BundleContext context) { 050 instance = this; 051 this.context = context; 052 pkgAdm = context.getServiceReference(PackageAdmin.class.getName()); 053 // TODO workaround to disable service tracker on regular Nuxeo distribs until finding a better solution 054 if (!"Nuxeo".equals(context.getProperty(Constants.FRAMEWORK_VENDOR))) { 055 httpServiceTracker = new ServiceTracker(context, HttpService.class.getName(), this); 056 httpServiceTracker.open(); 057 } 058 059 ApplicationManager.getInstance().start(context); 060 } 061 062 @Override 063 public void stop(BundleContext context) { 064 ApplicationManager.getInstance().stop(context); 065 066 if (httpServiceTracker != null) { 067 httpServiceTracker.close(); 068 httpServiceTracker = null; 069 } 070 ServletRegistry.dispose(); 071 instance = null; 072 context.ungetService(pkgAdm); 073 pkgAdm = null; 074 this.context = null; 075 } 076 077 public BundleContext getContext() { 078 return context; 079 } 080 081 public PackageAdmin getPackageAdmin() { 082 return (PackageAdmin) context.getService(pkgAdm); 083 } 084 085 @Override 086 public Object addingService(ServiceReference reference) { 087 Object service = context.getService(reference); 088 try { 089 if (service instanceof HttpService) { 090 ServletRegistry.getInstance().initHttpService((HttpService) service); 091 } 092 } catch (ServletException | NamespaceException e) { 093 throw new RuntimeException("Failed to initialize http service", e); 094 } 095 return service; 096 } 097 098 @Override 099 public void removedService(ServiceReference reference, Object service) { 100 try { 101 if (ServletRegistry.getInstance().getHttpService() == service) { 102 ServletRegistry.getInstance().initHttpService(null); 103 } 104 } catch (ServletException | NamespaceException e) { 105 log.error("Failed to remove http service", e); 106 } finally { 107 context.ungetService(reference); 108 } 109 } 110 111 @Override 112 public void modifiedService(ServiceReference reference, Object service) { 113 try { 114 if (ServletRegistry.getInstance().getHttpService() == service) { 115 ServletRegistry.getInstance().initHttpService(null); 116 ServletRegistry.getInstance().initHttpService((HttpService) service); 117 } 118 } catch (ServletException | NamespaceException e) { 119 log.error("Failed to update http service", e); 120 } 121 } 122}