title: El implementation is not found for hibernate-validator in osgi-container
body: <p>I try to run Hibernate validator in osgi container.</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;javax.el&lt;/groupId&gt;
      &lt;artifactId&gt;javax.el-api&lt;/artifactId&gt;
      &lt;version&gt;2.2.4&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.glassfish.web&lt;/groupId&gt;
      &lt;artifactId&gt;javax.el&lt;/artifactId&gt;
      &lt;version&gt;2.2.4&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.servicemix.bundles&lt;/groupId&gt;
      &lt;artifactId&gt;org.apache.servicemix.bundles.hibernate-validator&lt;/artifactId&gt;
      &lt;version&gt;5.0.2.Final_1&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;joda-time&lt;/groupId&gt;
      &lt;artifactId&gt;joda-time&lt;/artifactId&gt;
      &lt;version&gt;2.7&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
      &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
      &lt;version&gt;1.8.1&lt;/version&gt;
  &lt;/dependency&gt;
  
  
  public class HibernateValidationProviderResolver implements ValidationProviderResolver {
      @Override
      public List&lt;ValidationProvider&lt;?&gt;&gt; getValidationProviders() {
          List&lt;ValidationProvider&lt;?&gt;&gt; list = new ArrayList&lt;&gt;(1);
          list.add(new HibernateValidator());
          return list;
      }
  }
  
  
  Configuration&lt;?&gt; configuration = Validation.byDefaultProvider().providerResolver(
      new HibernateValidationProviderResolver()
  ).configure();
  
  ValidatorFactory validatorFactory = configuration.buildValidatorFactory();
  Validator validator = validatorFactory.getValidator();
  Set&lt;ConstraintViolation&lt;Group&gt;&gt; constraintViolations = validator.validate(group);
  
  
  public class Group {
      @NotNull
      @Size(min=2)
      private String title;
  }
  </code></pre>
  
  <p>Try to run, equinox console is okay:</p>
  
  <pre><code>10      RESOLVED    org.glassfish.web.javax.el_2.2.4
  39      RESOLVED    org.apache.servicemix.bundles.hibernate-validator_5.0.2.Final_1
  47      RESOLVED    javax.validation.api_1.1.0.Final
  49      RESOLVED    javax.el-api_2.2.4
  </code></pre>
  
  <p>If I pass Group class instance with <code>title = null</code>, then validation is okay and constraintViolations contains one violation "not null".
  If I pass Group class instance with <code>title = "A"</code> (one character against minimal length = 2), then it throws an exception</p>
  
  <pre><code>Caused by: javax.el.ELException: Provider com.sun.el.ExpressionFactoryImpl not found
  Caused by: java.lang.ClassNotFoundException: com.sun.el.ExpressionFactoryImpl
  </code></pre>
  
  <p>It 100% caused by osgi, but how I should setup hibernate-validator in osgi? All articles what I can found describes creating of <code>HibernateValidationProviderResolver</code> and that's all.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <pre><code>Maven: javax.el:javax.el-api:2.2.4
  
  Export-Package: javax.el;version="2.2.4"
  Import-Package: javax.el;version="2.2.4"
  
  Maven: org.glassfish.web:javax.el:2.2.4 MANIFEST.MF
  
  Export-Package: com.sun.el;uses:="javax.el";version="2.2.4"
  Private-Package: com.sun.el.lang;version="2.2.4",com.sun.el.parser;version="2.2.4",com.sun.el.util;version="2.2.4"
  Import-Package: com.sun.el;version="2.2.4",javax.el;version="2.2"
  
  Maven: org.apache.servicemix.bundles:org.apache.servicemix.bundles.hibernate-validator:5.0.2.Final_1
  
  Implementation-Version: 5.0.2.Final
  Import-Package: javax.el,javax.persistence;resolution:=optional, ...
  
  Export-Package: org.hibernate.validator.internal.engine.messageinterpola
  tion.el;uses:="javax.el,javax.validation,org.hibernate.validator.intern
  al.engine.messageinterpolation";version="5.0.2.Final",org.hibernate.val
  idator.internal.engine.messageinterpolation;uses:="javax.validation.met
  adata,org.hibernate.validator.internal.engine.messageinterpolation.el,j
  avax.el,javax.validation,org.hibernate.validator.internal.util.logging"
  ;version="5.0.2.Final", ...
  </code></pre>
  
  <p>Any version for import in hibernate bundle, 2.2.4 in export of el-api and el-impl and el-impl imports el-api as 2.2, not a 2.2.4. All bundles are resolved.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p><strong>Decision 1</strong></p>
  
  <p>my implementation of @hwellmann's idea. @hwellmann, is it correct?</p>
  
  <pre><code>public void createGroup(Group group) {
      ClassLoader prevClassLoader = Thread.currentThread().getContextClassLoader();
  
      try {
          ClassLoader[] classLoaders = new ClassLoader[] {
              prevClassLoader,
              ExpressionFactoryImpl.class.getClassLoader()
          };
  
          // build composite classloader
  
          Thread.currentThread().setContextClassLoader(compositeClassLoader);
  
          Set&lt;ConstraintViolation&lt;Group&gt;&gt; constraintViolations = validator.validate(group);
  
      } finally {
          Thread.currentThread().setContextClassLoader(prevClassLoader);
      }
  }
  </code></pre>
  
  <p>It works but looks strange. And change TCCL on each validation processing looks as some overhead.</p>
  
  <p><strong>Decision 2</strong></p>
  
  <p>The error has gone when I add my own message attribute to each validation annotation, for example for Group:</p>
  
  <pre><code>public class Group {
      @NotNull
      @Size(min=2, message="field.too_short")
      private String title;
  }
  </code></pre>
  
  <p>It seems in this case hibernate interpolator is not started so <code>ExpressionFactoryImpl</code> is not retrieved from TCCL (previously we read min=2 value, now we don't). If it is okay for us, this decision is simplest.
  I will investigate this area futhermore and share my observations there.</p>