diff --git a/src/examples/java/org/vertx/java/examples/upload/UploadClient.java b/src/examples/java/org/vertx/java/examples/upload/UploadClient.java index 2db65a008bd..f3d30ccc94e 100644 --- a/src/examples/java/org/vertx/java/examples/upload/UploadClient.java +++ b/src/examples/java/org/vertx/java/examples/upload/UploadClient.java @@ -16,6 +16,8 @@ package org.vertx.java.examples.upload; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.CompletionHandler; import org.vertx.java.core.Future; import org.vertx.java.core.Handler; @@ -53,22 +55,22 @@ public void handle(HttpClientResponse response) { // For a chunked upload you don't need to specify size, just do: // req.setChunked(true); - FileSystem.instance.open(filename).handler(new CompletionHandler() { - public void handle(Future completion) { - final AsyncFile file = completion.result(); + FileSystem.instance.open(filename, new AsyncResultHandler() { + public void handle(AsyncResult ar) { + final AsyncFile file = ar.result; Pump pump = new Pump(file.getReadStream(), req); pump.start(); file.getReadStream().endHandler(new SimpleHandler() { public void handle() { - file.close().handler(new CompletionHandler() { - public void handle(Future completion) { - if (completion.succeeded()) { + file.close(new AsyncResultHandler() { + public void handle(AsyncResult ar) { + if (ar.exception == null) { req.end(); System.out.println("Sent request"); } else { - completion.exception().printStackTrace(System.err); + ar.exception.printStackTrace(System.err); } } }); diff --git a/src/examples/java/org/vertx/java/examples/upload/UploadServer.java b/src/examples/java/org/vertx/java/examples/upload/UploadServer.java index a9446fc9366..9853b26290e 100644 --- a/src/examples/java/org/vertx/java/examples/upload/UploadServer.java +++ b/src/examples/java/org/vertx/java/examples/upload/UploadServer.java @@ -16,6 +16,8 @@ package org.vertx.java.examples.upload; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.CompletionHandler; import org.vertx.java.core.Future; import org.vertx.java.core.Handler; @@ -43,21 +45,21 @@ public void handle(final HttpServerRequest req) { final String filename = "upload/file-" + UUID.randomUUID().toString() + ".upload"; - FileSystem.instance.open(filename).handler(new CompletionHandler() { - public void handle(Future deferred) { - final AsyncFile file = deferred.result(); + FileSystem.instance.open(filename, new AsyncResultHandler() { + public void handle(AsyncResult ar) { + final AsyncFile file = ar.result; final Pump pump = new Pump(req, file.getWriteStream()); final long start = System.currentTimeMillis(); req.endHandler(new SimpleHandler() { public void handle() { - file.close().handler(new CompletionHandler() { - public void handle(Future deferred) { - if (deferred.succeeded()) { + file.close(new AsyncResultHandler() { + public void handle(AsyncResult ar) { + if (ar.exception == null) { req.response.end(); long end = System.currentTimeMillis(); System.out.println("Uploaded " + pump.getBytesPumped() + " bytes to " + filename + " in " + (end - start) + " ms"); } else { - deferred.exception().printStackTrace(System.err); + ar.exception.printStackTrace(System.err); } } }); diff --git a/src/main/java/org/vertx/java/core/AsyncResult.java b/src/main/java/org/vertx/java/core/AsyncResult.java index da2d8311121..29d1f0381a2 100644 --- a/src/main/java/org/vertx/java/core/AsyncResult.java +++ b/src/main/java/org/vertx/java/core/AsyncResult.java @@ -21,16 +21,16 @@ */ public class AsyncResult { - final T result; - final Exception e; + public final T result; + public final Exception exception; public AsyncResult(T result) { this.result = result; - this.e = null; + this.exception = null; } - public AsyncResult(Exception e) { - this.e = e; + public AsyncResult(Exception exception) { + this.exception = exception; this.result = null; } } diff --git a/src/main/java/org/vertx/java/core/AsyncResultHandler.java b/src/main/java/org/vertx/java/core/AsyncResultHandler.java new file mode 100644 index 00000000000..362802f2426 --- /dev/null +++ b/src/main/java/org/vertx/java/core/AsyncResultHandler.java @@ -0,0 +1,23 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed 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.vertx.java.core; + +/** + * @author Tim Fox + */ +public interface AsyncResultHandler extends Handler> { +} diff --git a/src/main/java/org/vertx/java/core/SimpleAsyncResultHandler.java b/src/main/java/org/vertx/java/core/SimpleAsyncResultHandler.java new file mode 100644 index 00000000000..27121b796b4 --- /dev/null +++ b/src/main/java/org/vertx/java/core/SimpleAsyncResultHandler.java @@ -0,0 +1,30 @@ +/* + * Copyright 2011-2012 the original author or authors. + * + * Licensed 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.vertx.java.core; + +/** + * @author Tim Fox + */ +public abstract class SimpleAsyncResultHandler implements AsyncResultHandler { + + @Override + public void handle(AsyncResult event) { + //To change body of implemented methods use File | Settings | File Templates. + } + + +} diff --git a/src/main/java/org/vertx/java/core/file/AsyncFile.java b/src/main/java/org/vertx/java/core/file/AsyncFile.java index 211d26c9711..d8ae2c53159 100644 --- a/src/main/java/org/vertx/java/core/file/AsyncFile.java +++ b/src/main/java/org/vertx/java/core/file/AsyncFile.java @@ -16,6 +16,8 @@ package org.vertx.java.core.file; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.BlockingAction; import org.vertx.java.core.CompletionHandler; import org.vertx.java.core.Context; @@ -89,7 +91,7 @@ public class AsyncFile { * is called on the Deferred instance returned by this method. * @return a Deferred representing the as-yet unexecuted action. */ - public Deferred closeDeferred() { + private Deferred closeDeferred() { check(); closed = true; @@ -122,13 +124,30 @@ public void run() { return deferred; } + private void wrapHandler(Future fut, final AsyncResultHandler handler) { + fut.handler(new CompletionHandler() { + public void handle(Future event) { + if (event.succeeded()) { + handler.handle(new AsyncResult(event.result())); + } else { + handler.handle(new AsyncResult(event.exception())); + } + } + }); + } + + /** * Close the file asynchronously.

* This method must be called using the same event loop the file was opened from. * @return a Future representing the future result of closing the file. */ - public Future close() { - return closeDeferred().execute(); + public void close() { + closeDeferred().execute(); + } + + public void close(AsyncResultHandler handler) { + wrapHandler(closeDeferred().execute(), handler); } /** @@ -136,7 +155,7 @@ public Future close() { * is called on the Deferred instance returned by this method. * @return a Deferred representing the as-yet unexecuted action. */ - public Deferred writeDeferred(Buffer buffer, int position) { + private Deferred writeDeferred(Buffer buffer, int position) { check(); ByteBuffer bb = buffer.getChannelBuffer().toByteBuffer(); return doWrite(bb, position); @@ -151,8 +170,8 @@ public Deferred writeDeferred(Buffer buffer, int position) { * This method must be called using the same event loop the file was opened from. * @return a Future representing the future result of the write. */ - public Future write(Buffer buffer, int position) { - return writeDeferred(buffer, position).execute(); + public void write(Buffer buffer, int position, AsyncResultHandler handler) { + wrapHandler(writeDeferred(buffer, position).execute(), handler); } /** @@ -160,7 +179,7 @@ public Future write(Buffer buffer, int position) { * is called on the Deferred instance returned by this method. * @return a Deferred representing the as-yet unexecuted action. */ - public Deferred readDeferred(Buffer buffer, int offset, int position, int length) { + private Deferred readDeferred(Buffer buffer, int offset, int position, int length) { check(); ByteBuffer bb = ByteBuffer.allocate(length); return doRead(buffer, offset, bb, position); @@ -176,8 +195,8 @@ public Deferred readDeferred(Buffer buffer, int offset, int position, in * This method must be called using the same event loop the file was opened from. * @return a Future representing the future result of the write. */ - public Future read(Buffer buffer, int offset, int position, int length) { - return readDeferred(buffer, offset, position, length).execute(); + public void read(Buffer buffer, int offset, int position, int length, AsyncResultHandler handler) { + wrapHandler(readDeferred(buffer, offset, position, length).execute(), handler); } /** @@ -279,14 +298,12 @@ void doRead() { if (!readInProgress) { readInProgress = true; Buffer buff = Buffer.create(BUFFER_SIZE); - Future deferred = read(buff, 0, pos, BUFFER_SIZE); - - deferred.handler(new CompletionHandler() { + read(buff, 0, pos, BUFFER_SIZE, new AsyncResultHandler() { - public void handle(Future deferred) { - if (deferred.succeeded()) { + public void handle(AsyncResult ar) { + if (ar.exception == null) { readInProgress = false; - Buffer buffer = deferred.result(); + Buffer buffer = ar.result; if (buffer.length() == 0) { // Empty buffer represents end of file handleEnd(); @@ -298,7 +315,7 @@ public void handle(Future deferred) { } } } else { - handleException(deferred.exception()); + handleException(ar.exception); } } }); diff --git a/src/main/java/org/vertx/java/core/file/FileSystem.java b/src/main/java/org/vertx/java/core/file/FileSystem.java index ab3bbdb65e7..5d84bd54717 100644 --- a/src/main/java/org/vertx/java/core/file/FileSystem.java +++ b/src/main/java/org/vertx/java/core/file/FileSystem.java @@ -17,12 +17,14 @@ package org.vertx.java.core.file; import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.BlockingAction; import org.vertx.java.core.CompletionHandler; import org.vertx.java.core.Context; import org.vertx.java.core.Deferred; import org.vertx.java.core.Future; import org.vertx.java.core.Handler; +import org.vertx.java.core.SimpleAsyncResultHandler; import org.vertx.java.core.SynchronousAction; import org.vertx.java.core.VertxInternal; import org.vertx.java.core.buffer.Buffer; @@ -65,6 +67,18 @@ public class FileSystem { private FileSystem() { } + private void wrapHandler(Future fut, final AsyncResultHandler handler) { + fut.handler(new CompletionHandler() { + public void handle(Future event) { + if (event.succeeded()) { + handler.handle(new AsyncResult(event.result())); + } else { + handler.handle(new AsyncResult(event.exception())); + } + } + }); + } + private SynchronousAction copyDeferred(String from, String to) { return copyDeferred(from, to, false); } @@ -75,8 +89,8 @@ private SynchronousAction copyDeferred(String from, String to) { * The actual copy will happen asynchronously. * @return a Future representing the future result of the action. */ - public Future copy(String from, String to) { - return copyDeferred(from, to).execute(); + public void copy(String from, String to, AsyncResultHandler handler) { + wrapHandler(copyDeferred(from, to).execute(), handler); } public void copySync(String from, String to) throws Exception { @@ -131,25 +145,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) * The copy will fail if the destination if the destination already exists.

* @return a Future representing the future result of the action. */ - public Future copy(String from, String to, boolean recursive) { - return copyDeferred(from, to, recursive).execute(); - } - - public void copy2(String from, String to, boolean recursive, final Handler> handler) { - Future fut = copyDeferred(from, to, recursive).execute(); - wrapHandler(fut, handler); - } - - private void wrapHandler(Future fut, final Handler> handler) { - fut.handler(new CompletionHandler() { - public void handle(Future event) { - if (event.succeeded()) { - handler.handle(new AsyncResult(event.result())); - } else { - handler.handle(new AsyncResult(event.exception())); - } - } - }); + public void copy(String from, String to, boolean recursive, AsyncResultHandler handler) { + wrapHandler(copyDeferred(from, to, recursive).execute(), handler); } public void copySync(String from, String to, boolean recursive) throws Exception { @@ -181,8 +178,8 @@ public Void action() throws Exception { * The actual move will happen asynchronously. * @return a Future representing the future result of the action. */ - public Future move(String from, String to) { - return moveDeferred(from, to).execute(); + public void move(String from, String to, AsyncResultHandler handler) { + wrapHandler(moveDeferred(from, to).execute(), handler); } public void moveSync(String from, String to) throws Exception { @@ -219,8 +216,8 @@ public Void action() throws Exception { * The operation will fail if the file does not exist or {@code len} is less than {@code zero}. * @return a Future representing the future result of the action. */ - public Future truncate(String path, long len) { - return truncateDeferred(path, len).execute(); + public void truncate(String path, long len, AsyncResultHandler handler) { + wrapHandler(truncateDeferred(path, len).execute(), handler); } public void truncateSync(String path, long len) throws Exception { @@ -236,8 +233,8 @@ private SynchronousAction chmodDeferred(String path, String perms) { * The permission String takes the form rwxr-x--- as specified here.

* @return a Future representing the future result of the action. */ - public Future chmod(String path, String perms) { - return chmodDeferred(path, perms).execute(); + public void chmod(String path, String perms, AsyncResultHandler handler) { + wrapHandler(chmodDeferred(path, perms).execute(), handler); } public void chmodSync(String path, String perms) throws Exception { @@ -284,8 +281,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO * be set to {@code dirPerms}, whilst any normal file permissions will be set to {@code perms}.

* @return a Future representing the future result of the action. */ - public Future chmod(String path, String perms, String dirPerms) { - return chmodDeferred(path, perms, dirPerms).execute(); + public void chmod(String path, String perms, String dirPerms, AsyncResultHandler handler) { + wrapHandler(chmodDeferred(path, perms, dirPerms).execute(), handler); } @@ -303,8 +300,8 @@ private SynchronousAction propsDeferred(String path) { * If the file is a link, the link will be followed. * @return a Future representing the future result of the action. */ - public Future props(String path) { - return propsDeferred(path).execute(); + public void props(String path, AsyncResultHandler handler) { + wrapHandler(propsDeferred(path).execute(), handler); } public FileProps propsSync(String path) throws Exception { @@ -319,8 +316,8 @@ private SynchronousAction lpropsDeferred(String path) { * Obtain properties for the link represented by {@code path}, asynchronously. The link will not be followed. * @return a Future representing the future result of the action. */ - public Future lprops(String path) { - return lpropsDeferred(path).execute(); + public void lprops(String path, AsyncResultHandler handler) { + wrapHandler(lpropsDeferred(path).execute(), handler); } public FileProps lpropsSync(String path) throws Exception { @@ -355,8 +352,8 @@ private SynchronousAction linkDeferred(String link, String existing) { * Create a hard link on the file system from {@code link} to {@code existing}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future link(String link, String existing) { - return linkDeferred(link, existing).execute(); + public void link(String link, String existing, AsyncResultHandler handler) { + wrapHandler(linkDeferred(link, existing).execute(), handler); } public void linkSync(String link, String existing) throws Exception { @@ -371,8 +368,8 @@ private SynchronousAction symlinkDeferred(String link, String existing) { * Create a symbolic link on the file system from {@code link} to {@code existing}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future symlink(String link, String existing) { - return symlinkDeferred(link, existing).execute(); + public void symlink(String link, String existing, AsyncResultHandler handler) { + wrapHandler(symlinkDeferred(link, existing).execute(), handler); } public void symlinkSync(String link, String existing) throws Exception { @@ -407,8 +404,8 @@ private SynchronousAction unlinkDeferred(String link) { * Unlinks the link on the file system represented by the path {@code link}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future unlink(String link) { - return unlinkDeferred(link).execute(); + public void unlink(String link, AsyncResultHandler handler) { + wrapHandler(unlinkDeferred(link).execute(), handler); } public void unlinkSync(String link) throws Exception { @@ -433,8 +430,8 @@ public String action() throws Exception { * Returns the path representing the file that the symbolic link specified by {@code link} points to, asynchronously.

* @return a Future representing the future result of the action. */ - public Future readSymlink(String link) { - return readSymlinkDeferred(link).execute(); + public void readSymlink(String link, AsyncResultHandler handler) { + wrapHandler(readSymlinkDeferred(link).execute(), handler); } public String readSymlinkSync(String link) throws Exception { @@ -449,8 +446,8 @@ private SynchronousAction deleteDeferred(String path) { * Deletes the file represented by the specified {@code path}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future delete(String path) { - return deleteDeferred(path).execute(); + public void delete(String path, AsyncResultHandler handler) { + wrapHandler(deleteDeferred(path).execute(), handler); } public void deleteSync(String path) throws Exception { @@ -496,8 +493,8 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce * If the path represents a directory, then the directory and its contents will be deleted recursively.

* @return a Future representing the future result of the action. */ - public Future delete(String path, boolean recursive) { - return deleteDeferred(path, recursive).execute(); + public void delete(String path, boolean recursive, AsyncResultHandler handler) { + wrapHandler(deleteDeferred(path, recursive).execute(), handler); } public void deleteSync(String path, boolean recursive) throws Exception { @@ -513,8 +510,8 @@ private SynchronousAction mkdirDeferred(String path) { * The operation will fail if the directory already exists.

* @return a Future representing the future result of the action. */ - public Future mkdir(String path) { - return mkdirDeferred(path).execute(); + public void mkdir(String path, AsyncResultHandler handler) { + wrapHandler(mkdirDeferred(path).execute(), handler); } public void mkdirSync(String path) throws Exception { @@ -532,8 +529,8 @@ private SynchronousAction mkdirDeferred(String path, boolean createParents * The operation will fail if the directory already exists.

* @return a Future representing the future result of the action. */ - public Future mkdir(String path, boolean createParents) { - return mkdirDeferred(path, createParents).execute(); + public void mkdir(String path, boolean createParents, AsyncResultHandler handler) { + wrapHandler(mkdirDeferred(path, createParents).execute(), handler); } public void mkdirSync(String path, boolean createParents) throws Exception { @@ -551,8 +548,8 @@ private SynchronousAction mkdirDeferred(String path, String perms) { * The operation will fail if the directory already exists.

* @return a Future representing the future result of the action. */ - public Future mkdir(String path, String perms) { - return mkdirDeferred(path, perms).execute(); + public void mkdir(String path, String perms, AsyncResultHandler handler) { + wrapHandler(mkdirDeferred(path, perms).execute(), handler); } public void mkdirSync(String path, String perms) throws Exception { @@ -598,8 +595,8 @@ public Void action() throws Exception { * The operation will fail if the directory already exists.

* @return a Future representing the future result of the action. */ - public Future mkdir(String path, String perms, boolean createParents) { - return mkdirDeferred(path, perms, createParents).execute(); + public void mkdir(String path, String perms, boolean createParents, AsyncResultHandler handler) { + wrapHandler(mkdirDeferred(path, perms, createParents).execute(), handler); } public void mkdirSync(String path, String perms, boolean createParents) throws Exception { @@ -615,8 +612,8 @@ private SynchronousAction readDirDeferred(String path) { * @return a Future representing the future result of the action. * The result is an array of String representing the paths of the files inside the directory. */ - public Future readDir(String path) { - return readDirDeferred(path).execute(); + public void readDir(String path, AsyncResultHandler handler) { + wrapHandler(readDirDeferred(path).execute(), handler); } public String[] readDirSync(String path) throws Exception { @@ -667,8 +664,8 @@ public boolean accept(File dir, String name) { * @return a Future representing the future result of the action. * The result is an array of String representing the paths of the files inside the directory. */ - public Future readDir(String path, String filter) { - return readDirDeferred(path, filter).execute(); + public void readDir(String path, String filter, AsyncResultHandler handler) { + wrapHandler(readDirDeferred(path, filter).execute(), handler); } public String[] readDirSync(String path, String filter) throws Exception { @@ -692,8 +689,8 @@ public Buffer action() throws Exception { * Do not user this method to read very large files or you risk running out of available RAM.

* @return a Future representing the future result of the action. */ - public Future readFile(String path) { - return readFileDeferred(path).execute(); + public void readFile(String path, AsyncResultHandler handler) { + wrapHandler(readFileDeferred(path).execute(), handler); } public Buffer readFileSync(String path) throws Exception { @@ -715,8 +712,8 @@ public Void action() throws Exception { * Creates the file, and writes the specified {@code Buffer data} to the file represented by the path {@code path}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future writeFile(String path, Buffer data) { - return writeFileDeferred(path, data).execute(); + public void writeFile(String path, Buffer data, AsyncResultHandler handler) { + wrapHandler(writeFileDeferred(path, data).execute(), handler); } public void writeFileSync(String path, Buffer data) throws Exception { @@ -749,8 +746,8 @@ private SynchronousAction openDeferred(String path) { * Write operations will not automatically flush to storage. * @return a Future representing the future result of the action. */ - public Future open(String path) { - return openDeferred(path).execute(); + public void open(String path, AsyncResultHandler handler) { + wrapHandler(openDeferred(path).execute(), handler); } public AsyncFile openSync(String path) throws Exception { @@ -768,8 +765,8 @@ private SynchronousAction openDeferred(String path, String perms) { * Write operations will not automatically flush to storage. * @return a Future representing the future result of the action. */ - public Future open(String path, String perms) { - return openDeferred(path, perms).execute(); + public void open(String path, String perms, AsyncResultHandler handler) { + wrapHandler(openDeferred(path, perms).execute(), handler); } public AsyncFile openSync(String path, String perms) throws Exception { @@ -788,8 +785,8 @@ private SynchronousAction openDeferred(String path, String perms, boo * Write operations will not automatically flush to storage. * @return a Future representing the future result of the action. */ - public Future open(String path, String perms, boolean createNew) { - return openDeferred(path, perms, createNew).execute(); + public void open(String path, String perms, boolean createNew, AsyncResultHandler handler) { + wrapHandler(openDeferred(path, perms, createNew).execute(), handler); } public AsyncFile openSync(String path, String perms, boolean createNew) throws Exception { @@ -810,8 +807,8 @@ private SynchronousAction openDeferred(String path, String perms, boo * Write operations will not automatically flush to storage. * @return a Future representing the future result of the action. */ - public Future open(String path, String perms, boolean read, boolean write, boolean createNew) { - return openDeferred(path, perms, read, write, createNew).execute(); + public void open(String path, String perms, boolean read, boolean write, boolean createNew, AsyncResultHandler handler) { + wrapHandler(openDeferred(path, perms, read, write, createNew).execute(), handler); } public AsyncFile openSync(String path, String perms, boolean read, boolean write, boolean createNew) throws Exception { @@ -843,9 +840,9 @@ public AsyncFile action() throws Exception { * storage on each write.

* @return a Future representing the future result of the action. */ - public Future open(String path, String perms, boolean read, boolean write, boolean createNew, - boolean flush) { - return openDeferred(path, perms, read, write, createNew, flush).execute(); + public void open(String path, String perms, boolean read, boolean write, boolean createNew, + boolean flush, AsyncResultHandler handler) { + wrapHandler(openDeferred(path, perms, read, write, createNew, flush).execute(), handler); } public AsyncFile openSync(String path, String perms, boolean read, boolean write, boolean createNew, boolean flush) throws Exception { @@ -866,8 +863,8 @@ private SynchronousAction createFileDeferred(String path) { * Creates an empty file with the specified {@code path}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future createFile(String path) { - return createFileDeferred(path).execute(); + public void createFile(String path, AsyncResultHandler handler) { + wrapHandler(createFileDeferred(path).execute(), handler); } public void createFileSync(String path) throws Exception { @@ -898,8 +895,8 @@ public Void action() throws Exception { * Creates an empty file with the specified {@code path} and permissions {@code perms}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future createFile(String path, String perms) { - return createFileDeferred(path, perms).execute(); + public void createFile(String path, String perms, AsyncResultHandler handler) { + wrapHandler(createFileDeferred(path, perms).execute(), handler); } public void createFileSync(String path, String perms) throws Exception { @@ -920,8 +917,8 @@ public Boolean action() throws Exception { * Determines whether the file as specified by the path {@code path} exists, asynchronously.

* @return a Future representing the future result of the action. */ - public Future exists(String path) { - return existsDeferred(path).execute(); + public void exists(String path, AsyncResultHandler handler) { + wrapHandler(existsDeferred(path).execute(), handler); } public boolean existsSync(String path) throws Exception { @@ -943,8 +940,8 @@ public FileSystemProps action() throws Exception { * Returns properties of the file-system being used by the specified {@code path}, asynchronously.

* @return a Future representing the future result of the action. */ - public Future fsProps(String path) { - return fsPropsDeferred(path).execute(); + public void fsProps(String path, AsyncResultHandler handler) { + wrapHandler(fsPropsDeferred(path).execute(), handler); } public FileSystemProps fsPropsSync(String path) throws Exception { diff --git a/src/main/javascript/core/filesystem.js b/src/main/javascript/core/filesystem.js index a947cda2234..74c5d94ced7 100644 --- a/src/main/javascript/core/filesystem.js +++ b/src/main/javascript/core/filesystem.js @@ -22,26 +22,25 @@ if (!vertx.FileSystem) { (function() { var j_fs = org.vertx.java.core.file.FileSystem.instance; - function wrapHandler(handler, fut) { - fut.handler(function() { - if (fut.succeeded()) { - handler(null, fut.result()); + function wrapHandler(handler) { + return function(asyncResult) { + if (!asyncResult.exception) { + handler(null, asyncResult.result); } else { - handler(fut.exception(), null); + handler(asyncResult.exception, null); } - }); + } } - function wrapProps(handler, fut) { - fut.handler(function() { - if (fut.succeeded()) { - var j_res = fut.result(); - var jsProps = convertProps(j_res); + function wrapPropsHandler(handler) { + return function(asyncResult) { + if (!asyncResult.exception) { + var jsProps = convertProps(asyncResult.result); handler(null, jsProps); } else { - handler(fut.exception(), null); + handler(asyncResult.exception, null); } - }); + } } function convertProps(j_props) { @@ -67,8 +66,7 @@ if (!vertx.FileSystem) { handler = arg2; recursive = false; } - var fut = j_fs.copy(from, to, recursive); - wrapHandler(handler, fut); + j_fs.copy(from, to, recursive, wrapHandler(handler)); } vertx.FileSystem.copySync = function(from, to, recursive) { @@ -77,8 +75,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.move = function(from, to, handler) { - var fut = j_fs.move(from, to); - wrapHandler(handler, fut); + j_fs.move(from, to, wrapHandler(handler)); } vertx.FileSystem.moveSync = function(from, to) { @@ -86,8 +83,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.truncate = function(path, len, handler) { - var fut = j_fs.truncate(path, len); - wrapHandler(handler, fut); + j_fs.truncate(path, len, wrapHandler(handler)); } vertx.FileSystem.truncateSync = function(path, len) { @@ -104,8 +100,7 @@ if (!vertx.FileSystem) { handler = arg2; dirPerms = null; } - var fut = j_fs.chmod(path, perms, dirPerms); - wrapHandler(handler, fut); + j_fs.chmod(path, perms, dirPerms, wrapHandler(handler)); } vertx.FileSystem.chmodSync = function(path, perms, dirPerms) { @@ -114,8 +109,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.props = function(path, handler) { - var fut = j_fs.props(path); - wrapProps(handler, fut); + j_fs.props(path, wrapPropsHandler(handler)); } vertx.FileSystem.propsSync = function(path) { @@ -124,8 +118,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.lprops = function(path, handler) { - var fut = j_fs.lprops(path); - wrapProps(handler, fut); + j_fs.lprops(path, wrapPropsHandler(handler)); } vertx.FileSystem.lpropsSync = function(path) { @@ -134,8 +127,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.link = function(link, existing, handler) { - var fut = j_fs.link(link, existing); - wrapHandler(handler, fut); + j_fs.link(link, existing, wrapHandler(handler)); } vertx.FileSystem.linkSync = function(link, existing) { @@ -143,8 +135,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.symlink = function(link, existing, handler) { - var fut = j_fs.symlink(link, existing); - wrapHandler(handler, fut); + j_fs.symlink(link, existing, wrapHandler(handler)); } vertx.FileSystem.symlinkSync = function(link, existing) { @@ -152,8 +143,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.unlink = function(link, handler) { - var fut = j_fs.unlink(link); - wrapHandler(handler, fut); + j_fs.unlink(link, wrapHandler(handler)); } vertx.FileSystem.unlinkSync = function(link) { @@ -161,8 +151,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.readSymlink = function(link, handler) { - var fut = j_fs.readSymlink(link); - wrapHandler(handler, fut); + j_fs.readSymlink(link, wrapHandler(handler)); } vertx.FileSystem.readSymlinkSync = function(link, handler) { @@ -179,8 +168,7 @@ if (!vertx.FileSystem) { handler = arg1; recursive = false; } - var fut = j_fs.delete(path, recursive); - wrapHandler(handler, fut); + j_fs.delete(path, recursive, wrapHandler(handler)); } vertx.FileSystem.deleteSync = function(path, recursive) { @@ -211,8 +199,7 @@ if (!vertx.FileSystem) { default: throw 'Invalid number of arguments'; } - var fut = j_fs.mkdir(path, perms, createParents); - wrapHandler(handler, fut); + j_fs.mkdir(path, perms, createParents, wrapHandler(handler)); } vertx.FileSystem.mkDirSync = function(path, arg1, arg2) { @@ -247,8 +234,7 @@ if (!vertx.FileSystem) { handler = arg1; filter = null; } - var fut = j_fs.readDir(path, filter); - wrapHandler(handler, fut); + j_fs.readDir(path, filter, wrapHandler(handler)); } vertx.FileSystem.readDirSync = function(path, filter) { @@ -257,8 +243,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.readFile = function(path, handler) { - var fut = j_fs.readFile(path); - wrapHandler(handler, fut); + j_fs.readFile(path, wrapHandler(handler)); } vertx.FileSystem.readFileSync = function(path) { @@ -269,8 +254,7 @@ if (!vertx.FileSystem) { if (typeof data === 'string') { data = org.vertx.java.core.buffer.Buffer.create(data); } - var fut = j_fs.writeFile(path, data); - wrapHandler(handler, fut); + j_fs.writeFile(path, data, wrapHandler(handler)); } vertx.FileSystem.writeFileSync = function(path, data) { @@ -366,17 +350,13 @@ if (!vertx.FileSystem) { var write = (openFlags & vertx.FileSystem.OPEN_WRITE) == vertx.FileSystem.OPEN_WRITE; var createNew = (openFlags & vertx.FileSystem.CREATE_NEW) == vertx.FileSystem.CREATE_NEW; - var fut = j_fs.open(path, perms, read, write, createNew, flush); - - fut.handler(function() { - if (fut.succeeded()) { - var j_af = fut.result(); - + j_fs.open(path, perms, read, write, createNew, flush, function(asyncResult) { + if (!asyncResult.exception) { + var j_af = asyncResult.result; var wrapped = wrapAsyncFile(j_af); - handler(null, wrapped); } else { - handler(fut.exception(), null); + handler(asyncResult.exception, null); } }); } @@ -385,20 +365,18 @@ if (!vertx.FileSystem) { return { close: function(handler) { if (handler) { - j_af.closeDeferred().handler(handler).execute(); + j_af.close(wrapHandler(handler)) } else { j_af.close(); } }, write: function(buffer, position, handler) { - var fut = j_af.write(buffer, position); - wrapHandler(handler, fut); + j_af.write(buffer, position, wrapHandler(handler)); }, read: function(buffer, offset, position, length, handler) { - var fut = j_af.read(buffer, offset, position, length); - wrapHandler(handler, fut); + j_af.read(buffer, offset, position, length, wrapHandler(handler)); }, getWriteStream: function() { @@ -411,7 +389,7 @@ if (!vertx.FileSystem) { flush: function(handler) { if (handler) { - j_af.flushDeferred().handler(handler).execute(); + j_af.flush(wrapHandler(handler)); } else { j_af.flush(); } @@ -420,8 +398,7 @@ if (!vertx.FileSystem) { } vertx.FileSystem.createFile = function(path, handler) { - var fut = j_fs.createFile(path); - wrapHandler(handler, fut); + j_fs.createFile(path, wrapHandler(handler)); } vertx.FileSystem.createFileSync = function(path) { @@ -429,14 +406,17 @@ if (!vertx.FileSystem) { } vertx.FileSystem.exists = function(path, handler) { - var fut = j_fs.exists(path); - wrapHandler(handler, fut); + j_fs.exists(path, wrapHandler(handler)); } vertx.FileSystem.existsSync = function(path) { return j_fs.existsSync(path); } + vertx.FileSystem.fsProps = function(path, handler) { + j_fs.fsProps(path, wrapHandler(handler)); + } + vertx.FileSystem.fsPropsSync = function(path) { return j_fs.fsPropsSync(path); } diff --git a/src/main/ruby/core/file_system.rb b/src/main/ruby/core/file_system.rb index e4b8657d4b1..1a79e20d5ac 100644 --- a/src/main/ruby/core/file_system.rb +++ b/src/main/ruby/core/file_system.rb @@ -100,25 +100,6 @@ def usable_space # @author {http://tfox.org Tim Fox} class AsyncFile - # TODO combine this with one on FileSystem - # @private - def AsyncFile._wrap_handler(j_fut, handler, &result_converter) - if handler - j_fut.handler do - if j_fut.succeeded - if result_converter - handler.call(nil, result_converter.call(j_fut.result)) - else - handler.call(nil, j_fut.result) - end - else - handler.call(j_fut.exception, nil) - end - end - end - end - - # @private def initialize(j_file) @j_file = j_file @@ -128,7 +109,7 @@ def initialize(j_file) # This method must be called using the same event loop the file was opened from. # @return [Future] a Future representing the future result of closing the file. def close(&block) - AsyncFile._wrap_handler(@j_file.close, block) + @j_file.close(FSWrappedHandler.new(block)) end # Write a {Buffer} to the file, asynchronously. @@ -140,7 +121,7 @@ def close(&block) # starts with zero at the beginning of the file. # @return [Future] a Future representing the future result of the action. def write(buffer, position, &block) - AsyncFile._wrap_handler(@j_file.write(buffer._to_java_buffer, position), block) + @j_file.write(buffer._to_java_buffer, position, FSWrappedHandler.new(block)) end # Reads some data from a file into a buffer, asynchronously. @@ -153,7 +134,7 @@ def write(buffer, position, &block) # @param [FixNum] length The number of bytes to read. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is {Buffer}. def read(buffer, offset, position, length, &block) - AsyncFile._wrap_handler(@j_file.read(buffer._to_java_buffer, offset, position, length), block){ |j_buff| Buffer.new(j_buff) } + @j_file.read(buffer._to_java_buffer, offset, position, length, FSWrappedHandler.new(block) { |j_buff| Buffer.new(j_buff) }) end # @return [WriteStream] A write stream operating on the file. @@ -195,37 +176,42 @@ def initialize(j_rs) end - # Represents the file-system and contains a broad set of operations for manipulating files. - # @author {http://tfox.org Tim Fox} - class FileSystem + # @private + class FSWrappedHandler + include org.vertx.java.core.AsyncResultHandler - # @private - def FileSystem._wrap_handler(j_fut, handler, &result_converter) - if handler - j_fut.handler do - if j_fut.succeeded - if result_converter - p result_converter - puts "result converter is #{result_converter}" - handler.call(nil, result_converter.call(j_fut.result)) - else - handler.call(nil, j_fut.result) - end + def initialize(handler, &result_converter) + @handler = handler + @result_converter = result_converter + end + + def handle(async_result) + if @handler + if !async_result.exception + if @result_converter + @handler.call(nil, @result_converter.call(async_result.result)) else - handler.call(j_fut.exception, nil) + @handler.call(nil, async_result.result) end + else + @handler.call(async_result.exception, nil) end end end + end + + # Represents the file-system and contains a broad set of operations for manipulating files. + # @author {http://tfox.org Tim Fox} + class FileSystem + # Copy a file, asynchronously. The copy will fail if from does not exist, or if to already exists. # @param [String] from Path of file to copy # @param [String] to Path of file to copy to # @param [Block] hndlr a block representing the handler which is called on completion. # @return [Future] a Future representing the future result of the action. def FileSystem.copy(from, to, &block) - j_fut = org.vertx.java.core.file.FileSystem.instance.copy(from, to) - _wrap_handler(j_fut, block) + org.vertx.java.core.file.FileSystem.instance.copy(from, to, FSWrappedHandler.new(block)) end def FileSystem.copy_sync(from, to) @@ -239,7 +225,7 @@ def FileSystem.copy_sync(from, to) # @param [String] to Path of file to copy to # @return [Future] a Future representing the future result of the action. def FileSystem.copy_recursive(from, to, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.copy(from, to, true), block) + org.vertx.java.core.file.FileSystem.instance.copy(from, to, true, FSWrappedHandler.new(block)) end def FileSystem.copy_recursive_sync(from, to) @@ -251,7 +237,7 @@ def FileSystem.copy_recursive_sync(from, to) # @param [String] to Path of file to move to # @return [Future] a Future representing the future result of the action. def FileSystem.move(from, to, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.move(from, to), block) + org.vertx.java.core.file.FileSystem.instance.move(from, to, FSWrappedHandler.new(block)) end def FileSystem.move_sync(from, to) @@ -263,7 +249,7 @@ def FileSystem.move_sync(from, to) # @param [FixNum] len Length to truncate file to. Will fail if len < 0. If len > file size then will do nothing. # @return [Future] a Future representing the future result of the action. def FileSystem.truncate(path, len, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.truncate(path, len), block) + org.vertx.java.core.file.FileSystem.instance.truncate(path, len, FSWrappedHandler.new(block)) end def FileSystem.truncate_sync(path, len) @@ -278,7 +264,7 @@ def FileSystem.truncate_sync(path, len) # @param [String] dir_perms A permission string of the form rwxr-x---. Used to set permissions for regular files. # @return [Future] a Future representing the future result of the action. def FileSystem.chmod(path, perms, dir_perms = nil, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.chmod(path, perms, dir_perms), block) + org.vertx.java.core.file.FileSystem.instance.chmod(path, perms, dir_perms, FSWrappedHandler.new(block)) end def FileSystem.chmod_sync(path, perms, dir_perms = nil) @@ -289,7 +275,7 @@ def FileSystem.chmod_sync(path, perms, dir_perms = nil) # @param [String] path Path to file # @return [Future] a Future representing the future result of the action. The type of {Future#result} is {FileProps}. def FileSystem.props(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.props(path), block) { |j_props| FileProps.new(j_props) } + org.vertx.java.core.file.FileSystem.instance.props(path, FSWrappedHandler.new(block) { |j_props| FileProps.new(j_props) }) end def FileSystem.props_sync(path) @@ -302,7 +288,7 @@ def FileSystem.props_sync(path) # @param [String] existing Path of where the link points to. # @return [Future] a Future representing the future result of the action. def FileSystem.link(link, existing, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.link(link, existing), block) + org.vertx.java.core.file.FileSystem.instance.link(link, existing, FSWrappedHandler.new(block)) end def FileSystem.link_sync(link, existing) @@ -314,7 +300,7 @@ def FileSystem.link_sync(link, existing) # @param [String] existing Path of where the link points to. # @return [Future] a Future representing the future result of the action. def FileSystem.sym_link(link, existing, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.symLink(link, existing), block) + org.vertx.java.core.file.FileSystem.instance.symLink(link, existing, FSWrappedHandler.new(block)) end def FileSystem.sym_link_sync(link, existing) @@ -325,7 +311,7 @@ def FileSystem.sym_link_sync(link, existing) # @param [String] link Path of the link to unlink. # @return [Future] a Future representing the future result of the action. def FileSystem.unlink(link, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.unlink(link), block) + org.vertx.java.core.file.FileSystem.instance.unlink(link, FSWrappedHandler.new(block)) end def FileSystem.unlinkSync(link) @@ -336,7 +322,7 @@ def FileSystem.unlinkSync(link) # @param [String] link Path of the link to read. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is String.. def FileSystem.read_sym_link(link, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.readSymLink(link), block) + org.vertx.java.core.file.FileSystem.instance.readSymLink(link, FSWrappedHandler.new(block)) end def FileSystem.read_sym_link_sync(link) @@ -348,7 +334,7 @@ def FileSystem.read_sym_link_sync(link) # @param [String] path Path of the file to delete. # @return [Future] a Future representing the future result of the action. def FileSystem.delete(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.delete(path), block) + org.vertx.java.core.file.FileSystem.instance.delete(path, FSWrappedHandler.new(block)) end def FileSystem.delete_sync(path) @@ -361,7 +347,7 @@ def FileSystem.delete_sync(path) # @param [String] path Path of the file to delete. # @return [Future] a Future representing the future result of the action. def FileSystem.delete_recursive(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.delete(path, true), block) + org.vertx.java.core.file.FileSystem.instance.delete(path, true, FSWrappedHandler.new(block)) end def FileSystem.delete_recursive_sync(path) @@ -375,7 +361,7 @@ def FileSystem.delete_recursive_sync(path) # @param [String] perms. A permission string of the form rwxr-x--- to give directory. # @return [Future] a Future representing the future result of the action. def FileSystem.mkdir(path, perms = nil, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.mkdir(path, perms), block) + org.vertx.java.core.file.FileSystem.instance.mkdir(path, perms, FSWrappedHandler.new(block)) end def FileSystem.mkdir_sync(path, perms = nil) @@ -388,7 +374,7 @@ def FileSystem.mkdir_sync(path, perms = nil) # @param [String] perms. A permission string of the form rwxr-x--- to give the created directory(ies). # @return [Future] a Future representing the future result of the action. def FileSystem.mkdir_with_parents(path, perms = nil, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.mkdir(path, perms, true), block) + org.vertx.java.core.file.FileSystem.instance.mkdir(path, perms, true, FSWrappedHandler.new(block)) end def FileSystem.mkdir_with_parents_sync(path, perms = nil) @@ -402,7 +388,7 @@ def FileSystem.mkdir_with_parents_sync(path, perms = nil) # then only files which match the filter will be returned. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is an Array of String. def FileSystem.read_dir(path, filter = nil, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.readDir(path, filter), block) + org.vertx.java.core.file.FileSystem.instance.readDir(path, filter, FSWrappedHandler.new(block)) end def FileSystem.read_dir_sync(path, filter = nil) @@ -413,7 +399,7 @@ def FileSystem.read_dir_sync(path, filter = nil) # @param [String] path Path of the file to read. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is {Buffer}. def FileSystem.read_file_as_buffer(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.readFile(path), block) + org.vertx.java.core.file.FileSystem.instance.readFile(path, FSWrappedHandler.new(block)) end def FileSystem.read_file_as_buffer_sync(path) @@ -425,7 +411,7 @@ def FileSystem.read_file_as_buffer_sync(path) # @param [String] buffer The Buffer to write # @return [Future] a Future representing the future result of the action. def FileSystem.write_buffer_to_file(path, buffer, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.writeFile(path, buffer), block) + org.vertx.java.core.file.FileSystem.instance.writeFile(path, buffer, FSWrappedHandler.new(block)) end def FileSystem.write_buffer_to_file_sync(path, buffer) @@ -457,7 +443,7 @@ def FileSystem.unwatch_file(path) # @param [Boolean] flush Whenever any data is written to the file, flush all changes to permanent storage immediately? # @return [Future] a Future representing the future result of the action. The type of {Future#result} is {AsyncFile}. def FileSystem.open(path, perms = nil, read = true, write = true, create_new = true, flush = false, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.open(path, perms, read, write, create_new, flush), block) { |j_file| AsyncFile.new(j_file)} + org.vertx.java.core.file.FileSystem.instance.open(path, perms, read, write, create_new, flush, FSWrappedHandler.new(block){ |j_file| AsyncFile.new(j_file)}) end def FileSystem.open_sync(path, perms = nil, read = true, write = true, create_new = true, flush = false) @@ -470,8 +456,7 @@ def FileSystem.open_sync(path, perms = nil, read = true, write = true, create_ne # @param [String] perms The file will be created with these permissions. # @return [Future] a Future representing the future result of the action. def FileSystem.create_file(path, perms = nil, &block) - j_fut = org.vertx.java.core.file.FileSystem.instance.createFile(path, perms) - _wrap_handler(j_fut, block) + org.vertx.java.core.file.FileSystem.instance.createFile(path, perms, FSWrappedHandler.new(block)) end def FileSystem.create_file_sync(path, perms = nil) @@ -482,7 +467,7 @@ def FileSystem.create_file_sync(path, perms = nil) # @param [String] path Path of the file to check. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is boolean. def FileSystem.exists?(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.exists(path), block) + org.vertx.java.core.file.FileSystem.instance.exists(path, FSWrappedHandler.new(block)) end def FileSystem.exists_sync?(path) @@ -493,11 +478,11 @@ def FileSystem.exists_sync?(path) # @param [String] path Path in the file system. # @return [Future] a Future representing the future result of the action. The type of {Future#result} is {FSProps}. def FileSystem.fs_props(path, &block) - _wrap_handler(org.vertx.java.core.file.FileSystem.instance.fsProps(path), block){ |j_props| FSProps.new(j_props)} + org.vertx.java.core.file.FileSystem.instance.fsProps(path, FSWrappedHandler.new(block) { |j_props| FSProps.new(j_props)}) end def FileSystem.fs_props_sync(path) - j_fsprops = org.vertx.java.core.file.FileSystem.instance.fsProps(path) + j_fsprops = org.vertx.java.core.file.FileSystem.instance.fsPropsSync(path) FSProps.new(j_fsprops) end diff --git a/src/tests/controllers/org/vertx/java/tests/core/filesystem/JavaFileSystemTest.java b/src/tests/controllers/org/vertx/java/tests/core/filesystem/JavaFileSystemTest.java index e1d2d053637..63aad5305c8 100644 --- a/src/tests/controllers/org/vertx/java/tests/core/filesystem/JavaFileSystemTest.java +++ b/src/tests/controllers/org/vertx/java/tests/core/filesystem/JavaFileSystemTest.java @@ -17,6 +17,8 @@ package org.vertx.java.tests.core.filesystem; import org.junit.Test; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.buffer.Buffer; import org.vertx.java.core.file.FileSystem; import org.vertx.java.core.logging.Logger; @@ -328,10 +330,18 @@ public void testFSProps() throws Exception { startTest(getMethodName()); } + + private AsyncResultHandler createHandler() { + return new AsyncResultHandler() { + public void handle(AsyncResult event) { + } + }; + } + @Test public void testExistsNoContext() throws Exception { try { - FileSystem.instance.exists("foo"); + FileSystem.instance.exists("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -342,7 +352,7 @@ public void testExistsNoContext() throws Exception { @Test public void testChmod1NoContext() throws Exception { try { - FileSystem.instance.chmod("foo", "bar"); + FileSystem.instance.chmod("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -352,7 +362,7 @@ public void testChmod1NoContext() throws Exception { @Test public void testChmod2NoContext() throws Exception { try { - FileSystem.instance.chmod("foo", "bar", "quux"); + FileSystem.instance.chmod("foo", "bar", "quux", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -362,7 +372,7 @@ public void testChmod2NoContext() throws Exception { @Test public void testCopy1NoContext() throws Exception { try { - FileSystem.instance.copy("foo", "bar"); + FileSystem.instance.copy("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -372,7 +382,7 @@ public void testCopy1NoContext() throws Exception { @Test public void testCopy2NoContext() throws Exception { try { - FileSystem.instance.copy("foo", "bar", true); + FileSystem.instance.copy("foo", "bar", true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -382,7 +392,7 @@ public void testCopy2NoContext() throws Exception { @Test public void testCreateFile1NoContext() throws Exception { try { - FileSystem.instance.createFile("foo"); + FileSystem.instance.createFile("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -392,7 +402,7 @@ public void testCreateFile1NoContext() throws Exception { @Test public void testCreateFile2NoContext() throws Exception { try { - FileSystem.instance.createFile("foo", "bar"); + FileSystem.instance.createFile("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -402,7 +412,7 @@ public void testCreateFile2NoContext() throws Exception { @Test public void testDelete1NoContext() throws Exception { try { - FileSystem.instance.delete("foo"); + FileSystem.instance.delete("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -412,7 +422,7 @@ public void testDelete1NoContext() throws Exception { @Test public void testDelete2NoContext() throws Exception { try { - FileSystem.instance.delete("foo", true); + FileSystem.instance.delete("foo", true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -423,7 +433,7 @@ public void testDelete2NoContext() throws Exception { @Test public void testFSPropsNoContext() throws Exception { try { - FileSystem.instance.fsProps("foo"); + FileSystem.instance.fsProps("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -433,7 +443,7 @@ public void testFSPropsNoContext() throws Exception { @Test public void testLinkNoContext() throws Exception { try { - FileSystem.instance.link("foo", "bar"); + FileSystem.instance.link("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -444,7 +454,7 @@ public void testLinkNoContext() throws Exception { @Test public void testLpropsNoContext() throws Exception { try { - FileSystem.instance.lprops("foo"); + FileSystem.instance.lprops("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -454,7 +464,7 @@ public void testLpropsNoContext() throws Exception { @Test public void testMkdir1NoContext() throws Exception { try { - FileSystem.instance.mkdir("foo"); + FileSystem.instance.mkdir("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -464,7 +474,7 @@ public void testMkdir1NoContext() throws Exception { @Test public void testMkdir2NoContext() throws Exception { try { - FileSystem.instance.mkdir("foo", "bar"); + FileSystem.instance.mkdir("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -474,7 +484,7 @@ public void testMkdir2NoContext() throws Exception { @Test public void testMkdir3NoContext() throws Exception { try { - FileSystem.instance.mkdir("foo", true); + FileSystem.instance.mkdir("foo", true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -484,7 +494,7 @@ public void testMkdir3NoContext() throws Exception { @Test public void testMkdir4NoContext() throws Exception { try { - FileSystem.instance.mkdir("foo", "bar", true); + FileSystem.instance.mkdir("foo", "bar", true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -495,7 +505,7 @@ public void testMkdir4NoContext() throws Exception { @Test public void testMoveNoContext() throws Exception { try { - FileSystem.instance.move("foo", "bar"); + FileSystem.instance.move("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -505,7 +515,7 @@ public void testMoveNoContext() throws Exception { @Test public void testOpen1NoContext() throws Exception { try { - FileSystem.instance.open("foo"); + FileSystem.instance.open("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -515,7 +525,7 @@ public void testOpen1NoContext() throws Exception { @Test public void testOpen2NoContext() throws Exception { try { - FileSystem.instance.open("foo", "bar"); + FileSystem.instance.open("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -525,7 +535,7 @@ public void testOpen2NoContext() throws Exception { @Test public void testOpen3NoContext() throws Exception { try { - FileSystem.instance.open("foo", "bar", true); + FileSystem.instance.open("foo", "bar", true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -535,7 +545,7 @@ public void testOpen3NoContext() throws Exception { @Test public void testOpen4NoContext() throws Exception { try { - FileSystem.instance.open("foo", "bar", true, true, true); + FileSystem.instance.open("foo", "bar", true, true, true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -545,7 +555,7 @@ public void testOpen4NoContext() throws Exception { @Test public void testOpen5NoContext() throws Exception { try { - FileSystem.instance.open("foo", "bar", true, true, true, true); + FileSystem.instance.open("foo", "bar", true, true, true, true, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -556,7 +566,7 @@ public void testOpen5NoContext() throws Exception { @Test public void testPropsNoContext() throws Exception { try { - FileSystem.instance.props("foo"); + FileSystem.instance.props("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -566,7 +576,7 @@ public void testPropsNoContext() throws Exception { @Test public void testReadDirNoContext() throws Exception { try { - FileSystem.instance.readDir("foo"); + FileSystem.instance.readDir("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -576,7 +586,7 @@ public void testReadDirNoContext() throws Exception { @Test public void testReadDir2NoContext() throws Exception { try { - FileSystem.instance.readDir("foo", "bar"); + FileSystem.instance.readDir("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -587,7 +597,7 @@ public void testReadDir2NoContext() throws Exception { @Test public void testReadFileNoContext() throws Exception { try { - FileSystem.instance.readFile("foo"); + FileSystem.instance.readFile("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -597,7 +607,7 @@ public void testReadFileNoContext() throws Exception { @Test public void testReadSymLinkNoContext() throws Exception { try { - FileSystem.instance.readSymlink("foo"); + FileSystem.instance.readSymlink("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -607,7 +617,7 @@ public void testReadSymLinkNoContext() throws Exception { @Test public void testSymLinkNoContext() throws Exception { try { - FileSystem.instance.symlink("foo", "bar"); + FileSystem.instance.symlink("foo", "bar", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -617,7 +627,7 @@ public void testSymLinkNoContext() throws Exception { @Test public void testTruncateNoContext() throws Exception { try { - FileSystem.instance.truncate("foo", 1234); + FileSystem.instance.truncate("foo", 1234, createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -628,7 +638,7 @@ public void testTruncateNoContext() throws Exception { @Test public void testUnlinkNoContext() throws Exception { try { - FileSystem.instance.unlink("foo"); + FileSystem.instance.unlink("foo", createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok @@ -638,7 +648,7 @@ public void testUnlinkNoContext() throws Exception { @Test public void testWriteFileNoContext() throws Exception { try { - FileSystem.instance.writeFile("foo", Buffer.create("foo")); + FileSystem.instance.writeFile("foo", Buffer.create("foo"), createHandler()); fail("Should throw exception"); } catch (IllegalStateException e) { // Ok diff --git a/src/tests/java/vertx/tests/core/filesystem/TestClient.java b/src/tests/java/vertx/tests/core/filesystem/TestClient.java index f83275515e4..cb2bce6a343 100644 --- a/src/tests/java/vertx/tests/core/filesystem/TestClient.java +++ b/src/tests/java/vertx/tests/core/filesystem/TestClient.java @@ -16,6 +16,8 @@ package vertx.tests.core.filesystem; +import org.vertx.java.core.AsyncResult; +import org.vertx.java.core.AsyncResultHandler; import org.vertx.java.core.CompletionHandler; import org.vertx.java.core.Future; import org.vertx.java.core.Handler; @@ -183,11 +185,11 @@ public void handle() { private void testCopy(final String source, final String target, final boolean recursive, final boolean shouldPass, final Handler afterOK) { - CompletionHandler compl = createHandler(shouldPass, afterOK); + AsyncResultHandler handler = createHandler(shouldPass, afterOK); if (recursive) { - FileSystem.instance.copy(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target, true).handler(compl); + FileSystem.instance.copy(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target, true, handler); } else { - FileSystem.instance.copy(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target).handler(compl); + FileSystem.instance.copy(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target, handler); } } @@ -265,7 +267,7 @@ public void handle() { } private void testMove(final String source, final String target, final boolean shouldPass, final Handler afterOK) throws Exception { - FileSystem.instance.move(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target).handler(createHandler(shouldPass, afterOK)); + FileSystem.instance.move(TEST_DIR + pathSep + source, TEST_DIR + pathSep + target, createHandler(shouldPass, afterOK)); } public void testTruncate() throws Exception { @@ -289,7 +291,7 @@ public void testTruncateFileDoesNotExist() throws Exception { private void testTruncate(final String file, final long truncatedLen, final boolean shouldPass, final Handler afterOK) throws Exception { - FileSystem.instance.truncate(TEST_DIR + pathSep + file, truncatedLen).handler(createHandler(shouldPass, afterOK)); + FileSystem.instance.truncate(TEST_DIR + pathSep + file, truncatedLen, createHandler(shouldPass, afterOK)); } public void testChmodNonRecursive1() throws Exception { @@ -384,11 +386,11 @@ private void testChmod(final String file, final String perms, final String dirPe } else { tu.azzert("rw-r--r--".equals(getPerms(file))); } - CompletionHandler compl = createHandler(shouldPass, afterOK); + AsyncResultHandler handler = createHandler(shouldPass, afterOK); if (dirPerms != null) { - FileSystem.instance.chmod(TEST_DIR + pathSep + file, perms, dirPerms).handler(compl); + FileSystem.instance.chmod(TEST_DIR + pathSep + file, perms, dirPerms, handler); } else { - FileSystem.instance.chmod(TEST_DIR + pathSep + file, perms).handler(compl); + FileSystem.instance.chmod(TEST_DIR + pathSep + file, perms, handler); } } @@ -464,23 +466,23 @@ public void handle(FileProps st) { private void testProps(final String fileName, final boolean link, final boolean shouldPass, final Handler afterOK) throws Exception { - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { + if (ar.exception != null) { if (shouldPass) { - tu.exception(completion.exception(), "stat failed"); + tu.exception(ar.exception, "stat failed"); } else { - tu.azzert(completion.exception() instanceof FileSystemException); + tu.azzert(ar.exception instanceof FileSystemException); if (afterOK != null) { - afterOK.handle(completion.result()); + afterOK.handle(ar.result); } tu.testComplete(); } } else { if (shouldPass) { if (afterOK != null) { - afterOK.handle(completion.result()); + afterOK.handle(ar.result); } tu.testComplete(); } else { @@ -490,9 +492,9 @@ public void handle(Future completion) { } }; if (link) { - FileSystem.instance.lprops(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.lprops(TEST_DIR + pathSep + fileName, handler); } else { - FileSystem.instance.props(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.props(TEST_DIR + pathSep + fileName, handler); } } @@ -524,12 +526,12 @@ public void handle() { private void testLink(final String from, final String to, final boolean symbolic, final boolean shouldPass, final Handler afterOK) throws Exception { - CompletionHandler compl = createHandler(shouldPass, afterOK); + AsyncResultHandler handler = createHandler(shouldPass, afterOK); if (symbolic) { // Symlink is relative - FileSystem.instance.symlink(TEST_DIR + pathSep + from, to).handler(compl); + FileSystem.instance.symlink(TEST_DIR + pathSep + from, to, handler); } else { - FileSystem.instance.link(TEST_DIR + pathSep + from, TEST_DIR + pathSep + to).handler(compl); + FileSystem.instance.link(TEST_DIR + pathSep + from, TEST_DIR + pathSep + to, handler); } } @@ -540,12 +542,12 @@ public void testUnlink() throws Exception { final String linkName = "some-link.txt"; Files.createLink(Paths.get(TEST_DIR + pathSep + linkName), Paths.get(TEST_DIR + pathSep + fileName)); tu.azzert(fileSize == fileLength(linkName)); - CompletionHandler compl = createHandler(true, new SimpleHandler() { + AsyncResultHandler handler = createHandler(true, new SimpleHandler() { public void handle() { tu.azzert(!fileExists(linkName)); } }); - FileSystem.instance.unlink(TEST_DIR + pathSep + linkName).handler(compl); + FileSystem.instance.unlink(TEST_DIR + pathSep + linkName, handler); } public void testReadSymLink() throws Exception { @@ -554,18 +556,18 @@ public void testReadSymLink() throws Exception { createFileWithJunk(fileName, fileSize); final String linkName = "some-link.txt"; Files.createSymbolicLink(Paths.get(TEST_DIR + pathSep + linkName), Paths.get(fileName)); - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { - tu.exception(completion.exception(), "Read failed"); + if (ar.exception != null) { + tu.exception(ar.exception, "Read failed"); } else { - tu.azzert(fileName.equals(completion.result())); + tu.azzert(fileName.equals(ar.result)); tu.testComplete(); } } }; - FileSystem.instance.readSymlink(TEST_DIR + pathSep + linkName).handler(compl); + FileSystem.instance.readSymlink(TEST_DIR + pathSep + linkName, handler); } public void testSimpleDelete() throws Exception { @@ -624,11 +626,11 @@ public void handle() { private void testDelete(final String fileName, final boolean recursive, final boolean shouldPass, final Handler afterOK) throws Exception { - CompletionHandler compl = createHandler(shouldPass, afterOK); + AsyncResultHandler handler = createHandler(shouldPass, afterOK); if (recursive) { - FileSystem.instance.delete(TEST_DIR + pathSep + fileName, recursive).handler(compl); + FileSystem.instance.delete(TEST_DIR + pathSep + fileName, recursive, handler); } else { - FileSystem.instance.delete(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.delete(TEST_DIR + pathSep + fileName, handler); } } @@ -685,18 +687,18 @@ public void handle() { private void testMkdir(final String dirName, final String perms, final boolean createParents, final boolean shouldPass, final Handler afterOK) throws Exception { - CompletionHandler compl = createHandler(shouldPass, afterOK); + AsyncResultHandler handler = createHandler(shouldPass, afterOK); if (createParents) { if (perms != null) { - FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, perms, createParents).handler(compl); + FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, perms, createParents, handler); } else { - FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, createParents).handler(compl); + FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, createParents, handler); } } else { if (perms != null) { - FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, perms).handler(compl); + FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, perms, handler); } else { - FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName).handler(compl); + FileSystem.instance.mkdir(TEST_DIR + pathSep + dirName, handler); } } } @@ -764,14 +766,14 @@ public void handle(String[] fileNames) { private void testReadDir(final String dirName, final String filter, final boolean shouldPass, final Handler afterOK) throws Exception { - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { + if (ar.exception != null) { if (shouldPass) { - tu.exception(completion.exception(), "read failed"); + tu.exception(ar.exception, "read failed"); } else { - tu.azzert(completion.exception() instanceof FileSystemException); + tu.azzert(ar.exception instanceof FileSystemException); if (afterOK != null) { afterOK.handle(null); } @@ -780,7 +782,7 @@ public void handle(Future completion) { } else { if (shouldPass) { if (afterOK != null) { - afterOK.handle(completion.result()); + afterOK.handle(ar.result); } tu.testComplete(); } else { @@ -790,9 +792,9 @@ public void handle(Future completion) { } }; if (filter == null) { - FileSystem.instance.readDir(TEST_DIR + pathSep + dirName).handler(compl); + FileSystem.instance.readDir(TEST_DIR + pathSep + dirName, handler); } else { - FileSystem.instance.readDir(TEST_DIR + pathSep + dirName, filter).handler(compl); + FileSystem.instance.readDir(TEST_DIR + pathSep + dirName, filter, handler); } } @@ -800,29 +802,29 @@ public void testReadFile() throws Exception { final byte[] content = TestUtils.generateRandomByteArray(1000); final String fileName = "some-file.dat"; createFile(fileName, content); - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { - tu.exception(completion.exception(), "failed to read"); + if (ar.exception != null) { + tu.exception(ar.exception, "failed to read"); } else { - tu.azzert(TestUtils.buffersEqual(Buffer.create(content), completion.result())); + tu.azzert(TestUtils.buffersEqual(Buffer.create(content), ar.result)); tu.testComplete(); } } }; - FileSystem.instance.readFile(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.readFile(TEST_DIR + pathSep + fileName, handler); } public void testWriteFile() throws Exception { final byte[] content = TestUtils.generateRandomByteArray(1000); final Buffer buff = Buffer.create(content); final String fileName = "some-file.dat"; - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { - tu.exception(completion.exception(), "failed to write"); + if (ar.exception != null) { + tu.exception(ar.exception, "failed to write"); } else { tu.azzert(fileExists(fileName)); tu.azzert(fileLength(fileName) == content.length); @@ -838,7 +840,7 @@ public void handle(Future completion) { } } }; - FileSystem.instance.writeFile(TEST_DIR + pathSep + fileName, buff).handler(compl); + FileSystem.instance.writeFile(TEST_DIR + pathSep + fileName, buff, handler); } public void testWriteAsync() throws Exception { @@ -849,17 +851,17 @@ public void testWriteAsync() throws Exception { byte[] content = TestUtils.generateRandomByteArray(chunkSize * chunks); final Buffer buff = Buffer.create(content); - FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, false, true, true, true).handler(new CompletionHandler() { + FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, false, true, true, true, new AsyncResultHandler() { int count; - public void handle(Future completion) { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { + if (ar.exception == null) { for (int i = 0; i < chunks; i++) { Buffer chunk = buff.copy(i * chunkSize, (i + 1) * chunkSize); tu.azzert(chunk.length() == chunkSize); - completion.result().write(chunk, i * chunkSize).handler(new CompletionHandler() { - public void handle(Future completion) { - if (completion.succeeded()) { + ar.result.write(chunk, i * chunkSize, new AsyncResultHandler() { + public void handle(AsyncResult ar) { + if (ar.exception == null) { if (++count == chunks) { tu.azzert(fileExists(fileName)); byte[] readBytes; @@ -874,13 +876,13 @@ public void handle(Future completion) { tu.testComplete(); } } else { - tu.exception(completion.exception(), "Failed to write"); + tu.exception(ar.exception, "Failed to write"); } } }); } } else { - tu.exception(completion.exception(), "Failed to open"); + tu.exception(ar.exception, "Failed to open"); } } }); @@ -893,29 +895,29 @@ public void testReadAsync() throws Exception { byte[] content = TestUtils.generateRandomByteArray(chunkSize * chunks); final Buffer expected = Buffer.create(content); createFile(fileName, content); - FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, true, false, false).handler(new CompletionHandler() { + FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, true, false, false, new AsyncResultHandler() { int reads; - public void handle(Future completion) { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { + if (ar.exception == null) { final Buffer buff = Buffer.create(chunks * chunkSize); for (int i = 0; i < chunks; i++) { - completion.result().read(buff, i * chunkSize, i * chunkSize, chunkSize).handler(new CompletionHandler() { - public void handle(Future completion) { - if (completion.succeeded()) { + ar.result.read(buff, i * chunkSize, i * chunkSize, chunkSize, new AsyncResultHandler() { + public void handle(AsyncResult ar) { + if (ar.exception == null) { if (++reads == chunks) { tu.azzert(TestUtils.buffersEqual(expected, buff)); - tu.azzert(buff == completion.result()); + tu.azzert(buff == ar.result); tu.testComplete(); } } else { - tu.exception(completion.exception(), "failed to read"); + tu.exception(ar.exception, "failed to read"); } } }); } } else { - tu.exception(completion.exception(), "failed to open file"); + tu.exception(ar.exception, "failed to open file"); } } }); @@ -927,11 +929,11 @@ public void testWriteStream() throws Exception { final int chunks = 10; byte[] content = TestUtils.generateRandomByteArray(chunkSize * chunks); final Buffer buff = Buffer.create(content); - FileSystem.instance.open(TEST_DIR + pathSep + fileName).handler(new CompletionHandler() { - public void handle(Future completion) { + FileSystem.instance.open(TEST_DIR + pathSep + fileName, new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { - WriteStream ws = completion.result().getWriteStream(); + if (ar.exception == null) { + WriteStream ws = ar.result.getWriteStream(); ws.exceptionHandler(new Handler() { public void handle(Exception e) { @@ -946,11 +948,11 @@ public void handle(Exception e) { ws.writeBuffer(chunk); } - completion.result().close().handler(new CompletionHandler() { - public void handle(Future completion) { + ar.result.close(new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.failed()) { - tu.exception(completion.exception(), "failed to close"); + if (ar.exception != null) { + tu.exception(ar.exception, "failed to close"); } else { tu.azzert(fileExists(fileName)); byte[] readBytes; @@ -966,7 +968,7 @@ public void handle(Future completion) { } }); } else { - tu.exception(completion.exception(), "failed to open"); + tu.exception(ar.exception, "failed to open"); } } }); @@ -979,11 +981,11 @@ public void testReadStream() throws Exception { final byte[] content = TestUtils.generateRandomByteArray(chunkSize * chunks); createFile(fileName, content); - FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, true, false, false).handler(new CompletionHandler() { - public void handle(Future completion) { + FileSystem.instance.open(TEST_DIR + pathSep + fileName, null, true, false, false, new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { - ReadStream rs = completion.result().getReadStream(); + if (ar.exception == null) { + ReadStream rs = ar.result.getReadStream(); final Buffer buff = Buffer.create(0); rs.dataHandler(new Handler() { @@ -1008,7 +1010,7 @@ public void handle() { } }); } else { - tu.exception(completion.exception(), "failed to open"); + tu.exception(ar.exception, "failed to open"); } } }); @@ -1023,31 +1025,31 @@ public void testPumpFileStreams() throws Exception { final byte[] content = TestUtils.generateRandomByteArray(fileSize); createFile(fileName1, content); - FileSystem.instance.open(TEST_DIR + pathSep + fileName1, null, true, false, false).handler(new CompletionHandler() { - public void handle(Future completion) { + FileSystem.instance.open(TEST_DIR + pathSep + fileName1, null, true, false, false, new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { - final ReadStream rs = completion.result().getReadStream(); + if (ar.exception == null) { + final ReadStream rs = ar.result.getReadStream(); //Open file for writing - FileSystem.instance.open(TEST_DIR + pathSep + fileName2, null, true, true, true).handler(new CompletionHandler() { + FileSystem.instance.open(TEST_DIR + pathSep + fileName2, null, true, true, true, new AsyncResultHandler() { - public void handle(final Future completion) { + public void handle(final AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { + if (ar.exception == null) { - WriteStream ws = completion.result().getWriteStream(); + WriteStream ws = ar.result.getWriteStream(); Pump p = new Pump(rs, ws); p.start(); rs.endHandler(new SimpleHandler() { public void handle() { tu.checkContext(); - completion.result().close().handler(new CompletionHandler() { + ar.result.close(new AsyncResultHandler() { - public void handle(Future completion) { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.failed()) { - tu.exception(completion.exception(), "failed to close"); + if (ar.exception != null) { + tu.exception(ar.exception, "failed to close"); } else { tu.azzert(fileExists(fileName2)); byte[] readBytes; @@ -1065,12 +1067,12 @@ public void handle(Future completion) { } }); } else { - tu.exception(completion.exception(), "failed to open"); + tu.exception(ar.exception, "failed to open"); } } }); } else { - tu.exception(completion.exception(), "failed to open"); + tu.exception(ar.exception, "failed to open"); } } }); @@ -1091,14 +1093,14 @@ public void testCreateFileAlreadyExists() throws Exception { private void testCreateFile(final String perms, final boolean shouldPass) throws Exception { final String fileName = "some-file.dat"; - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.failed()) { + if (ar.exception != null) { if (shouldPass) { - tu.exception(completion.exception(), "failed to create"); + tu.exception(ar.exception, "failed to create"); } else { - tu.azzert(completion.exception() instanceof FileSystemException); + tu.azzert(ar.exception instanceof FileSystemException); tu.testComplete(); } } else { @@ -1116,9 +1118,9 @@ public void handle(Future completion) { } }; if (perms != null) { - FileSystem.instance.createFile(TEST_DIR + pathSep + fileName, perms).handler(compl); + FileSystem.instance.createFile(TEST_DIR + pathSep + fileName, perms, handler); } else { - FileSystem.instance.createFile(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.createFile(TEST_DIR + pathSep + fileName, handler); } } @@ -1136,22 +1138,22 @@ private void testExists(final boolean exists) throws Exception { createFileWithJunk(fileName, 100); } - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (completion.succeeded()) { + if (ar.exception == null) { if (exists) { - tu.azzert(completion.result()); + tu.azzert(ar.result); } else { - tu.azzert(!completion.result()); + tu.azzert(!ar.result); } tu.testComplete(); } else { - tu.exception(completion.exception(), "failed to check"); + tu.exception(ar.exception, "failed to check"); } } }; - FileSystem.instance.exists(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.exists(TEST_DIR + pathSep + fileName, handler); } public void testFSProps() throws Exception { @@ -1171,29 +1173,29 @@ public void handle(FileSystemProps props) { private void testFSProps(final String fileName, final Handler afterOK) throws Exception { - CompletionHandler compl = new CompletionHandler() { - public void handle(Future completion) { + AsyncResultHandler handler = new AsyncResultHandler() { + public void handle(AsyncResult ar) { tu.checkContext(); - if (!completion.succeeded()) { - tu.exception(completion.exception(), "props failed"); + if (ar.exception != null) { + tu.exception(ar.exception, "props failed"); } else { - afterOK.handle(completion.result()); + afterOK.handle(ar.result); tu.testComplete(); } } }; - FileSystem.instance.fsProps(TEST_DIR + pathSep + fileName).handler(compl); + FileSystem.instance.fsProps(TEST_DIR + pathSep + fileName, handler); } - private CompletionHandler createHandler(final boolean shouldPass, final Handler afterOK) { - return new CompletionHandler() { - public void handle(Future completion) { + private AsyncResultHandler createHandler(final boolean shouldPass, final Handler afterOK) { + return new AsyncResultHandler() { + public void handle(AsyncResult event) { tu.checkContext(); - if (!completion.succeeded()) { + if (event.exception != null) { if (shouldPass) { - tu.exception(completion.exception(), "operation failed"); + tu.exception(event.exception, "operation failed"); } else { - tu.azzert(completion.exception() instanceof FileSystemException); + tu.azzert(event.exception instanceof FileSystemException); if (afterOK != null) { afterOK.handle(null); }