This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 98e07ce  [CAMEL-13263] Lenient IPFS connection check on startup
98e07ce is described below

commit 98e07ce51041f37b1fe077e3d6d75adc393865aa
Author: Thomas Diesler <tdies...@redhat.com>
AuthorDate: Tue Feb 26 14:56:25 2019 +0100

    [CAMEL-13263] Lenient IPFS connection check on startup
---
 .../apache/camel/component/ipfs/IPFSComponent.java | 16 -------
 .../apache/camel/component/ipfs/IPFSEndpoint.java  | 53 +++++++++++++++++----
 .../camel/component/ipfs/SimpleIPFSTest.java       | 54 ++++++++--------------
 3 files changed, 64 insertions(+), 59 deletions(-)

diff --git 
a/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSComponent.java
 
b/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSComponent.java
index a7260aa..4d162b1 100644
--- 
a/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSComponent.java
+++ 
b/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSComponent.java
@@ -19,9 +19,6 @@ package org.apache.camel.component.ipfs;
 import java.net.URI;
 import java.util.Map;
 
-import io.nessus.ipfs.client.DefaultIPFSClient;
-import io.nessus.ipfs.client.IPFSClient;
-
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
@@ -29,8 +26,6 @@ import org.apache.camel.support.DefaultComponent;
 @Component("ipfs")
 public class IPFSComponent extends DefaultComponent {
 
-    private IPFSClient client;
-
     @Override
     protected Endpoint createEndpoint(String urispec, String remaining, 
Map<String, Object> params) throws Exception {
 
@@ -55,17 +50,6 @@ public class IPFSComponent extends DefaultComponent {
         }
         config.setIpfsCmd(cmd);
 
-        client = createClient(config);
-
         return new IPFSEndpoint(urispec, this, config);
     }
-
-    public IPFSClient getIPFSClient() {
-        return client;
-    }
-
-    private synchronized IPFSClient createClient(IPFSConfiguration config) {
-        IPFSClient ipfsClient = new DefaultIPFSClient(config.getIpfsHost(), 
config.getIpfsPort());
-        return ipfsClient.connect();
-    }
 }
diff --git 
a/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSEndpoint.java
 
b/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSEndpoint.java
index 0298143..87eac75 100644
--- 
a/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSEndpoint.java
+++ 
b/components/camel-ipfs/src/main/java/org/apache/camel/component/ipfs/IPFSEndpoint.java
@@ -27,7 +27,9 @@ import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 
 import io.ipfs.multihash.Multihash;
+import io.nessus.ipfs.client.DefaultIPFSClient;
 import io.nessus.ipfs.client.IPFSClient;
