Fix #495 EventSender might leak service-references or miss them at all

Signed-off-by: laeubi <laeubi@laeubi-soft.de>
This commit is contained in:
laeubi 2016-04-08 08:51:05 +02:00
parent 2fb9b55da2
commit f7ffadfc2f
1 changed files with 23 additions and 23 deletions

View File

@ -26,6 +26,7 @@ import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;
/**
* Utility class for emiting OSGi EventAdmin events
@ -41,14 +42,13 @@ public class EventSender
private static final EventSender __instance = new EventSender();
private Bundle _myBundle;
private EventAdmin _eventAdmin;
private ServiceTracker _serviceTracker;
private EventSender ()
{
_myBundle = FrameworkUtil.getBundle(EventSender.class);
ServiceReference ref = _myBundle.getBundleContext().getServiceReference(EventAdmin.class.getName());
if (ref != null)
_eventAdmin = (EventAdmin)_myBundle.getBundleContext().getService(ref);
_serviceTracker = new ServiceTracker(_myBundle.getBundleContext(),EventAdmin.class.getName(),null);
_serviceTracker.open();
}
public static EventSender getInstance()
@ -66,24 +66,24 @@ public class EventSender
public void send (String topic, Bundle wab, String contextPath, Exception ex)
{
if (_eventAdmin == null)
return;
Dictionary<String,Object> props = new Hashtable<String,Object>();
props.put("bundle.symbolicName", wab.getSymbolicName());
props.put("bundle.id", wab.getBundleId());
props.put("bundle", wab);
props.put("bundle.version", wab.getVersion());
props.put("context.path", contextPath);
props.put("timestamp", System.currentTimeMillis());
props.put("extender.bundle", _myBundle);
props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
props.put("extender.bundle.id", _myBundle.getBundleId());
props.put("extender.bundle.version", _myBundle.getVersion());
if (FAILED_EVENT.equalsIgnoreCase(topic) && ex != null)
props.put("exception", ex);
_eventAdmin.sendEvent(new Event(topic, props));
EventAdmin service = (EventAdmin)_serviceTracker.getService();
if (service != null) {
Dictionary<String,Object> props = new Hashtable<String,Object>();
props.put("bundle.symbolicName", wab.getSymbolicName());
props.put("bundle.id", wab.getBundleId());
props.put("bundle", wab);
props.put("bundle.version", wab.getVersion());
props.put("context.path", contextPath);
props.put("timestamp", System.currentTimeMillis());
props.put("extender.bundle", _myBundle);
props.put("extender.bundle.symbolicName", _myBundle.getSymbolicName());
props.put("extender.bundle.id", _myBundle.getBundleId());
props.put("extender.bundle.version", _myBundle.getVersion());
if (FAILED_EVENT.equalsIgnoreCase(topic) && ex != null)
props.put("exception", ex);
service.sendEvent(new Event(topic, props));
}
}
}