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 *     Bogdan Stefanescu
011 *     Florent Guillaume
012 */
013package org.nuxeo.ecm.core.event;
014
015import org.nuxeo.ecm.core.event.impl.EventListenerDescriptor;
016import org.nuxeo.ecm.core.event.impl.EventServiceImpl;
017import org.nuxeo.runtime.api.Framework;
018import org.nuxeo.runtime.model.ComponentContext;
019import org.nuxeo.runtime.model.ComponentInstance;
020import org.nuxeo.runtime.model.DefaultComponent;
021
022/**
023 * Event Service Component, allowing registration of contributions and doing the event service shutdown upon
024 * deactivation.
025 */
026public class EventServiceComponent extends DefaultComponent {
027
028    public static final int APPLICATION_STARTED_ORDER = -500;
029
030    public static final String EVENT_LISTENER_XP = "listener";
031
032    public static final long DEFAULT_SHUTDOWN_TIMEOUT = 5 * 1000; // 5 seconds
033
034    protected EventServiceImpl service;
035
036    @Override
037    public void activate(ComponentContext context) {
038        service = new EventServiceImpl();
039    }
040
041    @Override
042    public void deactivate(ComponentContext context) {
043        if (service != null) {
044            String s = Framework.getProperty("org.nuxeo.ecm.core.event.shutdown.timeoutMillis");
045            long timeout = s == null ? DEFAULT_SHUTDOWN_TIMEOUT : Long.parseLong(s);
046            try {
047                service.shutdown(timeout);
048            } catch (InterruptedException e) {
049                // restore interrupted status
050                Thread.currentThread().interrupt();
051                throw new RuntimeException(e);
052            }
053            service = null;
054        }
055    }
056
057    @Override
058    public int getApplicationStartedOrder() {
059        return APPLICATION_STARTED_ORDER;
060    }
061
062    @Override
063    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
064        if (EVENT_LISTENER_XP.equals(extensionPoint)) {
065            EventListenerDescriptor descriptor = (EventListenerDescriptor) contribution;
066            descriptor.setRuntimeContext(contributor.getRuntimeContext());
067            service.addEventListener(descriptor);
068        }
069    }
070
071    @Override
072    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
073        if (EVENT_LISTENER_XP.equals(extensionPoint)) {
074            service.removeEventListener((EventListenerDescriptor) contribution);
075        }
076    }
077
078    @SuppressWarnings("unchecked")
079    @Override
080    public <T> T getAdapter(Class<T> adapter) {
081        if (EventService.class == adapter || EventProducer.class == adapter || EventServiceAdmin.class == adapter) {
082            return (T) service;
083        }
084        return null;
085    }
086
087}