+import io.nessus.ipfs.client.IPFSException;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
@@ -37,6 +39,8 @@ import 
org.apache.camel.component.ipfs.IPFSConfiguration.IPFSCommand;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.support.DefaultEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The camel-ipfs component provides access to the Interplanetary File System
@@ -45,16 +49,39 @@ import org.apache.camel.support.DefaultEndpoint;
 @UriEndpoint(firstVersion = "2.23.0", scheme = "ipfs", title = "IPFS", syntax 
= "ipfs:host:port/cmd", producerOnly = true, label = "file,ipfs")
 public class IPFSEndpoint extends DefaultEndpoint {
 
-    static long defaultTimeout = 10000L;
+    public static final long DEFAULT_TIMEOUT = 10000L;
+    
+    private static final Logger LOG = 
LoggerFactory.getLogger(IPFSComponent.class);
     
     @UriParam
-    private final IPFSConfiguration configuration;
+    private final IPFSConfiguration config;
+
+    private IPFSClient client;
 
-    public IPFSEndpoint(String uri, IPFSComponent component, IPFSConfiguration 
configuration) {
+    public IPFSEndpoint(String uri, IPFSComponent component, IPFSConfiguration 
config) {
         super(uri, component);
-        this.configuration = configuration;
+        this.config = config;
+        this.client = createClient(config);
+    }
+
+    public IPFSClient getIPFSClient() {
+        return client;
     }
 
+    public void setClient(IPFSClient client) {
+        this.client = client;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        try {
+            client.connect();
+        } catch (IPFSException ex) {
+            LOG.warn(ex.getMessage());
+        }
+    }
+    
     @Override
     public IPFSComponent getComponent() {
         return (IPFSComponent)super.getComponent();
@@ -76,11 +103,11 @@ public class IPFSEndpoint extends DefaultEndpoint {
     }
 
     IPFSConfiguration getConfiguration() {
-        return configuration;
+        return config;
     }
 
     IPFSCommand getCommand() {
-        String cmd = configuration.getIpfsCmd();
+        String cmd = config.getIpfsCmd();
         try {
             return IPFSCommand.valueOf(cmd);
         } catch (IllegalArgumentException ex) {
@@ -101,7 +128,7 @@ public class IPFSEndpoint extends DefaultEndpoint {
         Multihash mhash = Multihash.fromBase58(cid);
         Future<InputStream> future = ipfs().cat(mhash);
         try {
-            return future.get(defaultTimeout, TimeUnit.MILLISECONDS);
+            return future.get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
         } catch (InterruptedException | ExecutionException ex) {
             throw new IOException("Cannot obtain: " + cid, ex);
         }
@@ -111,13 +138,21 @@ public class IPFSEndpoint extends DefaultEndpoint {
         Multihash mhash = Multihash.fromBase58(cid);
         Future<Path> future = ipfs().get(mhash, outdir);
         try {
-            return future.get(defaultTimeout, TimeUnit.MILLISECONDS);
+            return future.get(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
         } catch (InterruptedException | ExecutionException ex) {
             throw new IOException("Cannot obtain: " + cid, ex);
         }
     }
 
     private IPFSClient ipfs() {
-        return getComponent().getIPFSClient();
+        if (!client.hasConnection()) {
+            client.connect();
+        }
+        return client;
+    }
+    
+    private IPFSClient createClient(IPFSConfiguration config) {
+        IPFSClient ipfsClient = new DefaultIPFSClient(config.getIpfsHost(), 
config.getIpfsPort());
+        return ipfsClient;
     }
 }
diff --git 
a/components/camel-ipfs/src/test/java/org/apache/camel/component/ipfs/SimpleIPFSTest.java
 
b/components/camel-ipfs/src/test/java/org/apache/camel/component/ipfs/SimpleIPFSTest.java
index 00bcf6a..f375427 100644
--- 
a/components/camel-ipfs/src/test/java/org/apache/camel/component/ipfs/SimpleIPFSTest.java
+++ 
b/components/camel-ipfs/src/test/java/org/apache/camel/component/ipfs/SimpleIPFSTest.java
@@ -25,7 +25,6 @@ import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.List;
 
-
 import io.nessus.ipfs.client.DefaultIPFSClient;
 import io.nessus.ipfs.client.IPFSClient;
 import io.nessus.ipfs.client.IPFSException;
@@ -37,24 +36,10 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.junit.Assert;
 import org.junit.Assume;
-import org.junit.Before;
 import org.junit.Test;
 
 public class SimpleIPFSTest {
 
-    IPFSClient ipfs;
-    
-    @Before
-    public void before() {
-        ipfs = new DefaultIPFSClient("127.0.0.1", 5001);
-        try {
-            ipfs.connect();
-        } catch (IPFSException ex) {
-            // ignore
-        }
-        Assume.assumeTrue(ipfs.hasConnection());
-    }
-    
     @Test
     public void ipfsVersion() throws Exception {
 
@@ -69,8 +54,8 @@ public class SimpleIPFSTest {
         });
 
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
             ProducerTemplate producer = camelctx.createProducerTemplate();
             String resA = producer.requestBody("direct:startA", null, 
String.class);
@@ -97,12 +82,11 @@ public class SimpleIPFSTest {
             }
         });
 
-        Path path = Paths.get("src/test/resources/html/index.html");
-
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
+            Path path = Paths.get("src/test/resources/html/index.html");
             ProducerTemplate producer = camelctx.createProducerTemplate();
             String res = producer.requestBody("direct:start", path, 
String.class);
             Assert.assertEquals(hash, res);
@@ -125,12 +109,11 @@ public class SimpleIPFSTest {
             }
         });
 
-        Path path = Paths.get("src/test/resources/html");
-
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
+            Path path = Paths.get("src/test/resources/html");
             ProducerTemplate producer = camelctx.createProducerTemplate();
             List<String> res = producer.requestBody("direct:start", path, 
List.class);
             Assert.assertEquals(10, res.size());
@@ -154,8 +137,8 @@ public class SimpleIPFSTest {
         });
 
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
             ProducerTemplate producer = camelctx.createProducerTemplate();
             InputStream res = producer.requestBody("direct:start", hash, 
InputStream.class);
@@ -179,8 +162,8 @@ public class SimpleIPFSTest {
         });
 
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
             ProducerTemplate producer = camelctx.createProducerTemplate();
             Path res = producer.requestBody("direct:start", hash, Path.class);
@@ -205,8 +188,8 @@ public class SimpleIPFSTest {
         });
 
         camelctx.start();
-        assumeIPFS(camelctx);
-
+        assumeIPFSAvailable(camelctx);
+        
         try {
             ProducerTemplate producer = camelctx.createProducerTemplate();
             Path res = producer.requestBody("direct:start", hash, Path.class);
@@ -224,8 +207,11 @@ public class SimpleIPFSTest {
         Assert.assertEquals("The quick brown fox jumps over the lazy dog.", 
new String(baos.toByteArray()));
     }
 
-    private void assumeIPFS(CamelContext camelctx) {
-        IPFSComponent comp = camelctx.getComponent("ipfs", 
IPFSComponent.class);
-        Assume.assumeTrue(comp.getIPFSClient().hasConnection());
+    private void assumeIPFSAvailable(CamelContext camelctx) throws Exception {
+        IPFSEndpoint ipfsEp = camelctx.getEndpoints().stream()
+                .filter(ep -> ep instanceof IPFSEndpoint)
+                .map(ep -> (IPFSEndpoint)ep)
+                .findFirst().get();
+        Assume.assumeTrue(ipfsEp.getIPFSClient().hasConnection());
     }
 }

Reply via email to