Search in sources :

Example 6 with ServiceTrackerCustomizer

use of org.osgi.util.tracker.ServiceTrackerCustomizer in project stanbol by apache.

the class ChainsRootResource method activate.

@Activate
public void activate(ComponentContext ctx) {
    final BundleContext bc = ctx.getBundleContext();
    chainTracker = new ChainsTracker(ctx.getBundleContext(), 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
                _chainCache = null;
            }
            return service;
        }

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

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

Example 7 with ServiceTrackerCustomizer

use of org.osgi.util.tracker.ServiceTrackerCustomizer 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 8 with ServiceTrackerCustomizer

use of org.osgi.util.tracker.ServiceTrackerCustomizer in project stanbol by apache.

the class EntityhubComponent method activate.

@Activate
protected void activate(final ComponentContext context) throws ConfigurationException {
    this.bc = context.getBundleContext();
    Dictionary<?, ?> properties = context.getProperties();
    log.info("Activate Entityhub Component:");
    this.entityhubID = OsgiUtils.checkProperty(properties, ID).toString();
    if (entityhubID == null || entityhubID.isEmpty()) {
        throw new ConfigurationException(ID, "The id for the Entityhub MUST NOT be empty!");
    } else {
        log.debug("   + id: {}", entityhubID);
    }
    this.entityhubName = OsgiUtils.checkProperty(properties, NAME, this.entityhubID).toString();
    if (entityhubName.isEmpty()) {
        throw new ConfigurationException(NAME, "The name for the Entityhub MUST NOT be empty!");
    } else {
        log.debug("   + name: {}", entityhubName);
    }
    Object entityhubDescriptionObject = properties.get(DESCRIPTION);
    this.entityhubDescription = entityhubDescriptionObject == null ? null : entityhubDescriptionObject.toString();
    log.debug("   + description: {}", entityhubDescription == null ? "<none>" : entityhubDescription);
    this.entityhubPrefix = OsgiUtils.checkProperty(properties, PREFIX).toString();
    if (entityhubPrefix.isEmpty()) {
        throw new ConfigurationException(PREFIX, "The UIR preix for the Entityub MUST NOT be empty!");
    }
    try {
        new URI(entityhubPrefix);
        log.info("   + prefix: " + entityhubPrefix);
    } catch (URISyntaxException e) {
        throw new ConfigurationException(PREFIX, "The URI prefix for the Entityhub " + "MUST BE an valid URI (prefix=" + entityhubPrefix + ")", e);
    }
    Object defaultSymbolState = properties.get(DEFAULT_SYMBOL_STATE);
    if (defaultSymbolState == null) {
        this.defaultSymblStateString = ManagedEntity.DEFAULT_SYMBOL_STATE.name();
    } else {
        this.defaultSymblStateString = defaultSymbolState.toString();
    }
    Object defaultMappingState = properties.get(DEFAULT_MAPPING_STATE);
    if (defaultMappingState == null) {
        this.defaultMappingStateString = EntityMapping.DEFAULT_MAPPING_STATE.name();
    } else {
        this.defaultMappingStateString = defaultMappingState.toString();
    }
    Object fieldMappingConfigObject = OsgiUtils.checkProperty(properties, FIELD_MAPPINGS);
    if (fieldMappingConfigObject instanceof String[]) {
        this.fieldMappingConfig = (String[]) fieldMappingConfigObject;
    } else {
        throw new ConfigurationException(FIELD_MAPPINGS, "Values for this property must be of type Stirng[]!");
    }
    String entityhubYardId = OsgiUtils.checkProperty(properties, ENTITYHUB_YARD_ID).toString();
    String filterString = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Yard.class.getName(), Yard.ID, entityhubYardId);
    log.debug(" ... tracking EntityhubYard by Filter:" + filterString);
    Filter filter;
    try {
        filter = context.getBundleContext().createFilter(filterString);
    } catch (InvalidSyntaxException e) {
        throw new ConfigurationException(ENTITYHUB_YARD_ID, "Unable to parse OSGI filter '" + filterString + "' for configured Yard id '" + entityhubYardId + "'!", e);
    }
    entityhubYardTracker = new ServiceTracker(context.getBundleContext(), filter, new ServiceTrackerCustomizer() {

        final BundleContext bc = context.getBundleContext();

        @Override
        public void removedService(ServiceReference reference, Object service) {
            if (service.equals(entityhubYard)) {
                entityhubYard = (Yard) entityhubYardTracker.getService();
                updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
            }
            bc.ungetService(reference);
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
            //the service.ranking might have changed ... so check if the
            //top ranked yard is a different one
            Yard newYard = (Yard) entityhubYardTracker.getService();
            if (newYard == null || !newYard.equals(entityhubYard)) {
                //set the new yard
                entityhubYard = newYard;
                //and update the service registration
                updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
            }
        }

        @Override
        public Object addingService(ServiceReference reference) {
            Object service = bc.getService(reference);
            if (service != null) {
                if (//the first added Service or
                entityhubYardTracker.getServiceReference() == null || //the new service as higher ranking as the current
                (reference.compareTo(entityhubYardTracker.getServiceReference()) > 0)) {
                    entityhubYard = (Yard) service;
                    updateServiceRegistration(bc, entityhubYard, siteManager, nsPrefixService);
                }
            // else the new service has lower ranking as the currently use one
            }
            //else service == null -> ignore
            return service;
        }
    });
    //start the tracking
    entityhubYardTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ServiceReference(org.osgi.framework.ServiceReference) Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ConfigurationException(org.osgi.service.cm.ConfigurationException) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 9 with ServiceTrackerCustomizer

use of org.osgi.util.tracker.ServiceTrackerCustomizer in project stanbol by apache.

