You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2014/04/18 13:47:10 UTC

svn commit: r1588449 - in /stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype: ValueTypeParserRegistry.java ValueTypeSerializerRegistry.java

Author: rwesten
Date: Fri Apr 18 11:47:10 2014
New Revision: 1588449

URL: http://svn.apache.org/r1588449
Log:
fixed to additional cases of org.osgi.framework.ServiceException because of cycles for components that use a ServiceTracker to track components of an interface they implement themself. (STANBOL-1322)

Modified:
    stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeParserRegistry.java
    stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeSerializerRegistry.java

Modified: stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeParserRegistry.java
URL: http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeParserRegistry.java?rev=1588449&r1=1588448&r2=1588449&view=diff
==============================================================================
--- stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeParserRegistry.java (original)
+++ stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeParserRegistry.java Fri Apr 18 11:47:10 2014
@@ -82,18 +82,37 @@ public class ValueTypeParserRegistry {
         if(!inOsgi && valueTypeParsers == null){
             initValueTypeParser(); //running outside OSGI
         }
-        parserLock.readLock().lock();
-        try {
-            if(!inOsgi){
+        if(!inOsgi){
+            parserLock.readLock().lock();
+            try {
                 return (ValueTypeParser<T>)valueTypeParsers.get(type);
-            } else {
+            } finally {
+                parserLock.readLock().unlock();
+            }
+        } else {
+            ServiceTracker parserTracker = this.parserTracker;
+            //check if we need to lazily open the ServiceTracker
+            if(valueTypeParserRefs == null && parserTracker != null){
+                synchronized (parserTracker) {
+                    if(valueTypeParserRefs == null){ 
+                        valueTypeParserRefs = new HashMap<Class<?>,List<ServiceReference>>();
+                        //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.) 
+                        parserTracker.open();
+                    }
+                }
+            }
+            parserLock.readLock().lock();
+            try {
                 List<ServiceReference> refs = valueTypeParserRefs.get(type);
                 return refs == null || refs.isEmpty() ? null :
                     (ValueTypeParser<T>) parserTracker.getService(
                         refs.get(refs.size()-1));
+            } finally {
+                parserLock.readLock().unlock();
             }
-        } finally {
-            parserLock.readLock().unlock();
         }
     }
     
@@ -101,23 +120,31 @@ public class ValueTypeParserRegistry {
     protected void activate(ComponentContext ctx){
         inOsgi = true;
         final BundleContext bc = ctx.getBundleContext();
-        valueTypeParserRefs = new HashMap<Class<?>,List<ServiceReference>>();
         parserTracker = new ServiceTracker(bc, ValueTypeParser.class.getName(), 
             new ParserTracker(bc));
-        parserTracker.open();
+        //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.) 
     }
     
     @Deactivate
     protected void deactivate(ComponentContext ctx){
         inOsgi = false;
-        parserTracker.close();
-        parserTracker = null;
+        if(parserTracker != null){
+            parserTracker.close();
+            parserTracker = null;
+        }
         parserLock.writeLock().lock();
         try {
             if(valueTypeParsers != null){
                 valueTypeParsers.clear();
                 valueTypeParsers = null;
             }
+            if(valueTypeParserRefs != null){
+                valueTypeParserRefs.clear();
+                valueTypeParserRefs = null;
+            }
         } finally {
             parserLock.writeLock().unlock();
         }

Modified: stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeSerializerRegistry.java
URL: http://svn.apache.org/viewvc/stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeSerializerRegistry.java?rev=1588449&r1=1588448&r2=1588449&view=diff
==============================================================================
--- stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeSerializerRegistry.java (original)
+++ stanbol/branches/release-0.12/enhancer/generic/nlp-json/src/main/java/org/apache/stanbol/enhancer/nlp/json/valuetype/ValueTypeSerializerRegistry.java Fri Apr 18 11:47:10 2014
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -84,18 +85,37 @@ public class ValueTypeSerializerRegistry
         if(!inOsgi && valueTypeSerializers == null){
             initValueTypeSerializer(); //running outside OSGI
         }
-        serializerLock.readLock().lock();
-        try {
-            if(!inOsgi){
+        if(!inOsgi){
+            serializerLock.readLock().lock();
+            try {
                 return (ValueTypeSerializer<T>)valueTypeSerializers.get(type);
-            } else {
+            } finally {
+                serializerLock.readLock().unlock();
+            }
+        } else {
+            ServiceTracker serializerTracker = this.serializerTracker;
+            //check if we need to lazily open the ServiceTracker
+            if(valueTypeSerializerRefs == null && serializerTracker != null){
+                synchronized (serializerTracker) {
+                    if(valueTypeSerializerRefs == null){
+                        valueTypeSerializerRefs = new HashMap<Class<?>,List<ServiceReference>>();
+                        //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();
+                    }
+                }
+            }
+            serializerLock.readLock().lock();
+            try {
                 List<ServiceReference> refs = valueTypeSerializerRefs.get(type);
                 return refs == null || refs.isEmpty() ? null :
                     (ValueTypeSerializer<T>) serializerTracker.getService(
                         refs.get(refs.size()-1));
+            } finally {
+                serializerLock.readLock().unlock();
             }
-        } finally {
-            serializerLock.readLock().unlock();
         }
     }
     
@@ -103,10 +123,13 @@ public class ValueTypeSerializerRegistry
     protected void activate(ComponentContext ctx){
         inOsgi = true;
         final BundleContext bc = ctx.getBundleContext();
-        valueTypeSerializerRefs = new HashMap<Class<?>,List<ServiceReference>>();
         serializerTracker = new ServiceTracker(bc, ValueTypeSerializer.class.getName(), 
             new SerializerTracker(bc));
-        serializerTracker.open();
+        //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();
     }
     
     @Deactivate
@@ -120,6 +143,10 @@ public class ValueTypeSerializerRegistry
                 valueTypeSerializers.clear();
                 valueTypeSerializers = null;
             }
+            if(valueTypeSerializerRefs != null){
+                valueTypeSerializerRefs.clear();
+                valueTypeSerializerRefs = null;
+            }
         } finally {
             serializerLock.writeLock().unlock();
         }