use of org.osgi.framework.Filter in project felix by apache.
the class ResourceProvider method add.
@SuppressWarnings("deprecation")
public void add(ServiceReference ref, ResourceHandler handler) {
String filterString = (String) ref.getProperty("filter");
Filter filter = null;
if (filterString != null) {
try {
filter = m_context.createFilter(filterString);
} catch (InvalidSyntaxException e) {
Assert.fail("Could not create filter for resource handler: " + e);
return;
}
}
for (int i = 0; i < m_resources.length; i++) {
if (filter == null || filter.match(ResourceUtil.createProperties(m_resources[i]))) {
synchronized (m_handlers) {
m_handlers.put(handler, filter);
}
handler.added(m_resources[i]);
}
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class ResourceProvider method destroy.
public void destroy() {
Map<ResourceHandler, Filter> handlers = new HashMap<>();
synchronized (m_handlers) {
handlers.putAll(m_handlers);
}
for (Map.Entry<ResourceHandler, Filter> e : handlers.entrySet()) {
ResourceHandler handler = e.getKey();
Filter filter = e.getValue();
removeResources(handler, filter);
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class EventDispatcher method fireEventImmediately.
private static void fireEventImmediately(EventDispatcher dispatcher, int type, Map<BundleContext, List<ListenerInfo>> listeners, EventObject event, Dictionary<String, ?> oldProps) {
if (!listeners.isEmpty()) {
// Notify appropriate listeners.
for (Entry<BundleContext, List<ListenerInfo>> entry : listeners.entrySet()) {
for (ListenerInfo info : entry.getValue()) {
Bundle bundle = info.getBundle();
EventListener l = info.getListener();
Filter filter = info.getParsedFilter();
Object acc = info.getSecurityContext();
try {
if (type == Request.FRAMEWORK_EVENT) {
invokeFrameworkListenerCallback(bundle, l, event);
} else if (type == Request.BUNDLE_EVENT) {
invokeBundleListenerCallback(bundle, l, event);
} else if (type == Request.SERVICE_EVENT) {
invokeServiceListenerCallback(bundle, l, filter, acc, event, oldProps);
}
} catch (Throwable th) {
if ((type != Request.FRAMEWORK_EVENT) || (((FrameworkEvent) event).getType() != FrameworkEvent.ERROR)) {
System.out.println("EventDispatcher: Error during dispatch.");
th.printStackTrace();
dispatcher.fireFrameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, bundle, th));
}
}
}
}
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class EventDispatcher method updateListener.
public Filter updateListener(BundleContext bc, Class clazz, EventListener l, Filter filter) {
if (clazz == ServiceListener.class) {
synchronized (this) {
// Verify that the bundle context is still valid.
try {
bc.getBundle();
} catch (IllegalStateException ex) {
// Bundle context is no longer valid, so just return.
}
// See if the service listener is already registered; if so then
// update its filter per the spec.
List<ListenerInfo> infos = m_svcListeners.get(bc);
for (int i = 0; (infos != null) && (i < infos.size()); i++) {
ListenerInfo info = infos.get(i);
if (info.getBundleContext().equals(bc) && (info.getListenerClass() == clazz) && (info.getListener() == l)) {
// The spec says to update the filter in this case.
Filter oldFilter = info.getParsedFilter();
ListenerInfo newInfo = new ListenerInfo(info.getBundle(), info.getBundleContext(), info.getListenerClass(), info.getListener(), filter, info.getSecurityContext(), info.isRemoved());
m_svcListeners = updateListenerInfo(m_svcListeners, i, newInfo);
return oldFilter;
}
}
}
}
return null;
}
use of org.osgi.framework.Filter in project felix by apache.
the class EventDispatcher method addListener.
public Filter addListener(BundleContext bc, Class clazz, EventListener l, Filter filter) {
// Verify the listener.
if (l == null) {
throw new IllegalArgumentException("Listener is null");
} else if (!clazz.isInstance(l)) {
throw new IllegalArgumentException("Listener not of type " + clazz.getName());
}
// See if we can simply update the listener, if so then
// return immediately.
Filter oldFilter = updateListener(bc, clazz, l, filter);
if (oldFilter != null) {
return oldFilter;
}
// Lock the object to add the listener.
synchronized (this) {
// Verify that the bundle context is still valid.
try {
bc.getBundle();
} catch (IllegalStateException ex) {
// Bundle context is no longer valid, so just return.
}
Map<BundleContext, List<ListenerInfo>> listeners;
Object acc = null;
if (clazz == FrameworkListener.class) {
listeners = m_fwkListeners;
} else if (clazz == BundleListener.class) {
if (SynchronousBundleListener.class.isInstance(l)) {
listeners = m_syncBndlListeners;
} else {
listeners = m_bndlListeners;
}
} else if (clazz == ServiceListener.class) {
// Remember security context for filtering service events.
/* Object sm = System.getSecurityManager();
if (sm != null)
{
acc = ((SecurityManager) sm).getSecurityContext();
}*/
// We need to create a Set for keeping track of matching service
// registrations so we can fire ServiceEvent.MODIFIED_ENDMATCH
// events. We need a Set even if filter is null, since the
// listener can be updated and have a filter added later.
listeners = m_svcListeners;
} else {
throw new IllegalArgumentException("Unknown listener: " + l.getClass());
}
// Add listener.
ListenerInfo info = new ListenerInfo(bc.getBundle(), bc, clazz, l, filter, acc, false);
listeners = addListenerInfo(listeners, info);
if (clazz == FrameworkListener.class) {
m_fwkListeners = listeners;
} else if (clazz == BundleListener.class) {
if (SynchronousBundleListener.class.isInstance(l)) {
m_syncBndlListeners = listeners;
} else {
m_bndlListeners = listeners;
}
} else if (clazz == ServiceListener.class) {
m_svcListeners = listeners;
}
}
return null;
}
Aggregations