You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/24 17:39:31 UTC

[commons-vfs] branch master updated: Use Unchecked

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new a5d35776 Use Unchecked
a5d35776 is described below

commit a5d35776d08b83cbb9ff6f64f83bfce55c4db17f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 13:39:26 2023 -0400

    Use Unchecked
---
 .../commons/vfs2/provider/DefaultURLStreamHandler.java  | 10 +++-------
 .../apache/commons/vfs2/provider/ftp/FtpFileObject.java |  8 ++------
 .../commons/vfs2/provider/hdfs/HdfsFileSystem.java      | 10 +++-------
 .../commons/vfs2/provider/http4/Http4FileSystem.java    | 15 ++++++---------
 .../commons/vfs2/provider/http5/Http5FileSystem.java    | 15 ++++++---------
 .../apache/commons/vfs2/provider/tar/TarFileObject.java | 12 +++---------
 .../apache/commons/vfs2/provider/zip/ZipFileObject.java | 12 +++---------
 .../org/apache/commons/vfs2/filter/BaseFilterTest.java  | 17 +++++------------
 .../vfs2/provider/hdfs/HdfsFileProviderTestCase.java    |  4 ++--
 9 files changed, 33 insertions(+), 70 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultURLStreamHandler.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultURLStreamHandler.java
index 41a1b374..c1565c49 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultURLStreamHandler.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultURLStreamHandler.java
@@ -17,13 +17,12 @@
 package org.apache.commons.vfs2.provider;
 
 import java.io.IOException;
