Skip to content

Commit

Permalink
Merge pull request #4 from civitaspo/test
Browse files Browse the repository at this point in the history
Add Test
  • Loading branch information
civitaspo committed Nov 18, 2015
2 parents b52e0ee + b4785c8 commit 4fd7324
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: java
jdk:
- openjdk7
- oraclejdk7
- oraclejdk8
script:
- ./gradlew test
after_success:
- ./gradlew jacocoTestReport coveralls
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Sftp file output plugin for Embulk
[![Build Status](https://travis-ci.org/civitaspo/embulk-output-sftp.svg)](https://travis-ci.org/civitaspo/embulk-output-sftp)
[![Coverage Status](https://coveralls.io/repos/civitaspo/embulk-output-sftp/badge.svg?branch=master&service=github)](https://coveralls.io/github/civitaspo/embulk-output-sftp?branch=master)

Stores files on a SFTP Server

Expand Down
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "com.jfrog.bintray" version "1.1"
id "com.github.jruby-gradle.base" version "0.1.5"
id "com.github.kt3k.coveralls" version "2.4.0"
id "jacoco"
id "java"
}
import com.github.jrubygradle.JRubyExec
Expand All @@ -17,12 +19,22 @@ sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
compile "org.embulk:embulk-core:0.7.4"
provided "org.embulk:embulk-core:0.7.4"
compile "org.embulk:embulk-core:0.7.+"
provided "org.embulk:embulk-core:0.7.+"
// compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
compile "org.apache.commons:commons-vfs2:2.+"
compile "com.jcraft:jsch:0.1.53"
testCompile "junit:junit:4.+"
testCompile "org.embulk:embulk-core:0.7.+:tests"
testCompile "org.embulk:embulk-standards:0.7.+"
testCompile "org.apache.sshd:apache-sshd:1.+"
}

jacocoTestReport {
reports {
xml.enabled = true // coveralls plugin depends on xml format report
html.enabled = true
}
}

task classpath(type: Copy, dependsOn: ["jar"]) {
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/org/embulk/output/sftp/SftpFileOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.IdentityInfo;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
Expand Down Expand Up @@ -35,6 +34,7 @@ public class SftpFileOutput
private final String userInfo;
private final String host;
private final int port;
private final int maxConnectionRetry;
private final String pathPrefix;
private final String sequenceFormat;
private final String fileNameExtension;
Expand Down Expand Up @@ -82,6 +82,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
if (task.getSecretKeyFilePath().isPresent()) {
IdentityInfo identityInfo = new IdentityInfo(new File((task.getSecretKeyFilePath().get())), task.getSecretKeyPassphrase().getBytes());
SftpFileSystemConfigBuilder.getInstance().setIdentityInfo(fsOptions, identityInfo);
logger.info("set identity: {}", task.getSecretKeyFilePath().get());
}
}
catch (FileSystemException e) {
Expand All @@ -99,6 +100,7 @@ private FileSystemOptions initializeFsOptions(PluginTask task)
this.fsOptions = initializeFsOptions(task);
this.host = task.getHost();
this.port = task.getPort();
this.maxConnectionRetry = task.getMaxConnectionRetry();
this.pathPrefix = task.getPathPrefix();
this.sequenceFormat = task.getSequenceFormat();
this.fileNameExtension = task.getFileNameExtension();
Expand Down Expand Up @@ -204,6 +206,26 @@ private String getOutputFilePath()
private FileObject newSftpFile(URI sftpUri)
throws FileSystemException
{
return manager.resolveFile(sftpUri.toString(), fsOptions);
int count = 0;
while (true) {
try {
return manager.resolveFile(sftpUri.toString(), fsOptions);
}
catch (FileSystemException e) {
if (++count == maxConnectionRetry) {
throw e;
}
logger.warn("failed to connect sftp server: " + e.getMessage(), e);

try {
Thread.sleep(count * 1000); // milliseconds
}
catch (InterruptedException e1) {
// Ignore this exception
logger.warn(e.getMessage(), e);
}
logger.warn("retry to connect sftp server: " + count + " times");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public interface PluginTask
@ConfigDefault("600") // 10 minutes
public int getSftpConnectionTimeout();

@Config("max_connection_retry")
@ConfigDefault("5") // 5 times retry to connect sftp server if failed.
public int getMaxConnectionRetry();

@Config("path_prefix")
public String getPathPrefix();

Expand Down
Loading

0 comments on commit 4fd7324

Please sign in to comment.