下面列出了org.osgi.framework.Filter#matches ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Internal implies method. Used by the implies and the permission
* collection implies methods.
*
* @param requested The requested CoordinationPermission which has already
* be validated as a proper argument. The requested
* CoordinationPermission must not have a filter expression.
* @param effective The effective actions with which to start.
* @return {@code true} if the specified permission is implied by this
* object; {@code false} otherwise.
*/
boolean implies0(CoordinationPermission requested, int effective) {
/* check actions first - much faster */
effective |= action_mask;
final int desired = requested.action_mask;
if ((effective & desired) != desired) {
return false;
}
/* Get filter */
Filter f = filter;
if (f == null) {
// it's "*"
return true;
}
return f.matches(requested.getProperties());
}
private void addToProvidersIfMatching(Resource res, List<Capability> providers, Requirement req) {
String f = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
Filter filter = null;
if(f != null) {
try {
filter = bc.createFilter(f);
} catch (InvalidSyntaxException e) {
// TODO log filter failure, skip
System.err.println("Failed, " + f + ". " + e);
return;
}
}
for(Capability c : res.getCapabilities(req.getNamespace())) {
if(filter != null && !filter.matches(c.getAttributes())) {
continue;
}
providers.add(c);
}
}
public Collection<ServiceReferenceDTO> getServiceReferences(String filter) throws InvalidSyntaxException {
Filter f = context.createFilter(filter);
List<ServiceReferenceDTO> filtered = new ArrayList<ServiceReferenceDTO>();
for(ServiceReferenceDTO r : getServiceReferences()){
if(f.matches(r.properties)){
filtered.add(r);
}
}
return filtered;
}
/**
* Internal implies method. Used by the implies and the permission
* collection implies methods.
*
* @param requested The requested SubsystemPermision which has already been
* validated as a proper argument. The requested SubsystemPermission
* must not have a filter expression.
* @param effective The effective actions with which to start.
* @return {@code true} if the specified permission is implied by this
* object; {@code false} otherwise.
*/
boolean implies0(SubsystemPermission requested, int effective) {
/* check actions first - much faster */
effective |= action_mask;
final int desired = requested.action_mask;
if ((effective & desired) != desired) {
return false;
}
/* Get our filter */
Filter f = filter;
if (f == null) {
// it's "*"
return true;
}
/* is requested a wildcard filter? */
if (requested.subsystem == null) {
return false;
}
Map<String, Object> requestedProperties = requested.getProperties();
if (requestedProperties == null) {
/*
* If the requested properties are null, then we have detected a
* recursion getting the subsystem location. So we return true to
* permit the subsystem location request in the SubsystemPermission
* check up the stack to succeed.
*/
return true;
}
return f.matches(requestedProperties);
}
@SuppressWarnings("unchecked")
public Collection<BundleCapability> findProviders(Requirement requirement) {
final String namespace = requirement.getNamespace();
final String filterStr = requirement.getDirectives().get("filter");
Filter filter;
if (filterStr != null) {
try {
filter = FrameworkUtil.createFilter(filterStr);
} catch (InvalidSyntaxException ise) {
final String msg = "Invalid filter directive '" + filterStr + "': " + ise;
throw new IllegalArgumentException(msg, ise);
}
} else {
filter = null;
}
HashSet<BundleCapability> res = new HashSet<BundleCapability>();
for (BundleGeneration bg : fwCtx.bundles.getBundleGenerations(null)) {
BundleRevisionImpl bri = bg.bundleRevision;
if (bri != null) {
for (BundleCapability bc : bri.getDeclaredCapabilities(namespace)) {
if (null == filter || filter.matches(bc.getAttributes())) {
res.add(bc);
}
}
}
}
return res;
}
static List<Capability> filterWithIndex(final Requirement requirement,
final String filterStr,
final Concierge.CapabilityRegistry capabilityIndex)
throws InvalidSyntaxException {
final Set<String> values = new HashSet<String>();
final String namespace = requirement.getNamespace();
final Filter filter = fromString(filterStr);
final int prefilterResult = prefilter(namespace, filter,
capabilityIndex, INSUFFICIENT, false, values);
final List<Capability> candidates;
if (prefilterResult == REQUIRED) {
if (values.size() != 1) {
return Collections.emptyList();
}
return capabilityIndex.getByValue(namespace,
values.iterator().next());
} else if (prefilterResult == NECESSARY) {
// FIXME: check
if (values.size() != 1) {
candidates = capabilityIndex.getAll(namespace);
} else {
candidates = capabilityIndex.getByKey(namespace,
values.iterator().next());
}
} else {
candidates = capabilityIndex.getAll(namespace);
}
if (candidates == null) {
return Collections.emptyList();
}
final ArrayList<Capability> matches = new ArrayList<Capability>();
for (final Capability cap : candidates) {
if (filter.matches(cap.getAttributes()) && Concierge
.matches0(namespace, requirement, cap, filterStr)) {
matches.add(cap);
}
}
return matches;
}
/**
* Return true if the Declaration can be linked to the ImporterService.
*
* @param declaration The Declaration
* @param declarationBinderRef The ServiceReference<ImporterService> of the ImporterService
* @return true if the Declaration can be linked to the ImporterService
*/
public boolean canBeLinked(D declaration, ServiceReference<S> declarationBinderRef) {
// Evaluate the target filter of the ImporterService on the Declaration
Filter filter = bindersManager.getTargetFilter(declarationBinderRef);
return filter.matches(declaration.getMetadata());
}