下面列出了org.osgi.framework.hooks.bundle.EventHook#org.osgi.framework.hooks.service.FindHook 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Bundle[] filterWithBundleHooks(final BundleContext context,
final Collection<Bundle> bundles) {
final ConciergeCollections.RemoveOnlyList<Bundle> list = new ConciergeCollections.RemoveOnlyList<Bundle>(
bundles);
for (final ServiceReferenceImpl<org.osgi.framework.hooks.bundle.FindHook> sref : bundleFindHooks) {
final org.osgi.framework.hooks.bundle.FindHook findHook = sref
.getService(Concierge.this);
if (findHook != null) {
try {
findHook.find(context, list);
} catch (final Throwable t) {
// TODO: log?
}
}
sref.ungetService(Concierge.this);
}
if(context == this.context){
// if called from system bundle context, return original unfiltered bundles
return bundles.toArray(new Bundle[bundles.size()]);
}
return list.toArray(new Bundle[list.size()]);
}
/**
* Workaround https://issues.apache.org/jira/browse/KARAF-4899 by
* re-adding probe bundle to root region before any service lookup
* (avoids spurious service lookup issues due to region filtering)
*/
private void installRegionWorkaround(final BundleContext context) {
Map<String, ?> maxRanking = singletonMap(SERVICE_RANKING, Integer.MAX_VALUE);
// add probe to root region on install
context.registerService(EventHook.class,
(event, contexts) -> {
if (event.getType() == BundleEvent.INSTALLED) {
fixProbeRegion(event.getBundle());
}
}, new Hashtable<>(maxRanking));
// add probe to root region whenever a service it's listening to changes
context.registerService(EventListenerHook.class,
(event, listeners) -> {
if (((String[]) event.getServiceReference().getProperty(Constants.OBJECTCLASS))[0].contains("pax.exam")) {
listeners.keySet().stream().map(BundleContext::getBundle).forEach(this::fixProbeRegion);
}
}, new Hashtable<>(maxRanking));
// add probe to root region whenever it's directly looking up a service
context.registerService(FindHook.class,
(findContext, name, filter, allServices, references) -> {
if ((name != null && name.contains("pax.exam")) || (filter != null && filter.contains("pax.exam"))) {
fixProbeRegion(findContext.getBundle());
}
}, new Hashtable<>(maxRanking));
}
/**
*
*/
void filterServiceReferences(BundleContextImpl bc,
String service,
String filter,
boolean allServices,
Collection<ServiceReference<?>> refs)
{
@SuppressWarnings({ "unchecked", "rawtypes" })
final List<ServiceRegistrationImpl<FindHook>> srl
= (List) fwCtx.services.get(FindHook.class.getName());
if (srl != null) {
final RemoveOnlyCollection<ServiceReference<?>> filtered
= new RemoveOnlyCollection<ServiceReference<?>>(refs);
for (final ServiceRegistrationImpl<FindHook> fhr : srl) {
final ServiceReferenceImpl<FindHook> sr = fhr.reference;
final FindHook fh = sr.getService();
if (fh != null) {
try {
fh.find(bc, service, filter, allServices, filtered);
} catch (final Exception e) {
fwCtx.frameworkError(bc,
new BundleException("Failed to call find hook #" +
sr.getProperty(Constants.SERVICE_ID), e));
}
}
}
}
}
private final ServiceReference<?>[] getServiceReferences(
final String clazz, final String filter, final boolean all)
throws InvalidSyntaxException {
checkValid();
final Filter theFilter = RFC1960Filter.fromString(filter);
final Collection<ServiceReference<?>> references;
if (clazz == null) {
references = serviceRegistry.getAllValues();
} else {
references = serviceRegistry.get(clazz);
}
final List<ServiceReference<?>> result = new ArrayList<ServiceReference<?>>();
if (references != null) {
final ServiceReferenceImpl<?>[] refs = references
.toArray(new ServiceReferenceImpl[references.size()]);
for (int i = 0; i < refs.length; i++) {
if (theFilter.match(refs[i]) && (all
|| refs[i].isAssignableTo(bundle, (String[]) refs[i]
.getProperty(Constants.OBJECTCLASS)))) {
result.add(refs[i]);
}
}
}
if (!serviceFindHooks.isEmpty()) {
final Collection<ServiceReference<?>> c = new ConciergeCollections.RemoveOnlyList<ServiceReference<?>>(
result);
for (final Iterator<ServiceReferenceImpl<FindHook>> iter = serviceFindHooks
.iterator(); iter.hasNext();) {
final ServiceReferenceImpl<FindHook> hookRef = iter.next();
final FindHook hook = getService(hookRef);
try {
hook.find(this, clazz, filter, all, c);
} catch (final Throwable t) {
notifyFrameworkListeners(FrameworkEvent.ERROR,
Concierge.this, t);
}
ungetService(hookRef);
}
if(this != Concierge.this.context) {
return c.size() == 0 ? null
: (ServiceReference[]) c
.toArray(new ServiceReference[c.size()]);
}
}
if (LOG_ENABLED && DEBUG_SERVICES) {
logger.log(LogService.LOG_INFO,
"Framework: REQUESTED SERVICES "
+ (clazz == null ? "(no class)" : clazz) + " "
+ (filter == null ? "(no filter)"
: "filter=" + filter));
logger.log(LogService.LOG_INFO, "\tRETURNED " + result);
}
return result.size() == 0 ? null
: (ServiceReference[]) result
.toArray(new ServiceReference[result.size()]);
}