-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HDDS-11981. Add annotation for registering feature validator based on a generic version #7603
base: master
Are you sure you want to change the base?
Changes from all commits
3770c71
d56af1f
35c84f7
aba274a
a499c41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* 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.ozone.annotations; | ||
|
||
import java.util.Set; | ||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.annotation.processing.SupportedSourceVersion; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ElementKind; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.ArrayType; | ||
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import javax.tools.Diagnostic; | ||
|
||
/** | ||
* This class is an annotation processor that is hooked into the java compiler | ||
* and is used to validate the RegisterValidator annotations in the | ||
* codebase, to ensure that the annotated classes have the proper methods returning appropriate object types. | ||
* | ||
* The module is compiled in a different execution via Maven before anything | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The preprocessor apidoc change for the OmRequestFeatureValidatorProcessor should be applied here also. |
||
* else is compiled, and then javac picks this class up as an annotation | ||
* processor from the classpath via a ServiceLoader, based on the | ||
* META-INF/services/javax.annotation.processing.Processor file in the module's | ||
* resources folder. | ||
*/ | ||
@SupportedAnnotationTypes("org.apache.hadoop.ozone.request.validation.RegisterValidator") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
public class RegisterValidatorProcessor extends AbstractProcessor { | ||
|
||
public static final String ANNOTATION_SIMPLE_NAME = "RegisterValidator"; | ||
public static final String VERSION_CLASS_NAME = "org.apache.hadoop.ozone.Version"; | ||
public static final String REQUEST_PROCESSING_PHASE_CLASS_NAME = "org.apache.hadoop.ozone.om.request.validation" + | ||
".RequestProcessingPhase"; | ||
public static final String APPLY_BEFORE_METHOD_NAME = "applyBefore"; | ||
public static final String REQUEST_TYPE_METHOD_NAME = "requestType"; | ||
public static final String PROCESSING_PHASE_METHOD_NAME = "processingPhase"; | ||
|
||
public static final String MAX_VERSION_NOT_FOUND_ERROR_MESSAGE = "Method " + APPLY_BEFORE_METHOD_NAME + | ||
" returning an enum implementing " + VERSION_CLASS_NAME + " not found"; | ||
public static final String REQUEST_TYPE_NOT_FOUND_ERROR_MESSAGE = "Method " + REQUEST_TYPE_METHOD_NAME + | ||
" returning an enum not found"; | ||
public static final String PROCESSING_PHASE_NOT_FOUND_ERROR_MESSAGE = "Method " + PROCESSING_PHASE_METHOD_NAME | ||
+ " returning an enum implementing " + REQUEST_PROCESSING_PHASE_CLASS_NAME + " not found"; | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
for (TypeElement annotation : annotations) { | ||
if (!annotation.getSimpleName().contentEquals(ANNOTATION_SIMPLE_NAME)) { | ||
continue; | ||
} | ||
processElements(roundEnv.getElementsAnnotatedWith(annotation)); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean validateArrayMethod(ExecutableElement method, String expectedMethodName, | ||
ElementKind expectedReturnType, | ||
String expectedReturnClass) { | ||
Elements elementUtils = processingEnv.getElementUtils(); | ||
Types types = processingEnv.getTypeUtils(); | ||
TypeElement expectedReturnInterface = expectedReturnClass == null || expectedReturnClass.equals("") ? null : | ||
elementUtils.getTypeElement(expectedReturnClass); | ||
return method.getSimpleName().toString().equals(expectedMethodName) && (expectedReturnType == null || | ||
TypeKind.ARRAY.equals(method.getReturnType().getKind()) && | ||
types.asElement(((ArrayType)method.getReturnType()).getComponentType()).getKind() == expectedReturnType) && | ||
(expectedReturnInterface == null || | ||
types.isAssignable(types.asElement(method.getReturnType()).asType(), expectedReturnInterface.asType())); | ||
} | ||
|
||
private boolean validateMethod(ExecutableElement method, String expectedMethodName, ElementKind expectedReturnType, | ||
String expectedReturnClass) { | ||
Elements elementUtils = processingEnv.getElementUtils(); | ||
Types types = processingEnv.getTypeUtils(); | ||
TypeElement expectedReturnInterface = expectedReturnClass == null || expectedReturnClass.equals("") ? null : | ||
elementUtils.getTypeElement(expectedReturnClass); | ||
return method.getSimpleName().toString().equals(expectedMethodName) && (expectedReturnType == null || | ||
types.asElement(method.getReturnType()) != null && | ||
types.asElement(method.getReturnType()).getKind() == expectedReturnType) && | ||
(expectedReturnInterface == null || | ||
types.isAssignable(types.asElement(method.getReturnType()).asType(), expectedReturnInterface.asType())); | ||
} | ||
|
||
private void processElements(Set<? extends Element> annotatedElements) { | ||
for (Element element : annotatedElements) { | ||
if (element.getKind().equals(ElementKind.ANNOTATION_TYPE)) { | ||
boolean hasApplyBeforeMethod = false; | ||
boolean hasRequestType = false; | ||
boolean hasRequestProcessPhase = false; | ||
for (Element enclosedElement : element.getEnclosedElements()) { | ||
// Check if the annotation has a method called "validatorName" returning a String | ||
if (enclosedElement instanceof ExecutableElement) { | ||
ExecutableElement method = (ExecutableElement) enclosedElement; | ||
hasApplyBeforeMethod = hasApplyBeforeMethod || validateMethod(method, APPLY_BEFORE_METHOD_NAME, | ||
ElementKind.ENUM, VERSION_CLASS_NAME); | ||
hasRequestType = hasRequestType || validateArrayMethod(method, REQUEST_TYPE_METHOD_NAME, ElementKind.ENUM, | ||
null); | ||
hasRequestProcessPhase = hasRequestProcessPhase || validateMethod(method, PROCESSING_PHASE_METHOD_NAME, | ||
ElementKind.ENUM, REQUEST_PROCESSING_PHASE_CLASS_NAME); | ||
} | ||
} | ||
if (!hasApplyBeforeMethod) { | ||
emitErrorMsg(MAX_VERSION_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
element.getSimpleName().toString()); | ||
} | ||
if (!hasRequestType) { | ||
emitErrorMsg(REQUEST_TYPE_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
element.getSimpleName().toString()); | ||
} | ||
if (!hasRequestProcessPhase) { | ||
emitErrorMsg(PROCESSING_PHASE_NOT_FOUND_ERROR_MESSAGE + " for " + | ||
element.getSimpleName().toString()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private void emitErrorMsg(String s) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, s); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.hadoop.ozone.request.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.apache.hadoop.ozone.Versioned; | ||
|
||
/** | ||
* Annotations to register a validator. {@link org.apache.ozone.annotations.RegisterValidatorProcessor} | ||
* enforces other annotation to have the following methods: | ||
* applyBefore : Returns an enum which implement {@link Versioned} | ||
* requestType: Returns an Enum value. | ||
* processingPhase: Returns {@link RequestProcessingPhase} | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.ANNOTATION_TYPE) | ||
public @interface RegisterValidator { | ||
String APPLY_BEFORE_METHOD_NAME = "applyBefore"; | ||
String REQUEST_TYPE_METHOD_NAME = "requestType"; | ||
String PROCESSING_PHASE_METHOD_NAME = "processingPhase"; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||
* 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. | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Request's validation handling. | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* This package holds facilities to add new situation specific behaviour to | ||||||||||||||||||||||||||||||||||||
* request handling without cluttering the basic logic of the request handler | ||||||||||||||||||||||||||||||||||||
* code for any server. | ||||||||||||||||||||||||||||||||||||
* {@link org.apache.hadoop.ozone.request.validation.RegisterValidator} | ||||||||||||||||||||||||||||||||||||
* is used to register any validator which has the following methods: | ||||||||||||||||||||||||||||||||||||
* - applyBefore : Returns an enum which implement {@link org.apache.hadoop.ozone.Versioned} | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
* - requestType: Returns an Enum value. | ||||||||||||||||||||||||||||||||||||
* - processingPhase: Returns {@link org.apache.hadoop.ozone.request.validation.RequestProcessingPhase} | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* The system uses a reflection based discovery to find methods that are | ||||||||||||||||||||||||||||||||||||
* annotated with the | ||||||||||||||||||||||||||||||||||||
* {@link org.apache.hadoop.ozone.request.validation.RegisterValidator} | ||||||||||||||||||||||||||||||||||||
* annotation. | ||||||||||||||||||||||||||||||||||||
* This annotation is used to register a particular annotation which inturn would be used to specify conditions in | ||||||||||||||||||||||||||||||||||||
* which a certain validator has to be used, the request type to which the validation should be applied, | ||||||||||||||||||||||||||||||||||||
* and the request processing phase in which we apply the validation and the maxVersion corresponding to which this | ||||||||||||||||||||||||||||||||||||
* is supposed to run. | ||||||||||||||||||||||||||||||||||||
Comment on lines
+30
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
package org.apache.hadoop.ozone.request.validation; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not related to this PR directly, but as we adding a new annotation preprocessor and the second part of the class api doc is copied to the new class, let's address the earlier change in the build in the class' api doc please.
In the current build, the annotation processors are directly specified to only those modules where the annotation is intended to be used, and it happens with the general build. This is also visible in the hadoop-ozone/ozone-manager/pom.xml change. So there is no ServiceLoader in action anymore, and these annotation preprocessors along with the actual enablement of the use of the related annotation is controlled by the pom.xml of all individual modules, while in the project pom it is disabled for all modules that does not enable it.