You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by mb...@apache.org on 2022/03/19 15:58:03 UTC

[ant-antlibs-s3] branch main updated: ProjectUtils to interface

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

mbenson pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ant-antlibs-s3.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a87873  ProjectUtils to interface
8a87873 is described below

commit 8a878730ce5e0c6a727556f7375368a0c4ed27f5
Author: Matt Benson <mb...@apache.org>
AuthorDate: Sat Mar 19 10:55:40 2022 -0500

    ProjectUtils to interface
---
 src/main/org/apache/ant/s3/CompareSelect.java   |  6 +--
 src/main/org/apache/ant/s3/Credentials.java     |  4 +-
 src/main/org/apache/ant/s3/LoggingTask.java     |  2 +-
 src/main/org/apache/ant/s3/ObjectResource.java  | 21 +++++-----
 src/main/org/apache/ant/s3/ObjectResources.java |  8 +---
 src/main/org/apache/ant/s3/ProjectUtils.java    | 54 +++++++++++++++++--------
 src/main/org/apache/ant/s3/Put.java             | 16 +++-----
 src/main/org/apache/ant/s3/S3DataType.java      | 16 +-------
 8 files changed, 61 insertions(+), 66 deletions(-)

diff --git a/src/main/org/apache/ant/s3/CompareSelect.java b/src/main/org/apache/ant/s3/CompareSelect.java
index 8d43173..4cb4fd4 100644
--- a/src/main/org/apache/ant/s3/CompareSelect.java
+++ b/src/main/org/apache/ant/s3/CompareSelect.java
@@ -36,7 +36,7 @@ import org.apache.tools.ant.util.StringUtils;
  * S3 {@link ResourceComparator}/{@link ResourceSelector} base/class
  * organization.
  */