-import java.io.UncheckedIOException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.vfs2.FileObject;
-import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemOptions;
 
 /**
@@ -62,7 +61,7 @@ public class DefaultURLStreamHandler extends URLStreamHandler {
 
     @Override
     protected void parseURL(final URL u, final String spec, final int start, final int limit) {
-        try {
+        Uncheck.run(() -> {
             final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
 
             final FileObject newURL;
@@ -80,10 +79,7 @@ public class DefaultURLStreamHandler extends URLStreamHandler {
             final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
 
             setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
-        } catch (final FileSystemException fse) {
-            // This is rethrown to MalformedURLException in URL anyway
-            throw new UncheckedIOException(fse);
-        }
+        });
     }
 
     @Override
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileObject.java
index fa81274f..3d377a2b 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileObject.java
@@ -20,7 +20,6 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.UncheckedIOException;
 import java.time.Instant;
 import java.util.Calendar;
 import java.util.Collections;
@@ -31,6 +30,7 @@ import java.util.TimeZone;
 import java.util.TreeMap;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -594,11 +594,7 @@ public class FtpFileObject extends AbstractFileObject<FtpFileSystem> {
     @Override
     protected void onChildrenChanged(final FileName child, final FileType newType) {
         if (childMap != null && newType.equals(FileType.IMAGINARY)) {
-            try {
-                childMap.remove(UriParser.decode(child.getBaseName()));
-            } catch (final FileSystemException e) {
-                throw new UncheckedIOException(e);
-            }
+            Uncheck.run(() -> childMap.remove(UriParser.decode(child.getBaseName())));
         } else {
             // if child was added we have to rescan the children
             // TODO - get rid of this
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystem.java
index c4df7c0b..b416ba8c 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystem.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileSystem.java
@@ -18,12 +18,12 @@ package org.apache.commons.vfs2.provider.hdfs;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.UncheckedIOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.Collection;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.vfs2.CacheStrategy;
@@ -72,12 +72,8 @@ public class HdfsFileSystem extends AbstractFileSystem {
      */
     @Override
     public void close() {
-        try {
-            if (null != fs) {
-                fs.close();
-            }
-        } catch (final IOException e) {
-            throw new UncheckedIOException("Error closing HDFS client", e);
+        if (null != fs) {
+            Uncheck.run(fs::close);
         }
         super.close();
     }
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java
index 188ddb69..684859c4 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java
@@ -16,11 +16,11 @@
  */
 package org.apache.commons.vfs2.provider.http4;
 
-import java.io.IOException;
-import java.io.UncheckedIOException;
+import java.io.Closeable;
 import java.net.URI;
 import java.util.Collection;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.vfs2.Capability;
 import org.apache.commons.vfs2.FileName;
 import org.apache.commons.vfs2.FileObject;
@@ -29,7 +29,6 @@ import org.apache.commons.vfs2.provider.AbstractFileName;
 import org.apache.commons.vfs2.provider.AbstractFileSystem;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.protocol.HttpClientContext;
-import org.apache.http.impl.client.CloseableHttpClient;
 
 /**
  * http4 file system.
@@ -88,12 +87,10 @@ public class Http4FileSystem extends AbstractFileSystem {
 
     @Override
     protected void doCloseCommunicationLink() {
-        if (httpClient instanceof CloseableHttpClient) {
-            try {
-                ((CloseableHttpClient) httpClient).close();
-            } catch (final IOException e) {
-                throw new UncheckedIOException("Error closing HttpClient", e);
-            }
+        if (httpClient instanceof Closeable) {
+            // TODO "Error closing HttpClient" Commons IO
+            // Uncheck.run(() -> ((Closeable) httpClient).close(), () -> "Error closing HttpClient");
+            Uncheck.run(() -> ((Closeable) httpClient).close());
         }
     }
 
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java
index c1b81f7c..078a8dbe 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java
@@ -16,11 +16,11 @@
  */
 package org.apache.commons.vfs2.provider.http5;
 
-import java.io.IOException;
-import java.io.UncheckedIOException;
+import java.io.Closeable;
 import java.net.URI;
 import java.util.Collection;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.vfs2.Capability;
 import org.apache.commons.vfs2.FileName;
 import org.apache.commons.vfs2.FileObject;
@@ -28,7 +28,6 @@ import org.apache.commons.vfs2.FileSystemOptions;
 import org.apache.commons.vfs2.provider.AbstractFileName;
 import org.apache.commons.vfs2.provider.AbstractFileSystem;
 import org.apache.hc.client5.http.classic.HttpClient;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
 import org.apache.hc.client5.http.protocol.HttpClientContext;
 
 /**
@@ -88,12 +87,10 @@ public class Http5FileSystem extends AbstractFileSystem {
 
     @Override
     protected void doCloseCommunicationLink() {
-        if (httpClient instanceof CloseableHttpClient) {
-            try {
-                ((CloseableHttpClient) httpClient).close();
-            } catch (final IOException e) {
-                throw new UncheckedIOException("Error closing HttpClient", e);
-            }
+        if (httpClient instanceof Closeable) {
+            // TODO "Error closing HttpClient" Commons IO
+            // Uncheck.run(() -> ((Closeable) httpClient).close(), () -> "Error closing HttpClient");
+            Uncheck.run(() -> ((Closeable) httpClient).close());
         }
     }
 
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileObject.java
index 620ae034..dc6d3226 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/tar/TarFileObject.java
@@ -17,10 +17,10 @@
 package org.apache.commons.vfs2.provider.tar;
 
 import java.io.InputStream;
-import java.io.UncheckedIOException;
 import java.util.HashSet;
 
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.vfs2.FileName;
 import org.apache.commons.vfs2.FileSystemException;
@@ -111,15 +111,9 @@ public class TarFileObject extends AbstractFileObject<TarFileSystem> {
      */
     @Override
     protected String[] doListChildren() {
-        try {
-            if (!getType().hasChildren()) {
-                return null;
-            }
-        } catch (final FileSystemException e) {
-            // should not happen as the type has already been cached.
-            throw new UncheckedIOException(e);
+        if (!Uncheck.get(this::getType).hasChildren()) {
+            return null;
         }
-
         return children.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
     }
 
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java
index 26b4e920..cebf38b9 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileObject.java
@@ -17,10 +17,10 @@
 package org.apache.commons.vfs2.provider.zip;
 
 import java.io.InputStream;
-import java.io.UncheckedIOException;
 import java.util.HashSet;
 import java.util.zip.ZipEntry;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.vfs2.FileName;
 import org.apache.commons.vfs2.FileSystemException;
@@ -120,15 +120,9 @@ public class ZipFileObject extends AbstractFileObject<ZipFileSystem> {
      */
     @Override
     protected String[] doListChildren() {
-        try {
-            if (!getType().hasChildren()) {
-                return null;
-            }
-        } catch (final FileSystemException e) {
-            // should not happen as the type has already been cached.
-            throw new UncheckedIOException(e);
+        if (!Uncheck.get(this::getType).hasChildren()) {
+            return null;
         }
-
         return children.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
     }
 
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/filter/BaseFilterTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/filter/BaseFilterTest.java
index f38d335e..ac746d50 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/filter/BaseFilterTest.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/filter/BaseFilterTest.java
@@ -24,7 +24,6 @@ import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.UncheckedIOException;
 import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.List;
@@ -32,6 +31,7 @@ import java.util.Objects;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
+import org.apache.commons.io.function.Uncheck;
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSelectInfo;
 import org.apache.commons.vfs2.FileSystemException;
@@ -116,17 +116,12 @@ public abstract class BaseFilterTest {
      * @return File select info.
      */
     protected static FileSelectInfo createFileSelectInfo(final File file) {
-        try {
-            final FileSystemManager fsManager = VFS.getManager();
-            final FileObject fileObject = fsManager.toFileObject(file);
+        return Uncheck.get(() -> {
+            final FileObject fileObject = VFS.getManager().toFileObject(file);
             return new FileSelectInfo() {
                 @Override
                 public FileObject getBaseFolder() {
-                    try {
-                        return fileObject.getParent();
-                    } catch (final FileSystemException ex) {
-                        throw new UncheckedIOException(ex);
-                    }
+                    return Uncheck.get(fileObject::getParent);
                 }
 
                 @Override
@@ -144,9 +139,7 @@ public abstract class BaseFilterTest {
                     return Objects.toString(fileObject);
                 }
             };
-        } catch (final FileSystemException ex) {
-            throw new UncheckedIOException(ex);
-        }
+        });
     }
 
     protected static void delete(final File file) {
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileProviderTestCase.java
index 7e8b9e4a..d1d9b953 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileProviderTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/hdfs/HdfsFileProviderTestCase.java
@@ -20,7 +20,6 @@ import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectoryFile;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.UncheckedIOException;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.vfs2.AbstractProviderTestConfig;
@@ -36,6 +35,7 @@ import org.apache.hadoop.hdfs.DFSConfigKeys;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
+import org.junit.jupiter.api.Assertions;
 
 import junit.framework.Test;
 
@@ -100,7 +100,7 @@ public class HdfsFileProviderTestCase extends AbstractProviderTestConfig {
                 cluster = new MiniDFSCluster(PORT, conf, 1, true, true, true, null, null, null, null);
                 cluster.waitActive();
             } catch (final IOException e) {
-                throw new UncheckedIOException("Error setting up mini cluster", e);
+                Assertions.fail("Error setting up mini cluster", e);
             }
             hdfs = cluster.getFileSystem();