Search in sources :

Example 76 with Activate

use of org.apache.felix.scr.annotations.Activate 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 77 with Activate

use of org.apache.felix.scr.annotations.Activate in project stanbol by apache.

the class SolrDispatchFilterComponent method activate.

@Activate
protected void activate(ComponentContext context) throws ConfigurationException, ServletException {
    this.context = context;
    BundleContext bc = context.getBundleContext();
    Object value = context.getProperties().get(PROPERTY_SERVER_NAME);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_NAME, "The configured CoreContainer name MUST NOT be NULL nor empty!");
    }
    serverName = value.toString();
    String filterString = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, CoreContainer.class.getName(), SolrConstants.PROPERTY_SERVER_NAME, serverName);
    try {
        tracker = new ServiceTracker(bc, bc.createFilter(filterString), trackerCustomizer);
    } catch (InvalidSyntaxException e) {
        throw new ConfigurationException(PROPERTY_SERVER_NAME, "Unable to build Filter for parsed CoreContainer name '" + serverName + "'", e);
    }
    value = context.getProperties().get(PROPERTY_PREFIX_PATH);
    final String prefixPath;
    if (value != null) {
        prefix = value.toString();
        if (prefix.charAt(0) != '/') {
            prefix = '/' + prefix;
        }
        prefixPath = prefix;
        if (!prefix.endsWith("*")) {
            // TODO: check if this is a good idea
            prefix = prefix + "/.*";
        }
    } else {
        prefixPath = null;
        prefix = "/.*";
    }
    filterPrpoerties = new Hashtable<String, Object>();
    if (prefixPath != null) {
        filterPrpoerties.put("path-prefix", prefixPath);
    }
    // now start tracking! ...
    // ... as soon as the first CoreContainer is tracked the Filter will
    // be created and added to the ExtHttpService
    tracker.open();
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceTracker(org.osgi.util.tracker.ServiceTracker) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 78 with Activate

use of org.apache.felix.scr.annotations.Activate in project stanbol by apache.

the class DirectoryDataFileProvider method activate.

@Activate
protected void activate(ComponentContext ctx) throws ConfigurationException {
    String folderName = requireProperty(ctx.getProperties(), DATA_FILES_FOLDER_PROP, String.class);
    dataFilesFolder = new File(folderName);
    if (!dataFilesFolder.exists()) {
        if (!dataFilesFolder.mkdirs()) {
            throw new ConfigurationException(DATA_FILES_FOLDER_PROP, "Unable to create the configured Directory " + dataFilesFolder);
        }
    } else if (!dataFilesFolder.isDirectory()) {
        throw new ConfigurationException(DATA_FILES_FOLDER_PROP, "The configured DataFile directory " + dataFilesFolder + " does already exists but is not a directory!");
    }
// else exists and is a directory!
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) File(java.io.File) Activate(org.apache.felix.scr.annotations.Activate)

Example 79 with Activate

use of org.apache.felix.scr.annotations.Activate in project stanbol by apache.

the class ValueTypeParserRegistry method activate.

@Activate
protected void activate(ComponentContext ctx) {
    inOsgi = true;
    final BundleContext bc = ctx.getBundleContext();
    parserTracker = new ServiceTracker(bc, ValueTypeParser.class.getName(), new ParserTracker(bc));
// NOTE: do not open within activate(..) because of
// org.apache.stanbol.enhancer.nlp.json FrameworkEvent
// ERROR (org.osgi.framework.ServiceException:
// ServiceFactory.getService() resulted in a cycle.)
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 80 with Activate

use of org.apache.felix.scr.annotations.Activate in project stanbol by apache.

the class ValueTypeSerializerRegistry method activate.

@Activate
protected void activate(ComponentContext ctx) {
    inOsgi = true;
    final BundleContext bc = ctx.getBundleContext();
    serializerTracker = new ServiceTracker(bc, ValueTypeSerializer.class.getName(), new SerializerTracker(bc));
// NOTE: do not open within activate(..) because of
// org.apache.stanbol.enhancer.nlp.json FrameworkEvent
// ERROR (org.osgi.framework.ServiceException:
// ServiceFactory.getService() resulted in a cycle.)
// serializerTracker.open();
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

Activate (org.apache.felix.scr.annotations.Activate)153 ConfigurationException (org.osgi.service.cm.ConfigurationException)31 ServiceTracker (org.osgi.util.tracker.ServiceTracker)20 BundleContext (org.osgi.framework.BundleContext)19 File (java.io.File)15 OsgiWhiteboard (org.apache.jackrabbit.oak.osgi.OsgiWhiteboard)12 URL (java.net.URL)11 Hashtable (java.util.Hashtable)11 ServiceReference (org.osgi.framework.ServiceReference)11 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)9 HashSet (java.util.HashSet)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 Map (java.util.Map)6 Session (javax.jcr.Session)5 StandardMBean (javax.management.StandardMBean)5 Whiteboard (org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard)5 Filter (org.osgi.framework.Filter)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4