diff options
| author | Dirk Fauth | 2016-09-27 19:54:09 +0000 |
|---|---|---|
| committer | Dirk Fauth | 2016-10-09 19:50:24 +0000 |
| commit | 7131b4c3382420058bf7f66827a13bddc7426b0b (patch) | |
| tree | c7f41d34c449ba35059089b6c5580bd619c6fd6c | |
| parent | b72545c66adc05f1563afb6daaa27c7557d0b32c (diff) | |
| download | eclipse.platform.ui-7131b4c3382420058bf7f66827a13bddc7426b0b.tar.gz eclipse.platform.ui-7131b4c3382420058bf7f66827a13bddc7426b0b.tar.xz eclipse.platform.ui-7131b4c3382420058bf7f66827a13bddc7426b0b.zip | |
Bug 501918 - Remove dependency org.eclipse.osgi from o.e.e4.ui.services
Added service reference to EventAdmin in EventBrokerFactory to ensure
that it only gets activated if an EventAdmin service is available.
Therefore removed the Activator and the error logging statements
completely. Also removed the static init code in EventBroker that should
start the bundle org.eclipse.equinox.event manually.
Change-Id: I6747e68b72100c30d6ab98f538201cb43daedf6a
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
7 files changed, 31 insertions, 193 deletions
diff --git a/bundles/org.eclipse.e4.ui.services/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.ui.services/META-INF/MANIFEST.MF index 5d39090fe1d..2a060a412d4 100644 --- a/bundles/org.eclipse.e4.ui.services/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.ui.services/META-INF/MANIFEST.MF @@ -12,10 +12,10 @@ Import-Package: javax.annotation, org.osgi.framework;version="1.3.0", org.osgi.service.component.annotations;version="1.2.0";resolution:=optional, org.osgi.service.event;resolution:=optional, + org.osgi.util.tracker;version="1.5.1", org.w3c.dom.css Require-Bundle: org.eclipse.core.commands;bundle-version="[3.4.0,4.0.0)", org.eclipse.e4.core.services;bundle-version="0.9.0", - org.eclipse.osgi;bundle-version="[3.6.0,4.0.0)", org.eclipse.e4.core.contexts;bundle-version="0.9.0", org.eclipse.e4.core.di;bundle-version="0.9.0", org.eclipse.e4.ui.di;bundle-version="0.9.0" @@ -24,7 +24,6 @@ Export-Package: org.eclipse.e4.ui.internal.services;x-friends:="org.eclipse.e4.u org.eclipse.e4.ui.services.events;x-internal:=true, org.eclipse.e4.ui.services.help;x-friends:="org.eclipse.e4.ui.workbench.renderers.swt,org.eclipse.ui.workbench", org.eclipse.e4.ui.services.internal.events;x-internal:=true -Bundle-Activator: org.eclipse.e4.ui.internal.services.Activator Service-Component: OSGI-INF/org.eclipse.e4.ui.services.events.EventBrokerFactory.xml Require-Capability: osgi.extender; filter:="(&(osgi.extender=osgi.component)(version>=1.2)(!(version>=2.0)))", diff --git a/bundles/org.eclipse.e4.ui.services/OSGI-INF/org.eclipse.e4.ui.services.events.EventBrokerFactory.xml b/bundles/org.eclipse.e4.ui.services/OSGI-INF/org.eclipse.e4.ui.services.events.EventBrokerFactory.xml index 75a1c9152c3..57af8220014 100644 --- a/bundles/org.eclipse.e4.ui.services/OSGI-INF/org.eclipse.e4.ui.services.events.EventBrokerFactory.xml +++ b/bundles/org.eclipse.e4.ui.services/OSGI-INF/org.eclipse.e4.ui.services.events.EventBrokerFactory.xml @@ -4,5 +4,6 @@ <service> <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/> </service> + <reference bind="setEventAdmin" cardinality="1..1" interface="org.osgi.service.event.EventAdmin" name="EventAdmin" policy="static"/> <implementation class="org.eclipse.e4.ui.services.events.EventBrokerFactory"/> </scr:component>
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/Activator.java b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/Activator.java deleted file mode 100644 index 19c76f5abb0..00000000000 --- a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/Activator.java +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2015 IBM Corporation 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.e4.ui.internal.services; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.service.event.EventAdmin; -import org.osgi.util.tracker.ServiceTracker; - -public class Activator implements BundleActivator { - - private static Activator singleton; - - private ServiceTracker<EventAdmin, EventAdmin> eventAdminTracker; - private BundleContext bundleContext; - - /* - * Returns the singleton for this Activator. Callers should be aware that - * this will return null if the bundle is not active. - */ - public static Activator getDefault() { - return singleton; - } - - @Override - public void start(BundleContext context) throws Exception { - bundleContext = context; - singleton = this; - } - - /* - * Return the debug options service, if available. - */ - public EventAdmin getEventAdmin() { - if (eventAdminTracker == null) { - eventAdminTracker = new ServiceTracker<>(bundleContext, EventAdmin.class, null); - eventAdminTracker.open(); - } - return eventAdminTracker.getService(); - } - - @Override - public void stop(BundleContext context) throws Exception { - if (eventAdminTracker != null) { - eventAdminTracker.close(); - eventAdminTracker = null; - } - bundleContext = null; - singleton = null; - } - - public BundleContext getBundleContext() { - return bundleContext; - } -} diff --git a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/ServiceMessages.java b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/ServiceMessages.java deleted file mode 100644 index 4a0fad96d77..00000000000 --- a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/ServiceMessages.java +++ /dev/null @@ -1,30 +0,0 @@ -/********************************************************************** - * Copyright (c) 2005, 2015 IBM Corporation 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: - * IBM - Initial API and implementation - **********************************************************************/ -package org.eclipse.e4.ui.internal.services; - -import org.eclipse.osgi.util.NLS; - -public class ServiceMessages extends NLS { - - private static final String BUNDLE_NAME = "org.eclipse.e4.ui.internal.services.serviceMessages"; //$NON-NLS-1$ - - // Event broker - public static String NO_EVENT_ADMIN; - public static String NO_BUNDLE_CONTEXT; - - static { - // load message values from bundle file - reloadMessages(); - } - - public static void reloadMessages() { - NLS.initializeMessages(BUNDLE_NAME, ServiceMessages.class); - } -}
\ No newline at end of file diff --git a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/serviceMessages.properties b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/serviceMessages.properties deleted file mode 100644 index 2c6580e778e..00000000000 --- a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/internal/services/serviceMessages.properties +++ /dev/null @@ -1,14 +0,0 @@ -############################################################################### -# Copyright (c) 2009 IBM Corporation 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: -# IBM Corporation - initial API and implementation -############################################################################### - -# Event broker messages -NO_EVENT_ADMIN=Event Admin service is not available, unable to publish event {0}. -NO_BUNDLE_CONTEXT=Bundle context is not available, unable to subscribe for {0}. diff --git a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/events/EventBrokerFactory.java b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/events/EventBrokerFactory.java index 9bacdcb5301..6a861f15ed4 100644 --- a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/events/EventBrokerFactory.java +++ b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/events/EventBrokerFactory.java @@ -17,6 +17,8 @@ import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.ui.services.internal.events.EventBroker; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.event.EventAdmin; @@ -25,6 +27,16 @@ import org.osgi.service.component.annotations.Component; */ @Component(service = IContextFunction.class, property = "service.context.key=org.eclipse.e4.core.services.events.IEventBroker") public class EventBrokerFactory extends ContextFunction { + + // mandatory static reference to EventAdmin to ensure it is available before + // the factory is activated + + @Reference + void setEventAdmin(EventAdmin admin) { + // don't need to do anything with the EventAdmin here + // we only need to ensure it is available before starting this factory + } + @Override public Object compute(IEclipseContext context, String contextKey) { EventBroker broker = context.getLocal(EventBroker.class); diff --git a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/internal/events/EventBroker.java b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/internal/events/EventBroker.java index 9804f0a6ca7..9a5fbd3e49b 100644 --- a/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/internal/events/EventBroker.java +++ b/bundles/org.eclipse.e4.ui.services/src/org/eclipse/e4/ui/services/internal/events/EventBroker.java @@ -23,14 +23,9 @@ import javax.annotation.PreDestroy; import javax.inject.Inject; import org.eclipse.e4.core.di.annotations.Optional; import org.eclipse.e4.core.services.events.IEventBroker; -import org.eclipse.e4.core.services.log.Logger; import org.eclipse.e4.ui.di.UISynchronize; -import org.eclipse.e4.ui.internal.services.Activator; -import org.eclipse.e4.ui.internal.services.ServiceMessages; -import org.eclipse.osgi.util.NLS; -import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleException; +import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceRegistration; import org.osgi.service.event.Event; import org.osgi.service.event.EventAdmin; @@ -40,57 +35,20 @@ import org.osgi.service.event.EventHandler; public class EventBroker implements IEventBroker { // TBD synchronization - private Map<EventHandler, Collection<ServiceRegistration<?>>> registrations = new HashMap<EventHandler, Collection<ServiceRegistration<?>>>(); + private Map<EventHandler, Collection<ServiceRegistration<?>>> registrations = new HashMap<>(); @Inject @Optional - Logger logger; + UISynchronize uiSync; @Inject - @Optional - UISynchronize uiSync; + EventAdmin eventAdmin; - // This is a temporary code to ensure that bundle containing - // EventAdmin implementation is started. This code it to be removed once - // the proper method to start EventAdmin is added. - static { - EventAdmin eventAdmin = Activator.getDefault().getEventAdmin(); - if (eventAdmin == null) { - Bundle[] bundles = Activator.getDefault().getBundleContext().getBundles(); - for (Bundle bundle : bundles) { - if (!"org.eclipse.equinox.event".equals(bundle.getSymbolicName())) - continue; - try { - bundle.start(Bundle.START_TRANSIENT); - } catch (BundleException e) { - e.printStackTrace(); - } - break; - } - } - } - - public EventBroker() { - // placeholder - } + BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext(); @Override public boolean send(String topic, Object data) { Event event = constructEvent(topic, data); - Activator activator = Activator.getDefault(); - if (activator == null) { - if (logger != null) { - logger.error(NLS.bind(ServiceMessages.NO_EVENT_ADMIN, event.toString())); - } - return false; - } - EventAdmin eventAdmin = activator.getEventAdmin(); - if (eventAdmin == null) { - if (logger != null) { - logger.error(NLS.bind(ServiceMessages.NO_EVENT_ADMIN, event.toString())); - } - return false; - } eventAdmin.sendEvent(event); return true; } @@ -98,20 +56,6 @@ public class EventBroker implements IEventBroker { @Override public boolean post(String topic, Object data) { Event event = constructEvent(topic, data); - Activator activator = Activator.getDefault(); - if (activator == null) { - if (logger != null) { - logger.error(NLS.bind(ServiceMessages.NO_EVENT_ADMIN, event.toString())); - } - return false; - } - EventAdmin eventAdmin = activator.getEventAdmin(); - if (eventAdmin == null) { - if (logger != null) { - logger.error(NLS.bind(ServiceMessages.NO_EVENT_ADMIN, event.toString())); - } - return false; - } eventAdmin.postEvent(event); return true; } @@ -146,10 +90,11 @@ public class EventBroker implements IEventBroker { } event = new Event(topic, map); } else { - Dictionary<String, Object> d = new Hashtable<String, Object>(2); + Dictionary<String, Object> d = new Hashtable<>(2); d.put(EventConstants.EVENT_TOPIC, topic); - if (data != null) + if (data != null) { d.put(IEventBroker.DATA, data); + } event = new Event(topic, d); } return event; @@ -171,26 +116,18 @@ public class EventBroker implements IEventBroker { @Override public boolean subscribe(String topic, String filter, EventHandler eventHandler, boolean headless) { - BundleContext bundleContext = Activator.getDefault().getBundleContext(); - if (bundleContext == null) { - if (logger != null) { - logger.error(NLS.bind(ServiceMessages.NO_BUNDLE_CONTEXT, topic)); - } - return false; - } String[] topics = new String[] {topic}; - Dictionary<String, Object> d = new Hashtable<String, Object>(); + Dictionary<String, Object> d = new Hashtable<>(); d.put(EventConstants.EVENT_TOPIC, topics); - if (filter != null) + if (filter != null) { d.put(EventConstants.EVENT_FILTER, filter); + } EventHandler wrappedHandler = new UIEventHandler(eventHandler, headless ? null : uiSync); - ServiceRegistration<?> registration = bundleContext.registerService( - EventHandler.class.getName(), wrappedHandler, d); - Collection<ServiceRegistration<?>> handled = registrations - .get(eventHandler); + ServiceRegistration<?> registration = bundleContext.registerService(EventHandler.class.getName(), + wrappedHandler, d); + Collection<ServiceRegistration<?>> handled = registrations.get(eventHandler); if (handled == null) { - registrations.put(eventHandler, - handled = new ArrayList<ServiceRegistration<?>>()); + registrations.put(eventHandler, handled = new ArrayList<>()); } handled.add(registration); return true; @@ -198,8 +135,7 @@ public class EventBroker implements IEventBroker { @Override public boolean unsubscribe(EventHandler eventHandler) { - Collection<ServiceRegistration<?>> handled = registrations - .remove(eventHandler); + Collection<ServiceRegistration<?>> handled = registrations.remove(eventHandler); if (handled == null || handled.isEmpty()) return false; for (ServiceRegistration<?> r : handled) { @@ -210,13 +146,10 @@ public class EventBroker implements IEventBroker { @PreDestroy void dispose() { - Collection<Collection<ServiceRegistration<?>>> values = new ArrayList<Collection<ServiceRegistration<?>>>( - registrations.values()); + Collection<Collection<ServiceRegistration<?>>> values = new ArrayList<>(registrations.values()); registrations.clear(); for (Collection<ServiceRegistration<?>> handled : values) { for (ServiceRegistration<?> registration : handled) { - // System.out.println("EventBroker dispose:" + registration[i] + - // ")"); registration.unregister(); } } |
