capint
6/26/2016 - 1:29 PM

Eclipse >> OSGI >> EventAdmin

Eclipse >> OSGI >> EventAdmin

Map<String, Object> properties = Maps.<String, Object> newHashMap();
properties.put("shouldRefresh", true);
BundleContext ctx = FrameworkUtil.getBundle(Activator.getDefault().getClass())
		.getBundleContext();
ServiceReference ref = ctx.getServiceReference(EventAdmin.class.getName());
EventAdmin eventAdmin = (EventAdmin) ctx.getService(ref);
String TOPIC = "org/osgi/framework/BundleEvent/SHOULD_REFRESH"
Event postEvent = new Event(topic, properties);
eventAdmin.sendEvent(postEvent);
EventAdmin service is part of the OSGi Compendium Specification and allows you 
to publish and listen to events via a pubish-subcribe system
Dictionary<String, String> dictionary = new Hashtable<String, String>();
String TOPIC = "org/osgi/framework/BundleEvent/SHOULD_REFRESH"
dictionary.put(EventConstants.EVENT_TOPIC, TOPIC);
BundleContext ctx = FrameworkUtil.getBundle(Activator.getDefault().getClass())
    .getBundleContext();
EventHandler handler = new EventHandler() {
	public void handleEvent(final Event event) {
		boolean shouldRefresh = event.getProperty("shouldRefresh");
		if (shouldRefresh) {
			getViewer().refresh();
		}
	}
};
ctx.registerService(EventHandler.class.getName(), handler, dictionary);