C0urante commented on code in PR #14304:
URL: https://github.com/apache/kafka/pull/14304#discussion_r1316306495


##########
clients/src/main/java/org/apache/kafka/common/utils/Utils.java:
##########
@@ -1090,6 +1090,23 @@ public interface UncheckedCloseable extends 
AutoCloseable {
         void close();
     }
 
+    /**
+     * Closes {@code maybeCloseable} if it implements the {@link 
AutoCloseable} interface,
+     * and if an exception is thrown, it is logged at the WARN level.
+     * <b>Be cautious when passing method references as an argument.</b> For 
example:
+     * <p>
+     * {@code closeQuietly(task::stop, "source task");}
+     * <p>
+     * Although this method gracefully handles null {@link AutoCloseable} 
objects, attempts to take a method

Review Comment:
   🤦 Thanks, I had a feeling I was missing something.
   
   Considering the utility of this method is that it's type-blind, I'm not sure 
we need to worry about it being invoked with a method reference. What are the 
odds that a method reference returns something that a dev, at compile time, 
isn't sure is `AutoCloseable` or not?
   
   I've tentatively removed all of the language around method references from 
this section. If you think it's better to err on the side of safety, I can 
restore it with your suggestion applied.



##########
clients/src/main/java/org/apache/kafka/common/utils/Utils.java:
##########
@@ -1090,6 +1090,23 @@ public interface UncheckedCloseable extends 
AutoCloseable {
         void close();
     }
 
+    /**
+     * Closes {@code maybeCloseable} if it implements the {@link 
AutoCloseable} interface,
+     * and if an exception is thrown, it is logged at the WARN level.
+     * <b>Be cautious when passing method references as an argument.</b> For 
example:
+     * <p>
+     * {@code closeQuietly(task::stop, "source task");}
+     * <p>
+     * Although this method gracefully handles null {@link AutoCloseable} 
objects, attempts to take a method

Review Comment:
   🤦 Thanks, I had a feeling I was missing something.
   
   Considering the utility of this method is that it's type-blind, I'm not sure 
we need to worry about it being invoked with a method reference. What are the 
odds that a method reference returns something that a dev, at compile time, 
isn't sure is `AutoCloseable` or not?
   
   I've tentatively removed all of the language around method references from 
this section. If you think it's better to err on the side of safety, I can 
restore it with your suggestion applied.



##########
connect/runtime/src/main/java/org/apache/kafka/connect/util/ConcreteSubClassValidator.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.kafka.connect.util;
+
+import org.apache.kafka.common.config.ConfigDef;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.utils.Utils;
+
+import java.lang.reflect.Modifier;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class ConcreteSubClassValidator implements ConfigDef.Validator {
+    private final Class<?> expectedSuperClass;
+
+    private ConcreteSubClassValidator(Class<?> expectedSuperClass) {
+        this.expectedSuperClass = expectedSuperClass;
+    }
+
+    public static ConcreteSubClassValidator forSuperClass(Class<?> 
expectedSuperClass) {
+        return new ConcreteSubClassValidator(expectedSuperClass);
+    }
+
+    @Override
+    public void ensureValid(String name, Object value) {
+        if (value == null) {
+            // The value will be null if the class couldn't be found; no point 
in performing follow-up validation
+            return;
+        }

Review Comment:
   Cool, filed https://issues.apache.org/jira/browse/KAFKA-15436 to track



##########
connect/runtime/src/main/java/org/apache/kafka/connect/util/InstantiableClassValidator.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.kafka.connect.util;
+
+import org.apache.kafka.common.config.ConfigDef;
+import org.apache.kafka.common.config.ConfigException;
+import org.apache.kafka.common.utils.Utils;
+
+public class InstantiableClassValidator implements ConfigDef.Validator {
+
+    @Override
+    public void ensureValid(String name, Object value) {
+        if (value == null) {
+            // The value will be null if the class couldn't be found; no point 
in performing follow-up validation
+            return;
+        }
+
+        Class<?> cls = (Class<?>) value;
+        try {
+            Object o = cls.getDeclaredConstructor().newInstance();
+            Utils.maybeCloseQuietly(o, o + " (instantiated for preflight 
validation");
+        } catch (NoSuchMethodException e) {
+            throw new ConfigException(name, cls.getName(), "Could not find a 
public no-argument constructor for class" + (e.getMessage() != null ? ": " + 
e.getMessage() : ""));
+        } catch (ReflectiveOperationException | RuntimeException e) {
+            throw new ConfigException(name, cls.getName(), "Could not 
instantiate class" + (e.getMessage() != null ? ": " + e.getMessage() : ""));
+        }

Review Comment:
   Considering the duplication is minor and it sets us up for flexibility in 
the future if we choose to expand our coverage for error messages, I'd prefer 
to keep this as-is for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to