Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
package io.avaje.validation.http;

import io.avaje.inject.BeanScopeBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

import io.avaje.http.api.Validator;
import io.avaje.inject.BeanScopeBuilder;

/**
* Plugin for avaje inject that provides a default Http Validator instance.
*/
public final class HttpValidatorProvider implements io.avaje.inject.spi.InjectPlugin {

private static final Class<?> VALIDATOR_HTTP_CLASS = avajeHttpOnClasspath();

private static Class<?> avajeHttpOnClasspath() {
try {
return Class.forName("io.avaje.http.api.Validator");
} catch (ClassNotFoundException e) {
return null;
}
private static final boolean WIRE_VALIDATOR = avajeHttpOnClasspath();

private static boolean avajeHttpOnClasspath() {
var modules = ModuleLayer.boot();
return modules
.findModule("io.avaje.validation.http")
.map(m -> modules.findModule("io.avaje.http.api").isPresent())
.orElseGet(() -> {
try {
return Validator.class != null;
} catch (NoClassDefFoundError e) {
return false;
}
});
}

@Override
public Class<?>[] provides() {
return VALIDATOR_HTTP_CLASS == null ? new Class<?>[]{} : new Class<?>[]{VALIDATOR_HTTP_CLASS};
return WIRE_VALIDATOR ? new Class<?>[] {Validator.class} : new Class<?>[] {};
}

@Override
public void apply(BeanScopeBuilder builder) {
if (VALIDATOR_HTTP_CLASS == null) {
if (!WIRE_VALIDATOR) {
return;
}

builder.provideDefault(null, VALIDATOR_HTTP_CLASS, () -> {
builder.provideDefault(null, Validator.class, () -> {
final var props = builder.configPlugin();
final var locales = new ArrayList<Locale>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Locale;

import io.avaje.inject.BeanScopeBuilder;
import io.avaje.inject.aop.Aspect;
import io.avaje.inject.aop.AspectProvider;
import io.avaje.inject.spi.GenericType;
import io.avaje.inject.spi.InjectPlugin;
Expand All @@ -22,12 +23,17 @@ public final class DefaultValidatorProvider implements InjectPlugin {
private static final boolean WIRE_ASPECTS = aspectsOnClasspath();

private static boolean aspectsOnClasspath() {
try {
Class.forName("io.avaje.inject.aop.Aspect");
return true;
} catch (ClassNotFoundException e) {
return false;
}
var modules = ModuleLayer.boot();
return modules
.findModule("io.avaje.validation.plugin")
.map(m -> modules.findModule("io.avaje.inject.aop").isPresent())
.orElseGet(() -> {
try {
return Aspect.class != null;
} catch (NoClassDefFoundError e) {
return false;
}
});
}

@Override
Expand Down