Skip to content
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

Integrate Java Bean validation so it works out of the box on the persistence path #4444

Open
remmeier opened this issue Dec 19, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@remmeier
Copy link
Contributor

commented Dec 19, 2018

We bundle Java Bean validation with one of our Cordapps. This used to work fine but we are now running into issues with the current master branch and upcoming Corda 4.0:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
	at java.lang.Class.getMethod0(Class.java:3018)
	at java.lang.Class.getMethod(Class.java:1784)
	at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:107)
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:276)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration.buildSessionFactory(HibernateConfiguration.kt:142)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration.makeSessionFactoryForSchemas(HibernateConfiguration.kt:100)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration.access$makeSessionFactoryForSchemas(HibernateConfiguration.kt:29)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration$sessionFactoryForSchemas$1.invoke(HibernateConfiguration.kt:70)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration$sessionFactoryForSchemas$1.invoke(HibernateConfiguration.kt:29)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration$sam$java_util_function_Function$0.apply(HibernateConfiguration.kt)
	at com.github.benmanes.caffeine.cache.BoundedLocalCache.lambda$doComputeIfAbsent$14(BoundedLocalCache.java:2039)
	at java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1853)
	at com.github.benmanes.caffeine.cache.BoundedLocalCache.doComputeIfAbsent(BoundedLocalCache.java:2037)
	at com.github.benmanes.caffeine.cache.BoundedLocalCache.computeIfAbsent(BoundedLocalCache.java:2020)
	at com.github.benmanes.caffeine.cache.LocalCache.computeIfAbsent(LocalCache.java:112)
	at com.github.benmanes.caffeine.cache.LocalManualCache.get(LocalManualCache.java:54)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration.sessionFactoryForSchemas(HibernateConfiguration.kt:70)
	at net.corda.nodeapi.internal.persistence.HibernateConfiguration.<init>(HibernateConfiguration.kt:66)
	at net.corda.nodeapi.internal.persistence.CordaPersistence$hibernateConfig$2$1.invoke(CordaPersistence.kt:82)
	at net.corda.nodeapi.internal.persistence.CordaPersistence$hibernateConfig$2$1.invoke(CordaPersistence.kt:67)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:161)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:141)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:147)
	at net.corda.nodeapi.internal.persistence.CordaPersistence$hibernateConfig$2.invoke(CordaPersistence.kt:81)
	at net.corda.nodeapi.internal.persistence.CordaPersistence$hibernateConfig$2.invoke(CordaPersistence.kt:67)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.getHibernateConfig(CordaPersistence.kt)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.getEntityManagerFactory(CordaPersistence.kt:86)
	at net.corda.nodeapi.internal.persistence.DatabaseTransaction$sessionDelegate$1.invoke(DatabaseTransaction.kt:37)
	at net.corda.nodeapi.internal.persistence.DatabaseTransaction$sessionDelegate$1.invoke(DatabaseTransaction.kt:19)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at net.corda.nodeapi.internal.persistence.DatabaseTransaction.getSession(DatabaseTransaction.kt)
	at net.corda.node.services.network.PersistentNetworkMapCache$getNodesByLegalName$1.invoke(PersistentNetworkMapCache.kt:123)
	at net.corda.node.services.network.PersistentNetworkMapCache$getNodesByLegalName$1.invoke(PersistentNetworkMapCache.kt:40)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:161)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:141)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:147)
	at net.corda.node.services.network.PersistentNetworkMapCache.getNodesByLegalName(PersistentNetworkMapCache.kt:123)
	at net.corda.node.internal.AbstractNode.getPreviousNodeInfoIfPresent(AbstractNode.kt:484)
	at net.corda.node.internal.AbstractNode.updateNodeInfo(AbstractNode.kt:452)
	at net.corda.node.internal.AbstractNode.access$updateNodeInfo(AbstractNode.kt:119)
	at net.corda.node.internal.AbstractNode$start$5.invoke(AbstractNode.kt:344)
	at net.corda.node.internal.AbstractNode$start$5.invoke(AbstractNode.kt:119)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.inTopLevelTransaction(CordaPersistence.kt:178)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:163)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:141)
	at net.corda.nodeapi.internal.persistence.CordaPersistence.transaction(CordaPersistence.kt:147)
	at net.corda.node.internal.AbstractNode.start(AbstractNode.kt:343)
	at net.corda.node.internal.Node.start(Node.kt:413)
	at net.corda.node.internal.NodeStartup.startNode(NodeStartup.kt:176)
	at net.corda.node.internal.NodeStartupCli$runProgram$2.run(NodeStartup.kt:105)
	at net.corda.node.internal.NodeStartup$initialiseAndRun$5.invoke(NodeStartup.kt:157)
	at net.corda.node.internal.NodeStartup$initialiseAndRun$5.invoke(NodeStartup.kt:112)
	at net.corda.node.internal.NodeStartupLogging$DefaultImpls.attempt(NodeStartup.kt:439)
	at net.corda.node.internal.NodeStartup.attempt(NodeStartup.kt:112)
	at net.corda.node.internal.NodeStartup.initialiseAndRun(NodeStartup.kt:155)
	at net.corda.node.internal.NodeStartupCli.runProgram(NodeStartup.kt:103)
	at net.corda.cliutils.CordaCliWrapper.call(CordaCliWrapper.kt:184)
	at net.corda.cliutils.CordaCliWrapper.call(CordaCliWrapper.kt:152)
	at picocli.CommandLine.execute(CommandLine.java:1056)
	at picocli.CommandLine.access$900(CommandLine.java:142)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:1246)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:1214)
	at picocli.CommandLine$AbstractParseResultHandler.handleParseResult(CommandLine.java:1122)
	at picocli.CommandLine.parseWithHandlers(CommandLine.java:1405)
	at net.corda.cliutils.CordaCliWrapperKt.start(CordaCliWrapper.kt:72)
	at net.corda.node.Corda.main(Corda.kt:13)
