Hi,<div><br></div><div><div class="gmail_quote">On 25 October 2011 14:34, Emmanuel Bernard <span dir="ltr">&lt;<a href="mailto:emmanuel@hibernate.org" target="_blank">emmanuel@hibernate.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





Your email made me think of a potential solution.<br>
<br>
We could use the service loader pattern to declare a constraint implementor service that would list the bindings between a constraint and its implementation.<br>
<br>
Add in implementation jars<br>
<br>
    META-INF/services/javax.validation.ConstraintValidator<br>
<br>
where `javax.validation.ConstraintValidator` contains the list of fully qualified class names of constraint implementations.<br>
<br>
&gt;From the class, we can infer the @Constraint being validated.<br>
<br>
I think this works very well:<br>
<br>
- this list is compiled when ValidatorFactory is built<br>
- this list can be equally built by the compiler and thus the annotation processor has equal knowledge<br>
<br>
The only gotcha I can see is that in modular environments like OSGi and co. They tend to hide such files from the classpath. So discovering the various resources named `META-INF/services/javax.validation.ConstraintValidator` might be flaky.<br>






That&#39;s an open subject in any case (ie. making BV run better in modular environments).<br>
<br>
Any one with better knowledge of OSGi or any other modular environment want to shred some lights?<font color="#888888"><br></font></blockquote><div><br></div><div>I&#39;m not an OSGi expert but I&#39;ve already implemented such mechanism to discover BV implementations, with a custom ValidationProviderResolver, in OSGi. For this test I&#39;ve used the following design, one bundle for the api and one bundle per implementation (the common use case). </div>





<div><br></div><div>In OSGi, the bundle resources are hidden to others bundle. To get something like that working you need to iterate on all bundles and check if the `META-INF/services/javax.validation.ConstraintValidator` is present with the bundle classloader. The main issue here is to deal with dynamism but that&#39;s not the goal :-) One drawback with that approach is that you expose your implementations which tend to be hidden in a modular world.</div>




<div><br></div><div>IMHO, if the mechanism used to discover the ConstraintValidator implementations is pluggable, it should work.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<font color="#888888">
Emmanuel<br>
</font><div><br>
On 24 oct. 2011, at 22:08, Gunnar Morling wrote:<br>
<br>
</div><div><div></div><div>&gt; Hi,<br>
&gt;<br>
&gt;&gt; - we would need to scan libs - talk to the CDI guys, this is not a trivial problem and at least forces people to put META-INF/bean.xml files in their JARs<br>
&gt;<br>
&gt; Would probably be the simplest solution for constraint authors and<br>
&gt; users, but I agree that this would be everything but easy. So I guess<br>
&gt; this approach is out of scope at least for BV 1.1.<br>
&gt;<br>
&gt;&gt; - we would need to list all bindings in an XML DD (this solution already exists in BV 1.0)<br>
&gt;<br>
&gt; Right, that works. One problem I still see from the POV of a<br>
&gt; constraint author is that as of BV 1.0 each constraint mapping must be<br>
&gt; imported in validation.xml. So a constraint library author could<br>
&gt; create distinct JARs for constraints and validators but the user still<br>
&gt; would need to import the mapping XML files.<br>
&gt;<br>
&gt; I think that&#39;s a general thing which we should cover in BV 1.1: enable<br>
&gt; the creation of re-usable constraint libraries which can easily be<br>
&gt; thrown into a project. Maybe this would en passant address the cycle<br>
&gt; problem.<br>
&gt;<br>
&gt;&gt; - we would need to list all binding via a programmatic API (that solution does break compile time checking BTW)<br>
&gt;<br>
&gt; Why would that break compile-time type-safety?<br>
&gt;<br>
&gt; --Gunnar<br>
&gt;<br>
&gt;<br>
&gt; 2011/10/24 Emmanuel Bernard &lt;<a href="mailto:emmanuel@hibernate.org" target="_blank">emmanuel@hibernate.org</a>&gt;:<br>
&gt;&gt; I personally am not bothered by this especially when comparing the drawbacks of alternatives:<br>
&gt;&gt; - we would need to scan libs - talk to the CDI guys, this is not a trivial problem and at least forces people to put META-INF/bean.xml files in their JARs<br>
&gt;&gt; - we would need to list libs in an XML DD<br>
&gt;&gt; - we would need to list all bindings in an XML DD (this solution already exists in BV 1.0)<br>
&gt;&gt; - we would need to list all binding via a programmatic API (that solution does break compile time checking BTW)<br>
&gt;&gt; - we would rely one some naming convention (not type-safe and require package scanning which is not trivial either esp in funny environments that use custom URL protocols)<br>
&gt;&gt; - we would rely on the container to provide the information - which means SE becomes a second class citizen<br>
&gt;&gt;<br>
&gt;&gt; We can imagine less evil solutions like a &quot;factory&quot; class declared in the XML DD and describing the binding programmatically but I&#39;m not yet convinced of the benefits. For that we would need to explore the programmatic configuration topic first though.<br>






&gt;&gt;<br>
&gt;&gt; Emmanuel<br>
&gt;&gt;<br>
&gt;&gt; On 23 oct. 2011, at 20:10, Gunnar Morling wrote:<br>
&gt;&gt;<br>
&gt;&gt;&gt; Hello experts,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; one thing about BV I&#39;m somewhat uncomfortable with is the (cyclic)<br>
&gt;&gt;&gt; dependency between constraint annotation types and their validators.<br>
&gt;&gt;&gt; This is, validators are referenced from constraints within the<br>
&gt;&gt;&gt; @Constraint meta-annotation, while constraint types are referenced by<br>
&gt;&gt;&gt; the type parameter in validator impl&#39;s.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; In particular this makes it impossible to clearly distinguish between<br>
&gt;&gt;&gt; the public API of a constraint library (the annotation types) and its<br>
&gt;&gt;&gt; implementation (the validators), e.g. by creating two distinct JARs.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Therefore I&#39;d personally prefer if there was only a reference from the<br>
&gt;&gt;&gt; validators to the constraints, but not the other way around. This of<br>
&gt;&gt;&gt; course raises the question how it would be determined which validators<br>
&gt;&gt;&gt; exist for a given constraint. One could think of using XML descriptors<br>
&gt;&gt;&gt; or some scanning-based approach. I guess from a technical POV this<br>
&gt;&gt;&gt; problem is somewhat related to what&#39;s to be done in CDI (finding bean<br>
&gt;&gt;&gt; implementations, processing qualifier annotations etc.).<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; That said, I&#39;d be very interested in your opinion on this topic. Do<br>
&gt;&gt;&gt; you think this is something we should address or do you think this<br>
&gt;&gt;&gt; sort of cycle is acceptable?<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thanks,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; --Gunnar<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; beanvalidation-dev mailing list<br>
&gt;&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; beanvalidation-dev mailing list<br>
&gt;&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
&gt;&gt;<br>
&gt; _______________________________________________<br>
&gt; beanvalidation-dev mailing list<br>
&gt; <a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
<br>
<br>
_______________________________________________<br>
beanvalidation-dev mailing list<br>
<a href="mailto:beanvalidation-dev@lists.jboss.org" target="_blank">beanvalidation-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/beanvalidation-dev</a><br>
</div></div></blockquote></div><br></div><div>--Kevin</div>