Author: rwesten
Date: Thu Feb  6 11:29:12 2014
New Revision: 1565177

URL: http://svn.apache.org/r1565177
Log:
implementation of STANBOL-1276 for 0.12.0

Added:
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ExecutorServiceProvider.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/StaticExecutorServiceProvider.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedDereferenceThreadPool.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedExecutorServiceProvider.java
Modified:
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferenceEngine.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferencer.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SiteDereferencer.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SitesDereferencer.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
    
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferenceEngine.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferenceEngine.java?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferenceEngine.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferenceEngine.java
 Thu Feb  6 11:29:12 2014
@@ -22,7 +22,11 @@ import static org.apache.stanbol.enhance
 import static org.osgi.framework.Constants.SERVICE_RANKING;
 
 import java.util.Dictionary;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.ConfigurationPolicy;
@@ -34,6 +38,8 @@ import org.apache.stanbol.commons.namesp
 import org.apache.stanbol.enhancer.engines.dereference.DereferenceConstants;
 import org.apache.stanbol.enhancer.engines.dereference.DereferenceEngineConfig;
 import org.apache.stanbol.enhancer.engines.dereference.EntityDereferenceEngine;
+import 
org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedDereferenceThreadPool;
+import 
org.apache.stanbol.enhancer.engines.dereference.entityhub.shared.SharedExecutorServiceProvider;
 import org.apache.stanbol.enhancer.servicesapi.EnhancementEngine;
 import org.apache.stanbol.enhancer.servicesapi.ServiceProperties;
 import org.apache.stanbol.entityhub.servicesapi.Entityhub;
@@ -45,6 +51,8 @@ import org.osgi.service.component.Compon
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
 /**
  * The EntityhubLinkingEngine in NOT an {@link EnhancementEngine} but only an
  * OSGI {@link Component} that allows to configure instances of the
@@ -72,6 +80,10 @@ import org.slf4j.LoggerFactory;
     @Property(name=DEREFERENCE_ENTITIES_FIELDS,cardinality=Integer.MAX_VALUE,
        
value={"rdfs:comment","geo:lat","geo:long","foaf:depiction","dbp-ont:thumbnail"}),
     @Property(name=DEREFERENCE_ENTITIES_LDPATH, cardinality=Integer.MAX_VALUE),
+    @Property(name=EntityhubDereferenceEngine.SHARED_THREAD_POOL_STATE,
+       boolValue=EntityhubDereferenceEngine.DEFAULT_SHARED_THREAD_POOL_STATE),
+    @Property(name=EntityhubDereferenceEngine.THREAD_POOL_SIZE,
+       intValue=EntityhubDereferenceEngine.DEFAULT_THREAD_POOL_SIZE),
     @Property(name=SERVICE_RANKING,intValue=0)
 })
 public class EntityhubDereferenceEngine implements ServiceTrackerCustomizer {
@@ -87,6 +99,18 @@ public class EntityhubDereferenceEngine 
      */
     public static final String SITE_ID = 
"enhancer.engines.dereference.entityhub.siteId";
 
+    /**
+     * If a Entityhub dereferencer should use the shared thread pool
+     */
+    public static final String SHARED_THREAD_POOL_STATE = 
"enhancer.engines.dereference.entityhub.threads.shared";
+
+    public static final boolean DEFAULT_SHARED_THREAD_POOL_STATE = true;
+    
+    public static final String THREAD_POOL_SIZE = 
"enhancer.engines.dereference.entityhub.threads.size";
+    
+    public static final int DEFAULT_THREAD_POOL_SIZE = 0;
+    
+    private int threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
     
     /**
      * The engine initialised based on the configuration of this component
@@ -111,6 +135,8 @@ public class EntityhubDereferenceEngine 
 
     private BundleContext bundleContext;
 
+       private ExecutorService executorService;
+
     /**
      * Default constructor as used by OSGI. This expects that 
      * {@link #activate(ComponentContext)} is called before usage
@@ -139,17 +165,61 @@ public class EntityhubDereferenceEngine 
             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, null);
+            entityDereferencer = new EntityhubDereferencer(bundleContext, 
this, esProvider);
         } else if(siteName.equals("*")){
             log.info("  ... init dereferencer for all referenced sites");
-            entityDereferencer = new SitesDereferencer(bundleContext, this, 
null);
+            entityDereferencer = new SitesDereferencer(bundleContext, this, 
esProvider);
         } else {
             log.info(" ... init dereferencer for referenced site {}", 
siteName);
-            entityDereferencer = new SiteDereferencer(bundleContext,siteName, 
this, null);
+            entityDereferencer = new SiteDereferencer(bundleContext,siteName, 
this, esProvider);
         }
         //set the namespace prefix service to the dereferencer
         entityDereferencer.setNsPrefixService(prefixService);
@@ -175,6 +245,10 @@ public class EntityhubDereferenceEngine 
             reg.unregister();
             engineRegistration = null;
         }
+        if(executorService != null){
+               executorService.shutdown();
+               executorService = null;
+        }
         //* reset engine
         entityDereferenceEngine = null;
         engineMetadata = null;

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferencer.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferencer.java?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferencer.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/EntityhubDereferencer.java
 Thu Feb  6 11:29:12 2014
@@ -16,8 +16,6 @@
 */
 package org.apache.stanbol.enhancer.engines.dereference.entityhub;
 
