use of org.osgi.util.tracker.ServiceTrackerCustomizer in project stanbol by apache.
the class MainLabelTokenizer method activate.
@Activate
protected void activate(ComponentContext ctx) {
final BundleContext bundleContext = ctx.getBundleContext();
final String managerServicePid = (String) ctx.getProperties().get(Constants.SERVICE_PID);
labelTokenizerTracker = new ServiceTracker(bundleContext, LabelTokenizer.class.getName(), new ServiceTrackerCustomizer() {
@Override
public Object addingService(ServiceReference reference) {
if (managerServicePid.equals(reference.getProperty(Constants.SERVICE_PID))) {
//do not track this manager!
return null;
}
LanguageConfiguration langConf = new LanguageConfiguration(SUPPORTED_LANUAGES, DEFAULT_LANG_CONF);
try {
langConf.setConfiguration(reference);
} catch (ConfigurationException e) {
log.error("Unable to track ServiceReference {} becuase of invalid LanguageConfiguration(" + SUPPORTED_LANUAGES + "=" + reference.getProperty(SUPPORTED_LANUAGES) + ")!", e);
return null;
}
Object service = bundleContext.getService(reference);
if (service != null) {
ref2LangConfig.put(reference, langConf);
langTokenizers.clear();
}
return service;
}
@Override
public void modifiedService(ServiceReference reference, Object service) {
if (managerServicePid.equals(reference.getProperty(Constants.SERVICE_PID))) {
//ignore this service!
return;
}
LanguageConfiguration langConf = new LanguageConfiguration(SUPPORTED_LANUAGES, DEFAULT_LANG_CONF);
try {
langConf.setConfiguration(reference);
ref2LangConfig.put(reference, langConf);
langTokenizers.clear();
} catch (ConfigurationException e) {
log.error("Unable to track ServiceReference {} becuase of invalid LanguageConfiguration(" + SUPPORTED_LANUAGES + "=" + reference.getProperty(SUPPORTED_LANUAGES) + ")!", e);
if (ref2LangConfig.remove(reference) != null) {
langTokenizers.clear();
}
}
}
@Override
public void removedService(ServiceReference reference, Object service) {
if (managerServicePid.equals(reference.getProperty(Constants.SERVICE_PID))) {
//ignore this service
return;
}
bundleContext.ungetService(reference);
if (ref2LangConfig.remove(reference) != null) {
langTokenizers.clear();
}
}
});
labelTokenizerTracker.open();
}
use of org.osgi.util.tracker.ServiceTrackerCustomizer in project stanbol by apache.
the class SolrIndexInstaller method activate.
@Activate
protected void activate(ComponentContext context) {
bc = context.getBundleContext();
serverTracker = new ServiceTracker(bc, ManagedSolrServer.class.getName(), new ServiceTrackerCustomizer() {
/**
* The servers managed by this instance
*/
private SortedMap<ServiceReference, ManagedSolrServer> servers = new TreeMap<ServiceReference, ManagedSolrServer>();
@Override
public void removedService(ServiceReference reference, Object service) {
lock.writeLock().lock();
try {
servers.remove(reference);
updateRegistration(servers);
} finally {
lock.writeLock().unlock();
}
}
@Override
public void modifiedService(ServiceReference reference, Object service) {
lock.writeLock().lock();
try {
servers.put(reference, (ManagedSolrServer) service);
updateRegistration(servers);
} finally {
lock.writeLock().unlock();
}
}
@Override
public Object addingService(ServiceReference reference) {
ManagedSolrServer server = (ManagedSolrServer) bc.getService(reference);
if (server != null) {
lock.writeLock().lock();
try {
servers.put(reference, server);
updateRegistration(servers);
} finally {
lock.writeLock().unlock();
}
}
return server;
}
});
serverTracker.open();
}
use of org.osgi.util.tracker.ServiceTrackerCustomizer in project karaf by apache.
the class Activator method doStart.
@Override
protected void doStart() throws Exception {
ConfigurationAdmin configurationAdmin = getTrackedService(ConfigurationAdmin.class);
if (configurationAdmin == null) {
return;
}
final BundleServiceImpl bundleService = new BundleServiceImpl(bundleContext);
register(BundleService.class, bundleService);
bundleStateServicesTracker = new ServiceTracker<>(bundleContext, BundleStateService.class, new ServiceTrackerCustomizer<BundleStateService, BundleStateService>() {
@Override
public BundleStateService addingService(ServiceReference<BundleStateService> reference) {
BundleStateService service = bundleContext.getService(reference);
bundleService.registerBundleStateService(service);
return service;
}
@Override
public void modifiedService(ServiceReference<BundleStateService> reference, BundleStateService service) {
}
@Override
public void removedService(ServiceReference<BundleStateService> reference, BundleStateService service) {
bundleService.unregisterBundleStateService(service);
bundleContext.ungetService(reference);
}
});
bundleStateServicesTracker.open();
bundleWatcher = new BundleWatcherImpl(bundleContext, new MavenConfigService(configurationAdmin), bundleService);
bundleWatcher.start();
register(BundleWatcher.class, bundleWatcher);
BundlesMBeanImpl bundlesMBeanImpl = new BundlesMBeanImpl(bundleContext, bundleService);
registerMBean(bundlesMBeanImpl, "type=bundle");
}
use of org.osgi.util.tracker.ServiceTrackerCustomizer in project sling by apache.
the class ResourceChangeListenerWhiteboard method activate.
public void activate(final BundleContext bundleContext, final ResourceProviderTracker resourceProviderTracker, final String[] searchPath) {
this.searchPath = searchPath;
this.resourceProviderTracker = resourceProviderTracker;
this.resourceProviderTracker.setObservationReporterGenerator(this);
this.tracker = new ServiceTracker<>(bundleContext, ResourceChangeListener.class, new ServiceTrackerCustomizer<ResourceChangeListener, ServiceReference<ResourceChangeListener>>() {
@Override
public void removedService(final ServiceReference<ResourceChangeListener> reference, final ServiceReference<ResourceChangeListener> service) {
final ResourceChangeListenerInfo info = listeners.remove(reference);
if (info != null) {
updateProviderTracker();
}
}
@Override
public void modifiedService(final ServiceReference<ResourceChangeListener> reference, final ServiceReference<ResourceChangeListener> service) {
removedService(reference, service);
addingService(reference);
}
@Override
public ServiceReference<ResourceChangeListener> addingService(final ServiceReference<ResourceChangeListener> reference) {
final ResourceChangeListenerInfo info = new ResourceChangeListenerInfo(reference, searchPath);
if (info.isValid()) {
final ResourceChangeListener listener = bundleContext.getService(reference);
if (listener != null) {
info.setListener(listener);
listeners.put(reference, info);
updateProviderTracker();
}
} else {
logger.warn("Ignoring invalid resource change listenr {}", reference);
}
return reference;
}
});
this.tracker.open();
}
use of org.osgi.util.tracker.ServiceTrackerCustomizer 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();
}
Aggregations