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 // 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 try { 086 service.addEventListener(descriptor); 087 } catch (RuntimeException e) { 088 String msg = "Failed to register event listener in component '" + contributor.getName() 089 + "': error initializing event listener '" + descriptor.getName() + "' (" + e.toString() + ')'; 090 Framework.getRuntime().getErrors().add(msg); 091 } 092 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 093 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 094 service.addEventPipe(descriptor); 095 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 096 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 097 service.addEventDispatcher(descriptor); 098 } 099 } 100 101 @Override 102 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 103 if (EVENT_LISTENER_XP.equals(extensionPoint)) { 104 service.removeEventListener((EventListenerDescriptor) contribution); 105 } else if (EVENT_PIPE_XP.equals(extensionPoint)) { 106 EventPipeDescriptor descriptor = (EventPipeDescriptor) contribution; 107 service.removeEventPipe(descriptor); 108 } else if (EVENT_DISPATCHER_XP.equals(extensionPoint)) { 109 EventDispatcherDescriptor descriptor = (EventDispatcherDescriptor) contribution; 110 service.removeEventDispatcher(descriptor); 111 } 112 } 113 114 @SuppressWarnings("unchecked") 115 @Override 116 public <T> T getAdapter(Class<T> adapter) { 117 if (EventService.class == adapter || EventProducer.class == adapter || EventServiceAdmin.class == adapter) { 118 return (T) service; 119 } 120 return null; 121 } 122 123}