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