-public abstract class CompareSelect extends ResourceComparator implements ResourceSelector {
+public abstract class CompareSelect extends ResourceComparator implements ResourceSelector, ProjectUtils {
 
     /**
      * {@link CompareSelect} {@link ForStringAttribute}.
@@ -136,8 +136,8 @@ public abstract class CompareSelect extends ResourceComparator implements Resour
          * @param matchAs
          */
         public void setMatchAs(final MatchAs matchAs) {
-            Exceptions.raiseIf(matchAs == null, ProjectUtils.buildExceptionAt(getLocation()),
-                "@matchas may not be null");
+            Exceptions.raiseIf(matchAs == null, buildException(), "@matchas may not be null");
+
             if (this.matchAs != matchAs) {
                 this.matchAs = matchAs;
                 predicate = null;
diff --git a/src/main/org/apache/ant/s3/Credentials.java b/src/main/org/apache/ant/s3/Credentials.java
index f186ba8..3efd63b 100644
--- a/src/main/org/apache/ant/s3/Credentials.java
+++ b/src/main/org/apache/ant/s3/Credentials.java
@@ -16,8 +16,6 @@
  */
 package org.apache.ant.s3;
 
-import static org.apache.ant.s3.ProjectUtils.buildExceptionAt;
-
 import java.util.function.Consumer;
 
 import org.apache.tools.ant.Project;
@@ -111,7 +109,7 @@ public class Credentials extends S3DataType implements Consumer<S3ClientBuilder>
         final AwsCredentialsProvider credentialsProvider;
 
         if (getProfile() == null) {
-            Exceptions.raiseIf(getAccessKey() == null || getSecretKey() == null, buildExceptionAt(getLocation()),
+            Exceptions.raiseIf(getAccessKey() == null || getSecretKey() == null, buildException(),
                 "%s requires both @accessKey and @secretKey in the absence of @profile", getDataTypeName());
 
             credentialsProvider =
diff --git a/src/main/org/apache/ant/s3/LoggingTask.java b/src/main/org/apache/ant/s3/LoggingTask.java
index 70c4eb5..c69203a 100644
--- a/src/main/org/apache/ant/s3/LoggingTask.java
+++ b/src/main/org/apache/ant/s3/LoggingTask.java
@@ -24,7 +24,7 @@ import org.apache.tools.ant.Task;
 /**
  * Define task logging convenience configuration.
  */
-abstract class LoggingTask extends Task {
+abstract class LoggingTask extends Task implements ProjectUtils {
 
     private int verbosity = Project.MSG_VERBOSE;
 
diff --git a/src/main/org/apache/ant/s3/ObjectResource.java b/src/main/org/apache/ant/s3/ObjectResource.java
index d95bf8f..f0653fc 100644
--- a/src/main/org/apache/ant/s3/ObjectResource.java
+++ b/src/main/org/apache/ant/s3/ObjectResource.java
@@ -16,9 +16,6 @@
  */
 package org.apache.ant.s3;
 
-import static org.apache.ant.s3.ProjectUtils.buildExceptionAt;
-import static org.apache.ant.s3.ProjectUtils.componentName;
-
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
@@ -64,7 +61,7 @@ import software.amazon.awssdk.services.s3.model.Tagging;
 /**
  * Amazon S3 object {@link Resource} implementation.
  */
-public class ObjectResource extends Resource {
+public class ObjectResource extends Resource implements ProjectUtils {
     private static class VersionInfo {
         final boolean deleteMarker;
         final boolean latest;
@@ -311,7 +308,7 @@ public class ObjectResource extends Resource {
     @SuppressWarnings({ "rawtypes", "unchecked" })
     public InlineProperties createMetadata() {
         checkChildrenAllowed();
-        Exceptions.raiseUnless(metadata == null, buildExceptionAt(getLocation()), "metadata already specified");
+        Exceptions.raiseUnless(metadata == null, buildException(), "metadata already specified");
 
         final InlineProperties result = new InlineProperties(getProject());
         metadata = (Map) result.properties;
@@ -356,7 +353,7 @@ public class ObjectResource extends Resource {
     @SuppressWarnings({ "unchecked", "rawtypes" })
     public InlineProperties createTagging() {
         checkChildrenAllowed();
-        Exceptions.raiseUnless(tagging == null, buildExceptionAt(getLocation()), "tagging already specified");
+        Exceptions.raiseUnless(tagging == null, buildException(), "tagging already specified");
 
         final InlineProperties result = new InlineProperties(getProject());
         tagging = (Map) result.properties;
@@ -405,8 +402,8 @@ public class ObjectResource extends Resource {
     public void addConfigured(Client s3) {
         checkChildrenAllowed();
 
-        Exceptions.raiseUnless(this.s3 == null, buildExceptionAt(getLocation()),
-            () -> String.format("%s already specified", componentName(getProject(), Client.class)));
+        Exceptions.raiseUnless(this.s3 == null, buildException(),
+            () -> String.format("%s already specified", componentName(Client.class)));
 
         this.s3 = Objects.requireNonNull(s3).get();
     }
@@ -418,7 +415,7 @@ public class ObjectResource extends Resource {
      */
     public void setClientRefid(String refid) {
         checkAttributesAllowed();
-        Exceptions.raiseIf(StringUtils.isBlank(refid), buildExceptionAt(getLocation()),
+        Exceptions.raiseIf(StringUtils.isBlank(refid), buildException(),
             "@clientrefid must not be null/empty/blank");
 
         addConfigured(getProject().<Client> getReference(refid));
@@ -654,8 +651,8 @@ public class ObjectResource extends Resource {
     private <B extends S3Request.Builder> Consumer<B> request(BiConsumer<B, String> setBucket,
         BiConsumer<B, String> setKey) {
         return b -> {
-            setBucket.accept(b, ProjectUtils.require(getBucket(), "@bucket"));
-            setKey.accept(b, ProjectUtils.require(getKey(), "@key"));
+            setBucket.accept(b, require(getBucket(), "@bucket"));
+            setKey.accept(b, require(getKey(), "@key"));
         };
     }
 
@@ -705,7 +702,7 @@ public class ObjectResource extends Resource {
     }
 
     private S3Client s3() {
-        return ProjectUtils.requireComponent(getProject(), s3, Client.class);
+        return requireComponent(s3, Client.class);
     }
 
     private void _setDirectory(boolean directory) {
diff --git a/src/main/org/apache/ant/s3/ObjectResources.java b/src/main/org/apache/ant/s3/ObjectResources.java
index 7f42ba7..ca40fc2 100644
--- a/src/main/org/apache/ant/s3/ObjectResources.java
+++ b/src/main/org/apache/ant/s3/ObjectResources.java
@@ -16,10 +16,6 @@
  */
 package org.apache.ant.s3;
 
-import static org.apache.ant.s3.ProjectUtils.componentName;
-import static org.apache.ant.s3.ProjectUtils.require;
-import static org.apache.ant.s3.ProjectUtils.requireComponent;
-
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -93,7 +89,7 @@ public class ObjectResources extends S3DataType implements ResourceCollection {
         checkChildrenAllowed();
 
         Exceptions.raiseUnless(this.s3 == null, buildException(),
-            () -> String.format("%s already specified", componentName(getProject(), Client.class)));
+            () -> String.format("%s already specified", componentName(Client.class)));
 
         this.s3 = Objects.requireNonNull(s3);
         resetResourceCache();
@@ -449,7 +445,7 @@ public class ObjectResources extends S3DataType implements ResourceCollection {
     }
 
     private S3Client s3() {
-        return requireComponent(getProject(), s3, Client.class).get();
+        return requireComponent(s3, Client.class).get();
     }
 
     private boolean isSelected(Resource resource) {
diff --git a/src/main/org/apache/ant/s3/ProjectUtils.java b/src/main/org/apache/ant/s3/ProjectUtils.java
index c4acda0..fa6cd4f 100644
--- a/src/main/org/apache/ant/s3/ProjectUtils.java
+++ b/src/main/org/apache/ant/s3/ProjectUtils.java
@@ -28,9 +28,20 @@ import org.apache.tools.ant.Location;
 import org.apache.tools.ant.Project;
 
 /**
- * Project utils.
+ * Interface providing behavior for Ant {@link Project} components.
  */
-class ProjectUtils {
+interface ProjectUtils {
+
+    /**
+     * Attempt to determine a component name for the specified type.
+     * 
+     * @param type
+     *
+     * @return {@link String}
+     */
+    default String componentName(final Class<?> type) {
+        return componentName(getProject(), type);
+    }
 
     /**
      * Attempt to determine a component name for the specified type relative to
@@ -40,7 +51,7 @@ class ProjectUtils {
      * @param type
      * @return {@link String}
      */
-    static String componentName(final Project project, final Class<?> type) {
+    default String componentName(final Project project, final Class<?> type) {
         Objects.requireNonNull(type, "type");
 
         if (project != null) {
@@ -64,22 +75,22 @@ class ProjectUtils {
      * @throws IllegalStateException
      *             if {@code t == null}
      */
-    static <T> T require(final T item, final String description) {
-        Exceptions.raiseIf(item == null, BuildException::new, "%s is required", description);
+    default <T> T require(final T item, final String description) {
+        Exceptions.raiseIf(item == null, buildException(), "%s is required", description);
         return item;
     }
 
     /**
      * Require the specified component.
-     *
-     * @param <T>
-     * @param project
+     * 
      * @param component
      * @param type
+     *
+     * @param <T>
      * @return {@code component}
      */
-    static <T> T requireComponent(final Project project, final T component, final Class<?> type) {
-        return require(component, componentName(project, type));
+    default <T> T requireComponent(final T component, final Class<?> type) {
+        return require(component, componentName(type));
     }
 
     /**
@@ -89,8 +100,8 @@ class ProjectUtils {
      * @param location
      * @return {@link Function}
      */
-    static Function<String, BuildException> buildExceptionAt(final Location location) {
-        return msg -> new BuildException(msg, location);
+    default Function<String, BuildException> buildException() {
+        return msg -> new BuildException(msg, getLocation());
     }
 
     /**
@@ -101,10 +112,21 @@ class ProjectUtils {
      * @param location
      * @return {@link BiFunction}
      */
-    static BiFunction<String, Throwable, BuildException> buildExceptionTriggeredAt(final Location location) {
-        return (msg, cause) -> new BuildException(msg, cause, location);
+    default BiFunction<String, Throwable, BuildException> buildExceptionTriggered() {
+        return (msg, cause) -> new BuildException(msg, cause, getLocation());
     }
 
-    private ProjectUtils() {
-    }
+    /**
+     * Get the {@link Project} of this item.
+     * 
+     * @return {@link Project}
+     */
+    Project getProject();
+
+    /**
+     * Get the {@link Location} of this item.
+     * 
+     * @return {@link Location}
+     */
+    Location getLocation();
 }
diff --git a/src/main/org/apache/ant/s3/Put.java b/src/main/org/apache/ant/s3/Put.java
index 7154bca..898e376 100644
--- a/src/main/org/apache/ant/s3/Put.java
+++ b/src/main/org/apache/ant/s3/Put.java
@@ -16,10 +16,6 @@
  */
 package org.apache.ant.s3;
 
-import static org.apache.ant.s3.ProjectUtils.buildExceptionAt;
-import static org.apache.ant.s3.ProjectUtils.require;
-import static org.apache.ant.s3.ProjectUtils.requireComponent;
-
 import java.io.IOException;
 import java.util.Objects;
 import java.util.Optional;
@@ -94,7 +90,7 @@ public class Put extends CopyResources {
      */
     @Override
     public void setAppend(boolean append) {
-        Exceptions.raiseIf(append, buildExceptionAt(getLocation()), "@append not supported by %s", getTaskName());
+        Exceptions.raiseIf(append, buildException(), "@append not supported by %s", getTaskName());
     }
 
     /**
@@ -106,12 +102,13 @@ public class Put extends CopyResources {
      */
     @Override
     public void setPreserveLastModified(boolean preserveLastModified) {
-        Exceptions.raiseIf(preserveLastModified, buildExceptionAt(getLocation()),
-            "@preserveLastModified not supported by %s", getTaskName());
+        Exceptions.raiseIf(preserveLastModified, buildException(), "@preserveLastModified not supported by %s",
+            getTaskName());
     }
 
     /**
      * Enforce "always overwrite."
+     * 
      * @return {@code true}
      */
     @Override
@@ -128,8 +125,7 @@ public class Put extends CopyResources {
      */
     @Override
     public void setOverwrite(boolean overwrite) {
-        Exceptions.raiseUnless(overwrite, buildExceptionAt(getLocation()), "%s only operates in overwrite mode",
-            getTaskName());
+        Exceptions.raiseUnless(overwrite, buildException(), "%s only operates in overwrite mode", getTaskName());
     }
 
     /**
@@ -185,6 +181,6 @@ public class Put extends CopyResources {
     }
 
     private S3Client s3() {
-        return requireComponent(getProject(), s3, Client.class).get();
+        return requireComponent(s3, Client.class).get();
     }
 }
diff --git a/src/main/org/apache/ant/s3/S3DataType.java b/src/main/org/apache/ant/s3/S3DataType.java
index e7a2bd9..f2c38f5 100644
--- a/src/main/org/apache/ant/s3/S3DataType.java
+++ b/src/main/org/apache/ant/s3/S3DataType.java
@@ -16,19 +16,15 @@
  */
 package org.apache.ant.s3;
 
-import static org.apache.ant.s3.ProjectUtils.buildExceptionAt;
-
 import java.util.Formatter;
-import java.util.function.Function;
 
-import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.DataType;
 
 /**
  * S3 {@link DataType}.
  */
-public abstract class S3DataType extends DataType {
+public abstract class S3DataType extends DataType implements ProjectUtils {
     /**
      * Create a new {@link S3DataType} instance.
      *
@@ -61,14 +57,4 @@ public abstract class S3DataType extends DataType {
     protected void log(final int level, final String format, final Object... args) {
         log(String.format(format, args), level);
     }
-
-    /**
-     * Get a {@link Function} capable of creating a {@link BuildException} from
-     * its message on behalf of this {@link S3DataType}.
-     *
-     * @return {@link Function}
-     */
-    protected Function<String, BuildException> buildException() {
-        return buildExceptionAt(getLocation());
-    }
 }