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 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.opencmis.bindings; 020 021import javax.servlet.ServletContextEvent; 022import javax.servlet.ServletContextListener; 023 024import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory; 025import org.apache.chemistry.opencmis.server.impl.CmisRepositoryContextListener; 026import org.nuxeo.runtime.RuntimeService; 027import org.nuxeo.runtime.RuntimeServiceEvent; 028import org.nuxeo.runtime.RuntimeServiceListener; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * Servlet context listener that sets up the CMIS service factory in the servlet context as expected by 033 * {@link org.apache.chemistry.opencmis.server.impl.atompub.CmisAtomPubServlet} or 034 * {@link org.apache.chemistry.opencmis.server.impl.browser.CmisBrowserBindingServlet} or 035 * {@link org.apache.chemistry.opencmis.server.impl.webservices.AbstractService} . 036 * 037 * @see CmisRepositoryContextListener 038 */ 039public class NuxeoCmisContextListener implements ServletContextListener { 040 041 @Override 042 public void contextInitialized(final ServletContextEvent sce) { 043 RuntimeService runtime = Framework.getRuntime(); 044 if (runtime == null || !runtime.isStarted()) { 045 Framework.addListener(new RuntimeServiceListener() { 046 047 @Override 048 public void handleEvent(RuntimeServiceEvent event) { 049 if (event.id != RuntimeServiceEvent.RUNTIME_STARTED) { 050 return; 051 } 052 Framework.removeListener(this); 053 activate(sce); 054 } 055 056 }); 057 } else { 058 activate(sce); 059 } 060 } 061 062 protected void activate(final ServletContextEvent sce) { 063 NuxeoCmisServiceFactoryManager manager = Framework.getService(NuxeoCmisServiceFactoryManager.class); 064 CmisServiceFactory factory = manager.getNuxeoCmisServiceFactory(); 065 sce.getServletContext().setAttribute(CmisRepositoryContextListener.SERVICES_FACTORY, factory); 066 } 067 068 @Override 069 public void contextDestroyed(ServletContextEvent sce) { 070 CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute( 071 CmisRepositoryContextListener.SERVICES_FACTORY); 072 if (factory != null) { 073 factory.destroy(); 074 } 075 } 076 077}