Search in sources :

Example 11 with ServiceTrackerCustomizer

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();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) LanguageConfiguration(org.apache.stanbol.enhancer.nlp.utils.LanguageConfiguration) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 12 with ServiceTrackerCustomizer

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();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) SortedMap(java.util.SortedMap) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 13 with ServiceTrackerCustomizer

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");
}
Also used : MavenConfigService(org.apache.karaf.bundle.core.internal.MavenConfigService) BundleStateService(org.apache.karaf.bundle.core.BundleStateService) BundleWatcherImpl(org.apache.karaf.bundle.core.internal.BundleWatcherImpl) BundleServiceImpl(org.apache.karaf.bundle.core.internal.BundleServiceImpl) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) BundlesMBeanImpl(org.apache.karaf.bundle.core.internal.BundlesMBeanImpl) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference)

Example 14 with ServiceTrackerCustomizer

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();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) ResourceChangeListener(org.apache.sling.api.resource.observation.ResourceChangeListener) ServiceReference(org.osgi.framework.ServiceReference)

Example 15 with ServiceTrackerCustomizer

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();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)19 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)19 ServiceTracker (org.osgi.util.tracker.ServiceTracker)10 Activate (org.apache.felix.scr.annotations.Activate)9 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 BundleContext (org.osgi.framework.BundleContext)4 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)3 SortedMap (java.util.SortedMap)2 KeystoreInstance (org.apache.karaf.jaas.config.KeystoreInstance)2 KeystoreManager (org.apache.karaf.jaas.config.KeystoreManager)2 Filter (org.osgi.framework.Filter)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Maps.newTreeMap (com.google.common.collect.Maps.newTreeMap)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1