Caused by: java.lang.ClassNotFoundException: javax.validation.ValidatorFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 71 more

I have not so far fully understood what is going on in the class loader area. For some strange reason BeanValidationIntegrator.isBeanValidationApiAvailable returns true, but later it is not able to fully load bean validation with the above error. What helped for us was adding:

  .setProperty("javax.persistence.validation.mode", "NONE")

to HibernateConfiguration.kd to turn off bean validation within the Corda Hibernate instance altogether.

Maybe bean validation is something that might be one day officially supported. Short-term this flag could be helpful for people. Or maybe rather a more general mechanism to set any Hibernate property.

It could also be interesting to have validation support natively built into Corda since it is probably quite a common topic.

Our validator looks like:


import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import lombok.NonNull;
import lombok.experimental.UtilityClass;
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;


@UtilityClass
public class ValidationUtil {

	private Validator validator;

	@NonNull
	public static Validator validator() {
		if (validator == null) {
			validator = Validation.byDefaultProvider()
					.configure()
					.messageInterpolator(new ParameterMessageInterpolator()) // without EL
					.buildValidatorFactory()
					.getValidator();
		}
		return validator;
	}

	@NonNull
	public static <T> T validate(@NonNull T bean, String context) throws ConstraintViolationFlowException {
		Set<ConstraintViolation<Object>> violations = validator().validate(bean);
		if (!violations.isEmpty()) {
			throw new ConstraintViolationFlowException(context + ", bean=" + bean + ", violations=" + violations, violations);
		}
		return b

and we make use of this dependency:

    compile 'org.hibernate:hibernate-validator:6.0.13.Final'
@shamsasari

This comment has been minimized.

@shamsasari

This comment has been minimized.

Copy link
Member

commented Dec 20, 2018

@remmeier It seems the issue steps from the fact that CorDapps are no longer added to the node's system classpath. Hibernate Validator automatically does its thing if it is. From https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-integration.html#validator-checkconstraints-db:

If Hibernate Validator is present in the classpath, Hibernate Annotations (or Hibernate EntityManager) will use it transparently. To avoid validation even though Hibernate Validator is in the classpath set javax.persistence.validation.mode to none.

The node has a jarDirs config where you can augment the node's classpath with extra Jars. Can you give that a go and let us know if it works. https://docs.corda.net/head/corda-configuration-file.html?highlight=jardirs

Thanks.

@shamsasari shamsasari self-assigned this Dec 20, 2018

@tudor-malene

This comment has been minimized.

Copy link
Contributor

commented Dec 20, 2018

To answer your question as to why although BeanValidationIntegrator.isBeanValidationApiAvailable returns true, it is later not able to fully load bean validation.

It seems to be a bug in the BeanValidationIntegrator.

It assumes that validation-api needs to be on the system classloader (where hibernate is).

These lines here:

final Class activatorClass = BeanValidationIntegrator.class.getClassLoader().loadClass( ACTIVATOR_CLASS_NAME );
try {
   final Method validateMethod = activatorClass.getMethod( VALIDATE_SUPPLIED_FACTORY_METHOD_NAME, Object.class );

While BeanValidationIntegrator.isBeanValidationApiAvailable is checked against the classLoaderService, which we customise to be the classloader where the cordapps live.

Probably worth raising with hibernate.

@shamsasari

This comment has been minimized.

Copy link
Member

commented Jan 21, 2019

Updating node.conf with
jarDirs = [ ${baseDirectory}"/libs" ]
and putting the validation-api jar into that directory resolves this issue.

@shamsasari shamsasari closed this Jan 21, 2019

@mikehearn

This comment has been minimized.

Copy link
Contributor

commented May 7, 2019

This came up again with a different user, I'll re-open this and re-target it to mean integrating HV out of the box.

@mikehearn mikehearn reopened this May 7, 2019

@mikehearn mikehearn changed the title Support Java Bean Validation Integrate Java Bean validation so it works out of the box on the persistence path May 7, 2019

@shamsasari

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.