-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hibernate-validator: better support for custom ConstraintValidatorFac…
…tory - simplify custom ConstraintValidatorFactory based on DI as well as manually created factories - integrates Validator factory into hibernate - fix #3595
- Loading branch information
Showing
5 changed files
with
199 additions
and
72 deletions.
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
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
70 changes: 70 additions & 0 deletions
70
.../main/java/io/jooby/internal/hibernate/validator/CompositeConstraintValidatorFactory.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,70 @@ | ||
/* | ||
* Jooby https://jooby.io | ||
* Apache License Version 2.0 https://jooby.io/LICENSE.txt | ||
* Copyright 2014 Edgar Espina | ||
*/ | ||
package io.jooby.internal.hibernate.validator; | ||
|
||
import java.util.Deque; | ||
import java.util.LinkedList; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import io.jooby.Jooby; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorFactory; | ||
|
||
public class CompositeConstraintValidatorFactory implements ConstraintValidatorFactory { | ||
private final Logger log; | ||
private final ConstraintValidatorFactory defaultFactory; | ||
private final Deque<ConstraintValidatorFactory> factories = new LinkedList<>(); | ||
|
||
public CompositeConstraintValidatorFactory( | ||
Jooby registry, ConstraintValidatorFactory defaultFactory) { | ||
this.log = registry.getLog(); | ||
this.defaultFactory = defaultFactory; | ||
this.factories.addLast(new RegistryConstraintValidatorFactory(registry)); | ||
} | ||
|
||
public ConstraintValidatorFactory add(ConstraintValidatorFactory factory) { | ||
this.factories.addFirst(factory); | ||
return this; | ||
} | ||
|
||
@Override | ||
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { | ||
if (isBuiltIn(key)) { | ||
// use default factory for built-in constraint validators | ||
return defaultFactory.getInstance(key); | ||
} else { | ||
for (var factory : factories) { | ||
var instance = factory.getInstance(key); | ||
if (instance != null) { | ||
return instance; | ||
} | ||
} | ||
// fallback or fail | ||
return defaultFactory.getInstance(key); | ||
} | ||
} | ||
|
||
@Override | ||
public void releaseInstance(ConstraintValidator<?, ?> instance) { | ||
if (isBuiltIn(instance.getClass())) { | ||
defaultFactory.releaseInstance(instance); | ||
} else { | ||
if (instance instanceof AutoCloseable closeable) { | ||
try { | ||
closeable.close(); | ||
} catch (Exception e) { | ||
log.debug("Failed to release constraint", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isBuiltIn(Class<?> key) { | ||
var name = key.getName(); | ||
return name.startsWith("org.hibernate.validator") || name.startsWith("jakarta.validation"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...c/main/java/io/jooby/internal/hibernate/validator/RegistryConstraintValidatorFactory.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,43 @@ | ||
/* | ||
* Jooby https://jooby.io | ||
* Apache License Version 2.0 https://jooby.io/LICENSE.txt | ||
* Copyright 2014 Edgar Espina | ||
*/ | ||
package io.jooby.internal.hibernate.validator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.jooby.Registry; | ||
import io.jooby.exception.RegistryException; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorFactory; | ||
|
||
public class RegistryConstraintValidatorFactory implements ConstraintValidatorFactory { | ||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
private final Registry registry; | ||
|
||
public RegistryConstraintValidatorFactory(Registry registry) { | ||
this.registry = registry; | ||
} | ||
|
||
@Override | ||
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { | ||
try { | ||
return registry.require(key); | ||
} catch (RegistryException notfound) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void releaseInstance(ConstraintValidator<?, ?> instance) { | ||
if (instance instanceof AutoCloseable closeable) { | ||
try { | ||
closeable.close(); | ||
} catch (Exception e) { | ||
log.debug("Failed to release constraint", e); | ||
} | ||
} | ||
} | ||
} |
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