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 2013/12/06 16:02:17 UTC

svn commit: r1548552 - in /stanbol/trunk/commons/namespaceprefix/service/src: main/java/org/apache/stanbol/commons/namespaceprefix/service/ test/java/org/apache/stanbol/commons/namespaceprefix/service/

Author: rwesten
Date: Fri Dec  6 15:02:17 2013
New Revision: 1548552

URL: http://svn.apache.org/r1548552
Log:
fix for STANBOL-1233 including a test case. Also added the possibility to import mappings from a inputstream (e.g. initialise the mapper from a resource in the classpath)

Modified:
    stanbol/trunk/commons/namespaceprefix/service/src/main/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixService.java
    stanbol/trunk/commons/namespaceprefix/service/src/test/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixServiceTest.java

Modified: stanbol/trunk/commons/namespaceprefix/service/src/main/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixService.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/service/src/main/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixService.java?rev=1548552&r1=1548551&r2=1548552&view=diff
==============================================================================
--- stanbol/trunk/commons/namespaceprefix/service/src/main/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixService.java (original)
+++ stanbol/trunk/commons/namespaceprefix/service/src/main/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixService.java Fri Dec  6 15:02:17 2013
@@ -81,6 +81,8 @@ public class StanbolNamespacePrefixServi
     private ReadWriteLock mappingsLock = new ReentrantReadWriteLock();
     private SortedMap<String,String> prefixMap = new TreeMap<String,String>();
     private SortedMap<String,List<String>> namespaceMap = new TreeMap<String,List<String>>();
+
+    private BundleContext bundleContext;
     
     /**
      * OSGI constructor <b> DO NOT USE</b> outside of an OSGI environment as this
@@ -94,7 +96,7 @@ public class StanbolNamespacePrefixServi
      * {@link NamespacePrefixProvider} implementations using the
      * Java {@link ServiceLoader} utility.
      * @param mappingFile the mapping file used to manage local mappings. If
-     * <code>null</code> no local mappings are supported.
+     * <code>null</code> no president local mappings are supported.
      * @throws IOException
      */
     public StanbolNamespacePrefixService(File mappingFile) throws IOException {
@@ -105,6 +107,14 @@ public class StanbolNamespacePrefixServi
         loader = ServiceLoader.load(NamespacePrefixProvider.class);
         
     }
