Search in sources :

Example 1 with DefaultFieldMapperImpl

use of org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl in project stanbol by apache.

the class TrackingDereferencerBase method setDereferencedFields.

/**
 * Setter for the dereferenced fields
 * @param dereferencedFields the set containing the fields that need to be
 * dereferenced. If <code>null</code> or an empty set all fields will be
 * dereferenced.
 */
public void setDereferencedFields(List<String> dereferencedFields) {
    if (dereferencedFields != null && !dereferencedFields.isEmpty()) {
        List<FieldMapping> mappings = new ArrayList<FieldMapping>(dereferencedFields.size());
        log.debug(" > parse configured field mappings");
        for (String configuredMapping : dereferencedFields) {
            log.trace(" - parse configure mapping '{}'", configuredMapping);
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                mappings.add(mapping);
            } else if (configuredMapping != null && !configuredMapping.isEmpty()) {
                log.warn("   - unable to parse FieldMapping '{}'", configuredMapping);
            }
        }
        if (!mappings.isEmpty()) {
            log.debug(" > apply {} valid mappings", mappings.size());
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
            for (FieldMapping mapping : mappings) {
                fieldMapper.addMapping(mapping);
            }
        } else {
            // no valid mapping parsed
            log.debug(" > no valid mapping parsed ... will dereference all fields");
            fieldMapper = null;
        }
    } else {
        fieldMapper = null;
    }
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList)

Example 2 with DefaultFieldMapperImpl

use of org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl in project stanbol by apache.

the class EntityhubDereferenceContext method initFieldMappings.

protected void initFieldMappings(List<String> fields) throws DereferenceConfigurationException {
    TrackingDereferencerBase<?> dereferencer = getEntityhubDereferencer();
    FieldMapper fieldMapper;
    if (fields != null && !fields.isEmpty()) {
        log.debug("parse FieldMappings from EnhancementProperties");
        List<FieldMapping> mappings = new ArrayList<FieldMapping>(fields.size());
        for (String configuredMapping : fields) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, dereferencer.getNsPrefixService());
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                mappings.add(mapping);
            } else if (configuredMapping != null && !configuredMapping.isEmpty()) {
                log.warn("   - unable to parse FieldMapping '{}'", configuredMapping);
            }
        }
        if (!mappings.isEmpty()) {
            log.debug(" > apply {} valid mappings", mappings.size());
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
            for (FieldMapping mapping : mappings) {
                fieldMapper.addMapping(mapping);
            }
        } else {
            // no valid mapping parsed
            log.debug(" > no valid mapping parsed ... will dereference all fields");
            fieldMapper = null;
        }
    } else if (dereferencer.getFieldMapper() != null) {
        fieldMapper = dereferencer.getFieldMapper().clone();
    } else {
        fieldMapper = null;
    }
    // TODO: uncomment this to merge context with engine mappings. Not sure
    // if this is desirable
    // if(fieldMapper != null){
    // if(dereferencer.getFieldMapper() != null){
    // //add mappings of the engine configuration to the context mappings
    // for(FieldMapping mapping : dereferencer.getFieldMapper().getMappings()){
    // fieldMapper.addMapping(mapping);
    // }
    // }
    // }
    // if a fieldMapper is present and languages are set we will add a language
    // filter to the fieldMapper. If the fieldmapper is null languages are
    // filtered separately.
    Collection<String> langs = getLanguages();
    if (langs != null && !langs.isEmpty()) {
        if (fieldMapper == null) {
            // create a fieldMapper for filtering languages
            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
        }
        fieldMapper.addMapping(new FieldMapping(new TextConstraint((String) null, langs.toArray(new String[langs.size()]))));
    }
    // set the field
    this.fieldMapper = fieldMapper;
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ArrayList(java.util.ArrayList) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 3 with DefaultFieldMapperImpl

use of org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl in project stanbol by apache.

the class ReferencedSiteComponent method activate.