the class ManagedSiteComponent method activate.

/**
     * Activates this {@link ManagedSiteComponent}. This might be overridden to
     * perform additional configuration. In such cases super should be called
     * before the additional configuration steps.
     * @param context
     * @throws ConfigurationException
     * @throws YardException
     * @throws InvalidSyntaxException
     */
@Activate
protected void activate(final ComponentContext context) throws ConfigurationException, YardException, InvalidSyntaxException {
    this.bundleContext = context.getBundleContext();
    //NOTE that the constructor also validation of the parsed configuration
    this.siteConfiguration = new ManagedSiteConfigurationImpl(context.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 Managed Site {}", siteConfiguration.getId());
    SiteUtils.extractSiteMetadata(siteConfiguration, InMemoryValueFactory.getInstance());
    //Initialise the Yard
    final String yardId = siteConfiguration.getYardId();
    String yardFilterString = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Yard.class.getName(), Yard.ID, yardId);
    Filter yardFilter = bundleContext.createFilter(yardFilterString);
    yardTracker = new ServiceTracker(bundleContext, yardFilter, new ServiceTrackerCustomizer() {

        @Override
        public void removedService(ServiceReference reference, Object service) {
            synchronized (yardReferenceLock) {
                if (reference.equals(yardReference)) {
                    deactivateManagedSite();
                    yardReference = null;
                }
                bundleContext.ungetService(reference);
            }
        }

        @Override
        public void modifiedService(ServiceReference reference, Object service) {
        }

        @Override
        public Object addingService(ServiceReference reference) {
            Yard yard = (Yard) bundleContext.getService(reference);
            synchronized (yardReferenceLock) {
                if (yardReference == null) {
                    if (yard != null) {
                        activateManagedSite(yard);
                        yardReference = reference;
                    } else {
                        log.warn("Unable to addService for ServiceReference because" + "unable to obtain referenced Yard via the BundleContext!");
                    }
                } else {
                    log.warn("Tracking two Yard instances with the Yard ID '{}' " + "configured for ManagedSite '{}'", yardId, siteConfiguration.getId());
                    log.warn("used  : {}", yardReference.getProperty(Constants.SERVICE_PID));
                    log.warn("unused: {}", reference.getProperty(Constants.SERVICE_PID));
                }
            }
            return yard;
        }
    });
    yardTracker.open();
//will be moved to a Solr specific implementation
//        //chaeck if we are allowed to init an yard with the provided id
//        boolean allowInit = false;
//        if(configAdmin!= null){
//            Configuration[] configs;
//            try {
//                String yardIdFilter = String.format("(%s=%s)",
//                    Yard.ID,yardId);
//                configs = configAdmin.listConfigurations(yardIdFilter);
//                if(configs == null || configs.length < 1){
//                    allowInit = true;
//                }
//            } catch (IOException e) {
//                log.warn("Unable to access ManagedService configurations ",e);
//            }
//        } else if (yardTracker.getService() == null){
//            log.warn("Unable to check for Yard configuration of ManagedSite {} "
//                + "Because the ConfigurationAdmin service is not available");
//            log.warn(" -> unable to create YardConfiguration");
//        }
//        if(allowInit){
//            //TODO: This has SolrYard specific code - this needs to be refactored
//            String factoryPid = "org.apache.stanbol.entityhub.yard.solr.impl.SolrYard";
//            try {
//                Configuration config = configAdmin.createFactoryConfiguration(factoryPid,null);
//                //configure the required properties
//                Dictionary<String,Object> yardConfig = new Hashtable<String,Object>();
//                yardConfig.put(Yard.ID, siteConfiguration.getYardId());
//                yardConfig.put(Yard.NAME, siteConfiguration.getYardId());
//                yardConfig.put(Yard.DESCRIPTION, "Yard for the ManagedSite "+siteConfiguration.getId());
//                yardConfig.put("org.apache.stanbol.entityhub.yard.solr.solrUri", siteConfiguration.getId());
//                yardConfig.put("org.apache.stanbol.entityhub.yard.solr.useDefaultConfig", true);
//                config.update(yardConfig); //this create the solrYard
//            } catch (IOException e) {
//                log.warn("Unable to create SolrYard configuration for MnagedSite "+siteConfiguration.getId(),e);
//            }
//        }
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) ConfigurationException(org.osgi.service.cm.ConfigurationException) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) ServiceReference(org.osgi.framework.ServiceReference) Activate(org.apache.felix.scr.annotations.Activate)

Example 10 with ServiceTrackerCustomizer

use of org.osgi.util.tracker.ServiceTrackerCustomizer in project sling by apache.

the class Activator method start.

/**
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
public void start(final BundleContext context) throws Exception {
    this.rootSubsystemTracker = new ServiceTracker<Subsystem, Subsystem>(context, context.createFilter("(&(" + Constants.OBJECTCLASS + "=" + Subsystem.class.getName() + ")" + "(" + SubsystemConstants.SUBSYSTEM_ID_PROPERTY + "=0))"), new ServiceTrackerCustomizer<Subsystem, Subsystem>() {

        public Subsystem addingService(final ServiceReference<Subsystem> reference) {
            final Subsystem service = context.getService(reference);
            if (service != null) {
                registerInstaller(context, service);
            }
            return service;
        }

        public void modifiedService(final ServiceReference<Subsystem> reference, final Subsystem service) {
        // nothing to do
        }

        public void removedService(final ServiceReference<Subsystem> reference, final Subsystem service) {
            unregisterInstaller();
        }
    });
    this.rootSubsystemTracker.open();
}
Also used : ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) Subsystem(org.osgi.service.subsystem.Subsystem) 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