Search in sources :

Example 1 with DereferenceContextFactory

use of org.apache.stanbol.enhancer.engines.dereference.DereferenceContextFactory in project stanbol by apache.

the class EntityhubDereferenceEngine method activate.

@Activate
@SuppressWarnings("unchecked")
protected void activate(ComponentContext ctx) throws ConfigurationException {
    Dictionary<String, Object> properties = ctx.getProperties();
    bundleContext = ctx.getBundleContext();
    log.info("> activate {}", getClass().getSimpleName());
    //get the metadata later set to the enhancement engine
    DereferenceEngineConfig engineConfig = new DereferenceEngineConfig(properties, prefixService);
    log.debug(" - engineName: {}", engineConfig.getEngineName());
    //parse the Entityhub Site used for dereferencing
    Object value = properties.get(SITE_ID);
    //init the EntitySource
    if (value == null) {
        //all referenced sites
        siteName = "*";
    } else {
        siteName = value.toString();
    }
    if (siteName.isEmpty()) {
        siteName = "*";
    }
    log.debug(" - siteName: {}", siteName);
    final boolean sharedPoolState;
    value = properties.get(SHARED_THREAD_POOL_STATE);
    if (value instanceof Boolean) {
        sharedPoolState = ((Boolean) value).booleanValue();
    } else if (value != null && !StringUtils.isBlank(value.toString())) {
        sharedPoolState = Boolean.parseBoolean(value.toString());
    } else {
        sharedPoolState = DEFAULT_SHARED_THREAD_POOL_STATE;
    }
    final ExecutorServiceProvider esProvider;
    log.debug(" - shared thread pool state: {}", sharedPoolState);
    if (sharedPoolState) {
        esProvider = new SharedExecutorServiceProvider(ctx.getBundleContext());
    } else {
        //we need to create our own ExecutorService
        value = properties.get(THREAD_POOL_SIZE);
        if (value instanceof Number) {
            this.threadPoolSize = ((Number) value).intValue();
        } else if (value != null) {
            try {
                this.threadPoolSize = Integer.parseInt(value.toString());
            } catch (NumberFormatException e) {
                throw new ConfigurationException(THREAD_POOL_SIZE, "Value '" + value + "'(type: " + value.getClass().getName() + ") can not be parsed " + "as Integer");
            }
        } else {
            this.threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
        }
        if (threadPoolSize > 0) {
            String namePattern = getClass().getSimpleName() + "-" + engineConfig.getEngineName() + "-thread-%s";
            ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(namePattern).setDaemon(true).build();
            log.debug(" - create Threadpool(namePattern='{}' | size='{}')", namePattern, threadPoolSize);
            executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
        } else {
            log.debug(" - no thread pool configured (poolSize: {})", threadPoolSize);
            executorService = null;
        }
        esProvider = new StaticExecutorServiceProvider(executorService);
    }
    //init the tracking entity searcher
    trackedServiceCount = 0;
    if (Entityhub.ENTITYHUB_IDS.contains(siteName.toLowerCase())) {
        log.info("  ... init Entityhub dereferencer");
        entityDereferencer = new EntityhubDereferencer(bundleContext, this, esProvider);
    } else if (siteName.equals("*")) {
        log.info("  ... init dereferencer for all referenced sites");
        entityDereferencer = new SitesDereferencer(bundleContext, this, esProvider);
    } else {
        log.info(" ... init dereferencer for referenced site {}", siteName);
        entityDereferencer = new SiteDereferencer(bundleContext, siteName, this, esProvider);
    }
    //set the namespace prefix service to the dereferencer
    entityDereferencer.setNsPrefixService(prefixService);
    //now parse dereference field config
    entityDereferencer.setDereferencedFields(engineConfig.getDereferenceFields());
    entityDereferencer.setLdPath(engineConfig.getLdPathProgram());
    entityDereferenceEngine = new EntityDereferenceEngine(entityDereferencer, engineConfig, new //we want to use our own DereferenceContext impl
    DereferenceContextFactory() {

        @Override
        public DereferenceContext createContext(EntityDereferenceEngine engine, Map<String, Object> enhancementProperties) throws DereferenceConfigurationException {
            return new EntityhubDereferenceContext(engine, enhancementProperties);
        }
    });
    //NOTE: registration of this instance as OSGI service is done as soon as the
    //      entityhub service backing the entityDereferencer is available.
    //finally start tracking
    entityDereferencer.open();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) DereferenceContextFactory(org.apache.stanbol.enhancer.engines.dereference.DereferenceContextFactory) DereferenceEngineConfig(org.apache.stanbol.enhancer.engines.dereference.DereferenceEngineConfig) SharedExecutorServiceProvider(org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider) EntityDereferenceEngine(org.apache.stanbol.enhancer.engines.dereference.EntityDereferenceEngine) SharedExecutorServiceProvider(org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider) ConfigurationException(org.osgi.service.cm.ConfigurationException) DereferenceConfigurationException(org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Map(java.util.Map) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Map (java.util.Map)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Activate (org.apache.felix.scr.annotations.Activate)1 DereferenceConfigurationException (org.apache.stanbol.enhancer.engines.dereference.DereferenceConfigurationException)1 DereferenceContextFactory (org.apache.stanbol.enhancer.engines.dereference.DereferenceContextFactory)1 DereferenceEngineConfig (org.apache.stanbol.enhancer.engines.dereference.DereferenceEngineConfig)1 EntityDereferenceEngine (org.apache.stanbol.enhancer.engines.dereference.EntityDereferenceEngine)1 SharedExecutorServiceProvider (org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1