Author: rharo
Date: Thu Jun 20 12:40:38 2013
New Revision: 1494957

URL: http://svn.apache.org/r1494957
Log:
Fixed STANBOL-1118

Modified:
    
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
    
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java

Modified: 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java?rev=1494957&r1=1494956&r2=1494957&view=diff
==============================================================================
--- 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
 (original)
+++ 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/main/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProvider.java
 Thu Jun 20 12:40:38 2013
@@ -17,17 +17,16 @@
 package org.apache.stanbol.commons.namespaceprefix.provider.prefixcc;
 
 import java.io.IOException;
+import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.Date;
 import java.util.List;
 import java.util.ServiceLoader;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
 
 import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixProvider;
 import 
org.apache.stanbol.commons.namespaceprefix.impl.NamespacePrefixProviderImpl;
@@ -38,13 +37,17 @@ public class PrefixccProvider implements
 
     private static final Logger log = 
LoggerFactory.getLogger(PrefixccProvider.class);
         
-    public static final URL GET_ALL;
+    public static final URLConnection GET_ALL;
     static {
         try {
-            GET_ALL = new URL("http://prefix.cc/popular/all.file.txt";);
+            URL url = new URL("http://prefix.cc/popular/all.file.txt";);
+            GET_ALL = url.openConnection();
+            GET_ALL.connect();
         } catch (MalformedURLException e) {
             throw new IllegalStateException("Unable to create http://prefix.cc 
URL",e);
-        }
+        } catch (IOException e) {
+               throw new IllegalStateException("Unable to open 
http://prefix.cc URLConnection",e);
+               }
     }
     private final ScheduledExecutorService scheduler = 
             Executors.newScheduledThreadPool(1);
@@ -112,9 +115,11 @@ public class PrefixccProvider implements
     protected final void loadMappings() {
         try {
             log.info("Load Namespace Prefix Mappings form {}",GET_ALL);
-            cache = new NamespacePrefixProviderImpl(GET_ALL.openStream());
-            cacheStamp = System.currentTimeMillis();
-            log.info("  ... completed");
+            if(GET_ALL.getContentType().equals("text/plain") && 
((HttpURLConnection)GET_ALL).getResponseCode() == 200){
+               cache = new 
NamespacePrefixProviderImpl(GET_ALL.getInputStream());
+               cacheStamp = System.currentTimeMillis();
+               log.info("  ... completed");
+            }
         } catch (IOException e) {
             log.warn("Unable to load prefix.cc NamespaceMappings (Message: "
                 + e.getMessage() +")",e);

Modified: 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java?rev=1494957&r1=1494956&r2=1494957&view=diff
==============================================================================
--- 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
 (original)
+++ 
stanbol/trunk/commons/namespaceprefix/prefixccprovider/src/test/java/org/apache/stanbol/commons/namespaceprefix/provider/prefixcc/PrefixccProviderTest.java
 Thu Jun 20 12:40:38 2013
@@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit;
 import org.apache.stanbol.commons.namespaceprefix.NamespacePrefixService;
 import 
org.apache.stanbol.commons.namespaceprefix.service.StanbolNamespacePrefixService;
 import org.junit.Assert;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -69,9 +68,18 @@ public class PrefixccProviderTest {
     
     @Test
     public void testServiceLoader() throws IOException{
-        //this test works only if online
+        
+       // Check if the service is down
+       PrefixccProvider pcp = new PrefixccProvider(10,TimeUnit.SECONDS);
+       if(!pcp.isAvailable()){
+               log.info("Unable to retrieve prefixes from http://prefix.cc ... 
deactivating "
+                      + PrefixccProvider.class.getSimpleName()+ "ServiceLoader 
support test");
+                  return;
+       }
+       
+       //this test works only if online
         try {
-            PrefixccProvider.GET_ALL.openStream();
+            PrefixccProvider.GET_ALL.getInputStream();
         } catch (IOException e) {
            log.info("Unable to connect to http://prefix.cc ... deactivating "
                + PrefixccProvider.class.getSimpleName()+ "ServiceLoader 
support test");


Reply via email to