use of org.osgi.framework.Filter in project aries by apache.
the class AbstractIntegrationTest method getOsgiService.
protected <T> T getOsgiService(BundleContext bc, Class<T> type, String filter, long timeout) {
ServiceTracker tracker = null;
try {
String flt;
if (filter != null) {
if (filter.startsWith("(")) {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
} else {
flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
}
} else {
flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
}
Filter osgiFilter = FrameworkUtil.createFilter(flt);
tracker = new ServiceTracker(bc == null ? bundleContext : bc, osgiFilter, null);
tracker.open();
// add tracker to the list of trackers we close at tear down
srs.add(tracker);
Object x = tracker.waitForService(timeout);
Object svc = type.cast(x);
if (svc == null) {
throw new RuntimeException("Gave up waiting for service " + flt);
}
return type.cast(svc);
} catch (InvalidSyntaxException e) {
throw new IllegalArgumentException("Invalid filter", e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
use of org.osgi.framework.Filter in project aries by apache.
the class AbstractJPATransactionTest method getService.
protected <T> T getService(Class<T> clazz, String filter, long timeout) throws InvalidSyntaxException {
Filter f = FrameworkUtil.createFilter(filter == null ? "(|(foo=bar)(!(foo=bar)))" : filter);
ServiceTracker<T, T> tracker = new ServiceTracker<T, T>(context, clazz, null) {
@Override
public T addingService(ServiceReference<T> reference) {
return f.match(reference) ? super.addingService(reference) : null;
}
};
tracker.open();
try {
T t = tracker.waitForService(timeout);
if (t == null) {
throw new NoSuchElementException(clazz.getName());
}
return t;
} catch (InterruptedException e) {
throw new RuntimeException("Error waiting for service " + clazz.getName(), e);
} finally {
trackers.add(tracker);
}
}
use of org.osgi.framework.Filter in project aries by apache.
the class ImportedServiceImpl method generateAttributeFilter.
private Filter generateAttributeFilter(Map<String, String> attrsToPopulate) throws InvalidAttributeException {
logger.debug(LOG_ENTRY, "generateAttributeFilter", new Object[] { attrsToPopulate });
Filter result = null;
try {
attrsToPopulate.put(ModellingConstants.OBR_SERVICE, ModellingConstants.OBR_SERVICE);
if (_blueprintFilter != null) {
// We may get blueprint filters of the form (&(a=b)(c=d)). We can't put these in 'whole' because we'll
// end up generating a filter of the form (&(objectClass=foo)(&(a=b)(c=d)) which subsequent calls to
// parseFilter will choke on. So as an interim fix we'll strip off a leading &( and trailing ) if present.
String reducedBlueprintFilter;
if (_blueprintFilter.startsWith("(&")) {
reducedBlueprintFilter = _blueprintFilter.substring(2, _blueprintFilter.length() - 1);
} else {
reducedBlueprintFilter = _blueprintFilter;
}
attrsToPopulate.put(ManifestHeaderProcessor.NESTED_FILTER_ATTRIBUTE, reducedBlueprintFilter);
}
if (_componentName != null) {
attrsToPopulate.put("osgi.service.blueprint.compname", _componentName);
}
if (_iface != null) {
attrsToPopulate.put(Constants.OBJECTCLASS, _iface);
}
_attribFilterString = ManifestHeaderProcessor.generateFilter(_attributes);
if (!"".equals(_attribFilterString)) {
result = FrameworkUtil.createFilter(FilterUtils.removeMandatoryFilterToken(_attribFilterString));
}
} catch (InvalidSyntaxException isx) {
InvalidAttributeException iax = new InvalidAttributeException("A syntax error occurred attempting to parse the blueprint filter string '" + _blueprintFilter + "' for element with id " + _id + ": " + isx.getLocalizedMessage(), isx);
logger.debug(LOG_EXIT, "generateAttributeFilter", new Object[] { isx });
throw iax;
}
logger.debug(LOG_EXIT, "generateAttributeFilter", new Object[] { result });
return result;
}
use of org.osgi.framework.Filter in project aries by apache.
the class ConsumerHeaderProcessor method processRequireCapabilityHeader.
private static Set<WeavingData> processRequireCapabilityHeader(String consumerHeader) throws InvalidSyntaxException {
Set<WeavingData> weavingData = new HashSet<WeavingData>();
List<GenericMetadata> requirements = ManifestHeaderProcessor.parseRequirementString(consumerHeader);
GenericMetadata extenderRequirement = findRequirement(requirements, SpiFlyConstants.EXTENDER_CAPABILITY_NAMESPACE, SpiFlyConstants.PROCESSOR_EXTENDER_NAME);
Collection<GenericMetadata> serviceLoaderRequirements = findAllMetadata(requirements, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE);
if (extenderRequirement != null) {
ArgRestrictions ar = new ArgRestrictions();
ar.addRestriction(0, Class.class.getName());
MethodRestriction mr = new MethodRestriction("load", ar);
List<BundleDescriptor> allowedBundles = new ArrayList<BundleDescriptor>();
for (GenericMetadata req : serviceLoaderRequirements) {
String slFilterString = req.getDirectives().get(SpiFlyConstants.FILTER_DIRECTIVE);
if (slFilterString != null) {
Filter slFilter = FrameworkUtil.createFilter(slFilterString);
allowedBundles.add(new BundleDescriptor(slFilter));
}
}
weavingData.add(createWeavingData(ServiceLoader.class.getName(), "load", mr, allowedBundles));
}
return weavingData;
}
use of org.osgi.framework.Filter in project aries by apache.
the class ProviderBundleTrackerCustomizer method findRequirement.
private static GenericMetadata findRequirement(List<GenericMetadata> requirements, String namespace, String type) throws InvalidSyntaxException {
Dictionary<String, String> nsAttr = new Hashtable<String, String>();
nsAttr.put(namespace, type);
for (GenericMetadata req : requirements) {
if (namespace.equals(req.getNamespace())) {
String filterString = req.getDirectives().get(SpiFlyConstants.FILTER_DIRECTIVE);
if (filterString != null) {
Filter filter = FrameworkUtil.createFilter(filterString);
if (filter.match(nsAttr)) {
return req;
}
}
}
}
return null;
}
Aggregations