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.servlet.config; 020 021import javax.servlet.ServletException; 022 023import org.nuxeo.runtime.model.ComponentContext; 024import org.nuxeo.runtime.model.ComponentInstance; 025import org.nuxeo.runtime.model.DefaultComponent; 026import org.nuxeo.ecm.webengine.jaxrs.ApplicationManager; 027import org.osgi.service.http.NamespaceException; 028 029/** 030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 031 */ 032public class ServletRegistryComponent extends DefaultComponent { 033 034 public static final String XP_SERVLETS = "servlets"; 035 036 public static final String XP_FILTERS = "filters"; 037 038 public static final String XP_RESOURCES = "resources"; 039 040 public static final String XP_SUBRESOURCES = "subresources"; 041 042 protected ServletRegistry registry; 043 044 public ServletRegistryComponent() { 045 } 046 047 @Override 048 public void activate(ComponentContext context) { 049 registry = ServletRegistry.getInstance(); 050 } 051 052 @Override 053 public void deactivate(ComponentContext context) { 054 ServletRegistry.dispose(); 055 registry = null; 056 } 057 058 @Override 059 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 060 if (XP_SERVLETS.equals(extensionPoint)) { 061 ((ServletDescriptor) contribution).setBundle(contributor.getContext().getBundle()); 062 try { 063 registry.addServlet((ServletDescriptor) contribution); 064 } catch (ServletException | NamespaceException e) { 065 throw new RuntimeException(e); 066 } 067 } else if (XP_FILTERS.equals(extensionPoint)) { 068 registry.addFilterSet((FilterSetDescriptor) contribution); 069 } else if (XP_RESOURCES.equals(extensionPoint)) { 070 ResourcesDescriptor rd = (ResourcesDescriptor) contribution; 071 rd.setBundle(contributor.getContext().getBundle()); 072 registry.addResources(rd); 073 } else if (XP_SUBRESOURCES.equals(extensionPoint)) { 074 ResourceExtension rxt = (ResourceExtension) contribution; 075 rxt.setBundle(contributor.getContext().getBundle()); 076 ApplicationManager.getInstance().getOrCreateApplication(rxt.getApplication()).addExtension(rxt); 077 } 078 } 079 080 @Override 081 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 082 if (XP_SERVLETS.equals(extensionPoint)) { 083 registry.removeServlet((ServletDescriptor) contribution); 084 } else if (XP_FILTERS.equals(extensionPoint)) { 085 registry.removeFilterSet((FilterSetDescriptor) contribution); 086 } else if (XP_RESOURCES.equals(extensionPoint)) { 087 ResourcesDescriptor rd = (ResourcesDescriptor) contribution; 088 rd.setBundle(contributor.getContext().getBundle()); 089 registry.removeResources(rd); 090 } else if (XP_SUBRESOURCES.equals(extensionPoint)) { 091 ResourceExtension rxt = (ResourceExtension) contribution; 092 rxt.setBundle(contributor.getContext().getBundle()); 093 ApplicationManager.getInstance().getOrCreateApplication(rxt.getApplication()).removeExtension(rxt); 094 } 095 } 096 097}