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}.
035 *
036 * @see CmisRepositoryContextListener
037 */
038public class NuxeoCmisContextListener implements ServletContextListener {
039
040    @Override
041    public void contextInitialized(final ServletContextEvent sce) {
042        RuntimeService runtime = Framework.getRuntime();
043        if (runtime == null || !runtime.isStarted()) {
044            Framework.addListener(new RuntimeServiceListener() {
045
046                @Override
047                public void handleEvent(RuntimeServiceEvent event) {
048                    if (event.id != RuntimeServiceEvent.RUNTIME_STARTED) {
049                        return;
050                    }
051                    Framework.removeListener(this);
052                    activate(sce);
053                }
054
055            });
056        } else {
057            activate(sce);
058        }
059    }
060
061    protected void activate(final ServletContextEvent sce) {
062        NuxeoCmisServiceFactoryManager manager = Framework.getService(NuxeoCmisServiceFactoryManager.class);
063        CmisServiceFactory factory = manager.getNuxeoCmisServiceFactory();
064        sce.getServletContext().setAttribute(CmisRepositoryContextListener.SERVICES_FACTORY, factory);
065    }
066
067    @Override
068    public void contextDestroyed(ServletContextEvent sce) {
069        CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute(
070                CmisRepositoryContextListener.SERVICES_FACTORY);
071        if (factory != null) {
072            factory.destroy();
073        }
074    }
075
076}