use of org.eclipse.osgi.framework.eventmgr.EventManager in project rt.equinox.framework by eclipse.
the class EquinoxEventPublisher method resetEventManager.
private void resetEventManager(EventManager newEventManager) {
EventManager currentEventManager;
synchronized (this.monitor) {
currentEventManager = eventManager;
eventManager = newEventManager;
}
if (currentEventManager != null) {
currentEventManager.close();
}
}
use of org.eclipse.osgi.framework.eventmgr.EventManager in project rt.equinox.framework by eclipse.
the class MRUBundleFileList method add.
/**
* Adds a BundleFile which is about to be opened to the MRU list. If
* the number of open BundleFiles == the fileLimit then the least
* recently used BundleFile is closed.
* @param bundleFile the bundle file about to be opened.
* @return true if back pressure is needed
*/
public boolean add(BundleFile bundleFile) {
if (fileLimit < MIN)
// MRU is disabled
return false;
BundleFile toRemove = null;
EventManager manager = null;
boolean backpressureNeeded = false;
synchronized (this) {
if (bundleFile.getMruIndex() >= 0)
// do nothing; someone is trying add a bundleFile that is already in an MRU list
return false;
// default to the first slot
int index = 0;
if (numOpen < fileLimit) {
// find the first null slot to use in the MRU
for (int i = 0; i < fileLimit; i++) if (bundleFileList[i] == null) {
index = i;
break;
}
} else {
// numOpen has reached the fileLimit
// find the least recently used bundleFile and close it
// and use its slot for the new bundleFile to be opened.
index = 0;
for (int i = 1; i < fileLimit; i++) if (useStampList[i] < useStampList[index])
index = i;
toRemove = bundleFileList[index];
if (toRemove.getMruIndex() != index)
// $NON-NLS-1$//$NON-NLS-2$
throw new IllegalStateException("The BundleFile has the incorrect mru index: " + index + " != " + toRemove.getMruIndex());
removeInternal(toRemove);
backpressureNeeded = isBackPressureNeeded();
}
// found an index to place to bundleFile to be opened
bundleFileList[index] = bundleFile;
bundleFile.setMruIndex(index);
incUseStamp(index);
numOpen++;
if (toRemove != null) {
if (bundleFileCloserManager == null)
// $NON-NLS-1$
bundleFileCloserManager = new EventManager("Bundle File Closer");
manager = bundleFileCloserManager;
}
}
// must not close the toRemove bundle file while holding the lock of another bundle file (bug 161976)
// This queues the bundle file for close asynchronously.
closeBundleFile(toRemove, manager);
return backpressureNeeded;
}
use of org.eclipse.osgi.framework.eventmgr.EventManager in project ecf by eclipse.
the class EndpointDescriptionLocator method start.
public void start() {
// For service info and endpoint description factories
// set the service ranking to Integer.MIN_VALUE
// so that any other registered factories will be preferred
final Properties properties = new Properties();
properties.put(Constants.SERVICE_RANKING, new Integer(Integer.MIN_VALUE));
serviceInfoFactory = new ServiceInfoFactory();
defaultServiceInfoFactoryRegistration = context.registerService(IServiceInfoFactory.class.getName(), serviceInfoFactory, (Dictionary) properties);
defaultEndpointDescriptionFactory = new DiscoveredEndpointDescriptionFactory();
defaultEndpointDescriptionFactoryRegistration = context.registerService(IDiscoveredEndpointDescriptionFactory.class.getName(), defaultEndpointDescriptionFactory, (Dictionary) properties);
// setup/register default endpointDescriptionReader
defaultEndpointDescriptionReaderRegistration = context.registerService(IEndpointDescriptionReader.class.getName(), new EndpointDescriptionReader(), (Dictionary) properties);
// Create thread group, event manager, and eventQueue, and setup to
// dispatch EndpointListenerEvents
ThreadGroup eventGroup = new ThreadGroup(// $NON-NLS-1$
"RSA EndpointDescriptionLocator ThreadGroup");
eventGroup.setDaemon(true);
eventManager = new EventManager("RSA EndpointDescriptionLocator Dispatcher", // $NON-NLS-1$
eventGroup);
eventQueue = new ListenerQueue(eventManager);
CopyOnWriteIdentityMap listeners = new CopyOnWriteIdentityMap();
listeners.put(this, this);
eventQueue.queueListeners(listeners.entrySet(), new EventDispatcher() {
public void dispatchEvent(Object eventListener, Object listenerObject, int eventAction, Object eventObject) {
// $NON-NLS-1$
final String logMethodName = "dispatchEvent";
// We now dispatch both EndpointListenerEvents
if (eventObject instanceof EndpointListenerEvent) {
final EndpointListenerEvent event = (EndpointListenerEvent) eventObject;
final EndpointListener endpointListener = event.getEndpointListener();
final EndpointDescription endpointDescription = event.getEndointDescription();
final String matchingFilter = event.getMatchingFilter();
try {
boolean discovered = event.isDiscovered();
trace("endpointListener.discovered=" + discovered + " ", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"fwk=" + getFrameworkUUID() + ", endpointListener=" + endpointListener + // $NON-NLS-1$
", endpointDescription=" + endpointDescription + // $NON-NLS-1$
", matchingFilter=" + matchingFilter);
if (discovered)
endpointListener.endpointAdded(endpointDescription, matchingFilter);
else
endpointListener.endpointRemoved(endpointDescription, matchingFilter);
} catch (Exception e) {
String message = // $NON-NLS-1$
"Exception in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
" description=" + endpointDescription + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
} catch (LinkageError e) {
String message = // $NON-NLS-1$
"LinkageError in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
" description=" + endpointDescription + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
} catch (AssertionError e) {
String message = // $NON-NLS-1$
"AssertionError in EndpointListener listener=" + endpointListener + // $NON-NLS-1$
" description=" + endpointDescription + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
}
// and EndpointEventListenerEvents
} else if (eventObject instanceof EndpointEventListenerEvent) {
final EndpointEventListenerEvent event = (EndpointEventListenerEvent) eventObject;
final EndpointEventListener endpointEventListener = event.getEndpointEventListener();
final EndpointEvent endpointEvent = event.getEndpointEvent();
final String matchingFilter = event.getMatchingFilter();
try {
trace("endpointEventListener.discovered=" + getEndpointEventTypeAsString(endpointEvent.getType()) + " ", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"fwk=" + getFrameworkUUID() + ", endpointEventListener=" + endpointEventListener + // $NON-NLS-1$
", endpointEvent=" + endpointEvent + // $NON-NLS-1$
", matchingFilter=" + matchingFilter);
endpointEventListener.endpointChanged(endpointEvent, matchingFilter);
} catch (Exception e) {
String message = // $NON-NLS-1$
"Exception in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
" event=" + endpointEvent + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
} catch (LinkageError e) {
String message = // $NON-NLS-1$
"LinkageError in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
" event=" + endpointEvent + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
} catch (AssertionError e) {
String message = // $NON-NLS-1$
"AssertionError in EndpointEventListener listener=" + endpointEventListener + // $NON-NLS-1$
" event=" + endpointEvent + // $NON-NLS-1$
" matchingFilter=" + matchingFilter;
logError(logMethodName, message, e);
}
}
}
});
// Register the endpoint listener tracker, so that endpoint listeners
// that are subsequently added
// will then be notified of discovered endpoints
endpointListenerTracker = new ServiceTracker(context, EndpointListener.class.getName(), new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
if (context == null)
return null;
EndpointListener listener = (EndpointListener) context.getService(reference);
if (listener == null)
return null;
Collection<org.osgi.service.remoteserviceadmin.EndpointDescription> allDiscoveredEndpointDescriptions = getEDs();
for (org.osgi.service.remoteserviceadmin.EndpointDescription ed : allDiscoveredEndpointDescriptions) {
EndpointDescriptionLocator.EndpointListenerHolder[] endpointListenerHolders = getMatchingEndpointListenerHolders(new ServiceReference[] { reference }, ed);
if (endpointListenerHolders != null) {
for (int i = 0; i < endpointListenerHolders.length; i++) {
queueEndpointDescription(endpointListenerHolders[i].getListener(), endpointListenerHolders[i].getDescription(), endpointListenerHolders[i].getMatchingFilter(), true);
}
}
}
return listener;
}
public void modifiedService(ServiceReference reference, Object service) {
}
public void removedService(ServiceReference reference, Object service) {
}
});
endpointListenerTracker.open();
// Register the endpoint event listener tracker, so that endpoint event
// listeners
// that are subsequently added
// will then be notified of discovered endpoints
endpointEventListenerTracker = new ServiceTracker(context, EndpointEventListener.class.getName(), new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
if (context == null)
return null;
EndpointEventListener listener = (EndpointEventListener) context.getService(reference);
if (listener == null)
return null;
Collection<org.osgi.service.remoteserviceadmin.EndpointDescription> allDiscoveredEndpointDescriptions = getEDs();
for (org.osgi.service.remoteserviceadmin.EndpointDescription ed : allDiscoveredEndpointDescriptions) {
EndpointDescriptionLocator.EndpointEventListenerHolder[] endpointEventListenerHolders = getMatchingEndpointEventListenerHolders(new ServiceReference[] { reference }, ed, EndpointEvent.ADDED);
if (endpointEventListenerHolders != null) {
for (int i = 0; i < endpointEventListenerHolders.length; i++) {
queueEndpointDescription(endpointEventListenerHolders[i].getListener(), endpointEventListenerHolders[i].getDescription(), endpointEventListenerHolders[i].getMatchingFilter(), endpointEventListenerHolders[i].getType());
}
}
}
return listener;
}
public void modifiedService(ServiceReference reference, Object service) {
}
public void removedService(ServiceReference reference, Object service) {
}
});
endpointEventListenerTracker.open();
locatorListeners = new HashMap();
// Create locator service tracker, so new IDiscoveryLocators can
// be used to discover endpoint descriptions
locatorServiceTracker = new ServiceTracker(context, IDiscoveryLocator.class.getName(), new LocatorTrackerCustomizer());
locatorServiceTracker.open();
// Create bundle tracker for reading local/xml-file endpoint
// descriptions
bundleTrackerCustomizer = new EndpointDescriptionBundleTrackerCustomizer();
bundleTracker = new BundleTracker(context, Bundle.ACTIVE | Bundle.STARTING, bundleTrackerCustomizer);
// This may trigger local endpoint description discovery
bundleTracker.open();
this.endpointLocatorReg = this.context.registerService(IEndpointDescriptionLocator.class, this, null);
}
use of org.eclipse.osgi.framework.eventmgr.EventManager in project ecf by eclipse.
the class DistributedEventAdmin method localDispatch.
/**
* Locally dispatch an Event. This method is used to deliver an
* {@link Event} to matching {@link EventHandler}s that are registered in
* the local OSGi service registry.
*
* @param dispatchedEvent
* the Event to dispatch. Will not be <code>null</code>.
* @param isAsync
* <code>true</code> if the dispatch should be done
* asynchronously (non-blocking), <code>false</code> if the
* dispatch should be done synchronously.
*/
protected void localDispatch(Event dispatchedEvent, boolean isAsync) {
EventManager currentManager = eventManager;
if (currentManager == null) {
return;
}
if (dispatchedEvent == null) {
log.log(LogService.LOG_ERROR, "Null event passed to EventAdmin was ignored.");
}
Event event = notifyPreLocalDispatch(dispatchedEvent);
if (event != null) {
String eventTopic = event.getTopic();
try {
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new TopicPermission(eventTopic, TopicPermission.PUBLISH));
} catch (SecurityException e) {
logError("Caller bundle does not have TopicPermission to publish topic " + eventTopic, e);
throw e;
}
Set eventHandlerWrappers = eventHandlerTracker.getHandlers(eventTopic);
SecurityManager sm = System.getSecurityManager();
Permission perm = (sm == null) ? null : new TopicPermission(eventTopic, TopicPermission.SUBSCRIBE);
CopyOnWriteIdentityMap listeners = new CopyOnWriteIdentityMap();
Iterator iter = eventHandlerWrappers.iterator();
while (iter.hasNext()) {
EventHandlerWrapper wrapper = (EventHandlerWrapper) iter.next();
listeners.put(wrapper, perm);
}
ListenerQueue listenerQueue = new ListenerQueue(currentManager);
listenerQueue.queueListeners(listeners.entrySet(), eventHandlerTracker);
if (isAsync) {
listenerQueue.dispatchEventAsynchronous(0, event);
} else {
listenerQueue.dispatchEventSynchronous(0, event);
}
notifyPostLocalDispatch(event);
}
}
use of org.eclipse.osgi.framework.eventmgr.EventManager in project ecf by eclipse.
the class DistributedEventAdmin method start.
/**
* Start this distributed event admin instance. This method should be called
* prior to registering this object as an {@link EventAdmin} implementation
* with the OSGi service registry.
*/
public void start() {
if (logTracker != null)
logTracker.open();
// $NON-NLS-1$
ThreadGroup eventGroup = new ThreadGroup("Distributed EventAdmin");
eventGroup.setDaemon(true);
eventManager = new EventManager("Distributed EventAdmin Async Event Dispatcher Thread", eventGroup);
eventHandlerTracker.open();
// Other services can contribute Event topic filters which will be ignored
// by the distribution part of DistributedEventAdmin. This is primarily useful
// in cases where the Event data cannot be serialized or serialization is too
// expensive.
etfServiceTracker = new ServiceTracker(this.context, EventTopicFilter.class, new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
final EventTopicFilter etf = (EventTopicFilter) context.getService(reference);
addEventTopicFilters(etf.getFilters());
return etf;
}
public void modifiedService(ServiceReference reference, Object service) {
// nop
}
public void removedService(ServiceReference reference, Object service) {
final EventTopicFilter etf = (EventTopicFilter) service;
removeEventTopicFilters(etf.getFilters());
}
});
etfServiceTracker.open();
// SerializationHandler are responsible to handle serialization of Event properties
shServiceTracker = new ServiceTracker(this.context, SerializationHandler.class, new ServiceTrackerCustomizer() {
public Object addingService(ServiceReference reference) {
final SerializationHandler sh = (SerializationHandler) context.getService(reference);
topic2serializationHandler.put(sh.getTopic(), sh);
return sh;
}
public void modifiedService(ServiceReference reference, Object service) {
// nop
}
public void removedService(ServiceReference reference, Object service) {
final SerializationHandler sh = (SerializationHandler) service;
topic2serializationHandler.remove(sh.getTopic());
}
});
shServiceTracker.open();
}
Aggregations