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.ecm.core.event.pipe.EventPipeDescriptor; 025import org.nuxeo.ecm.core.event.pipe.dispatch.EventDispatcherDescriptor; 026import org.nuxeo.runtime.api.Framework; 027import org.nuxeo.runtime.model.ComponentContext; 028import org.nuxeo.runtime.model.ComponentInstance; 029import org.nuxeo.runtime.model.DefaultComponent; 030 031/** 032 * Event Service Component, allowing registration of contributions and doing the event service shutdown upon 033 * deactivation. 034 */ 035public class EventServiceComponent extends DefaultComponent { 036 037 public static final int APPLICATION_STARTED_ORDER = -500; 038 039 public static final String EVENT_LISTENER_XP = "listener"; 040 041 public static final String EVENT_PIPE_XP = "pipe"; 042 043 public static final String EVENT_DISPATCHER_XP = "dispatcher"; 044 045 public static final long DEFAULT_SHUTDOWN_TIMEOUT = 5 * 1000; // 5 seconds 046 047 protected EventServiceImpl service; 048 049 @Override 050 public void activate(ComponentContext context) { 051 service = new EventServiceImpl(); 052 } 053 054 @Override 055 public void applicationStarted(ComponentContext context) { 056 service.init(); 057 } 058 059 @Override 060 public void deactivate(ComponentContext context) { 061 if (service != null) { 062 String s = Framework.getProperty("org.nuxeo.ecm.core.event.shutdown.timeoutMillis"); 063 long timeout = s == null ? DEFAULT_SHUTDOWN_TIMEOUT : Long.parseLong(s); 064 try { 065 service.shutdown(timeout); 066 } catch (InterruptedException e) { 067 // restore interrupted status 068 Thread.currentThread().interrupt(); 069 throw new RuntimeException(e); 070 } 071 service = null; 072 } 073 } 074 075 @Override 076 public int getApplicationStartedOrder() { 077 return APPLICATION_STARTED_ORDER; 078 } 079 080 @Override 081 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 082 if (EVENT_LISTENER_XP.equals(extensionPoint)) { 083 EventListenerDescriptor descriptor = (EventListenerDescriptor) contribution; 084 descriptor.setRuntimeContext(contributor.getRuntimeContext()); 085 service.addEventListener(descriptor); 086 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 087 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 088 service.addEventPipe(descriptor); 089 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 090 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 091 service.addEventDispatcher(descriptor); 092 } 093 } 094 095 @Override 096 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 097 if (EVENT_LISTENER_XP.equals(extensionPoint)) { 098 service.removeEventListener((EventListenerDescriptor) contribution); 099 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 100 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 101 service.removeEventPipe(descriptor); 102 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 103 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 104 service.removeEventDispatcher(descriptor); 105 } 106 } 107 108 @SuppressWarnings("unchecked") 109 @Override 110 public <T> T getAdapter(Class<T> adapter) { 111 if (EventService.class == adapter || EventProducer.class == adapter || EventServiceAdmin.class == adapter) { 112 return (T) service; 113 } 114 return null; 115 } 116 117}