swamirishi commented on code in PR #6932: URL: https://github.com/apache/ozone/pull/6932#discussion_r1889527668
########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/request/validation/ValidatorRegistry.java: ########## @@ -0,0 +1,278 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.ozone.request.validation; + +import com.google.common.collect.ImmutableMap; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.ozone.Version; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.TreeMap; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +/** + * Registry that loads and stores the request validators to be applied by + * a service. + */ +public class ValidatorRegistry<RequestType extends Enum<RequestType>> { + + private final Map<Class<? extends Version>, EnumMap<RequestType, + EnumMap<RequestProcessingPhase, IndexedItems<Method, Integer>>>> indexedValidatorMap; + + /** + * Creates a {@link ValidatorRegistry} instance that discovers validation + * methods in the provided package and the packages in the same resource. + * A validation method is recognized by all the annotations classes which + * are annotated by {@link RegisterValidator} annotation that contains + * important information about how and when to use the validator. + * @param validatorPackage the main package inside which validatiors should + * be discovered. + */ + public ValidatorRegistry(Class<RequestType> requestType, + String validatorPackage, + Set<Class<? extends Version>> allowedVersionTypes, + Set<RequestProcessingPhase> allowedProcessingPhases) { + this(requestType, ClasspathHelper.forPackage(validatorPackage), allowedVersionTypes, allowedProcessingPhases); + } + + private Class<?> getReturnTypeOfAnnotationMethod(Class<? extends Annotation> clzz, String methodName) { + try { + return clzz.getMethod(methodName).getReturnType(); + } catch (NoSuchMethodException e) { + throw new IllegalArgumentException("Method " + methodName + " not found in class:" + clzz.getCanonicalName()); + } + } + + /** + * Creates a {@link ValidatorRegistry} instance that discovers validation + * methods under the provided URL. + * A validation method is recognized by all annotations having the annotated by {@link RegisterValidator} + * annotation that contains important information about how and when to use + * the validator. + * @param searchUrls the path in which the annotated methods are searched. + */ + public ValidatorRegistry(Class<RequestType> requestType, + Collection<URL> searchUrls, + Set<Class<? extends Version>> allowedVersionTypes, + Set<RequestProcessingPhase> allowedProcessingPhases) { + Set<Class<? extends Annotation>> validatorsToBeRegistered = + new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("")) + .setScanners(Scanners.TypesAnnotated) + .setParallel(true)).getTypesAnnotatedWith(RegisterValidator.class).stream() + .filter(annotationClass -> getReturnTypeOfAnnotationMethod((Class<? extends Annotation>) annotationClass, + RegisterValidator.REQUEST_TYPE_METHOD_NAME) + .equals(requestType)) + .filter(annotationClass -> allowedVersionTypes.contains(getReturnTypeOfAnnotationMethod( + (Class<? extends Annotation>) annotationClass, + RegisterValidator.MAX_VERSION_METHOD_NAME))) + .map(annotationClass -> (Class<? extends Annotation>) annotationClass) Review Comment: We are checking it here: https://github.com/apache/ozone/blob/a39467057846bcf669380289f5674bb6eed9e752/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/request/validation/ValidatorRegistry.java#L251 We shouldn't silently filter out methods, without the logs we might not be able to figure out what got skipped. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
