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 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)) {
dispatcher.m_logger.log(bundle, Logger.LOG_ERROR, "EventDispatcher: Error during dispatch.", th);
dispatcher.fireFrameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, bundle, th));
}
}
}
}
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class ServiceRegistry method getServiceReferences.
/**
* Get available (and accessible) service references.
*
* @param className : required interface
* @param expr : LDAP filter
* @return : the list of available service references.
* @throws InvalidSyntaxException
* occurs when the LDAP filter is malformed.
*/
public ServiceReference[] getServiceReferences(String className, String expr) throws InvalidSyntaxException {
synchronized (m_regs) {
// Define filter if expression is not null.
Filter filter = null;
if (expr != null) {
filter = m_context.createFilter(expr);
}
List refs = new ArrayList();
for (int i = 0; i < m_regs.size(); i++) {
ServiceRegistrationImpl reg = (ServiceRegistrationImpl) m_regs.get(i);
// Determine if the registered services matches the search
// criteria.
boolean matched = false;
// If className is null, then look at filter only.
if ((className == null) && ((filter == null) || filter.match(reg.getProperties()))) {
matched = true;
} else if (className != null) {
// If className is not null, then first match the
// objectClass property before looking at the
// filter.
Dictionary props = ((ServiceRegistrationImpl) reg).getProperties();
String[] objectClass = (String[]) props.get(Constants.OBJECTCLASS);
for (int classIdx = 0; classIdx < objectClass.length; classIdx++) {
if (objectClass[classIdx].equals(className) && ((filter == null) || filter.match(props))) {
matched = true;
break;
}
}
}
// Add reference if it was a match.
if (matched) {
refs.add(reg.getReference());
}
}
if (!refs.isEmpty()) {
return (ServiceReference[]) refs.toArray(new ServiceReference[refs.size()]);
}
return null;
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class ServiceDependencyHandler method createServiceInstance.
/**
* Create a Service instance object form the given Element.
* This method parse the given element and configure the service instance object.
* @param service : the Element describing the service instance
* @param conf : the configuration from the composite instance
* @throws ConfigurationException : the service instance cannot be created correctly
*/
private void createServiceInstance(Element service, Dictionary conf) throws ConfigurationException {
// Prepare the configuration to append.
Properties toAppend = new Properties();
Enumeration keys = conf.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (!(key.equals("instance.name") || key.equals("component"))) {
// Remove instance.name and component
toAppend.put(key, conf.get(key));
}
}
String spec = service.getAttribute("specification");
if (spec == null) {
throw new ConfigurationException("Malformed service : the specification attribute is mandatory");
}
// Cannot reinstantiate yourself
String filter = "(&(!(factory.name=" + getCompositeManager().getFactory().getComponentDescription().getName() + "))(factory.state=1))";
String givenFilter = service.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
Filter fil;
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("Malformed filter " + filter, e);
}
Properties prop = new Properties();
Element[] props = service.getElements("property");
for (int k = 0; props != null && k < props.length; k++) {
try {
InstanceHandler.parseProperty(props[k], prop);
} catch (ParseException e) {
throw new ConfigurationException("An instance configuration is invalid", e);
}
}
Properties instanceConfiguration = new Properties();
instanceConfiguration.putAll(prop);
instanceConfiguration.putAll(toAppend);
String aggregate = service.getAttribute("aggregate");
boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");
String optional = service.getAttribute("optional");
boolean opt = optional != null && optional.equalsIgnoreCase("true");
int policy = DependencyMetadataHelper.getPolicy(service);
Comparator cmp = DependencyMetadataHelper.getComparator(service, getCompositeManager().getGlobalContext());
SvcInstance inst = new SvcInstance(this, spec, instanceConfiguration, agg, opt, fil, cmp, policy);
m_instances.add(inst);
String sources = service.getAttribute("context-source");
if (sources != null) {
SourceManager source = new SourceManager(sources, filter, inst, getCompositeManager());
if (m_sources == null) {
m_sources = new ArrayList(1);
}
m_sources.add(source);
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class ServiceDependencyHandler method createServiceImport.
/**
* Create a Service importer object from the given Element.
* This method parse the given element and configure the service importer object.
* @param imp : Element describing the import
* @param confFilter : instance filter customization
* @throws ConfigurationException : the service importer cannot be created correctly
*/
private void createServiceImport(Element imp, Dictionary confFilter) throws ConfigurationException {
boolean optional = false;
boolean aggregate = false;
String specification = imp.getAttribute("specification");
if (specification == null) {
// Malformed import
error("Malformed import: the specification attribute is mandatory");
throw new ConfigurationException("Malformed import : the specification attribute is mandatory");
} else {
String opt = imp.getAttribute("optional");
optional = opt != null && opt.equalsIgnoreCase("true");
String agg = imp.getAttribute("aggregate");
aggregate = agg != null && agg.equalsIgnoreCase("true");
// Cannot import yourself
String original = "(&(objectClass=" + specification + ")(!(instance.name=" + getCompositeManager().getInstanceName() + ")))";
String filter = original;
String givenFilter = imp.getAttribute("filter");
if (givenFilter != null) {
// NOPMD
filter = "(&" + filter + givenFilter + ")";
}
String identitity = imp.getAttribute("id");
String scope = imp.getAttribute("scope");
// Get the default bundle context.
BundleContext context = getCompositeManager().getGlobalContext();
if (scope != null) {
if (scope.equalsIgnoreCase("global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.GLOBAL);
} else if (scope.equalsIgnoreCase("composite")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL);
} else if (scope.equalsIgnoreCase("composite+global")) {
context = new PolicyServiceContext(getCompositeManager().getGlobalContext(), getCompositeManager().getParentServiceContext(), PolicyServiceContext.LOCAL_AND_GLOBAL);
}
}
// Configure instance filter if available
if (confFilter != null && identitity != null && confFilter.get(identitity) != null) {
filter = "(&" + original + (String) confFilter.get(identitity) + ")";
}
Filter fil = null;
if (filter != null) {
try {
fil = getCompositeManager().getGlobalContext().createFilter(filter);
} catch (InvalidSyntaxException e) {
throw new ConfigurationException("A required filter " + filter + " is malformed", e);
}
}
Comparator cmp = DependencyMetadataHelper.getComparator(imp, getCompositeManager().getGlobalContext());
Class spec = DependencyMetadataHelper.loadSpecification(specification, getCompositeManager().getGlobalContext());
int policy = DependencyMetadataHelper.getPolicy(imp);
ServiceImporter importer = new ServiceImporter(spec, fil, aggregate, optional, cmp, policy, context, identitity, this);
m_importers.add(importer);
String sources = imp.getAttribute("context-source");
if (sources != null) {
SourceManager source = new SourceManager(sources, filter, importer, getCompositeManager());
if (m_sources == null) {
m_sources = new ArrayList(1);
}
m_sources.add(source);
}
}
}
use of org.osgi.framework.Filter in project felix by apache.
the class FilterTest method testMissingAttribute.
public void testMissingAttribute() {
Dictionary dict = new Hashtable();
dict.put("one", "one-value");
dict.put("two", "two-value");
dict.put("three", "three-value");
Filter filter = null;
try {
filter = FrameworkUtil.createFilter("(missing=value)");
} catch (Exception ex) {
assertTrue("Filter should parse: " + ex, false);
}
assertFalse("Filter should not match: " + filter, filter.match(dict));
}
Aggregations