001/* 002 * (C) Copyright 2006-2017 Nuxeo (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 Thread.currentThread().interrupt(); 068 throw new RuntimeException(e); 069 } 070 service = null; 071 } 072 } 073 074 @Override 075 public int getApplicationStartedOrder() { 076 return APPLICATION_STARTED_ORDER; 077 } 078 079 @Override 080 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 081 if (EVENT_LISTENER_XP.equals(extensionPoint)) { 082 EventListenerDescriptor descriptor = (EventListenerDescriptor) contribution; 083 descriptor.setRuntimeContext(contributor.getRuntimeContext()); 084 try { 085 service.addEventListener(descriptor); 086 } catch (RuntimeException e) { 087 String msg = "Failed to register event listener in component '" + contributor.getName() 088 + "': error initializing event listener '" + descriptor.getName() + "' (" + e.toString() + ')'; 089 Framework.getRuntime().getMessageHandler().addError(msg); 090 } 091 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 092 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 093 service.addEventPipe(descriptor); 094 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 095 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 096 service.addEventDispatcher(descriptor); 097 } 098 } 099 100 @Override 101 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 102 if (EVENT_LISTENER_XP.equals(extensionPoint)) { 103 service.removeEventListener((EventListenerDescriptor) contribution); 104 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 105 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 106 service.removeEventPipe(descriptor); 107 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 108 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 109 service.removeEventDispatcher(descriptor); 110 } 111 } 112 113 @SuppressWarnings("unchecked") 114 @Override 115 public <T> T getAdapter(Class<T> adapter) { 116 if (EventService.class == adapter || EventProducer.class == adapter || EventServiceAdmin.class == adapter) { 117 return (T) service; 118 } 119 return null; 120 } 121 122}