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
commit f9dc10ca54d13a1fea2001d9a78701706cfd3028 Author: Gary Gregory <[email protected]> AuthorDate: Thu Feb 11 16:55:31 2021 -0500 Simpler backward compatibility. --- .../commons/vfs2/FileSystemConfigBuilder.java | 33 +++++++++++++++++++++- .../vfs2/provider/ftp/FtpClientFactory.java | 11 +++----- .../provider/ftp/FtpFileSystemConfigBuilder.java | 14 ++++----- .../apache/commons/vfs2/util/DurationUtils.java | 31 ++++++++++++++++++++ 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java index 9b9ea7c..ad7dcb4 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java @@ -20,6 +20,8 @@ import java.time.Duration; import java.util.Objects; import java.util.function.Function; +import org.apache.commons.vfs2.util.DurationUtils; + /** * Abstract class which has the right to fill FileSystemOptions. */ @@ -279,6 +281,35 @@ public abstract class FileSystemConfigBuilder { } /** + * Gets a named option as a Duration bound to the integer range. + * + * @param fileSystemOptions file system options to query, may be null. + * @param name the option name + * @return the option in {@code opts} or system properties, otherwise null + * @see #getLong(FileSystemOptions, String, Long) + * + * @since 2.8.0 + */ + protected Integer getDurationInteger(final FileSystemOptions fileSystemOptions, final String name) { + return getDurationInteger(fileSystemOptions, name, null); + } + + /** + * Gets a named option as a Duration bound to the integer range. + * + * @param fileSystemOptions file system options to query, may be null. + * @param name the option name + * @param defaultValue value to return if option is not present + * @return the option in {@code opts} or system properties, otherwise {@code defaultValue} + * + * @since 2.8.0 + */ + protected Integer getDurationInteger(final FileSystemOptions fileSystemOptions, final String name, + final Duration defaultValue) { + return DurationUtils.toMillisInt(getParam(fileSystemOptions, name, defaultValue, Duration::parse)); + } + + /** *Gets a named option as a Double. * * @param <E> enumeration type @@ -489,7 +520,7 @@ public abstract class FileSystemConfigBuilder { * @since 2.8.0 */ private <T> T getParam(final FileSystemOptions fileSystemOptions, final String name, final T defaultValue, - Function<String, T> function) { + final Function<String, T> function) { T value = getParam(fileSystemOptions, name); if (value == null) { final String str = getProperty(name); diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java index 341a076..6d75340 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java @@ -32,6 +32,7 @@ import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory; import org.apache.commons.vfs2.FileSystemException; import org.apache.commons.vfs2.FileSystemOptions; +import org.apache.commons.vfs2.util.DurationUtils; import org.apache.commons.vfs2.util.UserAuthenticatorUtils; /** @@ -142,7 +143,7 @@ public final class FtpClientFactory { try { final Duration connectTimeout = builder.getConnectTimeoutDuration(fileSystemOptions); if (connectTimeout != null) { - client.setDefaultTimeout(toIntMillisTimeout(connectTimeout)); + client.setDefaultTimeout(DurationUtils.toMillisInt(connectTimeout)); } final String controlEncoding = builder.getControlEncoding(fileSystemOptions); @@ -186,12 +187,12 @@ public final class FtpClientFactory { // Set dataTimeout value final Duration dataTimeout = builder.getDataTimeoutDuration(fileSystemOptions); if (dataTimeout != null) { - client.setDataTimeout(toIntMillisTimeout(dataTimeout)); + client.setDataTimeout(DurationUtils.toMillisInt(dataTimeout)); } final Duration socketTimeout = builder.getSoTimeoutDuration(fileSystemOptions); if (socketTimeout != null) { - client.setSoTimeout(toIntMillisTimeout(socketTimeout)); + client.setSoTimeout(DurationUtils.toMillisInt(socketTimeout)); } final Duration controlKeepAliveTimeout = builder.getControlKeepAliveTimeout(fileSystemOptions); @@ -234,10 +235,6 @@ public final class FtpClientFactory { } protected abstract void setupOpenConnection(C client, FileSystemOptions fileSystemOptions) throws IOException; - - private int toIntMillisTimeout(final Duration duration) { - return (int) Math.min(duration.toMillis(), Integer.MAX_VALUE); - } } /** Connection Factory, used to configure the FTPClient. */ diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java index 05862ac..295b0df 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java @@ -113,7 +113,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { */ @Deprecated public Integer getConnectTimeout(final FileSystemOptions options) { - return (int) getConnectTimeoutDuration(options).toMillis(); + return getDurationInteger(options, CONNECT_TIMEOUT); } /** @@ -141,7 +141,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { * @return The controlKeepAliveReplyTimeout value. * @since 2.8.0 */ - public Duration getControlKeepAliveReplyTimeout(FileSystemOptions options) { + public Duration getControlKeepAliveReplyTimeout(final FileSystemOptions options) { return getDuration(options, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT); } @@ -150,7 +150,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { * @return The controlKeepAliveTimeout value. * @since 2.8.0 */ - public Duration getControlKeepAliveTimeout(FileSystemOptions options) { + public Duration getControlKeepAliveTimeout(final FileSystemOptions options) { return getDuration(options, CONTROL_KEEP_ALIVE_TIMEOUT); } @@ -162,7 +162,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { */ @Deprecated public Integer getDataTimeout(final FileSystemOptions options) { - return (int) getDataTimeoutDuration(options).toMillis(); + return getDurationInteger(options, DATA_TIMEOUT); } /** @@ -310,7 +310,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { */ @Deprecated public Integer getSoTimeout(final FileSystemOptions options) { - return (int) getSoTimeoutDuration(options).toMillis(); + return getDurationInteger(options, SO_TIMEOUT); } /** @@ -407,7 +407,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { * @param duration timeout duration. * @since 2.8.0 */ - public void setControlKeepAliveReplyTimeout(FileSystemOptions options, final Duration duration) { + public void setControlKeepAliveReplyTimeout(final FileSystemOptions options, final Duration duration) { setParam(options, CONTROL_KEEP_ALIVE_REPLY_TIMEOUT, duration); } @@ -421,7 +421,7 @@ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { * @param duration The timeout duration. * @since 2.8.0 */ - public void setControlKeepAliveTimeout(FileSystemOptions options, final Duration duration) { + public void setControlKeepAliveTimeout(final FileSystemOptions options, final Duration duration) { setParam(options, CONTROL_KEEP_ALIVE_TIMEOUT, duration); } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/DurationUtils.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/DurationUtils.java new file mode 100644 index 0000000..eba9c02 --- /dev/null +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/DurationUtils.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.vfs2.util; + +import java.time.Duration; + +/** Temporary until Commons Lang 3.12.0. */ +public class DurationUtils { + + /** Temporary until Commons Lang 3.12.0. */ + public static int toMillisInt(final Duration duration) { + final long millis = duration.toMillis(); + return millis > 0 ? (int) Math.min(millis, Integer.MAX_VALUE) : (int) Math.max(millis, Integer.MIN_VALUE); + } + +}
