-
Notifications
You must be signed in to change notification settings - Fork 465
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
WFCORE-7065 Migrate JBoss Modules module identifier validator from org.wildfly:wildfly-clustering-common #6257
Merged
+77
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...ller/src/main/java/org/jboss/as/controller/operations/validation/ModuleNameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.jboss.as.controller.operations.validation; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jboss.as.controller.OperationFailedException; | ||
import org.jboss.as.controller.logging.ControllerLogger; | ||
import org.jboss.dmr.ModelNode; | ||
import org.jboss.dmr.ModelType; | ||
|
||
/** | ||
* Validates that a given parameter is a syntactically valid module name within JBoss Modules. | ||
* N.B. This does not validate that the module actually exists, i.e. can be loaded. | ||
*/ | ||
public class ModuleNameValidator extends ModelTypeValidator { | ||
public static final ParameterValidator INSTANCE = new ModuleNameValidator(); | ||
// Ensure module name is valid with filesystem module repository, permitting deprecated slot, if present | ||
private static final Predicate<String> MODULE_NAME_TESTER = Pattern.compile("(?:^\\w+|\\w+\\.\\w+|\\w+\\Q\\:\\E\\w+)+(?:\\:(?:\\w+|\\w+\\.\\w+))?$").asMatchPredicate(); | ||
|
||
private ModuleNameValidator() { | ||
super(ModelType.STRING); | ||
} | ||
|
||
@Override | ||
public void validateParameter(String parameterName, ModelNode value) throws OperationFailedException { | ||
super.validateParameter(parameterName, value); | ||
if (value.isDefined()) { | ||
String moduleName = value.asString(); | ||
if (!MODULE_NAME_TESTER.test(moduleName)) { | ||
throw ControllerLogger.MGMT_OP_LOGGER.invalidModuleNameParameter(parameterName, moduleName); | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/test/java/org/jboss/as/controller/operation/validation/ModuleNameValidatorTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright The WildFly Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.jboss.as.controller.operation.validation; | ||
|
||
import org.jboss.as.controller.OperationFailedException; | ||
import org.jboss.as.controller.operations.validation.ModuleNameValidator; | ||
import org.jboss.as.controller.operations.validation.ParameterValidator; | ||
import org.jboss.dmr.ModelNode; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for ModuleNameValidator | ||
*/ | ||
public class ModuleNameValidatorTestCase { | ||
|
||
@Test | ||
public void test() throws OperationFailedException { | ||
ParameterValidator validator = ModuleNameValidator.INSTANCE; | ||
|
||
validator.validateParameter("valid", new ModelNode("org.jboss.modules")); | ||
validator.validateParameter("valid", new ModelNode("org.jboss.modules:main")); | ||
validator.validateParameter("valid", new ModelNode("org.jboss.modules:1.9")); | ||
validator.validateParameter("escaped", new ModelNode("org.jboss.modules.foo\\:bar:main")); | ||
|
||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode(".foo.bar"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo..bar"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar."))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo.bar:"))); | ||
Assert.assertThrows(OperationFailedException.class, () -> validator.validateParameter("invalid", new ModelNode("foo:bar:main"))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@ropalka Does the JBoss Module expose any API to validate a module identifier? Maybe it could be useful to have something there