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 2015/08/05 13:30:40 UTC
[1/4] camel git commit: CAMEL-8887: Added unit test
Repository: camel
Updated Branches:
refs/heads/master e61f7f5a2 -> 01c0963d4
CAMEL-8887: Added unit test
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/82e9fd33
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/82e9fd33
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/82e9fd33
Branch: refs/heads/master
Commit: 82e9fd33ea78d2576a63e2641d89921dafd26bdd
Parents: e61f7f5
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Aug 5 12:49:38 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Aug 5 12:49:38 2015 +0200
----------------------------------------------------------------------
.../component/file/GenericFileConsumer.java | 25 +++---
.../file/remote/FromFtpSedaDeleteFileTest.java | 90 ++++++++++++++++++++
.../remote/sftp/SftpFromSedaDeleteFileTest.java | 80 +++++++++++++++++
3 files changed, 185 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index c188ebf..d1f58d6 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -432,17 +432,22 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
log.debug("About to process file: {} using exchange: {}", target, exchange);
- // process the exchange using the async consumer to support async routing engine
- // which can be supported by this file consumer as all the done work is
- // provided in the GenericFileOnCompletion
- getAsyncProcessor().process(exchange, new AsyncCallback() {
- public void done(boolean doneSync) {
- // noop
- if (log.isTraceEnabled()) {
- log.trace("Done processing file: {} {}", target, doneSync ? "synchronously" : "asynchronously");
+ if (endpoint.isSynchronous()) {
+ // process synchronously
+ getProcessor().process(exchange);
+ } else {
+ // process the exchange using the async consumer to support async routing engine
+ // which can be supported by this file consumer as all the done work is
+ // provided in the GenericFileOnCompletion
+ getAsyncProcessor().process(exchange, new AsyncCallback() {
+ public void done(boolean doneSync) {
+ // noop
+ if (log.isTraceEnabled()) {
+ log.trace("Done processing file: {} {}", target, doneSync ? "synchronously" : "asynchronously");
+ }
}
- }
- });
+ });
+ }
} catch (Exception e) {
// remove file from the in progress list due to failure
http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
new file mode 100644
index 0000000..dfb8806
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpSedaDeleteFileTest.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.remote;
+
+import java.io.File;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to test delete option.
+ */
+public class FromFtpSedaDeleteFileTest extends FtpServerTestSupport {
+
+ protected String getFtpUrl() {
+ return "ftp://admin@localhost:" + getPort() + "/deletefile?password=admin&binary=false&delete=true";
+ }
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ prepareFtpServer();
+ }
+
+ @Test
+ public void testPollFileAndShouldBeDeleted() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World this file will be deleted");
+
+ mock.assertIsSatisfied();
+
+ Thread.sleep(500);
+
+ // assert the file is deleted
+ File file = new File(FTP_ROOT_DIR + "/deletefile/hello.txt");
+ assertFalse("The file should have been deleted", file.exists());
+ }
+
+ private void prepareFtpServer() throws Exception {
+ // prepares the FTP Server by creating a file on the server that we want to unit
+ // test that we can pool and store as a local file
+ Endpoint endpoint = context.getEndpoint(getFtpUrl());
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World this file will be deleted");
+ exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+
+ // assert file is created
+ File file = new File(FTP_ROOT_DIR + "/deletefile/hello.txt");
+ assertTrue("The file should exists", file.exists());
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from(getFtpUrl()).to("seda:foo");
+
+ from("seda:foo")
+ .delay(750)
+ .log("${body}")
+ .delay(750)
+ .to("mock:result");
+ }
+ };
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/82e9fd33/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
new file mode 100644
index 0000000..5926a9e
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpFromSedaDeleteFileTest.java
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.remote.sftp;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test to test delete option.
+ */
+public class SftpFromSedaDeleteFileTest extends SftpServerTestSupport {
+
+ protected String getFtpUrl() {
+ return "sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR
+ + "?username=admin&knownHostsFile=./src/test/resources/known_hosts&privateKeyFile=./src/test/resources/id_rsa"
+ + "&privateKeyFilePassphrase=secret&delay=500&disconnect=false&delete=true";
+ }
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ createSampleFile();
+ }
+
+ @Test
+ public void testPollFileAndShouldBeDeleted() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World this file will be deleted");
+
+ mock.assertIsSatisfied();
+
+ Thread.sleep(500);
+
+ // assert the file is deleted
+ File file = new File(FTP_ROOT_DIR + "/hello.txt");
+ assertFalse("The file should have been deleted", file.exists());
+ }
+
+ private void createSampleFile() throws IOException {
+ File file = new File(FTP_ROOT_DIR + "/" + "foo.txt");
+
+ FileUtils.write(file, "Hello World this file will be deleted");
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from(getFtpUrl()).to("seda:foo");
+
+ from("seda:foo")
+ .delay(750)
+ .log("${body}")
+ .delay(750)
+ .to("mock:result");
+ }
+ };
+ }
+}
\ No newline at end of file
[3/4] camel git commit: CAMEL-9054: sftp - Reduce logging noise from
JSCH
Posted by da...@apache.org.
CAMEL-9054: sftp - Reduce logging noise from JSCH
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/dac35137
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/dac35137
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/dac35137
Branch: refs/heads/master
Commit: dac35137d0c8019ea00e789ef739c6738924bc08
Parents: 79e2f13
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Aug 5 13:19:07 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Aug 5 13:19:07 2015 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/LoggingLevel.java | 19 +++-----
.../java/org/apache/camel/LoggingLevelTest.java | 47 ++++++++++++++++++++
.../file/remote/SftpConfiguration.java | 5 +--
.../component/file/remote/SftpOperations.java | 10 ++---
4 files changed, 61 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/dac35137/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
index 434c9dd..b3c8e88 100644
--- a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
+++ b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
@@ -25,23 +25,18 @@ import javax.xml.bind.annotation.XmlEnum;
*/
@XmlEnum
public enum LoggingLevel {
- DEBUG, ERROR, INFO, TRACE, WARN, OFF;
+
+ TRACE, DEBUG, INFO, WARN, ERROR, OFF;
/**
* Is the given logging level equal or higher than the current level.
*/
- public boolean isGE(LoggingLevel level) {
- if (this == OFF) {
+ public boolean isEnabled(LoggingLevel level) {
+ // off is always false
+ if (this == OFF || level == OFF) {
return false;
- } else if (level == WARN) {
- return this == WARN || this == ERROR;
- } else if (level == INFO) {
- return this == INFO || this == WARN || this == ERROR;
- } else if (level == DEBUG) {
- return this == DEBUG || this == INFO || this == WARN || this == ERROR;
- } else if (level == TRACE) {
- return this != OFF;
}
- return false;
+
+ return this.compareTo(level) <= 0;
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/dac35137/camel-core/src/test/java/org/apache/camel/LoggingLevelTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/LoggingLevelTest.java b/camel-core/src/test/java/org/apache/camel/LoggingLevelTest.java
new file mode 100644
index 0000000..508d3bf
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/LoggingLevelTest.java
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel;
+
+import junit.framework.TestCase;
+import org.junit.Test;
+
+public class LoggingLevelTest extends TestCase {
+
+ @Test
+ public void testLoggingLevelInfo() throws Exception {
+ assertTrue(LoggingLevel.INFO.isEnabled(LoggingLevel.ERROR));
+ assertTrue(LoggingLevel.INFO.isEnabled(LoggingLevel.WARN));
+ assertTrue(LoggingLevel.INFO.isEnabled(LoggingLevel.INFO));
+
+ assertFalse(LoggingLevel.INFO.isEnabled(LoggingLevel.DEBUG));
+ assertFalse(LoggingLevel.INFO.isEnabled(LoggingLevel.TRACE));
+
+ assertFalse(LoggingLevel.INFO.isEnabled(LoggingLevel.OFF));
+ }
+
+ @Test
+ public void testLoggingLevelWARN() throws Exception {
+ assertTrue(LoggingLevel.WARN.isEnabled(LoggingLevel.ERROR));
+ assertTrue(LoggingLevel.WARN.isEnabled(LoggingLevel.WARN));
+
+ assertFalse(LoggingLevel.WARN.isEnabled(LoggingLevel.INFO));
+ assertFalse(LoggingLevel.WARN.isEnabled(LoggingLevel.DEBUG));
+ assertFalse(LoggingLevel.WARN.isEnabled(LoggingLevel.TRACE));
+
+ assertFalse(LoggingLevel.WARN.isEnabled(LoggingLevel.OFF));
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/dac35137/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
index 8d56d5d..9171b93 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
@@ -254,9 +254,8 @@ public class SftpConfiguration extends RemoteFileConfiguration {
}
/**
- * Sets the logging level of JSCH activity.
- * <p/>
- * Because JSCH is verbose by default at INFO level, the default value is <tt>WARN</tt>
+ * The logging level to use for JSCH activity logging.
+ * As JSCH is verbose at by default at INFO level the threshold is WARN by default.
*/
public void setJschLoggingLevel(LoggingLevel jschLoggingLevel) {
this.jschLoggingLevel = jschLoggingLevel;
http://git-wip-us.apache.org/repos/asf/camel/blob/dac35137/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index 752a678..f19f70b 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -343,15 +343,15 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
switch (level) {
case FATAL:
// use ERROR as FATAL
- return loggingLevel.isGE(LoggingLevel.ERROR) && LOG.isErrorEnabled();
+ return loggingLevel.isEnabled(LoggingLevel.ERROR) && LOG.isErrorEnabled();
case ERROR:
- return loggingLevel.isGE(LoggingLevel.ERROR) && LOG.isErrorEnabled();
+ return loggingLevel.isEnabled(LoggingLevel.ERROR) && LOG.isErrorEnabled();
case WARN:
- return loggingLevel.isGE(LoggingLevel.WARN) && LOG.isWarnEnabled();
+ return loggingLevel.isEnabled(LoggingLevel.WARN) && LOG.isWarnEnabled();
case INFO:
- return loggingLevel.isGE(LoggingLevel.INFO) && LOG.isInfoEnabled();
+ return loggingLevel.isEnabled(LoggingLevel.INFO) && LOG.isInfoEnabled();
default:
- return loggingLevel.isGE(LoggingLevel.DEBUG) && LOG.isDebugEnabled();
+ return loggingLevel.isEnabled(LoggingLevel.DEBUG) && LOG.isDebugEnabled();
}
}
[2/4] camel git commit: CAMEL-9054: sftp - Reduce logging noise from
JSCH
Posted by da...@apache.org.
CAMEL-9054: sftp - Reduce logging noise from JSCH
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/79e2f135
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/79e2f135
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/79e2f135
Branch: refs/heads/master
Commit: 79e2f1354314b01a780314f0ed56b5dd5f05b422
Parents: 82e9fd3
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Aug 5 13:00:56 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Aug 5 13:00:56 2015 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/LoggingLevel.java | 20 +++++++++++++-
.../file/remote/SftpConfiguration.java | 16 +++++++++++
.../component/file/remote/SftpOperations.java | 29 ++++++++++++--------
3 files changed, 53 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
index a8958bf..434c9dd 100644
--- a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
+++ b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
@@ -25,5 +25,23 @@ import javax.xml.bind.annotation.XmlEnum;
*/
@XmlEnum
public enum LoggingLevel {
- DEBUG, ERROR, INFO, TRACE, WARN, OFF
+ DEBUG, ERROR, INFO, TRACE, WARN, OFF;
+
+ /**
+ * Is the given logging level equal or higher than the current level.
+ */
+ public boolean isGE(LoggingLevel level) {
+ if (this == OFF) {
+ return false;
+ } else if (level == WARN) {
+ return this == WARN || this == ERROR;
+ } else if (level == INFO) {
+ return this == INFO || this == WARN || this == ERROR;
+ } else if (level == DEBUG) {
+ return this == DEBUG || this == INFO || this == WARN || this == ERROR;
+ } else if (level == TRACE) {
+ return this != OFF;
+ }
+ return false;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
index ac24e7c..8d56d5d 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.file.remote;
import java.net.URI;
import java.security.KeyPair;
+import org.apache.camel.LoggingLevel;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriParams;
@@ -59,6 +60,8 @@ public class SftpConfiguration extends RemoteFileConfiguration {
private int compression;
@UriParam
private String preferredAuthentications;
+ @UriParam(defaultValue = "WARN")
+ private LoggingLevel jschLoggingLevel = LoggingLevel.WARN;
public SftpConfiguration() {
setProtocol("sftp");
@@ -245,4 +248,17 @@ public class SftpConfiguration extends RemoteFileConfiguration {
public String getPreferredAuthentications() {
return preferredAuthentications;
}
+
+ public LoggingLevel getJschLoggingLevel() {
+ return jschLoggingLevel;
+ }
+
+ /**
+ * Sets the logging level of JSCH activity.
+ * <p/>
+ * Because JSCH is verbose by default at INFO level, the default value is <tt>WARN</tt>
+ */
+ public void setJschLoggingLevel(LoggingLevel jschLoggingLevel) {
+ this.jschLoggingLevel = jschLoggingLevel;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index dab4b8d..752a678 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -46,6 +46,7 @@ import com.jcraft.jsch.UserInfo;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.LoggingLevel;
import org.apache.camel.component.file.FileComponent;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileEndpoint;
@@ -163,7 +164,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
protected Session createSession(final RemoteFileConfiguration configuration) throws JSchException {
final JSch jsch = new JSch();
- JSch.setLogger(new JSchLogger());
+ JSch.setLogger(new JSchLogger(endpoint.getConfiguration().getJschLoggingLevel()));
SftpConfiguration sftpConfig = (SftpConfiguration) configuration;
@@ -332,19 +333,25 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
private static final class JSchLogger implements com.jcraft.jsch.Logger {
+ private final LoggingLevel loggingLevel;
+
+ private JSchLogger(LoggingLevel loggingLevel) {
+ this.loggingLevel = loggingLevel;
+ }
+
public boolean isEnabled(int level) {
switch (level) {
case FATAL:
// use ERROR as FATAL
- return LOG.isErrorEnabled();
+ return loggingLevel.isGE(LoggingLevel.ERROR) && LOG.isErrorEnabled();
case ERROR:
- return LOG.isErrorEnabled();
+ return loggingLevel.isGE(LoggingLevel.ERROR) && LOG.isErrorEnabled();
case WARN:
- return LOG.isWarnEnabled();
+ return loggingLevel.isGE(LoggingLevel.WARN) && LOG.isWarnEnabled();
case INFO:
- return LOG.isInfoEnabled();
+ return loggingLevel.isGE(LoggingLevel.INFO) && LOG.isInfoEnabled();
default:
- return LOG.isDebugEnabled();
+ return loggingLevel.isGE(LoggingLevel.DEBUG) && LOG.isDebugEnabled();
}
}
@@ -352,19 +359,19 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
switch (level) {
case FATAL:
// use ERROR as FATAL
- LOG.error("JSCH -> " + message);
+ LOG.error("JSCH -> {}", message);
break;
case ERROR:
- LOG.error("JSCH -> " + message);
+ LOG.error("JSCH -> {}", message);
break;
case WARN:
- LOG.warn("JSCH -> " + message);
+ LOG.warn("JSCH -> {}", message);
break;
case INFO:
- LOG.info("JSCH -> " + message);
+ LOG.info("JSCH -> {}", message);
break;
default:
- LOG.debug("JSCH -> " + message);
+ LOG.debug("JSCH -> {}", message);
break;
}
}
[4/4] camel git commit: CAMEL-8887: SFTP operations is not thread safe
Posted by da...@apache.org.
CAMEL-8887: SFTP operations is not thread safe
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/01c0963d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/01c0963d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/01c0963d
Branch: refs/heads/master
Commit: 01c0963d4e90621a6d8d3d1085ed630f5597bd2e
Parents: dac3513
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Aug 5 13:26:04 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Aug 5 13:26:04 2015 +0200
----------------------------------------------------------------------
.../component/file/remote/SftpOperations.java | 37 ++++++++++----------
1 file changed, 19 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/01c0963d/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index f19f70b..ab7e649 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -66,6 +66,8 @@ import static org.apache.camel.util.ObjectHelper.isNotEmpty;
/**
* SFTP remote file operations
+ * <p/>
+ * The JSCH session and channel are not thread-safe so we need to synchronize access to using this operation.
*/
public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry> {
private static final Logger LOG = LoggerFactory.getLogger(SftpOperations.class);
@@ -92,7 +94,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
this.endpoint = (SftpEndpoint) endpoint;
}
- public boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
+ public synchronized boolean connect(RemoteFileConfiguration configuration) throws GenericFileOperationFailedException {
if (isConnected()) {
// already connected
return true;
@@ -377,11 +379,11 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean isConnected() throws GenericFileOperationFailedException {
+ public synchronized boolean isConnected() throws GenericFileOperationFailedException {
return session != null && session.isConnected() && channel != null && channel.isConnected();
}
- public void disconnect() throws GenericFileOperationFailedException {
+ public synchronized void disconnect() throws GenericFileOperationFailedException {
if (session != null && session.isConnected()) {
session.disconnect();
}
@@ -390,7 +392,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean deleteFile(String name) throws GenericFileOperationFailedException {
+ public synchronized boolean deleteFile(String name) throws GenericFileOperationFailedException {
LOG.debug("Deleting file: {}", name);
try {
channel.rm(name);
@@ -400,7 +402,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean renameFile(String from, String to) throws GenericFileOperationFailedException {
+ public synchronized boolean renameFile(String from, String to) throws GenericFileOperationFailedException {
LOG.debug("Renaming file: {} to: {}", from, to);
try {
channel.rename(from, to);
@@ -410,7 +412,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException {
+ public synchronized boolean buildDirectory(String directory, boolean absolute) throws GenericFileOperationFailedException {
// must normalize directory first
directory = endpoint.getConfiguration().normalizePath(directory);
@@ -480,7 +482,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
return success;
}
- public String getCurrentDirectory() throws GenericFileOperationFailedException {
+ public synchronized String getCurrentDirectory() throws GenericFileOperationFailedException {
LOG.trace("getCurrentDirectory()");
try {
String answer = channel.pwd();
@@ -491,7 +493,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public void changeCurrentDirectory(String path) throws GenericFileOperationFailedException {
+ public synchronized void changeCurrentDirectory(String path) throws GenericFileOperationFailedException {
LOG.trace("changeCurrentDirectory({})", path);
if (ObjectHelper.isEmpty(path)) {
return;
@@ -555,7 +557,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public void changeToParentDirectory() throws GenericFileOperationFailedException {
+ public synchronized void changeToParentDirectory() throws GenericFileOperationFailedException {
LOG.trace("changeToParentDirectory()");
String current = getCurrentDirectory();
@@ -568,11 +570,11 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
changeCurrentDirectory(parent);
}
- public List<ChannelSftp.LsEntry> listFiles() throws GenericFileOperationFailedException {
+ public synchronized List<ChannelSftp.LsEntry> listFiles() throws GenericFileOperationFailedException {
return listFiles(".");
}
- public List<ChannelSftp.LsEntry> listFiles(String path) throws GenericFileOperationFailedException {
+ public synchronized List<ChannelSftp.LsEntry> listFiles(String path) throws GenericFileOperationFailedException {
LOG.trace("listFiles({})", path);
if (ObjectHelper.isEmpty(path)) {
// list current directory if file path is not given
@@ -596,7 +598,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean retrieveFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
+ public synchronized boolean retrieveFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
LOG.trace("retrieveFile({})", name);
if (ObjectHelper.isNotEmpty(endpoint.getLocalWorkDirectory())) {
// local work directory is configured so we should store file content as files in this local directory
@@ -607,8 +609,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- @Override
- public void releaseRetreivedFileResources(Exchange exchange) throws GenericFileOperationFailedException {
+ public synchronized void releaseRetreivedFileResources(Exchange exchange) throws GenericFileOperationFailedException {
InputStream is = exchange.getIn().getHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, InputStream.class);
if (is != null) {
@@ -769,7 +770,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
return true;
}
- public boolean storeFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
+ public synchronized boolean storeFile(String name, Exchange exchange) throws GenericFileOperationFailedException {
// must normalize name first
name = endpoint.getConfiguration().normalizePath(name);
@@ -933,7 +934,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
}
- public boolean existsFile(String name) throws GenericFileOperationFailedException {
+ public synchronized boolean existsFile(String name) throws GenericFileOperationFailedException {
LOG.trace("existsFile({})", name);
if (endpoint.isFastExistsCheck()) {
return fastExistsFile(name);
@@ -993,7 +994,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
}
- public boolean sendNoop() throws GenericFileOperationFailedException {
+ public synchronized boolean sendNoop() throws GenericFileOperationFailedException {
if (isConnected()) {
try {
session.sendIgnore();
@@ -1006,7 +1007,7 @@ public class SftpOperations implements RemoteFileOperations<ChannelSftp.LsEntry>
return false;
}
- public boolean sendSiteCommand(String command) throws GenericFileOperationFailedException {
+ public synchronized boolean sendSiteCommand(String command) throws GenericFileOperationFailedException {
// is not implemented
return true;
}