Search in sources :

Example 1 with EnginesTracker

use of org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker in project stanbol by apache.

the class EnhancementEnginesRootResource method activate.

@Activate
protected void activate(ComponentContext ctx) {
    final BundleContext bc = ctx.getBundleContext();
    engineTracker = new EnginesTracker(bc, Collections.<String>emptySet(), new ServiceTrackerCustomizer() {

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = bc.getService(reference);
            if (service != null) {
                //rebuild the cache on the next call
                _enginesCache = null;
            }
            return service;
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
            //rebuild the cache on the next call
            _enginesCache = null;
        }

        @Override
        public void removedService(ServiceReference reference, Object service) {
            if (reference != null) {
                bc.ungetService(reference);
                //rebuild the cache on the next call
                _enginesCache = null;
            }
        }
    });
    engineTracker.open();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) EnginesTracker(org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 2 with EnginesTracker

use of org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker in project stanbol by apache.

the class WeightedChain method activate.

@Override
protected void activate(ComponentContext ctx) throws ConfigurationException {
    super.activate(ctx);
    Object value = ctx.getProperties().get(PROPERTY_CHAIN);
    Set<String> configuredChain = new HashSet<String>();
    if (value instanceof String[]) {
        configuredChain.addAll(Arrays.asList((String[]) value));
    } else if (value instanceof Collection<?>) {
        for (Object o : (Collection<?>) value) {
            if (o instanceof String) {
                configuredChain.add((String) o);
            }
        }
    } else {
        throw new ConfigurationException(PROPERTY_CHAIN, "The engines of a Weigted Chain MUST BE configured as a Array or " + "Collection of Strings (parsed: " + (value != null ? value.getClass() : "null") + ")");
    }
    //now parse the configured chain
    try {
        chain = Collections.unmodifiableMap(parseConfig(configuredChain));
    } catch (IllegalArgumentException e) {
        throw new ConfigurationException(PROPERTY_CHAIN, "Unable to parse Chain Configuraiton (message: '" + e.getMessage() + "')!", e);
    }
    if (chain.isEmpty()) {
        throw new ConfigurationException(PROPERTY_CHAIN, "The configured chain MUST at least contain a single valid entry!");
    }
    //init the chain scoped enhancement properties
    chainScopedEnhProps = new HashMap<String, Map<String, Object>>();
    if (getChainProperties() != null) {
        chainScopedEnhProps.put(null, getChainProperties());
    }
    for (Entry<String, Map<String, List<String>>> entry : chain.entrySet()) {
        Map<String, Object> enhProp = ConfigUtils.getEnhancementProperties(entry.getValue());
        if (enhProp != null) {
            chainScopedEnhProps.put(entry.getKey(), enhProp);
        }
    }
    //start tracking the engines of the configured chain
    tracker = new EnginesTracker(ctx.getBundleContext(), chain.keySet(), this);
    tracker.open();
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) EnginesTracker(org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker) HashSet(java.util.HashSet)

Aggregations

EnginesTracker (org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Activate (org.apache.felix.scr.annotations.Activate)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1