/* * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Bogdan Stefanescu * Florent Guillaume */ package org.eclipse.ecr.core.event; import org.eclipse.ecr.core.event.impl.EventListenerDescriptor; import org.eclipse.ecr.core.event.impl.EventServiceImpl; import org.eclipse.ecr.runtime.api.Framework; import org.eclipse.ecr.runtime.model.ComponentContext; import org.eclipse.ecr.runtime.model.ComponentInstance; import org.eclipse.ecr.runtime.model.DefaultComponent; /** * Event Service Component, allowing registration of contributions and doing the * event service shutdown upon deactivation. */ public class EventServiceComponent extends DefaultComponent { public static final String EVENT_LISTENER_XP = "listener"; public static final long DEFAULT_SHUTDOWN_TIMEOUT = 5 * 1000; // 5 seconds protected EventServiceImpl service; @Override public void activate(ComponentContext context) throws Exception { service = new EventServiceImpl(); } @Override public void deactivate(ComponentContext context) throws Exception { if (service != null) { String s = Framework.getProperty("org.eclipse.ecr.core.event.shutdown.timeoutMillis"); long timeout = s == null ? DEFAULT_SHUTDOWN_TIMEOUT : Long.parseLong(s); service.shutdown(timeout); service = null; } } @Override public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) throws Exception { if (EVENT_LISTENER_XP.equals(extensionPoint)) { EventListenerDescriptor descriptor = (EventListenerDescriptor) contribution; descriptor.setRuntimeContext(contributor.getRuntimeContext()); service.addEventListener(descriptor); } } @Override public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) throws Exception { if (EVENT_LISTENER_XP.equals(extensionPoint)) { service.removeEventListener((EventListenerDescriptor) contribution); } } @SuppressWarnings("unchecked") @Override public <T> T getAdapter(Class<T> adapter) { if (EventService.class == adapter || EventProducer.class == adapter || EventServiceAdmin.class == adapter) { return (T) service; } return null; } }