Skip to content

Commit c65db78

Browse files
committed
Use reflection for DefaultLocaleResolver to avoid internal HV import
The DefaultLocaleResolver is in org.hibernate.validator.internal which is not exported by the HV bundle. Use reflection to instantiate it via HV's classloader, avoiding a direct package import that would fail OSGi feature resolution.
1 parent 20cea10 commit c65db78

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/ValidatorFactories.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.apache.camel.support.CamelContextHelper;
3232
import org.hibernate.validator.HibernateValidator;
3333
import org.hibernate.validator.HibernateValidatorConfiguration;
34-
import org.hibernate.validator.internal.engine.messageinterpolation.DefaultLocaleResolver;
3534
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
35+
import org.hibernate.validator.spi.messageinterpolation.LocaleResolver;
3636

3737
/**
3838
* OSGi-aware override of the upstream ValidatorFactories.
@@ -94,7 +94,7 @@ public static ValidatorFactory buildValidatorFactory(
9494
// ExpressionFactory parameter and calls buildExpressionFactory().
9595
messageInterpolator = new ResourceBundleMessageInterpolator(
9696
null, Collections.emptySet(), Locale.getDefault(),
97-
new DefaultLocaleResolver(), true, false, ef);
97+
createDefaultLocaleResolver(hvCl), true, false, ef);
9898
}
9999
}
100100

@@ -126,4 +126,18 @@ private static ExpressionFactory createExpressionFactory(ClassLoader hvCl) {
126126
return null;
127127
}
128128
}
129+
130+
/**
131+
* Create the HV DefaultLocaleResolver via reflection to avoid importing
132+
* the internal package which is not exported by the HV bundle.
133+
*/
134+
private static LocaleResolver createDefaultLocaleResolver(ClassLoader hvCl) {
135+
try {
136+
Class<?> clazz = hvCl.loadClass(
137+
"org.hibernate.validator.internal.engine.messageinterpolation.DefaultLocaleResolver");
138+
return (LocaleResolver) clazz.getDeclaredConstructor().newInstance();
139+
} catch (Exception e) {
140+
return null;
141+
}
142+
}
129143
}

0 commit comments

Comments
 (0)