+    /**
+     * Imports tab separated prefix mappings from the parsed Stream
+     * @param in
+     * @throws IOException
+     */
+    public void importPrefixMappings(InputStream in) throws IOException {
+        readPrefixMappings(in);
+    }
     
     public static NamespacePrefixService getInstance(){
         if(INSTANCE == null){
@@ -119,18 +129,24 @@ public class StanbolNamespacePrefixServi
     
     @Activate
     protected void activate(ComponentContext ctx) throws FileNotFoundException, IOException{
-        final BundleContext bc = ctx.getBundleContext();
+        bundleContext = ctx.getBundleContext();
         //(1) read the mappings
-        mappingsFile = bc.getDataFile(PREFIX_MAPPINGS);
+        mappingsFile = bundleContext.getDataFile(PREFIX_MAPPINGS);
         if(mappingsFile.isFile()){
             readPrefixMappings(new FileInputStream(mappingsFile));
         } //else no mappings yet ... nothing todo
-        providersTracker = new ServiceTracker(ctx.getBundleContext(), NamespacePrefixProvider.class.getName(),
+    }
+
+    /**
+     * 
+     */
+    private void openTracker() {
+        providersTracker = new ServiceTracker(bundleContext, NamespacePrefixProvider.class.getName(),
             new ServiceTrackerCustomizer() {
             
             @Override
             public void removedService(ServiceReference reference, Object service) {
-                bc.ungetService(reference);
+                bundleContext.ungetService(reference);
                 __sortedProviderRef = null;
             }
             
@@ -141,9 +157,9 @@ public class StanbolNamespacePrefixServi
             
             @Override
             public Object addingService(ServiceReference reference) {
-                Object service = bc.getService(reference);
+                Object service = bundleContext.getService(reference);
                 if(StanbolNamespacePrefixService.this.equals(service)){//we need not to track this instance
-                    bc.ungetService(reference);
+                    bundleContext.ungetService(reference);
                     return null;
                 }
                 __sortedProviderRef = null;
@@ -151,7 +167,6 @@ public class StanbolNamespacePrefixServi
             }
         });
         providersTracker.open();
-        ctx.getBundleContext().getDataFile(PREFIX_MAPPINGS);
     }
     /**
      * Expected to be called only during activation
@@ -208,10 +223,12 @@ public class StanbolNamespacePrefixServi
                 try {
                     //(1) persist the mapping
                     if(store){
-                        if(!mappingsFile.isFile()){
-                            mappingsFile.createNewFile();
-                        }
                         if(mappingsFile != null){
+                            if(!mappingsFile.isFile()){
+                                if(!mappingsFile.createNewFile()){
+                                    throw new IOException("Unable to create mapping file "+mappingsFile);
+                                }
+                            }
                             writePrefixMappings(new FileOutputStream(mappingsFile, false));
                         } //else do not persist mappings
                     }
@@ -251,7 +268,14 @@ public class StanbolNamespacePrefixServi
     }
     private ServiceReference[] getSortedProviderReferences(){
         ServiceReference[] refs = __sortedProviderRef;
-        if(providersTracker != null){ //OSGI variant
+        if(bundleContext != null){ //OSGI variant
+            if(providersTracker == null){ //lazy initialisation of the service tracker
+                synchronized (this) {
+                    if(providersTracker == null && bundleContext != null){
+                        openTracker();
+                    }
+                }
+            }
             //the check for the size ensures that registered/unregistered services
             //are not overlooked when that happens during this method is executed
             //by an other thread.
@@ -285,6 +309,7 @@ public class StanbolNamespacePrefixServi
     
     @Deactivate
     protected void deactivate(ComponentContext ctx) {
+        bundleContext = null;
         if(providersTracker != null) {
             providersTracker.close();
             providersTracker = null;

Modified: stanbol/trunk/commons/namespaceprefix/service/src/test/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixServiceTest.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/service/src/test/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixServiceTest.java?rev=1548552&r1=1548551&r2=1548552&view=diff
==============================================================================
--- stanbol/trunk/commons/namespaceprefix/service/src/test/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixServiceTest.java (original)
+++ stanbol/trunk/commons/namespaceprefix/service/src/test/java/org/apache/stanbol/commons/namespaceprefix/service/StanbolNamespacePrefixServiceTest.java Fri Dec  6 15:02:17 2013
@@ -16,10 +16,12 @@
 */
 package org.apache.stanbol.commons.namespaceprefix.service;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.List;
 
 import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService;
@@ -46,7 +48,17 @@ public class StanbolNamespacePrefixServi
         Assert.assertTrue(mappingFile.isFile());
         service = new StanbolNamespacePrefixService(mappingFile);
     }
-
+    @Test
+    public void testInitialisationWithoutLocalMappingFIle() throws IOException {
+        StanbolNamespacePrefixService service = new StanbolNamespacePrefixService(null);
+        service.setPrefix("dummy", "http://www.dummy.org/dummy#");
+        Assert.assertEquals("http://www.dummy.org/dummy#", service.getNamespace("dummy"));
+        service.importPrefixMappings(new ByteArrayInputStream(
+            "dummy2\thttp://www.dummy.org/dummy2#".getBytes(Charset.forName("UTF-8"))));
+        Assert.assertEquals("http://www.dummy.org/dummy2#", service.getNamespace("dummy2"));
+        
+    }
+    
     @Test
     public void testInitialisation(){
         //this tests the namespaces defined in namespaceprefix.mappings file