use of org.osgi.util.tracker.ServiceTracker in project sling by apache.
the class ResourceProviderTracker method activate.
public void activate(final BundleContext bundleContext, final EventAdmin eventAdmin, final ChangeListener listener) {
this.bundleContext = bundleContext;
this.eventAdmin = eventAdmin;
this.listener = listener;
this.tracker = new ServiceTracker(bundleContext, ResourceProvider.class.getName(), new ServiceTrackerCustomizer() {
@Override
public void removedService(final ServiceReference reference, final Object service) {
final ServiceReference ref = (ServiceReference) service;
final ResourceProviderInfo info = infos.remove(ref);
if (info != null) {
Object pid = ref.getProperty(Constants.SERVICE_PID);
if (pid != null && !(pid instanceof String)) {
pid = null;
}
unregister(info, (String) pid);
}
}
@Override
public void modifiedService(final ServiceReference reference, final Object service) {
removedService(reference, service);
addingService(reference);
}
@Override
public Object addingService(final ServiceReference reference) {
final ResourceProviderInfo info = new ResourceProviderInfo(reference);
infos.put(reference, info);
register(info);
return reference;
}
});
this.tracker.open();
}
use of org.osgi.util.tracker.ServiceTracker in project sling by apache.
the class LogbackManager method shutdown.
public void shutdown() {
if (bridgeHandlerInstalled) {
SLF4JBridgeHandler.uninstall();
}
logConfigManager.close();
for (ServiceTracker tracker : serviceTrackers) {
tracker.close();
}
for (ServiceRegistration reg : registrations) {
reg.unregister();
}
getLoggerContext().removeListener(osgiIntegrationListener);
getLoggerContext().stop();
}
use of org.osgi.util.tracker.ServiceTracker in project ddf by codice.
the class MigrateCommand method getCatalogProviders.
private List<CatalogProvider> getCatalogProviders() {
ServiceTracker st = new ServiceTracker(bundleContext, CatalogProvider.class.getName(), null);
st.open();
ServiceReference<CatalogProvider>[] serviceRefs = st.getServiceReferences();
Map<ServiceReference<CatalogProvider>, CatalogProvider> map = new TreeMap<>(new ServiceComparator());
if (null != serviceRefs) {
for (ServiceReference<CatalogProvider> serviceReference : serviceRefs) {
map.put(serviceReference, (CatalogProvider) st.getService(serviceReference));
}
}
return new ArrayList<>(map.values());
}
use of org.osgi.util.tracker.ServiceTracker in project ddf by codice.
the class ConfigurationAdminExt method getService.
/**
* Gets the service with the specified class name. Will create a new {@link ServiceTracker} if
* the service is not already retrieved.
*
* @param serviceName the service name to obtain
* @return the service or <code>null</code> if missing.
*/
final Object getService(String serviceName) {
ServiceTracker serviceTracker = services.get(serviceName);
if (serviceTracker == null) {
serviceTracker = new ServiceTracker(getBundleContext(), serviceName, null);
serviceTracker.open();
services.put(serviceName, serviceTracker);
}
return serviceTracker.getService();
}
use of org.osgi.util.tracker.ServiceTracker in project stanbol by apache.
the class ReferencedSiteComponent method activate.
@SuppressWarnings("unchecked")
@Activate
protected void activate(final ComponentContext ctx) throws ConfigurationException, YardException, InvalidSyntaxException {
log.debug("in {} activate with properties {}", ReferencedSiteImpl.class.getSimpleName(), ctx.getProperties());
if (ctx == null || ctx.getProperties() == null) {
throw new IllegalStateException("No Component Context and/or Dictionary properties object parsed to the acticate methode");
}
this.cc = ctx;
this.bc = ctx.getBundleContext();
// create the SiteConfiguration based on the parsed properties
// NOTE that the constructor also validation of the parsed configuration
siteConfiguration = new ReferencedSiteConfigurationImpl(ctx.getProperties());
if (PROHIBITED_SITE_IDS.contains(siteConfiguration.getId().toLowerCase())) {
throw new ConfigurationException(SiteConfiguration.ID, String.format("The ID '%s' of this Referenced Site is one of the following " + "prohibited IDs: {} (case insensitive)", siteConfiguration.getId(), PROHIBITED_SITE_IDS));
}
log.info(" > initialise Referenced Site {}", siteConfiguration.getName());
// if the accessUri is the same as the queryUri and both the
// dereferencer and the entitySearcher uses the same component, than we
//need only one component for both dependencies.
this.dereferencerEqualsEntitySearcherComponent = // (1) accessURI == queryURI
siteConfiguration.getAccessUri() != null && siteConfiguration.getAccessUri().equals(siteConfiguration.getQueryUri()) && // (2) entity dereferencer == entity searcher
siteConfiguration.getEntityDereferencerType() != null && siteConfiguration.getEntityDereferencerType().equals(siteConfiguration.getEntitySearcherType());
// init the fieldMapper based on the configuration
FieldMapper fieldMappings = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
if (siteConfiguration.getFieldMappings() != null) {
log.debug(" > Initialise configured field mappings");
for (String configuredMapping : siteConfiguration.getFieldMappings()) {
FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
if (mapping != null) {
log.debug(" - add FieldMapping {}", mapping);
fieldMappings.addMapping(mapping);
}
}
}
// now init the referenced Services
initDereferencerAndEntitySearcher();
// Reference to the cache!
if (siteConfiguration.getCacheId() != null) {
String cacheFilter = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Cache.class.getName(), Cache.CACHE_YARD, siteConfiguration.getCacheId());
cacheTracker = new ServiceTracker(ctx.getBundleContext(), ctx.getBundleContext().createFilter(cacheFilter), new ServiceTrackerCustomizer() {
@Override
public void removedService(ServiceReference reference, Object service) {
if (service.equals(cache)) {
cache = (Cache) cacheTracker.getService();
updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
}
bc.ungetService(reference);
}
@Override
public void modifiedService(ServiceReference reference, Object service) {
//the service.ranking might have changed ... so check if the
//top ranked Cache is a different one
Cache newCache = (Cache) cacheTracker.getService();
if (newCache == null || !newCache.equals(cache)) {
//set the new cahce
cache = newCache;
//and update the service registration
updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
}
}
@Override
public Object addingService(ServiceReference reference) {
Object service = bc.getService(reference);
if (service != null) {
if (//the first added Service or
cacheTracker.getServiceReference() == null || //the new service as higher ranking as the current
(reference.compareTo(cacheTracker.getServiceReference()) > 0)) {
cache = (Cache) service;
updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
}
// else the new service has lower ranking as the currently use one
}
//else service == null -> ignore
return service;
}
});
cacheTracker.open();
}
}
Aggregations