@SuppressWarnings("unchecked")
@Activate
protected void activate(final ComponentContext ctx) throws ConfigurationException, YardException, InvalidSyntaxException {
    log.debug("in {} activate with properties {}", ReferencedSiteImpl.class.getSimpleName(), ctx.getProperties());
    if (ctx == null || ctx.getProperties() == null) {
        throw new IllegalStateException("No Component Context and/or Dictionary properties object parsed to the acticate methode");
    }
    this.cc = ctx;
    this.bc = ctx.getBundleContext();
    // create the SiteConfiguration based on the parsed properties
    // NOTE that the constructor also validation of the parsed configuration
    siteConfiguration = new ReferencedSiteConfigurationImpl(ctx.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 Referenced Site {}", siteConfiguration.getName());
    // if the accessUri is the same as the queryUri and both the
    // dereferencer and the entitySearcher uses the same component, than we
    // need only one component for both dependencies.
    this.dereferencerEqualsEntitySearcherComponent = // (1) accessURI == queryURI
    siteConfiguration.getAccessUri() != null && siteConfiguration.getAccessUri().equals(siteConfiguration.getQueryUri()) && // (2) entity dereferencer == entity searcher
    siteConfiguration.getEntityDereferencerType() != null && siteConfiguration.getEntityDereferencerType().equals(siteConfiguration.getEntitySearcherType());
    // init the fieldMapper based on the configuration
    FieldMapper fieldMappings = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
    if (siteConfiguration.getFieldMappings() != null) {
        log.debug(" > Initialise configured field mappings");
        for (String configuredMapping : siteConfiguration.getFieldMappings()) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping, nsPrefixService);
            if (mapping != null) {
                log.debug("   - add FieldMapping {}", mapping);
                fieldMappings.addMapping(mapping);
            }
        }
    }
    // now init the referenced Services
    initDereferencerAndEntitySearcher();
    // Reference to the cache!
    if (siteConfiguration.getCacheId() != null) {
        String cacheFilter = String.format("(&(%s=%s)(%s=%s))", Constants.OBJECTCLASS, Cache.class.getName(), Cache.CACHE_YARD, siteConfiguration.getCacheId());
        cacheTracker = new ServiceTracker(ctx.getBundleContext(), ctx.getBundleContext().createFilter(cacheFilter), new ServiceTrackerCustomizer() {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                if (service.equals(cache)) {
                    cache = (Cache) cacheTracker.getService();
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
                bc.ungetService(reference);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                // the service.ranking might have changed ... so check if the
                // top ranked Cache is a different one
                Cache newCache = (Cache) cacheTracker.getService();
                if (newCache == null || !newCache.equals(cache)) {
                    // set the new cahce
                    cache = newCache;
                    // and update the service registration
                    updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                }
            }

            @Override
            public Object addingService(ServiceReference reference) {
                Object service = bc.getService(reference);
                if (service != null) {
                    if (// the first added Service or
                    cacheTracker.getServiceReference() == null || // the new service as higher ranking as the current
                    (reference.compareTo(cacheTracker.getServiceReference()) > 0)) {
                        cache = (Cache) service;
                        updateServiceRegistration(bc, siteConfiguration, dereferencerComponentInstance, entitySearcherComponentInstance, cache, nsPrefixService, offlineMode);
                    }
                // else the new service has lower ranking as the currently use one
                }
                // else service == null -> ignore
                return service;
            }
        });
        cacheTracker.open();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping) ServiceReference(org.osgi.framework.ServiceReference) ReferencedSiteConfigurationImpl(org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapper(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper) Cache(org.apache.stanbol.entityhub.servicesapi.yard.Cache) Activate(org.apache.felix.scr.annotations.Activate)

Example 4 with DefaultFieldMapperImpl

use of org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl in project stanbol by apache.

the class CacheUtils method readFieldConfig.

/**
 * Reads the field mapping config from an document
 * @param yard the yard of the parsed Representation
 * @param config the configuration MUST NOT be <code>null</code>
 * @param nsPrefixService if present '{prefix}:{localname}' configurations are
 * supported for the fieldmappings used by the cache.
 * @return A field mapper configured based on the configuration in the parsed {@link Representation}
 * @throws if the parsed {@link Representation} does not contain a value for {@value CacheConstants.FIELD_MAPPING_CONFIG_FIELD}.
 */
private static FieldMapper readFieldConfig(Yard yard, Representation config, NamespacePrefixService nsPrefixService) {
    Object mappingValue = config.getFirst(Cache.FIELD_MAPPING_CONFIG_FIELD);
    if (mappingValue != null) {
        DefaultFieldMapperImpl fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
        for (String mappingStirng : mappingValue.toString().split("\n")) {
            FieldMapping mapping = FieldMappingUtils.parseFieldMapping(mappingStirng, nsPrefixService);
            if (mapping != null) {
                log.info("  > add Mapping: " + mappingStirng);
                fieldMapper.addMapping(mapping);
            }
        }
        return fieldMapper;
    } else {
        return null;
    }
}
Also used : DefaultFieldMapperImpl(org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl) FieldMapping(org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)

Aggregations

DefaultFieldMapperImpl (org.apache.stanbol.entityhub.core.mapping.DefaultFieldMapperImpl)4 FieldMapping (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapping)4 ArrayList (java.util.ArrayList)2 FieldMapper (org.apache.stanbol.entityhub.servicesapi.mapping.FieldMapper)2 Activate (org.apache.felix.scr.annotations.Activate)1 ReferencedSiteConfigurationImpl (org.apache.stanbol.entityhub.core.site.ReferencedSiteConfigurationImpl)1 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)1 Cache (org.apache.stanbol.entityhub.servicesapi.yard.Cache)1 ServiceReference (org.osgi.framework.ServiceReference)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 ServiceTracker (org.osgi.util.tracker.ServiceTracker)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1