-import java.util.concurrent.ExecutorService;
-
 import org.apache.stanbol.enhancer.engines.dereference.EntityDereferencer;
 import org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend;
 import org.apache.stanbol.entityhub.servicesapi.Entityhub;
@@ -36,14 +34,15 @@ import at.newmedialab.ldpath.api.backend
  */
 public final class EntityhubDereferencer extends 
TrackingDereferencerBase<Entityhub> implements EntityDereferencer {
     
-    private final ExecutorService executorService;
     
-    public EntityhubDereferencer(BundleContext context, ExecutorService 
executorService) {
-        this(context,null,executorService);
+    public EntityhubDereferencer(BundleContext context) {
+        this(context,null,null);
+    }
+    public EntityhubDereferencer(BundleContext context, 
ExecutorServiceProvider executorServiceProvider) {
+        this(context,null,executorServiceProvider);
     }
-    public EntityhubDereferencer(BundleContext context, 
ServiceTrackerCustomizer customizer, ExecutorService executorService) {
-        super(context,Entityhub.class,null,customizer);
-        this.executorService = executorService;
+    public EntityhubDereferencer(BundleContext context, 
ServiceTrackerCustomizer customizer, ExecutorServiceProvider 
executorServiceProvider) {
+        super(context,Entityhub.class,null,customizer, 
executorServiceProvider);
     }
     
     @Override
@@ -56,10 +55,6 @@ public final class EntityhubDereferencer
     public boolean supportsOfflineMode() {
         return true; //the entityhub is always offline
     }
-    @Override
-    public ExecutorService getExecutor() {
-        return executorService;
-    }
     
     @Override
     protected RDFBackend<Object> createRdfBackend(Entityhub service) {

Added: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ExecutorServiceProvider.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ExecutorServiceProvider.java?rev=1565177&view=auto
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ExecutorServiceProvider.java
 (added)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ExecutorServiceProvider.java
 Thu Feb  6 11:29:12 2014
@@ -0,0 +1,25 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.engines.dereference.entityhub;
+
+import java.util.concurrent.ExecutorService;
+
+public interface ExecutorServiceProvider {
+
+       ExecutorService getExecutorService();
+       
+}

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SiteDereferencer.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SiteDereferencer.java?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SiteDereferencer.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SiteDereferencer.java
 Thu Feb  6 11:29:12 2014
@@ -35,17 +35,18 @@ public final class SiteDereferencer exte
 //    private final Logger log = 
LoggerFactory.getLogger(SiteDereferencer.class);
     
     private final String siteId;
-    private final ExecutorService executorService;
 
-    public SiteDereferencer(BundleContext context, String siteId, 
ExecutorService executorService){
-        this(context,siteId, null, executorService);
+    public SiteDereferencer(BundleContext context, String siteId){
+        this(context,siteId, null, null);
     }
-    public SiteDereferencer(BundleContext context, String siteId, 
ServiceTrackerCustomizer customizer, ExecutorService executorService) {
+    public SiteDereferencer(BundleContext context, String siteId, 
ExecutorServiceProvider executorServiceProvider){
+        this(context,siteId, null, executorServiceProvider);
+    }
+    public SiteDereferencer(BundleContext context, String siteId, 
ServiceTrackerCustomizer customizer, ExecutorServiceProvider 
executorServiceProvider) {
         super(context, Site.class, 
             Collections.singletonMap(SiteConfiguration.ID,siteId),
-            customizer);
+            customizer, executorServiceProvider);
         this.siteId = siteId;
-        this.executorService = executorService;
     }
     
     @Override
@@ -54,10 +55,6 @@ public final class SiteDereferencer exte
         //Do not throw an exception here if the site is not available. Just 
return false
         return site == null ? false : site.supportsLocalMode();
     }
-    @Override
-    public ExecutorService getExecutor() {
-        return executorService;
-    }
     
     @Override
     protected Representation getRepresentation(Site site, String id, boolean 
offlineMode) throws EntityhubException {

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SitesDereferencer.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SitesDereferencer.java?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SitesDereferencer.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/SitesDereferencer.java
 Thu Feb  6 11:29:12 2014
@@ -31,25 +31,22 @@ import at.newmedialab.ldpath.api.backend
 public final class SitesDereferencer extends 
TrackingDereferencerBase<SiteManager> {
     
 //    private final Logger log = 
LoggerFactory.getLogger(SiteDereferencer.class);
-    
-    private final ExecutorService executorService;
 
-    public SitesDereferencer(BundleContext context, ExecutorService 
executorService){
-        this(context, null, executorService);
+    public SitesDereferencer(BundleContext context){
+        this(context, null, null);
+    }
+    public SitesDereferencer(BundleContext context, ExecutorServiceProvider 
executorServiceProvider){
+        this(context, null, executorServiceProvider);
     }
-    public SitesDereferencer(BundleContext context, ServiceTrackerCustomizer 
customizer, ExecutorService executorService) {
-        super(context, SiteManager.class, null, customizer);
-        this.executorService = executorService;
+    public SitesDereferencer(BundleContext context, ServiceTrackerCustomizer 
customizer, 
+               ExecutorServiceProvider executorServiceprovider) {
+        super(context, SiteManager.class, null, customizer, 
executorServiceprovider);
     }
     
     @Override
     public boolean supportsOfflineMode() {
         return true; //can not be determined here .. return true
     }
-    @Override
-    public ExecutorService getExecutor() {
-        return executorService;
-    }
     
     @Override
     protected Representation getRepresentation(SiteManager sm, String id, 
boolean offlineMode) throws EntityhubException {

Added: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/StaticExecutorServiceProvider.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/StaticExecutorServiceProvider.java?rev=1565177&view=auto
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/StaticExecutorServiceProvider.java
 (added)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/StaticExecutorServiceProvider.java
 Thu Feb  6 11:29:12 2014
@@ -0,0 +1,39 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.engines.dereference.entityhub;
+
+import java.util.concurrent.ExecutorService;
+
+public class StaticExecutorServiceProvider implements ExecutorServiceProvider {
+
+       private ExecutorService executorService;
+
+       public StaticExecutorServiceProvider(ExecutorService executorService){
+               this.executorService = executorService;
+       }
+       
+       @Override
+       public ExecutorService getExecutorService() {
+               if(executorService != null && !executorService.isShutdown()){
+                       return executorService;
+               } else {
+                       return null;
+               }
+       }
+
+       
+}

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
 Thu Feb  6 11:29:12 2014
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.locks.Lock;
 
 import org.apache.clerezza.rdf.core.Language;
@@ -100,6 +101,8 @@ public abstract class TrackingDereferenc
     private Map<T,RDFBackend<Object>> rdfBackendCache = new 
IdentityHashMap<T,RDFBackend<Object>>();
 
     private final Class<T> serviceClass;
+
+       private ExecutorServiceProvider executorServiceProvider;
     /**
      * Creates a new instance for the parsed parameter
      * @param context the BundleContexed used to create the {@link 
ServiceTracker}
@@ -108,9 +111,11 @@ public abstract class TrackingDereferenc
      * @param filterEntries
      */
     protected TrackingDereferencerBase(BundleContext context, Class<T> 
serviceClass,
-            Map<String,String> filterEntries, ServiceTrackerCustomizer 
customizer){
+            Map<String,String> filterEntries, ServiceTrackerCustomizer 
customizer,
+            ExecutorServiceProvider executorServiceProvider){
         this.bundleContext = context;
         this.serviceClass = serviceClass;
+        this.executorServiceProvider = executorServiceProvider;
         //the fieldMapper allows to configure users fields that should be 
dereferenced
         if(filterEntries == null || filterEntries.isEmpty()){
             searchServiceTracker = new ServiceTracker(context, 
serviceClass.getName(), customizer);
@@ -271,6 +276,11 @@ public abstract class TrackingDereferenc
     }
     
     @Override
+    public final ExecutorService getExecutor() {
+       return executorServiceProvider == null ? null : 
executorServiceProvider.getExecutorService();
+    }
+    
+    @Override
     public final boolean dereference(UriRef uri, MGraph graph, Lock writeLock, 
DereferenceContext derefContext) throws DereferenceException {
         T service = getService();
         if(service == null){

Added: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedDereferenceThreadPool.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedDereferenceThreadPool.java?rev=1565177&view=auto
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedDereferenceThreadPool.java
 (added)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedDereferenceThreadPool.java
 Thu Feb  6 11:29:12 2014
@@ -0,0 +1,135 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.engines.dereference.entityhub.shared;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+/**
+ * Component that registers a Thread
+ * @author westei
+ *
+ */
+@Component(
+           configurationFactory = false, //only a single instance 
+           policy = ConfigurationPolicy.OPTIONAL, // the baseUri is required!
+           specVersion = "1.1", 
+           metatype = true, 
+           immediate = true)
+public class SharedDereferenceThreadPool {
+       
+       private final Logger log = 
LoggerFactory.getLogger(SharedDereferenceThreadPool.class);
+
+       
@Property(intValue=SharedDereferenceThreadPool.DEFAULT_SHARED_THREAD_POOL_SIZE)
+       public static final String SHARED_THREAD_POOL_SIZE = 
"enhancer.engines.dereference.entityhub.sharedthreadpool.size";
+       public static final int DEFAULT_SHARED_THREAD_POOL_SIZE = 8;
+       
+       /**
+        * used as key for {@link Filter}s to track the shared thread pool
+        */
+       static final String SHARED_THREAD_POOL_NAME = 
"enhancer.engines.dereference.entityhub.sharedthreadpool.name";
+       /**
+        * used as value for {@link Filter}s to track the shared thread pool
+        */
+       static final String DEFAULT_SHARED_THREAD_POOL_NAME = "shared";
+       
+       /**
+        * {@link Filter} string for tracking the {@link ExecutorService} 
registered
+        * by this component as OSGI service
+        */
+       public static String SHARED_THREAD_POOL_FILTER = String.format(
+                       "(&(%s=%s)(%s=%s))",
+                       Constants.OBJECTCLASS,ExecutorService.class.getName(),
+                       SHARED_THREAD_POOL_NAME, 
DEFAULT_SHARED_THREAD_POOL_NAME);
+
+       private ServiceRegistration serviceRegistration;
+
+       private ExecutorService executorService;
+       
+       @Activate
+       protected void activate(ComponentContext ctx) throws 
ConfigurationException {
+               log.info("activate {}",getClass().getSimpleName());
+               Object value = ctx.getProperties().get(SHARED_THREAD_POOL_SIZE);
+               int poolSize;
+               if(value == null){
+                       poolSize = DEFAULT_SHARED_THREAD_POOL_SIZE;
+               } else if(value instanceof Number){
+                       poolSize = ((Number)value).intValue();
+               } else {
+                       try {
+                               poolSize = Integer.parseInt(value.toString());
+                       } catch (NumberFormatException e){
+                       throw new 
ConfigurationException(SHARED_THREAD_POOL_SIZE, "Value '" + value
+                                       + "'(type: 
"+value.getClass().getName()+") can not be parsed "
+                                       + "as Integer");
+                       }
+               }
+               if(poolSize == 0){
+                       poolSize = DEFAULT_SHARED_THREAD_POOL_SIZE;
+               }
+               if(poolSize < 0){
+                       log.info("{} is deactivated because configured thread 
pool size < 0",
+                                       getClass().getSimpleName());
+               } else {
+            String namePattern = getClass().getSimpleName()+"-thread-%s";
+            ThreadFactory threadFactory = new ThreadFactoryBuilder()
+                .setNameFormat(namePattern)
+                .setDaemon(true).build();
+            log.debug(" - create Threadpool(namePattern='{}' | size='{}')",
+                namePattern,poolSize);
+            executorService = Executors.newFixedThreadPool(poolSize, 
threadFactory);
+            Dictionary<String, Object> dict = new Hashtable<String, Object>();
+            dict.put(SHARED_THREAD_POOL_SIZE, poolSize);
+            dict.put(SHARED_THREAD_POOL_NAME, DEFAULT_SHARED_THREAD_POOL_NAME);
+            log.debug(" - register ExecutorService");
+                       serviceRegistration = 
ctx.getBundleContext().registerService(
+                                       ExecutorService.class.getName(), 
executorService, dict);
+               }
+       }
+       
+       @Deactivate
+       protected void deactivate(ComponentContext ctx){
+               log.info("deactivate {}", getClass().getSimpleName());
+               if(serviceRegistration != null){
+                       serviceRegistration.unregister();
+               }
+               if(executorService != null){
+                       log.info(" ... shutdown ExecutorService");
+                       executorService.shutdown();
+                       executorService = null;
+               }
+       }
+       
+}

Added: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedExecutorServiceProvider.java
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedExecutorServiceProvider.java?rev=1565177&view=auto
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedExecutorServiceProvider.java
 (added)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/shared/SharedExecutorServiceProvider.java
 Thu Feb  6 11:29:12 2014
@@ -0,0 +1,62 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License.  You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.engines.dereference.entityhub.shared;
+
+import java.util.concurrent.ExecutorService;
+
+import 
org.apache.stanbol.enhancer.engines.dereference.entityhub.ExecutorServiceProvider;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * Utility that wraps a {@link ServiceTracker} to lookup the shared thread
+ * pool for dereferencing
+ * 
+ * @author Rupert Westenthaler
+ *
+ */
+public class SharedExecutorServiceProvider implements ExecutorServiceProvider {
+
+       private ServiceTracker tracker;
+
+       public SharedExecutorServiceProvider(BundleContext context) {
+               try {
+                       this.tracker = new ServiceTracker(context, 
context.createFilter(
+                                       
SharedDereferenceThreadPool.SHARED_THREAD_POOL_FILTER), null);
+               } catch (InvalidSyntaxException e) {
+                       throw new IllegalStateException("Unable to create 
filter for the " +
+                                       
SharedDereferenceThreadPool.class.getSimpleName(), e);
+               }
+       }
+       
+       @Override
+       public ExecutorService getExecutorService(){
+               ExecutorService executorService = 
(ExecutorService)tracker.getService();
+               if(executorService != null && !executorService.isShutdown()){
+                       return executorService;
+               } else {
+                       return null;
+               }
+       }
+       
+       public void close(){
+               tracker.close();
+       }
+
+       
+}

Modified: 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: 
http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1565177&r1=1565176&r2=1565177&view=diff
==============================================================================
--- 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/resources/OSGI-INF/metatype/metatype.properties
 (original)
+++ 
stanbol/branches/release-0.12/enhancement-engines/dereference/entityhub/src/main/resources/OSGI-INF/metatype/metatype.properties
 Thu Feb  6 11:29:12 2014
@@ -73,3 +73,14 @@ his engine will use both the configured 
 if it can dereference an Entity. If any of those configuration matches the \
 Entity will be dereferenced.
 
+enhancer.engines.dereference.entityhub.threads.size.name=Dereference Threads
+enhancer.engines.dereference.entityhub.threads.size.description=If no shared \
+Thread pool is used this allows to configure the size of the thread pool just \
+used by this engine. For values < 1 no Thread Pool will be created and the 
calling \
+thread will get used for dereferencing entities. 
+
+enhancer.engines.dereference.entityhub.threads.shared.name=Use Shared Thread 
Pool
+enhancer.engines.dereference.entityhub.threads.shared.description=If enabled 
the \
+Dereferencing will use a Thread Pool that is shared with other Entityhub 
Dereference \
+Engines. If disabled the 'Dereference Threads' property can be used to setup 
an \
+own Thread Pool used by just this engine instance.


Reply via email to