Using Hibernate Validator in Karaf

Hello I am trying to write an example using Hibernate Validation in Karaf but I am having trouble with some dependency errors.

What I did so far is download a fresh version of Karaf (version 4.1.4). Start this and enter the command “feature:install hibernate-validator”. My understanding of Karaf is that this should download hibernate validator and all its dependencies. When I do “bundle:list” I can see the bundles installed by the feature and they are all marked as active.

Next I deploy my application, which at the moment consists of one class with the following code;

class HibernateValidationProviderResolver implements ValidationProviderResolver {  
    @Override  
    public List getValidationProviders() {  
        List providers = new ArrayList(1);  
        providers.add(new HibernateValidator());  
        return providers;  
    }  
} 
public class App {
    public App(){
        System.out.println("(Ex HV App) Start.");
        ValidatorFactory factory = Validation.byDefaultProvider().providerResolver(
            new HibernateValidationProviderResolver()).configure().buildValidatorFactory();
        Validator validator = factory.getValidator();
        System.out.println("(Ex HV App) Ready.");
    }
}

(note, I use blueprint to create an instance of the class)

At the moment in the META-INF of my project I put DynamicImport-Package: * rather than list all packages under Import-Packages as I want to make sure my issue is not related to my bundle missing a dependency.

DynamicImport-Package: *

I next proceed to install my example bundle “bundle:install –s mvn:example/app/0.1.0”

However when I do so I get the following error:

Caused by: java.lang.ClassNotFoundException: 
com.sun.el.ExpressionFactoryImpl not found by org.hibernate.validator [70]

My understanding is that Hibernate Validator is starting but then fails because it cannot find the Expression Language dependency. That said, if I do bundle:list I can see that the feature:install done previously has install the expression language bundle as I can see the following;

66 │Active│80│ 2.2.4 │ Expression Language API 2.2
69 │Active│80│ 2.2.4 │ Expression Language 2.2 Implementation

Can someone help me understand what I am doing wrong?

Hi @sim085,

From what I have seen in the Karaf repository, they are still using HIbernate Validator 5.4. We fixed an issue regarding javax.el and OSGi during the 6.0.x lifecycle which looks a lot like your issue. You could work around the issue by using the externalClassLoader() trick and push your application class loader but I would advise you to upgrade: HV 6 is faster and should fix your issue.

It’s compatible with HV 5.4 so you shouldn’t have any compatibility issue if it’s an existing application.

Could you try to install the feature we published here:
http://search.maven.org/remotecontent?filepath=org/hibernate/validator/hibernate-validator-osgi-karaf-features/6.0.8.Final/hibernate-validator-osgi-karaf-features-6.0.8.Final-features.xml
(Karaf uses our feature file so it’s just an upgrade compared to installing it from Karaf)

HTH.

Answering to myself, looks like we have some issue installing our feature on a vanilla Karaf. So maybe you’ll be lucky (we are in our integration tests, don’t know why) but when I tried upgrading Karaf to 6.0.8.Final, I got the following error:

EnterpriseFeaturesTest: installHibernateValidatorFeature
Installing [hibernate-validator]
org.osgi.service.resolver.ResolutionException: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=hibernate-validator; type=karaf.feature; version="[6.0.8.Final,6.0.8.Final]"; filter:="(&(osgi.identity=hibernate-validator)(type=karaf.feature)(version>=6.0.8.Final)(version<=6.0.8.Final))" [caused by: Unable to resolve hibernate-validator/6.0.8.Final: missing requirement [hibernate-validator/6.0.8.Final] osgi.identity; osgi.identity=org.javamoney.moneta; type=osgi.bundle; version="[1.1.0,1.1.0]"; resolution:=mandatory [caused by: Unable to resolve org.javamoney.moneta/1.1.0: missing requirement [org.javamoney.moneta/1.1.0] osgi.wiring.package; filter:="(&(osgi.wiring.package=javax.annotation)(version>=1.2.0)(!(version>=2.0.0)))"]]

I’m working on fixing that, it will be fixed in our next micro.

Hi gsmet, I have manually installed the bundles listed in the linked feature file and can confirm that it works. Thanks for your help.

OK, cool, thanks for confirming.

I have made a few changes to our Karaf features for the upcoming 6.0.9.Final and I will submit it for inclusion into Karaf proper once it’s released.

Hello again, I am not sure if this is an issue with hibernate validator but thought best to report this here. So as explained using the bundles in the linked feature file does work.

However if the bundle “mvn:javax.el/javax.el-api/3.0.0” is already installed then the following error comes out:

Caused by: javax.validation.ValidationException: HV000183: 
Unable to initialize 'javax.el.ExpressionFactory'. 
Check that you have the EL dependencies on the classpath, 
or use ParameterMessageInterpolator instead

If bundle “mvn:javax.el/javax.el-api/3.0.0” is installed after the hibernate validator bundles then everything still works. However this means that for example the hibernate validator feature would always needs to be installed before the open jpa feature because this latter contains the bundle “mvn:javax.el/javax.el-api/3.0.0”

For me it is not a problem to change the order of how features/bundles are installed. However would like to know what causes this issue.

Yes, this can happen if you have 2 conflicting jars containing the javax.el dependency.

We are using the latest version, OpenJPA should migrate to the same jar we use.

I opened a PR to upgrade Hibernate Validator in Karaf: https://github.com/apache/karaf/pull/482.

Just take a look at our Karaf integration tests for the 5.4 branch:

https://github.com/hibernate/hibernate-validator/tree/5.4/osgi/integrationtest/mybkexperience

We also use Pax Exam. You should be able to make it work if you follow what we did.

BTW, I see you mention the annotation processor in your dependency for the 2nd time, it’s not something you need at runtime. The annotation processor is used to check that the annotations you used make sense at compile time. You should only enable it when you build your project.