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:
parent
2fb9b55da2
commit
f7ffadfc2f
|
@ -26,6 +26,7 @@ import org.osgi.framework.FrameworkUtil;
|
||||||
import org.osgi.framework.ServiceReference;
|
import org.osgi.framework.ServiceReference;
|
||||||
import org.osgi.service.event.Event;
|
import org.osgi.service.event.Event;
|
||||||
import org.osgi.service.event.EventAdmin;
|
import org.osgi.service.event.EventAdmin;
|
||||||
|
import org.osgi.util.tracker.ServiceTracker;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for emiting OSGi EventAdmin events
|
* Utility class for emiting OSGi EventAdmin events
|
||||||
|
@ -41,14 +42,13 @@ public class EventSender
|
||||||
|
|
||||||
private static final EventSender __instance = new EventSender();
|
private static final EventSender __instance = new EventSender();
|
||||||
private Bundle _myBundle;
|
private Bundle _myBundle;
|
||||||
private EventAdmin _eventAdmin;
|
private ServiceTracker _serviceTracker;
|
||||||
|
|
||||||
private EventSender ()
|
private EventSender ()
|
||||||
{
|
{
|
||||||
_myBundle = FrameworkUtil.getBundle(EventSender.class);
|
_myBundle = FrameworkUtil.getBundle(EventSender.class);
|
||||||
ServiceReference ref = _myBundle.getBundleContext().getServiceReference(EventAdmin.class.getName());
|
_serviceTracker = new ServiceTracker(_myBundle.getBundleContext(),EventAdmin.class.getName(),null);
|
||||||
if (ref != null)
|
_serviceTracker.open();
|
||||||
_eventAdmin = (EventAdmin)_myBundle.getBundleContext().getService(ref);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EventSender getInstance()
|
public static EventSender getInstance()
|
||||||
|
@ -66,24 +66,24 @@ public class EventSender
|
||||||
|
|
||||||
public void send (String topic, Bundle wab, String contextPath, Exception ex)
|
public void send (String topic, Bundle wab, String contextPath, Exception ex)
|
||||||
{
|
{
|
||||||
if (_eventAdmin == null)
|
EventAdmin service = (EventAdmin)_serviceTracker.getService();
|
||||||
return;
|
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());
|
||||||
|
|
||||||
Dictionary<String,Object> props = new Hashtable<String,Object>();
|
if (FAILED_EVENT.equalsIgnoreCase(topic) && ex != null)
|
||||||
props.put("bundle.symbolicName", wab.getSymbolicName());
|
props.put("exception", ex);
|
||||||
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)
|
service.sendEvent(new Event(topic, props));
|
||||||
props.put("exception", ex);
|
}
|
||||||
|
|
||||||
_eventAdmin.sendEvent(new Event(topic, props));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue