You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/01/09 09:24:17 UTC

[camel] branch master updated: CAMEL-12127: camel-ftp - Add option to turn on logging of transfer activity. Lets also see it in the JMX consumer so we can monitor when it last downloaded something.

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

davsclaus 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 2bc883d  CAMEL-12127: camel-ftp - Add option to turn on logging of transfer activity. Lets also see it in the JMX consumer so we can monitor when it last downloaded something.
2bc883d is described below

commit 2bc883da5c7a57cf518793dc02f93572e9d5ec64
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 9 09:56:47 2018 +0100

    CAMEL-12127: camel-ftp - Add option to turn on logging of transfer activity. Lets also see it in the JMX consumer so we can monitor when it last downloaded something.
---
 .../remote/DefaultFtpClientActivityListener.java   | 32 ++++++++++++++--------
 .../camel/component/file/remote/FtpEndpoint.java   | 14 ++++++++--
 .../camel/component/file/remote/FtpOperations.java |  6 +---
 .../camel/component/file/remote/FtpsEndpoint.java  |  2 ++
 4 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/DefaultFtpClientActivityListener.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/DefaultFtpClientActivityListener.java
index 9fea9cc..eecda94 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/DefaultFtpClientActivityListener.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/DefaultFtpClientActivityListener.java
@@ -23,15 +23,16 @@ import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.commons.net.io.CopyStreamEvent;
 import org.apache.commons.net.io.CopyStreamListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class DefaultFtpClientActivityListener implements FtpClientActivityListener, CopyStreamListener {
 
-    // TODO: allow to reconfigure level, interval, verbose etc via JMX
+    private static final Logger LOG = LoggerFactory.getLogger(FtpClientActivityListener.class);
 
     private final CamelLogger logger;
     private final String host;
-    private final boolean verbose;
-    private final int intervalSeconds;
+    private final FtpEndpoint endpoint;
     private boolean download = true;
 
     private String fileName;
@@ -45,10 +46,9 @@ public class DefaultFtpClientActivityListener implements FtpClientActivityListen
     private final StopWatch watch = new StopWatch();
     private final StopWatch interval = new StopWatch();
 
-    public DefaultFtpClientActivityListener(CamelLogger logger, boolean verbose, int intervalSeconds, String host) {
-        this.logger = logger;
-        this.verbose = verbose;
-        this.intervalSeconds = intervalSeconds;
+    public DefaultFtpClientActivityListener(FtpEndpoint endpoint, String host) {
+        this.logger = new CamelLogger(LOG);
+        this.endpoint = endpoint;
         this.host = host;
     }
 
@@ -162,10 +162,14 @@ public class DefaultFtpClientActivityListener implements FtpClientActivityListen
                 num = "99.9";
             }
             msg += " (progress: " + num + "%)";
+        } else {
+            // okay we do not know the total size, but then make what we have download so-far human readable
+            String size = StringHelper.humanReadableBytes(totalChunkSize);
+            msg += " (downloaded: " + size + ")";
         }
         doLogVerbose(msg);
         // however if the operation is slow then log once in a while
-        if (interval.taken() > intervalSeconds * 1000) {
+        if (interval.taken() > endpoint.getTransferLoggingIntervalSeconds() * 1000) {
             doLog(msg);
             interval.restart();
         }
@@ -208,11 +212,15 @@ public class DefaultFtpClientActivityListener implements FtpClientActivityListen
                 num = "99.9";
             }
             msg += " (progress: " + num + "%)";
+        } else {
+            // okay we do not know the total size, but then make what we have uploaded so-far human readable
+            String size = StringHelper.humanReadableBytes(totalChunkSize);
+            msg += " (uploaded: " + size + ")";
         }
         // each chunk is verbose
         doLogVerbose(msg);
         // however if the operation is slow then log once in a while
-        if (interval.taken() > intervalSeconds * 1000) {
+        if (interval.taken() > endpoint.getTransferLoggingIntervalSeconds() * 1000) {
             doLog(msg);
             interval.restart();
         }
@@ -251,14 +259,14 @@ public class DefaultFtpClientActivityListener implements FtpClientActivityListen
         // verbose implies regular log as well
         lastVerboseLogActivity = lastLogActivity;
         lastVerboseLogActivityTimestamp = lastLogActivityTimestamp;
-        logger.log(message);
+        logger.log(message, endpoint.getTransferLoggingLevel());
     }
 
     protected void doLogVerbose(String message) {
         lastVerboseLogActivity = message;
         lastVerboseLogActivityTimestamp = System.currentTimeMillis();
-        if (verbose) {
-            logger.log(message);
+        if (endpoint.isTransferLoggingVerbose()) {
+            logger.log(message, endpoint.getTransferLoggingLevel());
         }
     }
 }
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpEndpoint.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpEndpoint.java
index 00c169f..b67cbb5 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpEndpoint.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpEndpoint.java
@@ -24,6 +24,7 @@ import org.apache.camel.FailedToCreateProducerException;
 import org.apache.camel.LoggingLevel;
 import org.apache.camel.Processor;
 import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.component.file.GenericFileConfiguration;
 import org.apache.camel.component.file.GenericFileProducer;
 import org.apache.camel.component.file.remote.RemoteFileConfiguration.PathSeparator;
@@ -41,6 +42,7 @@ import org.apache.commons.net.ftp.FTPFile;
 @UriEndpoint(firstVersion = "1.1.0", scheme = "ftp", extendsScheme = "file", title = "FTP",
         syntax = "ftp:host:port/directoryName", alternativeSyntax = "ftp:username:password@host:port/directoryName",
         consumerClass = FtpConsumer.class, label = "file")
+@ManagedResource(description = "Managed FtpEndpoint")
 public class FtpEndpoint<T extends FTPFile> extends RemoteFileEndpoint<FTPFile> {
     protected int soTimeout;
     protected int dataTimeout;
@@ -260,7 +262,6 @@ public class FtpEndpoint<T extends FTPFile> extends RemoteFileEndpoint<FTPFile>
         this.dataTimeout = dataTimeout;
     }
 
-    @ManagedAttribute
     public LoggingLevel getTransferLoggingLevel() {
         return transferLoggingLevel;
     }
@@ -268,11 +269,20 @@ public class FtpEndpoint<T extends FTPFile> extends RemoteFileEndpoint<FTPFile>
     /**
      * Configure the logging level to use when logging the progress of upload and download operations.
      */
-    @ManagedAttribute(description = "Logging level to use when logging the progress of upload and download operations")
     public void setTransferLoggingLevel(LoggingLevel transferLoggingLevel) {
         this.transferLoggingLevel = transferLoggingLevel;
     }
 
+    @ManagedAttribute(description = "Logging level to use when logging the progress of upload and download operations")
+    public void setTransferLoggingLevelName(String transferLoggingLevel) {
+        this.transferLoggingLevel = getCamelContext().getTypeConverter().convertTo(LoggingLevel.class, transferLoggingLevel);
+    }
+
+    @ManagedAttribute
+    public String getTransferLoggingLevelName() {
+        return transferLoggingLevel.name();
+    }
+
     @ManagedAttribute
     public int getTransferLoggingIntervalSeconds() {
         return transferLoggingIntervalSeconds;
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
index f19156b..b9b10c1 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
@@ -35,7 +35,6 @@ import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.component.file.GenericFileEndpoint;
 import org.apache.camel.component.file.GenericFileExist;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
-import org.apache.camel.util.CamelLogger;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -57,7 +56,6 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> {
     protected final Logger log = LoggerFactory.getLogger(getClass());
     protected final FTPClient client;
     protected final FTPClientConfig clientConfig;
-    protected final CamelLogger transferLogger = new CamelLogger(LoggerFactory.getLogger(FtpClientActivityListener.class));
     protected FtpEndpoint<FTPFile> endpoint;
     protected FtpClientActivityListener clientActivityListener;
 
@@ -69,9 +67,7 @@ public class FtpOperations implements RemoteFileOperations<FTPFile> {
     public void setEndpoint(GenericFileEndpoint<FTPFile> endpoint) {
         this.endpoint = (FtpEndpoint<FTPFile>) endpoint;
         // setup download listener/logger when we have the endpoint configured
-        transferLogger.setLevel(this.endpoint.getTransferLoggingLevel());
-        this.clientActivityListener = new DefaultFtpClientActivityListener(transferLogger, this.endpoint.isTransferLoggingVerbose(),
-            this.endpoint.getTransferLoggingIntervalSeconds(), this.endpoint.getConfiguration().remoteServerInformation());
+        this.clientActivityListener = new DefaultFtpClientActivityListener(this.endpoint, this.endpoint.getConfiguration().remoteServerInformation());
     }
 
     public FtpClientActivityListener getClientActivityListener() {
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
index f080e6b..cb51dbb 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsEndpoint.java
@@ -27,6 +27,7 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocket;
 import javax.net.ssl.TrustManagerFactory;
 
+import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.util.IOHelper;
@@ -42,6 +43,7 @@ import org.apache.commons.net.ftp.FTPSClient;
 @UriEndpoint(firstVersion = "2.2.0", scheme = "ftps", extendsScheme = "file", title = "FTPS",
         syntax = "ftps:host:port/directoryName", alternativeSyntax = "ftps:username:password@host:port/directoryName",
         consumerClass = FtpConsumer.class, label = "file")
+@ManagedResource(description = "Managed FtpsEndpoint")
 public class FtpsEndpoint extends FtpEndpoint<FTPFile> {
     @UriParam
     protected FtpsConfiguration configuration;

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].