embulk-output-ftp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/gradlew.bat ADDED
@@ -0,0 +1,90 @@
1
+ @if "%DEBUG%" == "" @echo off
2
+ @rem ##########################################################################
3
+ @rem
4
+ @rem Gradle startup script for Windows
5
+ @rem
6
+ @rem ##########################################################################
7
+
8
+ @rem Set local scope for the variables with windows NT shell
9
+ if "%OS%"=="Windows_NT" setlocal
10
+
11
+ @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12
+ set DEFAULT_JVM_OPTS=
13
+
14
+ set DIRNAME=%~dp0
15
+ if "%DIRNAME%" == "" set DIRNAME=.
16
+ set APP_BASE_NAME=%~n0
17
+ set APP_HOME=%DIRNAME%
18
+
19
+ @rem Find java.exe
20
+ if defined JAVA_HOME goto findJavaFromJavaHome
21
+
22
+ set JAVA_EXE=java.exe
23
+ %JAVA_EXE% -version >NUL 2>&1
24
+ if "%ERRORLEVEL%" == "0" goto init
25
+
26
+ echo.
27
+ echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28
+ echo.
29
+ echo Please set the JAVA_HOME variable in your environment to match the
30
+ echo location of your Java installation.
31
+
32
+ goto fail
33
+
34
+ :findJavaFromJavaHome
35
+ set JAVA_HOME=%JAVA_HOME:"=%
36
+ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37
+
38
+ if exist "%JAVA_EXE%" goto init
39
+
40
+ echo.
41
+ echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42
+ echo.
43
+ echo Please set the JAVA_HOME variable in your environment to match the
44
+ echo location of your Java installation.
45
+
46
+ goto fail
47
+
48
+ :init
49
+ @rem Get command-line arguments, handling Windowz variants
50
+
51
+ if not "%OS%" == "Windows_NT" goto win9xME_args
52
+ if "%@eval[2+2]" == "4" goto 4NT_args
53
+
54
+ :win9xME_args
55
+ @rem Slurp the command line arguments.
56
+ set CMD_LINE_ARGS=
57
+ set _SKIP=2
58
+
59
+ :win9xME_args_slurp
60
+ if "x%~1" == "x" goto execute
61
+
62
+ set CMD_LINE_ARGS=%*
63
+ goto execute
64
+
65
+ :4NT_args
66
+ @rem Get arguments from the 4NT Shell from JP Software
67
+ set CMD_LINE_ARGS=%$
68
+
69
+ :execute
70
+ @rem Setup the command line
71
+
72
+ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73
+
74
+ @rem Execute Gradle
75
+ "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76
+
77
+ :end
78
+ @rem End local scope for the variables with windows NT shell
79
+ if "%ERRORLEVEL%"=="0" goto mainEnd
80
+
81
+ :fail
82
+ rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83
+ rem the _cmd.exe /c_ return code!
84
+ if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85
+ exit /b 1
86
+
87
+ :mainEnd
88
+ if "%OS%"=="Windows_NT" endlocal
89
+
90
+ :omega
@@ -0,0 +1,3 @@
1
+ Embulk::JavaPlugin.register_output(
2
+ "ftp", "org.embulk.output.ftp.FtpFileOutputPlugin",
3
+ File.expand_path('../../../../classpath', __FILE__))
Binary file
@@ -0,0 +1,277 @@
1
+ package org.embulk.output.ftp;
2
+
3
+ import com.google.common.base.Function;
4
+
5
+ import java.io.EOFException;
6
+ import java.io.IOException;
7
+ import java.io.InterruptedIOException;
8
+ import java.nio.ByteBuffer;
9
+ import java.nio.channels.ReadableByteChannel;
10
+ import java.nio.channels.WritableByteChannel;
11
+ import java.util.concurrent.Callable;
12
+ import java.util.concurrent.CancellationException;
13
+ import java.util.concurrent.ExecutionException;
14
+ import java.util.concurrent.ExecutorService;
15
+ import java.util.concurrent.Future;
16
+
17
+ public class BlockingTransfer
18
+ {
19
+ private final WriterChannel writerChannel;
20
+ private final ReaderChannel readerChannel;
21
+ private Future<?> transferCompletionFuture;
22
+
23
+ public static BlockingTransfer submit(ExecutorService executor,
24
+ Function<BlockingTransfer, Runnable> starterFactory)
25
+ {
26
+ BlockingTransfer transfer = new BlockingTransfer();
27
+ final Runnable starter = starterFactory.apply(transfer);
28
+ transfer.setTransferCompletionFuture(
29
+ executor.submit(new Callable<Void>() {
30
+ public Void call() throws Exception
31
+ {
32
+ starter.run();
33
+ return null;
34
+ }
35
+ })
36
+ );
37
+ return transfer;
38
+ }
39
+
40
+ private BlockingTransfer()
41
+ {
42
+ this.writerChannel = new WriterChannel();
43
+ this.readerChannel = new ReaderChannel();
44
+ }
45
+
46
+ private void setTransferCompletionFuture(Future<?> future)
47
+ {
48
+ this.transferCompletionFuture = future;
49
+ }
50
+
51
+ public ReadableByteChannel getReaderChannel()
52
+ {
53
+ return readerChannel;
54
+ }
55
+
56
+ public WritableByteChannel getWriterChannel()
57
+ {
58
+ return writerChannel;
59
+ }
60
+
61
+ public void transferFailed(Throwable exception)
62
+ {
63
+ readerChannel.overwriteException(exception);
64
+ }
65
+
66
+ void waitForTransferCompletion() throws IOException
67
+ {
68
+ Future<?> f = transferCompletionFuture;
69
+ if (f != null) {
70
+ try {
71
+ f.get();
72
+ }
73
+ catch (CancellationException | InterruptedException ex) {
74
+ throw new InterruptedIOException();
75
+ }
76
+ catch (ExecutionException ex) {
77
+ // transfer failed
78
+ Throwable e = ex.getCause();
79
+ if (e instanceof IOException) {
80
+ throw (IOException) e;
81
+ }
82
+ else if (e instanceof RuntimeException) {
83
+ throw (RuntimeException) e;
84
+ }
85
+ else if (e instanceof Error) {
86
+ throw (Error) e;
87
+ }
88
+ else {
89
+ throw new IOException(e);
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+ public class WriterChannel implements WritableByteChannel
96
+ {
97
+ public int write(ByteBuffer src) throws IOException
98
+ {
99
+ int sz = src.remaining();
100
+ if (sz <= 0) {
101
+ return sz;
102
+ }
103
+
104
+ synchronized (readerChannel) {
105
+ if (!readerChannel.waitForWritable()) {
106
+ return -1;
107
+ }
108
+
109
+ readerChannel.setBuffer(src);
110
+
111
+ if (!readerChannel.waitForWritable()) { // wait for complete processing src
112
+ return -1;
113
+ }
114
+ }
115
+
116
+ return sz - src.remaining();
117
+ }
118
+
119
+ public boolean isOpen()
120
+ {
121
+ return readerChannel.isOpen();
122
+ }
123
+
124
+ public void close() throws IOException
125
+ {
126
+ readerChannel.closePeer();
127
+ waitForTransferCompletion();
128
+ }
129
+ }
130
+
131
+ private static int transferByteBuffer(ByteBuffer src, ByteBuffer dst)
132
+ {
133
+ int pos = dst.position();
134
+
135
+ int srcrem = src.remaining();
136
+ int dstrem = dst.remaining();
137
+ if (dstrem < srcrem) {
138
+ int lim = src.limit();
139
+ try {
140
+ src.limit(src.position() + dstrem);
141
+ dst.put(src);
142
+ }
143
+ finally {
144
+ src.limit(lim);
145
+ }
146
+ }
147
+ else {
148
+ dst.put(src);
149
+ }
150
+
151
+ return dst.position() - pos;
152
+ }
153
+
154
+ public class ReaderChannel implements ReadableByteChannel
155
+ {
156
+ private ByteBuffer buffer;
157
+ private Throwable exception;
158
+
159
+ public synchronized int read(ByteBuffer dst) throws IOException
160
+ {
161
+ if (!waitForReadable()) {
162
+ return -1;
163
+ }
164
+
165
+ int len = transferByteBuffer(buffer, dst);
166
+ if (!buffer.hasRemaining()) {
167
+ setBuffer(null);
168
+ notifyAll();
169
+ }
170
+
171
+ return len;
172
+ }
173
+
174
+ public synchronized boolean isOpen()
175
+ {
176
+ return exception == null;
177
+ }
178
+
179
+ public void close() throws IOException
180
+ {
181
+ setException(new EOFException("reader closed channel"));
182
+ }
183
+
184
+ private void setBuffer(ByteBuffer buffer)
185
+ {
186
+ this.buffer = buffer;
187
+ notifyAll();
188
+ }
189
+
190
+ private synchronized boolean waitForWritable() throws IOException
191
+ {
192
+ while (buffer != null) {
193
+ if (exception != null) {
194
+ if (exception instanceof EOFException) {
195
+ return false;
196
+ }
197
+ throwException();
198
+ }
199
+
200
+ try {
201
+ wait();
202
+ }
203
+ catch (InterruptedException ex) {
204
+ // TODO throws ClosedByInterruptException or InterruptedIOException?
205
+ }
206
+ }
207
+
208
+ return true;
209
+ }
210
+
211
+ private boolean waitForReadable() throws IOException
212
+ {
213
+ while (buffer == null) {
214
+ if (exception != null) {
215
+ if (exception instanceof EOFException) {
216
+ return false;
217
+ }
218
+ throwException();
219
+ }
220
+
221
+ try {
222
+ wait();
223
+ }
224
+ catch (InterruptedException ex) {
225
+ // TODO throws ClosedByInterruptException or InterruptedIOException?
226
+ }
227
+ }
228
+
229
+ return true;
230
+ }
231
+
232
+ public synchronized void closePeer() throws IOException
233
+ {
234
+ waitForWritable();
235
+ if (exception != null && !(exception instanceof EOFException)) {
236
+ throwException();
237
+ }
238
+ setException(new EOFException("writer closed channel"));
239
+ }
240
+
241
+ public synchronized void setException(Throwable exception)
242
+ {
243
+ if (this.exception == null) {
244
+ this.exception = exception;
245
+ }
246
+ notifyAll();
247
+ }
248
+
249
+ public synchronized void overwriteException(Throwable exception)
250
+ {
251
+ this.exception = exception;
252
+ notifyAll();
253
+ }
254
+
255
+ public boolean hasException()
256
+ {
257
+ return exception != null;
258
+ }
259
+
260
+ public void throwException() throws IOException
261
+ {
262
+ Throwable ex = exception;
263
+ if (ex instanceof IOException) {
264
+ throw (IOException) ex;
265
+ }
266
+ else if (ex instanceof RuntimeException) {
267
+ throw (RuntimeException) ex;
268
+ }
269
+ else if (ex instanceof Error) {
270
+ throw (Error) ex;
271
+ }
272
+ else {
273
+ throw new IOException(ex);
274
+ }
275
+ }
276
+ }
277
+ }