tag:blogger.com,1999:blog-13069585906081740532025年10月17日 17:03:56 +0000JavaBean ValidationMavenJSR 303Dependency InjectionJAX-WSHibernate ValidatorJSR 223web servicesGenericsJRubyJava MagazinSpringcontract-firstscripting languagesEclipseGoogle GuiceTutorialType inferenceinterceptorAOPDBUnitIntelliJ IDEAJPA 2JSR 349MD5MapStructannotationbean mappingsdesign patterngeneric methodsApache AntApache Commons IOArchetype CatalogArchetype PluginBlogCDICRC32Connection PoolDTOData SourceEJBGlassfishGoogle CodeJAXBJDTJMXJSF 2JSR 299JSR 317JUnitJavaFXJettyJoda Time APIMessageDigestMetroMockitoMySQLOSGiOpenEJBOracleProxyRCPRedmineRuby on RailsSVNSeamSpELTestNGWarblerWoodstoxXMLXPathXmlBeansaspect-oriented programmingchecksumdependencydownloadsfeedback-neededkey words: JBoss Community Awardlanguagesshellstatisticsvisitor patternwstx-aslMusings of a Programming Addicthttp://musingsofaprogrammingaddict.blogspot.com/noreply@blogger.com (Anonymous)Blogger59125tag:blogger.com,1999:blog-1306958590608174053.post-3402099789562828252013年11月28日 22:37:00 +00002013年11月28日T23:37:45.521+01:00bean mappingsJavaMapStructMapStruct 1.0.0.Alpha2 has landed!<p><em>Note: This article originally appeared on <a href="http://mapstruct.org/news/2013/11/28/mapstruct-1_0_0_Alpha2-has-landed.html">mapstruct.org</a>. In order to spread the word on MapStruct I thought it's a good idea to post it in this blog, too.</em></p> <p>It is my great pleasure to announce the release of MapStruct 1.0.0.Alpha2.</p> <p>This took us a bit longer than expected, but the new release offers quite a few exciting new features we hope you'll enjoy. The JARs have already been synched to Maven Central. The coordinates are:</p> <ul> <li><a href="http://search.maven.org/#artifactdetails&#124;org.mapstruct&#124;mapstruct&#124;1.0.0.Alpha2&#124;jar">org.mapstruct:mapstruct:1.0.0.Alpha2</a> for the annotation JAR and </li> <li><a href="http://search.maven.org/#artifactdetails&#124;org.mapstruct&#124;mapstruct-processor&#124;1.0.0.Alpha2&#124;jar">org.mapstruct:mapstruct-processor:1.0.0.Alpha2</a> for the annotation processor.</li> </ul> <p>Alternatively you can get a <a href="http://sourceforge.net/projects/mapstruct/files/1.0.0.Alpha2/">distribution bundle</a> from SourceForge.</p> <p>Beside several new mapping features (e.g. combining several sources into one target object) the release provides integration with CDI and Spring to make the retrieval of mapper objects more comfortable. We've added several new implicit data type conversions and there is now also support for converting <em>Map</em> objects.</p> <p>Let's have a closer look at some of the additions.</p> <h3>Advanced mapping features</h3> <p>When working with data transfer objects (DTO) to pass data from the backend to the client, it is common to have one DTO which transports the data from several entities. For this purpose MapStruct supports now mapping methods with several source methods. The following shows an example:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> <a href="#n3" name="n3">3</a> <a href="#n4" name="n4">4</a> <a href="#n5" name="n5">5</a> <a href="#n6" name="n6">6</a> <a href="#n7" name="n7">7</a> <a href="#n8" name="n8">8</a> <a href="#n9" name="n9">9</a> </pre></td> <td class="code"><pre><span class="annotation">@Mapper</span> <span class="directive">public</span> <span class="type">interface</span> <span class="class">OrderMapper</span> { <span class="annotation">@Mappings</span>({ <span class="annotation">@Mapping</span>(source = <span class="string"><span class="delimiter">&quot;</span><span class="content">order.name</span><span class="delimiter">&quot;</span></span>, target = <span class="string"><span class="delimiter">&quot;</span><span class="content">name</span><span class="delimiter">&quot;</span></span>), <span class="annotation">@Mapping</span>(source = <span class="string"><span class="delimiter">&quot;</span><span class="content">houseNo</span><span class="delimiter">&quot;</span></span>, target = <span class="string"><span class="delimiter">&quot;</span><span class="content">houseNumber</span><span class="delimiter">&quot;</span></span>) }) OrderDto orderAndAddressToOrderDto(Order order, Address deliveryAddress); } </pre></td> </tr></table> </div> <p>As for single parameter methods all attributes are mapped by name from the source objects to the target object, performing a type conversion if required. In case a property with the same name exists in more than one source object, the source parameter from which to retrieve the property must be specified using the <em>@Mapping</em> annotation as shown for the <em>name</em> property.</p> <p>One of the core principles in MapStruct is type-safety. Therefore an error will be raised at generation time when such an ambiguity is not resolved. Note that when mapping a property which only exists once in the source objects to another target property name, it is optional to specify the source parameter's name.</p> <p>Also related to type-safety and correctness of generated mappings is the new option to raise an error in case an attribute of the mapping target is not populated, as this typically indicates an oversight or configuration error. By default a compiler warning is created in this case. To change this e.g. into a compile error instead, the <em>unmappedTargetPolicy</em> property can be used like this:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> <a href="#n3" name="n3">3</a> <a href="#n4" name="n4">4</a> <a href="#n5" name="n5">5</a> </pre></td> <td class="code"><pre><span class="annotation">@Mapper</span>(unmappedTargetPolicy=ERROR) <span class="directive">public</span> <span class="type">interface</span> <span class="class">OrderMapper</span> { <span class="comment">//...</span> } </pre></td> </tr></table> </div> <p>In some cases it is required to update an existing object with the properties from a given source object instead of instantiating a new target object. This use case can be addressed with help of the <em>@MappingTarget</em> annotation which denotes one method parameter as the target of the mapping like this:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> <a href="#n3" name="n3">3</a> <a href="#n4" name="n4">4</a> <a href="#n5" name="n5">5</a> </pre></td> <td class="code"><pre><span class="annotation">@Mapper</span> <span class="directive">public</span> <span class="type">interface</span> <span class="class">OrderMapper</span> { <span class="type">void</span> updateOrderEntityFromDto(OrderDto dto, <span class="annotation">@MappingTarget</span> Order order); } </pre></td> </tr></table> </div> <p>Instead of instantiating a new <em>Order</em> object, the generated implementation of <em>updateOrderEntityFromDto()</em> method will update the given order instance with the attributes from the passed <em>OrderDto</em>.</p> <h3>More implicit type conversions</h3> <p>Several new implicit type conversions have been added for the case that the source and target type of a mapped property differ. <em>BigDecimal</em> and <em>BigInteger</em> can now automatically be converted into other numeric types as well as into <em>String</em>. You can finde a list of all supported conversions in the <a href="http://localhost:4242/documentation/#section-05-01">reference documentation</a>.</p> <p>Please beware of a possible value or precision loss when performing such conversions from larger to smaller numeric types. It <a href="https://github.com/mapstruct/mapstruct/issues/5">is planned</a> for the next milestone to optionally raise a warning in this case.</p> <p>It is now also possible to convert <em>Date</em> into <em>String</em> and vice versa. For that purpose a new parameter has been added to the <em>@Mapping</em> annotation which allows to specify a format string as interpreted by <em>SimpleDateFormat</em>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> <a href="#n3" name="n3">3</a> <a href="#n4" name="n4">4</a> <a href="#n5" name="n5">5</a> <a href="#n6" name="n6">6</a> </pre></td> <td class="code"><pre><span class="annotation">@Mapper</span> <span class="directive">public</span> <span class="type">interface</span> <span class="class">OrderMapper</span> { <span class="annotation">@Mapping</span>(source=<span class="string"><span class="delimiter">&quot;</span><span class="content">orderDate</span><span class="delimiter">&quot;</span></span>, dateFormat=<span class="string"><span class="delimiter">&quot;</span><span class="content">dd.MM.yyyy HH:mm:ss</span><span class="delimiter">&quot;</span></span>) OrderDto orderToOrderDto(Order order); } </pre></td> </tr></table> </div> <h3>Integration with CDI and Spring</h3> <p>The recommended way for obtaining mapper instances in the 1.0.0.Alpha1 release was to use the <em>Mappers</em> factory.</p> <p>Alternatively it is now also possible to work with dependency injection. To make this possible, MapStruct can generate mappers which are CDI or Spring beans, based on which flavor of DI you prefer. In the following example MapStruct is adviced to make the generated mapper implementation a CDI bean by specifying "cdi" via the <em>componentModel</em> attribute in the <em>@Mapper</em> annotation:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> <a href="#n3" name="n3">3</a> <a href="#n4" name="n4">4</a> <a href="#n5" name="n5">5</a> </pre></td> <td class="code"><pre><span class="annotation">@Mapper</span>(componentModel=<span class="string"><span class="delimiter">&quot;</span><span class="content">cdi</span><span class="delimiter">&quot;</span></span>) <span class="directive">public</span> <span class="type">interface</span> <span class="class">OrderMapper</span> { <span class="comment">//...</span> } </pre></td> </tr></table> </div> <p>This allows to obtain an order mapper simply via <em>@Inject</em> (provided you have CDI enabled within your application):</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n1" name="n1">1</a> <a href="#n2" name="n2">2</a> </pre></td> <td class="code"><pre><span class="annotation">@Inject</span> <span class="directive">private</span> OrderMapper orderMapper; </pre></td> </tr></table> </div> <p>Note that all other mappers a generated mapper class references are also obtained via the configured component model. So if you e.g. hook in hand-written mapper classes via <em>@Mapper#uses()</em> make sure that these mappers are compliant with the chosen component model, e.g. are CDI beans themselves. Refer to the documentation which <a href="/documentation/#section-04-02">describes</a> all the specifics in detail.</p> <p>On a related note, if you prefer to work with the <em>Mappers</em> factory as before, you'll have to adapt your imports because this class has been moved to the new package <em>org.mapstruct.factory</em>.</p> <h3>Further info</h3> <p>This concludes our tour through the new features in MapStruct 1.0.0.Alpha2. You can find the complete list of addressed issues in the <a href="https://github.com/mapstruct/mapstruct/issues?milestone=2&amp;state=closed">change log</a> on GitHub. The <a href="/documentation">reference documentation</a> has been updated to cover all new functionality.</p> <p>If you have any kind of feedback please make sure to let us know. Either post a comment here or open a discussion in the <a href="https://groups.google.com/forum/?fromgroups#!forum/mapstruct-users">mapstruct-users</a> group. Bugs and feature requests can be reported in the <a href="https://github.com/mapstruct/mapstruct/issues">issue tracker</a> and your pull request on GitHub is highly welcome! The <a href="/contributing">contribution guide</a> has all the info you need to get started with hacking on MapStruct.</p> <p>Many thanks to <a href="https://github.com/agudian">Andreas Gudian</a> and <a href="https://github.com/kryger">Lukasz Kryger</a> who contributed to this release, that's awesome!</p> http://musingsofaprogrammingaddict.blogspot.com/2013/11/mapstruct-100alpha2-has-landed.htmlnoreply@blogger.com (Anonymous)15tag:blogger.com,1999:blog-1306958590608174053.post-54067833424492152982013年6月03日 16:26:00 +00002013年06月03日T18:26:15.750+02:00bean mappingsJavaMapStructAnnouncing MapStruct 1.0.0.Alpha1<p>It is a great pleasure for me to announce the first release of the <a href="http://mapstruct.org/">MapStruct</a> project.</p> <p>MapStruct is a code generator which simplifies the implementation of mappings between Java bean types by generating mapping code at compile time, following a convention-over-configuration approach. Unlike reflection-based mapping frameworks MapStruct generates mapping code at build time which is fast, type-safe and easy to understand.</p> <p>The official <a href="http://mapstruct.org/news/2013/06/03/announcing-mapstruct.html">release announcement</a> describes in detail what MapStruct can do for you and what's on the roadmap for the next releases. The release is available on <a href="http://sourceforge.net/projects/mapstruct/files/1.0.0.Alpha1/">SourceForge</a> and <a href="http://search.maven.org/#search|ga|1|g%3A%22org.mapstruct%22">Maven Central</a>.</p> <p>Give it a try, your feedback is highly welcome via the <a href="https://groups.google.com/forum/?fromgroups#!forum/mapstruct-users">mapstruct-users</a> Google group!</p> http://musingsofaprogrammingaddict.blogspot.com/2013/06/announcing-mapstruct-100alpha1.htmlnoreply@blogger.com (Anonymous)6tag:blogger.com,1999:blog-1306958590608174053.post-39120437093761164332013年3月06日 13:58:00 +00002013年03月06日T14:58:14.202+01:00EclipseJavaTestNGTestNG test fails to launch in debug mode in Eclipse<p>Every once in a while it happens to me that <a href="http://testng.org/doc/index.html">TestNG</a> tests fail to launch in Eclipse when starting in debug mode (run mode works fine). The progress bar freezes at 57% and Eclipse indefinitely hangs.</p> <p>Some googling brought me to to <a href="https://github.com/cbeust/testng-eclipse/issues/55">this bug</a> in the TestNG issue tracker. As it seems, the issue is caused by a conflict between the TestNG version used as (Maven) dependency in the project under test and the TestNG version used by the TestNG Eclipse plug-in. People commented at the issue that it helped for them to create a new Eclipse workspace. As I wanted to avoid that, I had a look into the <em>.metadata</em> folder of my workspace, searching for things which might be the potential cause.</p> <p>And indeed I stumbled upon the folder <em>WORKSPACE_DIR/.metadata/.plugins/org.testng.eclipse/</em>. After deleting this folder and restarting Eclipse, TestNG tests would launch in debug mode again as expected.</p> http://musingsofaprogrammingaddict.blogspot.com/2013/03/testng-test-fails-to-launch-in-debug.htmlnoreply@blogger.com (Anonymous)54tag:blogger.com,1999:blog-1306958590608174053.post-76201620969493807322012年11月18日 20:43:00 +00002012年11月18日T21:43:55.276+01:00annotationDependency InjectionJavaDagger - A new Java dependency injection framework<p>Watching <a href="http://www.infoq.com/presentations/Square">a talk</a> from Square's CTO <a href="http://blog.crazybob.org/">Bob Lee</a>, I just learned about <a href="http://square.github.com/dagger/">Dagger</a>, a new dependency injection framework for Java and Android which is currently in the works at Square, Inc.</p> <p>Considering the number of existing DI solutions in the Java space &#150; e.g. <a href="http://www.jcp.org/en/jsr/detail?id=299">CDI</a>, <a href="http://code.google.com/p/google-guice/">Google Guice</a> and <a href="http://www.springsource.org/spring-framework">Spring</a> &#150; one might wonder whether the world really needs yet another DI framework. According to Bob's talk, Dagger (a pun on "directed acyclic graph") is the attempt to create a modern and fast DI framework based on the insights gained during development and usage of Guice (Bob was the founder of the Guice project at Google). And indeed does Dagger come up with some quite interesting ideas which I'd like to discuss in more detail in the following.</p> <h2>Overview</h2> <p>Dagger is centered around the annotations for dependency injection defined by <a href="http://www.jcp.org/en/jsr/detail?id=330">JSR 330</a> (which Bob Lee co-led). This is a good thing because it increases portability of your code between different DI solutions.</p> <p>Dependencies are retrieved by annotating fields or constructors with <code>@Inject</code>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">Circus</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">final</span> Artist artist;<tt> </tt><tt> </tt> <span class="at">@Inject</span> <tt> </tt> <span class="di">public</span> Circus(Artist artist) {<tt> </tt> <span class="lv">this</span>.artist = artist;<tt> </tt> }<tt> </tt><tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>To satisfy dependencies, Dagger creates the required objects using their <code>@Inject</code>-annotated constructor (in turn creating and passing any dependencies) or the default no-args constructor.</p> <p>Where that's not possible (e.g. when an implementation of an interface needs to be injected) <em>provider methods</em> can be used. Provider methods must be annotated with <code>@Provides</code> and be defined in a class annotated with <code>@Module</code> like this:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Module</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CircusModule</span> {<tt> </tt> <span class="at">@Provides</span> Artist provideArtist() {<tt> </tt> <span class="kw">return</span> <span class="kw">new</span> Juggler();<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The @Module annotation is also used to define the <em>entry point</em> of an application:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Module</span>( entryPoints=CircusApp.class )<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CircusModule</span> {<tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>This entry point represents the root of the object graph managed by Dagger. As we'll see in a moment, explicitly defining the root allows for compile-time validation of the dependency graph. An instance of the entry point type can be retrieved from the <code>ObjectGraph</code> class, passing the module(s) to create the graph from:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">ObjectGraph objectGraph = ObjectGraph.create(<span class="kw">new</span> CircusModule());<tt> </tt>CircusApp circus = objectGraph.get(CircusApp.class);<tt> </tt>circus.startPerformance();<tt> </tt></pre></td> </tr></table> </div> <p>Dagger also also provides support for qualifiers, lazy injection, injection of providers and more. The project's <a href="http://square.github.com/dagger/">web site</a> gives a good overview. Apart from that it's interesting to see what Dagger deliberately does not support to avoid an increased complexity:</p> <ul> <li>Circular dependencies between objects</li> <li>Method injection</li> <li>Custom scopes (Objects are either newly created for each injection or singleton-scoped)</li> </ul> <h2>Code generation</h2> <p>DI frameworks usually make intensive use of reflection to examine annotations, find injection points, create managed objects etc. While reflection today isn't as expensive as it used to be in earlier years, it still can take a considerable amount of time to create large object graphs with lots of dependencies.</p> <p>Dagger tries to improve upon that with the help of code generation. It provides a JSR 269 based <a href="http://docs.oracle.com/javase/7/docs/api/index.html?javax/annotation/processing/package-summary.html">annotation processor</a> which is used at compile time to create an adapter class for each managed type. These adapter classes contain all the logic required at run time to set up the object graph by invoking constructors and populating references to other objects, without making use of reflection.</p> <p>This approach promises performance benefits over reflection-based ways for creating object graphs typically used by DI frameworks. On my machine Dagger needed roughly half the time to initialize the graph of the <a href="https://github.com/square/dagger/tree/master/example">CoffeeApp</a> example using the generated classes compared to using reflection (which it also supports as fallback). Of course this is by no means a comprehensive benchmark and can't be compared with other frameworks but it surely shows the potential of the code generation approach.</p> <p>The annotation processor also performs a validation of the object graph and its dependencies at compile time. So if for instance no matching type (or more than one) can be found for a given injection point, the build will fail with an error message describing the problem. This helps in reducing turn-around times compared to discovering this sort of error only at application start-up. Implementing this sort of checks using an annotation processor makes them available in IDEs (which typically can integrate annotation processors) as well as headless builds, e.g. on a CI server.</p> <h2>Object graph visualization</h2> <p>Not mentioned in the documentation, Dagger also provides an annotation processor which generates a <a href="http://www.graphviz.org/">GraphViz</a> file visualizing the object graph. This may be useful to get an understanding of unknown object graphs. The following shows the graph from the CoffeeApp example:</p> <div class="separator" style="clear: both; text-align: center;"> <img border="0" height="332" width="313" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjevFjbb8aEKWrxO5I170tFfu7w4mjzpK4vjFYU-nGw9JBI60u3OQoagYMVnOmos6tfhR8U8K0igL6Lp9qlqf1NgiGjWTRo6MzbQcYPtGzoAW8r8Df9MyVKQALGwHCgPAJL586r_A712hg/s400/2012_11_17_coffeeapp_object_graph.png" /></div> <h2>Summary</h2> <p>Dagger is a new dependency injection framework for Java and Android.</p> <p>While it's still in the works (the current version is 0.9 and there are still some <a href="https://github.com/square/dagger/issues/112">apparent</a> <a href="https://github.com/square/dagger/issues/113">bugs</a>), I find the concept of using an annotation processor for validating the object graph at compile time and generating code for a faster initialization at runtime very interesting. In particular on mobile devices fast start up times are essential for a good user experience.</p> <p>I also like the idea of leaving out features which might provide some value but would add much complexity. One thing I'm missing though is some sort of interceptor or decorator mechanism. This would be helpful for implementing typical cross-cutting concerns.</p> <p>It'll definitely be interesting to see how the code generation approach works out in practice and whether other DI solutions possibly adapt that idea.</p> http://musingsofaprogrammingaddict.blogspot.com/2012/11/dagger-new-java-dependency-injection.htmlnoreply@blogger.com (Anonymous)506tag:blogger.com,1999:blog-1306958590608174053.post-61340356905952833922012年8月29日 21:59:00 +00002012年08月29日T23:59:42.017+02:00Bean Validationfeedback-neededJSR 349Method validation and inheritance - feedback needed!<p><em>Note: This post originally appeared on <a href="http://www.beanvalidation.org/news/2012/08/29/methodvalidation-inheritance/">beanvalidation.org</a>. Please post any feedback over there.</em></p> <p>Now that everybody is returning from their summer holidays, also the Bean Validation team is getting back to their desks in order to work with full steam towards revision 1.1.</p> <p>As you know, the largest new feature will be <a href="http://beanvalidation.org/1.1/spec/#d0e2147">method validation</a>, that is the validation of method parameters and return values using constraint annotations. Bean Validation 1.1 <a href="http://beanvalidation.org/news/2012/03/13/release-1-1-edr1/">early draft 1</a> lays the ground for this, and right now we're tackling some <a href="https://hibernate.onjira.com/browse/BVAL-272">advanced questions</a> still open in that area (btw. if you haven't yet tried out the <a href="http://in.relation.to/Bloggers/FirstAlphaReleaseOfHibernateValidator5">reference implementation</a> of ED1, this is the perfect time to do so and give us your feedback).</p> <h2>The problem</h2> <p>One question the EG currently is <a href="http://lists.jboss.org/pipermail/beanvalidation-dev/2012-August/000504.html">discussing</a> is whether and, if so, how a refinement of method constraints should be allowed in sub-types. That is, if a class implements a method of an interface or overrides a method from a super class, should the sub-type be allowed to place any additional constraints?</p> <p>The current draft defines the following rules for such cases (see the <a href="http://beanvalidation.org/1.1/spec/#d0e2429">draft document</a> for all the gory details):</p> <ul> <li>No parameter constraints may be specified in addition to those constraints defined on the method in the interface or super class.</li> <li>Return value constraints may be added in sub-types.</li> </ul> <h2>The rationale</h2> <p>The rationale behind this is the principle of <a href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">behavioral sub-typing</a>, which demands that wherever a given type <code>T</code> is used, it should be possible to replace <code>T</code> with a sub-type <code>S</code> of <code>T</code>. This means that a sub-type must not strengthen a method's preconditions (by adding parameter constraints), as this might cause client code working correctly against <code>T</code> to fail when working against <code>S</code>. A sub-type may also not weaken a method's postconditions. However, a sub-type may strengthen the method's postconditions (by adding return value constraints), as client code working against <code>T</code> still will work against <code>S</code>.</p> <h2>Can you show me some code, please?</h2> <p>To give you an example, the following shows a constraint declaration considered illegal as of the current draft, as parameter constraints are added to the <code>placeOrder()</code> method in a sub-class of <code>OrderService</code>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">OrderService</span> {<tt> </tt> <span class="ty">void</span> placeOrder(<span class="at">@NotNull</span> <span class="pt">String</span> customerCode, <span class="at">@NotNull</span> Item item, <span class="ty">int</span> quantity) { ... }<tt> </tt>}<tt> </tt><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">SimpleOrderService</span> <span class="di">extends</span> OrderService {<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> placeOrder(<tt> </tt> <span class="at">@Size</span>(min=<span class="i">3</span>, max=<span class="i">20</span>) <span class="pt">String</span> customerCode,<tt> </tt> Item item,<tt> </tt> <span class="at">@Min</span>(<span class="i">1</span>) <span class="ty">int</span> quantity) { ... }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <h2>Alternatives</h2> <p>While this approach works, follows principles of clean OO design and also <a href="http://research.microsoft.com/en-us/projects/contracts/">is employed</a> by other <em>Programming by Contract</em> solutions, some voices in the EG expressed doubts whether the handling of parameter constraints isn't too restrictive and thus may limit innovation in that area. In particular with respect to legacy code, the question was raised whether it shouldn't be allowed to add parameter constraints in sub-types.</p> <p>One example may be a legacy interface, which <em>technically</em> has no constraints (that is, no parameter constraints are placed on its methods), but comes with a verbal description of preconditions in its documentation. In this case an implementor of that interface might wish to implement this contract by placing corresponding constraint annotations on the implementation.</p> <p>An open question in this situation is what should the behavior be if the interface is being constrained afterwards?</p> <h2>Give use your feedback!</h2> <p>So what do you think, should such a refinement of parameter constraints be allowed or not? Possible alternatives:</p> <ul> <li>allow such a refinement by default</li> <li>have some sort of switch controlling the behavior (either standardized or provider-specific)</li> </ul> <p>As there are pro's and con's of either approach, we'd very interested in user feedback on this.</p> <p>Let us know what you think by posting a comment directly to this blog, shooting a message to the <a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev">mailing list</a> or participating in this <a href="www.doodle.com/qp78u6mqzetuas7p">Doodle vote</a>. Which use cases you have encountered come to mind where the possibility to refine parameter constraints may help you?</p> http://musingsofaprogrammingaddict.blogspot.com/2012/08/method-validation-and-inheritance.htmlnoreply@blogger.com (Anonymous)10tag:blogger.com,1999:blog-1306958590608174053.post-74465935988219145812012年7月01日 14:08:00 +00002012年07月01日T16:08:23.848+02:00JavaJavaFXMavenUsing JavaFX 2.1 with Maven<p>While <a href="http://lists.jboss.org/pipermail/beanvalidation-dev/2012-June/000429.html">thinking about</a> how to take advantage of <a href="http://www.beanvalidation.org/">Bean Validation</a> within <a href="http://www.oracle.com/technetwork/java/javafx/overview/index.html">JavaFX 2</a> based applications, I just learned that JavaFX is actually part of the JDK installation since <a href="http://www.oracle.com/technetwork/java/javase/7u2-relnotes-1394228.html">Java SE 7 Update 2</a>. The latest JDK (<a href="http://www.oracle.com/technetwork/java/javase/7u5-relnotes-1653274.html">Update 5</a>) comes with JavaFX 2.1.1.</p> <p>This makes it very easy to use the JavaFX API within Maven based applications; all what's required is to add the following dependency to your POM file:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="ta">&lt;dependency&gt;</span><tt> </tt> <span class="ta">&lt;groupId&gt;</span>com.oracle<span class="ta">&lt;/groupId&gt;</span><tt> </tt> <span class="ta">&lt;artifactId&gt;</span>javafx-runtime<span class="ta">&lt;/artifactId&gt;</span><tt> </tt> <span class="ta">&lt;scope&gt;</span>system<span class="ta">&lt;/scope&gt;</span><tt> </tt> <span class="ta">&lt;version&gt;</span>2.1.1<span class="ta">&lt;/version&gt;</span><tt> </tt> <span class="ta">&lt;systemPath&gt;</span>${java.home}/../jre/lib/jfxrt.jar<span class="ta">&lt;/systemPath&gt;</span><tt> </tt><span class="ta">&lt;/dependency&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>That was simple :)</p> <p>Now to the bad news: as it seems the installation doesn't contain the JavaFX API sources or JavaDocs. But that's not really a problem, as a <a href="http://hg.openjdk.java.net/openjfx/2.1/master/rt/archive/5c3b3d524f07.zip">ZIP with the sources</a> can be downloaded from OpenJFX's Mercurial server and the latest Javadocs can be found <a href="http://docs.oracle.com/javafx/2/api/index.html">here</a>.</p>http://musingsofaprogrammingaddict.blogspot.com/2012/07/using-javafx-21-with-maven.htmlnoreply@blogger.com (Anonymous)2tag:blogger.com,1999:blog-1306958590608174053.post-30704048121754259662012年6月10日 13:43:00 +00002012年06月10日T15:43:39.512+02:00AOPinterceptorJavaSpringSpring AOP and AOP Alliance MethodValidators<p>The <a href="http://www.springsource.org/">Spring framework</a> has in general an excellent <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/">documentation</a>. One exception to me is the reference guide's <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html">chapter on Spring AOP</a>, which I personally find not as comprehensible as other parts of the documentation.</p> <p>What I'm missing in particular is a complete example demonstrating how to use Spring AOP together with <a href="http://aopalliance.sourceforge.net/">AOP Alliance</a> <code>MethodInterceptor</code>s. Where possible, I prefer to use AOP Alliance compliant interceptors over other Spring AOP <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-advice">advice types</a>, as they foster interoperability with other AOP frameworks compatible with the AOP Alliance API, such as <a href="http://code.google.com/p/google-guice/wiki/AOP">Google Guice</a>.</p> <p>So without further ado, an example for using AOP Alliance method interceptors with Spring AOP is shown in the following.</p> <p>First define an interceptor by implementing the interface <code>org.aopalliance.intercept.MethodInterceptor</code>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Component</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">InvocationCountInterceptor</span> <span class="di">implements</span> MethodInterceptor {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">AtomicLong</span> invocationCount = <span class="kw">new</span> <span class="pt">AtomicLong</span>();<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="pt">Object</span> invoke(MethodInvocation invocation) <span class="di">throws</span> <span class="pt">Throwable</span> {<tt> </tt><tt> </tt> invocationCount.incrementAndGet();<tt> </tt> <span class="kw">return</span> invocation.proceed();<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="pt">AtomicLong</span> getInvocationCount() {<tt> </tt> <span class="kw">return</span> invocationCount;<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>By specifying the <code>@Component</code> <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-stereotype-annotations">stereotype annotation</a>, the interceptor is enabled to be detected automatically by the Spring container using component scanning.</p> <p>Now the types/methods to which the interceptor shall be applied need to be configured. Using Spring's <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-aop">AOP schema</a>, this is done by defining an <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-schema-advisors">advisor</a> which references the interceptor bean and links it with a <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-schema-pointcuts">pointcut</a> expression describing the targeted methods:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="pp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><tt> </tt><span class="ta">&lt;beans</span> <span class="an">xmlns</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.springframework.org/schema/beans</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:aop</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.springframework.org/schema/aop</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xsi:schemaLocation</span>=<span class="s"><span class="dl">&quot;</span><tt> </tt> <span class="k">http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</span><tt> </tt> <span class="k">http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd </span><tt> </tt> <span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt><tt> </tt> ...<tt> </tt> <span class="ta">&lt;aop:config&gt;</span><tt> </tt> <span class="ta">&lt;aop:advisor</span> <span class="an">advice-ref</span>=<span class="s"><span class="dl">&quot;</span><span class="k">invocationCountInterceptor</span><span class="dl">&quot;</span></span> <span class="an">pointcut</span>=<span class="s"><span class="dl">&quot;</span><span class="k">within(de.gmorling.moapa.springaop.service..*)</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;/aop:config&gt;</span><tt> </tt> ...<tt> </tt><tt> </tt><span class="ta">&lt;/beans&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>Here the interceptor is applied to all invocations of methods defined within the <code>de.gmorling.moapa.springaop.service</code> package and its sub-packages. But the pointcut could also be more fine-grained and match only methods on specific types or even single methods.</p> <p>If several interceptors shall be bound to the same pointcut, the pointcut expression can also be defined separately and referenced from multiple advisors like so:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">...<tt> </tt><span class="ta">&lt;aop:config&gt;</span><tt> </tt> <span class="ta">&lt;aop:pointcut</span> <span class="an">id</span>=<span class="s"><span class="dl">&quot;</span><span class="k">serviceLayer</span><span class="dl">&quot;</span></span> <span class="an">expression</span>=<span class="s"><span class="dl">&quot;</span><span class="k">within(de.gmorling.moapa.springaop.service..*)</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;aop:advisor</span> <span class="an">advice-ref</span>=<span class="s"><span class="dl">&quot;</span><span class="k">invocationCountInterceptor</span><span class="dl">&quot;</span></span> <span class="an">pointcut-ref</span>=<span class="s"><span class="dl">&quot;</span><span class="k">serviceLayer</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;aop:advisor</span> <span class="an">advice-ref</span>=<span class="s"><span class="dl">&quot;</span><span class="k">anotherInterceptor</span><span class="dl">&quot;</span></span> <span class="an">pointcut-ref</span>=<span class="s"><span class="dl">&quot;</span><span class="k">serviceLayer</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt><span class="ta">&lt;/aop:config&gt;</span><tt> </tt>... <tt> </tt></pre></td> </tr></table> </div> <p>Note that the AspectJ weaver JAR and &#150; when applying interceptors to classes &#150; CGLIB 2 need to be on the class path. A complete <a href="https://github.com/gunnarmorling/musings-of-a-programming-addict/tree/master/spring-aop">sample Maven project</a> can be found on GitHub.</p>http://musingsofaprogrammingaddict.blogspot.com/2012/06/spring-aop-and-aop-alliance.htmlnoreply@blogger.com (Anonymous)2tag:blogger.com,1999:blog-1306958590608174053.post-70563007829734368282011年10月09日 17:36:00 +00002011年10月09日T19:36:35.240+02:00EclipseJavaOSGiUsing Pax Runner with Eclipse 3.6<p>While working on a little pet project based on OSGi I wanted to use the <a href="http://team.ops4j.org/wiki/display/paxrunner/Pax+Runner">Pax Runner</a> Eclipse plug-in to launch my OSGi application on the <a href="http://felix.apache.org/">Apache Felix</a> OSGi framework.</p><p>Unfortunately this attempt resulted in the following error:</p><p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">java.lang.NoClassDefFoundError: org/eclipse/pde/internal/ui/launcher/LaunchArgumentsHelper<tt> </tt>at org.ops4j.pax.cursor.Utils.getVMArgsOptions(Utils.java:216)<tt> </tt>at org.ops4j.pax.cursor.LaunchConfiguration.getProgramArguments(LaunchConfiguration.java:156)<tt> </tt>at org.eclipse.pde.launching.AbstractPDELaunchConfiguration.launch(AbstractPDELaunchConfiguration.java:72)<tt> </tt>at org.eclipse.pde.launching.OSGiLaunchConfigurationDelegate.launch(OSGiLaunchConfigurationDelegate.java:47)<tt> </tt>at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:853)<tt> </tt>at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:702)<tt> </tt>at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:923)<tt> </tt>at org.eclipse.debug.internal.ui.DebugUIPlugin8ドル.run(DebugUIPlugin.java:1126)<tt> </tt>at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)<tt> </tt></pre></td> </tr></table></div></p><p>This looked pretty much like an incompatability between the Pax Runner plug-in and the Eclipse platform. And indeed the plug-in unfortunately doesn't seem to keep up with PDE-internal changes in recent Eclipse versions due to <a href="http://permalink.gmane.org/gmane.comp.java.ops4j.general/12273">limited developer resources</a>. </p><p>Further googling then led me to <a href="http://team.ops4j.org/browse/PAXRUNNER-353">PAXRUNNER-353</a> at Pax Runner's issue tracker.</p><p>The ticket itself is not yet resolved, but the good news is that <a href="http://tux2323.blogspot.com/">Christian Baranowski</a> (whom I had the pleasure to meet during the <a href="http://germany.osgiusers.org/Main/SummerOCC2011">OSGi code camp</a> last week) created a <a href="https://github.com/tux2323/org.ops4j.pax.runner/tree/master/pax-runner-eclipse">fork</a> on GitHub fixing the issue for now. So let's hope that this fix gets quickly merged into the main branch.</p><p>Until then Christian even has established a temporary Eclipse <a href="http://tux2323.github.com/pax-runner-update-site/">update site</a> which makes upgrading the plug-in to the forked version a breeze. </p><p>So my kudos go to Christian (for fixing the plug-in) and to GitHub (for making forking easy) :-)</p>http://musingsofaprogrammingaddict.blogspot.com/2011/10/using-pax-runner-with-eclipse-36.htmlnoreply@blogger.com (Anonymous)3tag:blogger.com,1999:blog-1306958590608174053.post-49804524872271787022011年8月01日 20:36:00 +00002011年08月01日T22:36:48.008+02:00Bean ValidationJSR 349What would YOU like to see included in Bean Validation 1.1?<p>Maybe you know it already: last week <a href="http://jcp.org/en/jsr/detail?id=303">JSR 349</a> successfully passed the review ballot, meaning that the work on a new release of the Bean Validation API has officially started.</p><p>I'm really excited about this, because I've got the pleasure to be part of the JSR 349 expert group. I haven't been member of such a comitee before, so I guess this will be exciting times and I'll surely learn a whole lot of new things during the work on the specification.</p><p>What's most important right now is your feedback: what new features would you like to see included in Bean Validation 1.1, what problems did you face when working with BV 1.0 which you think should be addressed in the next release?</p><p>Your input and feedback is highly welcome at several places:</p><ul><li>The Bean Validation <a href="https://forum.hibernate.org/viewforum.php?f=26">feedback forum</a> is a good platform for discussing ideas, requests for clarifications etc.</li> <li>For concrete feature requests or bug reports you can open an issue at the Bean Validation <a href="http://beanvalidation.org/issues">issue tracker</a></li> </ul><p>As a starting point you might want to check out <a href="http://in.relation.to/Bloggers/BeanValidation11HasStartedJoinUs">these two</a> <a href="http://in.relation.to/Bloggers/JSRBeanValidation11WhatToPutIn">blog posts</a> by Emmanuel Bernard (the Bean Validation spec. lead), where a lot of possible features are discussed.</p><p>Finally I'd like to point your attention to <a href="http://beanvalidation.org/">http://beanvalidation.org/</a>, the JSR's official web site. There you'll find everything related, e.g. a <a href="http://beanvalidation.org/news/">news section</a>, the <a href="http://beanvalidation.org/roadmap/">road map</a> and also the Bean Validation 1.0 <a href="http://beanvalidation.org/1.0/spec">spec. document</a> for reference.</p><p>Now it's up to you &#150; tell us what would YOU like to see included in Bean Validation 1.1.</p>http://musingsofaprogrammingaddict.blogspot.com/2011/08/what-would-you-like-to-see-included-in.htmlnoreply@blogger.com (Anonymous)0tag:blogger.com,1999:blog-1306958590608174053.post-53778654596927690112011年5月08日 11:10:00 +00002011年05月08日T13:10:26.775+02:00Bean ValidationHibernate ValidatorJavaSeamIntroducing Seam Validation<p><em>Note: This article originally appeared on <a href="http://in.relation.to/Bloggers/SeamModuleSpotlightSeamValidation">in.relation.to</a> as part of the Seam Module Spotlight series. In order to spread the word on Seam Validation I thought it might be a good idea to post it in this blog, too.</em></p><p><a href="http://seamframework.org/Seam3/ValidationModule">Seam Validation</a> is a relatively new Seam module which provides an integration between <a href="http://www.hibernate.org/subprojects/validator.html">Hibernate Validator</a> which is the reference implementation of the Bean Validation standard (<a href="http://jcp.org/en/jsr/detail?id=303">JSR 303</a>) and CDI. The module's functionality falls into two areas:</p><ul><li>Enhanced dependency injection services for validators, validator factories and constraint validators</li> <li>Automatic validation of method parameters and return values based on Hibernate Validator's method validation API</li> </ul><p>Let's take a closer look at these features in the following.</p><h2>Dependency injection services</h2><p>The Bean Validation API is an integral part of the Java EE platform. So when running on a Java EE 6 compatible application server such as <a href="http://www.jboss.org/jbossas/">JBoss AS 6</a> you can easily retrieve <code>Validator</code> and <code>ValidatorFactory</code> instances via dependency injection.</p><p>Unfortunately this is not working out of the box when running in non-EE environments such as plain servlet containers. But don't worry, Seam Validation comes to the rescue. By adding the module as dependency to your project, validators and validator factories can be obtained using <code>@Inject</code>:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">Foo</span> {<tt> </tt><tt> </tt> <span class="at">@Inject</span><tt> </tt> <span class="di">private</span> <span class="pt">Validator</span> validator;<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> doSomething() {<tt> </tt><tt> </tt> Bar bar = <span class="kw">new</span> Bar();<tt> </tt><tt> </tt> <span class="pt">Set</span>&lt;ConstraintViolation&lt;Bar&gt;&gt; constraintViolations = validator.validate(bar);<tt> </tt> <span class="c">//examine the violations ...</span><tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>But Seam Validation goes one step further and enables dependency injection also within constraint validator implementations. For instance we might have a custom constraint <code>@ValidLicensePlate</code> which is used to ensure that a car has a license plate which is properly registered, still valid etc.:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">Car</span> {<tt> </tt><tt> </tt> <span class="at">@ValidLicensePlate</span><tt> </tt> <span class="di">private</span> <span class="pt">String</span> licensePlate;<tt> </tt><tt> </tt> <span class="c">//constructor, getter, setter etc.</span><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>In order to perform the required checks the constraint's validator invokes some sort of registration service (note: accessing external services can potentially be an expensive operation, so it may be a good idea to put the constraint into a separate <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-usingvalidator.html#validator-usingvalidator-validationgroups">validation group</a>, but that's out of scope for this article). The Seam Validation module allows to inject this service into the validator as into any other CDI bean:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">ValidLicensePlateValidator</span> <span class="di">implements</span> ConstraintValidator&lt;ValidLicensePlate, <span class="pt">String</span>&gt; {<tt> </tt><tt> </tt> <span class="at">@Inject</span><tt> </tt> <span class="di">private</span> RegistrationService registrationService;<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> initialize(ValidLicensePlate constraintAnnotation) {<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid(<span class="pt">String</span> value, ConstraintValidatorContext context) {<tt> </tt><tt> </tt> <span class="kw">if</span> (value == <span class="pc">null</span>) {<tt> </tt> <span class="kw">return</span> <span class="pc">true</span>;<tt> </tt> }<tt> </tt><tt> </tt> <span class="c">//delegate to registration service</span><tt> </tt> <span class="kw">return</span> registrationService.isValid(value);<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>There is just on more step required before the <code>@ValidLicensePlate</code> constraint can be used: We have to register Seam Validation's constraint validator factory with the Bean Validation provider. This happens by putting a file named validation.xml with the following content into the META-INF folder of the application:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="pp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><tt> </tt><span class="ta">&lt;validation-config</span> <tt> </tt> <span class="an">xmlns</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/configuration</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xsi:schemaLocation</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt><tt> </tt> <span class="ta">&lt;constraint-validator-factory&gt;</span><tt> </tt> org.jboss.seam.validation.InjectingConstraintValidatorFactory<tt> </tt> <span class="ta">&lt;/constraint-validator-factory&gt;</span><tt> </tt><span class="ta">&lt;/validation-config&gt;</span><tt> </tt></pre></td> </tr></table></div><h2>Method validation</h2><p>The other main feature of Seam Validation besides dependency injection is the automatic validation of method parameters and return values based on the <a href="http://docs.jboss.org/hibernate/validator/4.2/api/index.html?org/hibernate/validator/method/MethodValidator.html">API for method validation</a> introduced with Hibernate Validator 4.2 (note: at the time of writing Hibernate Validator 4.2 is released as Beta version, but no major changes are expected until the final release).</p><p>This API provides an easy-to-use facility for the "Programming by Contract" approach to program design based on the concepts defined by the Bean Validation API. That means that any Bean Validation constraint can be used to describe</p><ul><li>the preconditions that must be met before a method may be invoked (by annotating method parameters with constraints) and</li> <li>the postconditions that are guaranteed after a method invocation returns (by annotating methods).</li> </ul><p>Things should become clearer with an example:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@AutoValidating</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">RentalStation</span> {<tt> </tt><tt> </tt> <span class="at">@Valid</span><tt> </tt> <span class="di">public</span> Car rentCar(<span class="at">@NotNull</span> CustomerPK customerPK, <span class="at">@NotNull</span> <span class="at">@Future</span> <span class="pt">Date</span> startDate, <span class="at">@Min</span>(<span class="i">1</span>) <span class="ty">int</span> durationInDays) {<tt> </tt><tt> </tt> <span class="c">//highly complex car rental business logic ...</span><tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>Here the following pre- and postconditions are applied onto the <code>rentCar()</code> method:</p><ul><li>The customer's primary key may not be null</li> <li>The rental start date must not be null and must be in the future</li> <li>The rental duration must be at least one day</li> <li>The returned object must be valid with respect to all constraints of the <code>Car</code> class</li> </ul><p>By annotating the class <code>RentalStation</code> with <code>@AutoValidating</code>, all these constraints are automatically validated by a method interceptor whenever the <code>rentCar()</code> method is invoked. If any of the constraints can't be validated successfully, a <a href="http://docs.jboss.org/hibernate/validator/4.2/api/index.html?org/hibernate/validator/method/MethodConstraintViolation.html">runtime exception</a> describing the occurred constraint violation(s) is thrown.</p><p>This approach has several advantages over traditional parameter and return value checking:</p><ul><li>The checks don't have to be performed by hand. This results in less boiler-plate code to write (and to read).</li> <li>A method's pre- and postconditions are part of its API and generated documentation. It's not necessary to add comments such as "returns never null", as this information will automatically be put into the generated JavaDoc. This results in less redundancies and hence in less potential inconsistencies between implementation and documentation.</li> </ul><h2>Future steps</h2><p>This concludes our short tour through the Seam Validation module. If you feel like trying out the module yourself, the <a href="http://seamframework.org/Seam3/ValidationModule">module home page</a> is the right starting point. You might also be interested in the module's <a href="http://docs.jboss.org/seam/3/validation/snapshot/reference/en-US/html_single/">reference guide</a>, and in case you found a bug or want to create a feature request the module's <a href="https://issues.jboss.org/browse/SEAMVALIDATE">JIRA instance</a> is the place to go.</p><p>Possible future developments of Seam Validation include a closer integration with other Seam 3 modules, the possibility to chose the validation groups to be used for method validation and the evaluation of standard bean constraints upon method validation.</p>http://musingsofaprogrammingaddict.blogspot.com/2011/05/introducing-seam-validation.htmlnoreply@blogger.com (Anonymous)1tag:blogger.com,1999:blog-1306958590608174053.post-5712279045959888352011年3月05日 16:39:00 +00002011年03月05日T17:39:52.697+01:00key words: JBoss Community Award2011 JBoss Community Awards: I need your vote!<p>I need your help. </p><p>To be more concise, I need your vote for the <a href="http://www.jboss.org/jbcra/">2011 JBoss Community Recognition Awards</a>.</p><p>These awards are an effort of <a href="http://www.jboss.org/">JBoss/Red Hat</a> to honor significant contributions to the JBoss community over the last year and I'm more than proud to say that I've been nominated in the "New Features" category for my contributions to <a href="http://validator.hibernate.org/">Hibernate Validator</a>.</p><p>So if you appreciate my work on Hibernate Validator &#150; e.g. the HV <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e2704">annotation processor</a> or the <a href="http://musingsofaprogrammingaddict.blogspot.com/2011/01/method-validation-with-hibernate.html">method validation feature</a> I've built for HV 4.2 &#150; or the <a href="http://seamframework.org/Seam3/ValidationModule">Validation module</a> for Seam 3 (I'll blog on this in a separate post) I've recently started, just head over to <a href="http://community.jboss.org/polls/1066">the poll</a> and give me your vote. The voting is open until April, 2nd.</p><p>And if you really like my work, go and tell your friends that they should vote for me, too.</p><p>Just in case you feel like you need some more information to make a profound vote you might also check out my <a href="http://www.jboss.org/jbcra/nominees.html">nominee description</a>.</p><p>Another nomination I'd like to mention is the one of my fellow developer Kevin Pollet in the "Bug Fixes" category. He has done some really awesome work for HV 4.2. Besides fixing many bugs he implemented several new features like the integration with the Joda time API or the support for dynamic default group sequence definitions.</p><p>So when you (and your friends) have voted for me, don't forget to <a href="http://community.jboss.org/polls/1067">vote</a> for Kevin, too :)</p>http://musingsofaprogrammingaddict.blogspot.com/2011/03/2011-jboss-community-awards-i-need-your.htmlnoreply@blogger.com (Anonymous)0tag:blogger.com,1999:blog-1306958590608174053.post-25119200725099904872011年1月16日 21:32:00 +00002011年01月16日T22:32:13.593+01:00Bean ValidationHibernate ValidatorJavaJSR 303Method Validation with Hibernate Validator 4.2<p>Earlier this week the first beta version of Hibernate Validator 4.2 was released. Hardy's announcement on <a href="http://in.relation.to/18074.lace">in.relation.to</a> describes in detail what's new in this release, so be sure to check it out.</p><p>One of the new features I'm really excited about is method validation, which I therefore would like to discuss in more detail in the following.</p><h2>What's it about?</h2><p>Up to now constraint annotations were only supported at fields or at getter methods for property constraints and at type declarations for class-level constraints.</p><p>With HV 4.2 it is now also possible to use constraint annotations for a programming style known as <a href="http://en.wikipedia.org/wiki/Programming_by_contract">"Programming by Contract"</a>. More specifically this means that any Bean Validation constraints (built-in as well as custom constraints) can be used to specify </p><ul><li>any preconditions that must be met before a method invocation (by annotating method parameters with constraints) and</li> <li>any postconditions that are guaranteed after a method invocation (by annotating methods)</li> </ul><p>As example let's consider the following class from a fictitious video rental application:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">MovieRepository</span> {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="at">@NotNull</span> <span class="pt">Set</span>&lt;Movie&gt; findMoviesByTitle( <tt> </tt> <span class="at">@NotNull</span> <span class="at">@Size</span>(min = <span class="i">3</span>) <span class="pt">String</span> title) {<tt> </tt><tt> </tt> <span class="c">//search movie in database ...</span><tt> </tt> } <tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>The following pre- and postconditions will hold here (provided the method validation is triggered automatically by some integration layer, see below):</p><ul><li>The title parameter is guaranteed to be not null and at least 3 characters long</li> <li>The return set of movies is guaranteed to be not null</li> </ul><p>Compared to traditional ways of parameter or result value checking this approach has two big advantages:</p><ul><li>These checks don't have to be performed manually, which results in less code to write and maintain. You'll never again have to throw IllegalArgumentExceptions or similar by hand.</li> <li>The pre- and postconditions of a method don't have to be expressed again in the method's JavaDoc, since any of it's annotations will automatically be included in the generated JavaDoc. This means less redundancy which reduces the chance of inconsistencies between implementation and comments.</li> </ul><p>Method validation is not restricted to constraints related to parameters or to return values themselves. Using the @Valid annotation it is also possible to recursively validate parameters or return values:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">UserRepository</span> {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> persistUser(<span class="at">@NotNull</span> <span class="at">@Valid</span> User user) {<tt> </tt><tt> </tt> <span class="c">//persist user in database ...</span><tt> </tt> } <tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>In this example not only the @NotNull constraint at the user parameter would be checked, but also any constraints defined at the User type. As known from standard Bean Validation the @Valid annotation could also be applied to array-, <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Iterable.html">Iterable</a>- and <a href="http://download.oracle.com/javase/6/docs/api/java/util/Map.html">Map</a>-typed parameters or return values.</p><h2>The MethodValidator interface</h2><p>For the purposes of method validation Hibernate Validator defines the new interface <a href="http://docs.jboss.org/hibernate/validator/4.2/api/org/hibernate/validator/MethodValidator.html">MethodValidator</a> (any type or method names are working titles and might <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-415">change</a> until HV 4.2 is final), which provides methods for parameter and return value validation:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">interface</span> <span class="cl">MethodValidator</span> {<tt> </tt><tt> </tt> <span class="di">public</span> &lt;T&gt; <span class="pt">Set</span>&lt;MethodConstraintViolation&lt;T&gt;&gt; validateParameter(<tt> </tt> T object, <span class="pt">Method</span> method, <span class="pt">Object</span> parameterValue, <tt> </tt> <span class="ty">int</span> parameterIndex, <span class="pt">Class</span>&lt;?&gt;... groups);<tt> </tt><tt> </tt> <span class="di">public</span> &lt;T&gt; <span class="pt">Set</span>&lt;MethodConstraintViolation&lt;T&gt;&gt; validateParameters(<tt> </tt> T object, <span class="pt">Method</span> method, <span class="pt">Object</span><span class="ty">[]</span> parameterValues, <span class="pt">Class</span>&lt;?&gt;... groups);<tt> </tt><tt> </tt> <span class="di">public</span> &lt;T&gt; <span class="pt">Set</span>&lt;MethodConstraintViolation&lt;T&gt;&gt; validateReturnValue(<tt> </tt> T object, <span class="pt">Method</span> method, <span class="pt">Object</span> returnValue, <span class="pt">Class</span>&lt;?&gt;... groups);<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>To get hold of a method validator, <a href="http://download.oracle.com/javaee/6/api/javax/validation/Validator.html#unwrap(java.lang.Class)">unwrap</a> an instance of HV's <a href="http://download.oracle.com/javaee/6/api/javax/validation/Validator.html">Validator</a> implementation as follows:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">MethodValidator validator = Validation.byProvider( HibernateValidator.class )<tt> </tt> .configure()<tt> </tt> .buildValidatorFactory()<tt> </tt> .getValidator()<tt> </tt> .unwrap( MethodValidator.class );<tt> </tt></pre></td> </tr></table></div><p>All of MethodValidator's methods return a set of <a href="http://docs.jboss.org/hibernate/validator/4.2/api/org/hibernate/validator/MethodConstraintViolation.html">MethodConstraintValidation</a>s which describe in detail any constraint violations that occurred during validation. <code>MethodConstraintValidation</code> extends the standard <a href="http://download.oracle.com/javaee/6/api/javax/validation/ConstraintViolation.html">ConstraintViolation</a> and provides additional information such as method and parameter causing a constraint violation:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">interface</span> <span class="cl">MethodConstraintViolation</span>&lt;T&gt; <span class="di">extends</span> ConstraintViolation&lt;T&gt; {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="di">static</span> <span class="ty">enum</span> Kind {<tt> </tt><tt> </tt> PARAMETER,<tt> </tt><tt> </tt> RETURN_VALUE<tt> </tt> }<tt> </tt><tt> </tt> <span class="pt">Method</span> getMethod();<tt> </tt><tt> </tt> <span class="pt">Integer</span> getParameterIndex();<tt> </tt><tt> </tt> <span class="pt">String</span> getParameterName();<tt> </tt><tt> </tt> Kind getKind();<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>Unfortunately the byte code of a Java program normally doesn't contain any information on parameter names, therefore <code>getParameterName()</code> for now returns synthetic names such as <code>arg0</code> etc. For a future release we are investigating approaches for parameter naming, e.g. by leveraging JSR 330's @Named annotation. I created a JIRA <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-409">issue</a> for this, so if you have any feedback on this just add a comment to the ticket.</p><h2>Triggering method validation</h2><p>Hibernate Validator itself only provides the engine for method validation but it does not deal with triggering such a validation.</p><p>Instead this is typically done using frameworks/APIs which provide AOP or method interception services such as <a href="http://www.eclipse.org/aspectj/">AspectJ</a> or <a href="http://jcp.org/en/jsr/detail?id=299">CDI</a>. But also plain Java dynamic proxies can basically do the trick.</p><p>Typically such a validation interceptor does the following:</p><ul><li>intercept each method call to be validated,</li> <li>validate the invocation's parameter values,</li> <li>proceed with the method invocation and</li> <li>finally validate the invocation's return value. </li> </ul><p>If any of the two validation steps yields one ore more constraint violations the interceptor typically throws a <a href="http://docs.jboss.org/hibernate/validator/4.2/api/org/hibernate/validator/MethodConstraintViolationException.html">MethodConstraintViolationException</a> containing the constraint violations that have occurred.</p><h2>Trying it out yourself</h2><p>To see whether method validation works out as intended I started a project at GitHub called <a href="https://github.com/gunnarmorling/methodvalidation-integration">methodvalidation-integration</a> which provides validation handlers for some interception technologies popular these days. </p><p>Currently there are implementations for CDI as well as <a href="http://code.google.com/p/google-guice/">Google Guice</a> and an <a href="http://download.oracle.com/javase/6/docs/api/java/lang/reflect/InvocationHandler.html">InvocationHandler</a> which can be used with JDK dynamic proxies. I also plan to add support for Spring AOP/AspectJ when time permits.</p><p>To try method validation out yourself just get the bits from GitHub and have a look at the test in each of the sub-projects. Just give it a try and let me know how everything works.</p><h2>Future steps</h2><p>This post introduced the new method validation feature which will be part of Hibernate Validator 4.2. The first beta release contains the basic implementations of this, so we are more than interested in you feedback on this. Please report any bugs in HV's <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV">JIRA instance</a>, any questions or ideas for improvement are also highly welcome in HV's <a href="https://forum.hibernate.org/viewforum.php?f=9">forum</a>.</p><p>For HV 4.2 Beta2 we plan to <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-372">add documentation</a> on the method validation feature to HV's <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/">reference guide</a> which will also describe some advanced topics such as using method validation with group sequences etc. We will also <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-371">add support</a> for method level constraints to the constraint <a href="http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-metadata-api.html">meta-data API</a>. </p><p>Two further ideas scheduled for after 4.2 are providing the ability to specify method level constraints using <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-373">XML</a> and using the programmatic constraint configuration API.</p>http://musingsofaprogrammingaddict.blogspot.com/2011/01/method-validation-with-hibernate.htmlnoreply@blogger.com (Anonymous)17tag:blogger.com,1999:blog-1306958590608174053.post-24874047575470430752010年12月27日 17:38:00 +00002010年12月27日T18:38:59.245+01:00IntelliJ IDEAJavaMavenSetting the active Maven profile in IntelliJ IDEA<p>This is more a note to myself, but in case someone else wonders how to specify the active Maven profile in <a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a> for projects containing several profiles, that's the way to go:</p><ul><li>Open the "Maven Projects" window by selecting "Window" - "Tool Windows" - "Maven Projects"</li> <li>Expand the "Profiles" node and select the profile you want to use</li> </ul>http://musingsofaprogrammingaddict.blogspot.com/2010/12/setting-active-maven-profile-in.htmlnoreply@blogger.com (Anonymous)2tag:blogger.com,1999:blog-1306958590608174053.post-90945203275162449492010年12月27日 13:58:00 +00002010年12月27日T23:21:27.420+01:00Bean ValidationHibernate ValidatorJavaJSR 303SpELSpringPutting a SpEL(l) on the Bean Validation API<p>One of the new features introduced with version 3.0 of the <a href="http://www.springsource.org/">Spring framework</a> is the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html">Spring Expression Language</a> or short "SpEL". This language is tailored for the needs when working with Spring and can be used when creating XML/annotation based Spring bean definitions for instance.</p><p>So I thought it would be nice if it was possible to use SpEL together with Hibernate Validator's <a href="http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/constraints/ScriptAssert.html">@ScriptAssert</a> constraint which allows to express validation routines using script or expression languages.</p><p>Unfortunately this does not work since currently no SpEL language binding for <a href="http://jcp.org/en/jsr/detail?id=223">JSR 223</a> ("Scripting for the Java<sup>TM</sup> Platform") exists. As @ScriptAssert's validator uses JSR 223 for expression evaluation at least for now SpEL can't be used along with @ScriptAssert (there is an <a href="https://jira.springframework.org/browse/SPR-7651">issue</a> in Spring's JIRA addressing this problem).</p><p>But as shown in <a href="http://musingsofaprogrammingaddict.blogspot.com/2009/02/getting-started-with-jsr-303-bean.html">previous</a> <a href="http://musingsofaprogrammingaddict.blogspot.com/2010/05/providing-bean-validation-support-for.html">posts</a> it is very simple to create new constraint annotations for the Bean Validation API. So the idea is to build a new constraint @SpelAssert which resembles HV's @ScriptAssert but works with SpEL instead of the JSR 223 API.</p><p>Defining the annotation type is straight-forward:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Target</span>({ TYPE })<tt> </tt><span class="at">@Retention</span>(RUNTIME)<tt> </tt><span class="at">@Constraint</span>(validatedBy = SpelAssertValidator.class)<tt> </tt><span class="at">@Documented</span><tt> </tt><span class="di">public</span> <span class="at">@interface</span> SpelAssert {<tt> </tt><tt> </tt> <span class="pt">String</span> message() <span class="kw">default</span> <span class="s"><span class="dl">&quot;</span><span class="k">{de.gmorling.moapa.bvspel.SpelAssert.message}</span><span class="dl">&quot;</span></span>;<tt> </tt><tt> </tt> <span class="pt">Class</span>&lt;?&gt;<span class="ty">[]</span> groups() <span class="kw">default</span> {};<tt> </tt><tt> </tt> <span class="pt">Class</span>&lt;? <span class="di">extends</span> Payload&gt;<span class="ty">[]</span> payload() <span class="kw">default</span> {};<tt> </tt><tt> </tt> <span class="pt">String</span> value();<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>Besides the standard attributes <code>message()</code>, <code>groups()</code> and <code>payload()</code> mandated by the BV specification we define one more attribute <code>value()</code>, which takes the SpEL expression to evaluate.</p><p>Now let's come to the validator:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">SpelAssertValidator</span> <span class="di">implements</span><tt> </tt>ConstraintValidator&lt;SpelAssert, <span class="pt">Object</span>&gt; {<tt> </tt><tt> </tt> <span class="at">@Inject</span><tt> </tt> <span class="di">private</span> ExpressionParser parser;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Expression</span> expression;<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> initialize(SpelAssert constraintAnnotation) {<tt> </tt><tt> </tt> <span class="pt">String</span> rawExpression = constraintAnnotation.value();<tt> </tt><tt> </tt> <span class="kw">if</span> (rawExpression == <span class="pc">null</span>) {<tt> </tt> <span class="kw">throw</span> <span class="kw">new</span> <span class="ex">IllegalArgumentException</span>(<span class="s"><span class="dl">&quot;</span><span class="k">The expression specified in @</span><span class="dl">&quot;</span></span><tt> </tt> + SpelAssert.class.getSimpleName() + <span class="s"><span class="dl">&quot;</span><span class="k"> must not be null.</span><span class="dl">&quot;</span></span>);<tt> </tt> }<tt> </tt><tt> </tt> expression = parser.parseExpression(rawExpression);<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid(<span class="pt">Object</span> value, ConstraintValidatorContext context) {<tt> </tt><tt> </tt> <span class="kw">if</span> (value == <span class="pc">null</span>) {<tt> </tt> <span class="kw">return</span> <span class="pc">true</span>;<tt> </tt> }<tt> </tt><tt> </tt> <span class="kw">return</span> <span class="pt">Boolean</span>.TRUE.equals(expression.getValue(value, <span class="pt">Boolean</span>.class));<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>In <code>initialize()</code> we use an <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/expression/ExpressionParser.html">ExpressionParser</a> to parse the specified SpEL expression (so this happens only once) and evaluate the given object against it in <code>isValid()</code>.</p><p>But wait a minute, where does the ExpressionParser come from, it is not instantiated here?</p><p>Right, the cool thing is Spring comes with it's own <a href="http://download.oracle.com/javaee/6/api/javax/validation/ConstraintValidatorFactory.html">ConstraintValidatorFactory</a> which performs dependency injection on constraint validators. A validator relying on that feature of course is not portable, but as this validator is based on SpEL and Spring anyways this is not an issue here.</p><p>In order to have this working a parser bean must be part of the Spring application context. We just register a <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/expression/spel/standard/SpelExpressionParser.html">SpelExpressionParser</a>:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="ta">&lt;beans</span> <tt> </tt> <span class="an">xmlns</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.springframework.org/schema/beans</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xsi:schemaLocation</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.springframework.org/schema/beans</span><tt> </tt> <span class="k">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt><tt> </tt> <span class="ta">&lt;bean</span> <span class="an">id</span>=<span class="s"><span class="dl">&quot;</span><span class="k">parser</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">class</span>=<span class="s"><span class="dl">&quot;</span><span class="k">org.springframework.expression.spel.standard.SpelExpressionParser</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt><tt> </tt> <span class="ta">&lt;bean</span> <span class="an">id</span>=<span class="s"><span class="dl">&quot;</span><span class="k">validator</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">class</span>=<span class="s"><span class="dl">&quot;</span><span class="k">org.springframework.validation.beanvalidation.LocalValidatorFactoryBean</span><span class="dl">&quot;</span></span><span class="ta">/&gt;</span><tt> </tt><span class="ta">&lt;/beans&gt;</span><tt> </tt></pre></td> </tr></table></div><p>This context also shows how to provide a BV <a href="http://download.oracle.com/javaee/6/api/javax/validation/Validator.html">Validator</a> for dependency injection leveraging Spring's <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.html">LocalValidatorFactoryBean</a>.</p><p>Now let's have a look at the @SpelAssert constraint in action. The following shows the canonical example of a class CalendarEvent where the start date shall always be earlier than the end date:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@SpelAssert</span>(<span class="s"><span class="dl">&quot;</span><span class="k">startDate &lt; endDate</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> endDate;<tt> </tt><tt> </tt> <span class="c">// getters, setters etc.</span><tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>Note how SpEL allows dates to be compared using the "&lt;" operator and that no alias for the evaluated bean is required, as all unqualified attribute/method names are resolved against the annotated object.</p><p>Finally we should have a test showing that the validator works as expected by validating a valid and an invalid CalendarEvent instance:</p><div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt>32<tt> </tt>33<tt> </tt>34<tt> </tt>35<tt> </tt>36<tt> </tt>37<tt> </tt>38<tt> </tt>39<tt> </tt><strong>40</strong><tt> </tt>41<tt> </tt>42<tt> </tt>43<tt> </tt>44<tt> </tt>45<tt> </tt>46<tt> </tt>47<tt> </tt>48<tt> </tt>49<tt> </tt><strong>50</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@RunWith</span>(SpringJUnit4ClassRunner.class)<tt> </tt><span class="at">@ContextConfiguration</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">SpelAssertTest</span> {<tt> </tt><tt> </tt> <span class="at">@Inject</span><tt> </tt> <span class="di">private</span> <span class="pt">Validator</span> validator;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> startDate;<tt> </tt> <span class="di">private</span> <span class="pt">Date</span> endDate;<tt> </tt><tt> </tt> <span class="at">@Before</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> setUpDates() {<tt> </tt><tt> </tt> <span class="pt">Calendar</span> start = <span class="pt">Calendar</span>.getInstance();<tt> </tt> start.set(<span class="i">2010</span>, <span class="i">13</span>, <span class="i">24</span>);<tt> </tt> startDate = start.getTime();<tt> </tt><tt> </tt> <span class="pt">Calendar</span> end = <span class="pt">Calendar</span>.getInstance();<tt> </tt> end.set(<span class="i">2010</span>, <span class="i">13</span>, <span class="i">26</span>);<tt> </tt> endDate = end.getTime();<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> validEvent() {<tt> </tt><tt> </tt> CalendarEvent event = <span class="kw">new</span> CalendarEvent();<tt> </tt> event.setStartDate(startDate);<tt> </tt> event.setEndDate(endDate);<tt> </tt><tt> </tt> assertTrue(validator.validate(event).isEmpty());<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> invalidEventYieldsConstraintViolation() {<tt> </tt><tt> </tt> CalendarEvent event = <span class="kw">new</span> CalendarEvent();<tt> </tt> event.setStartDate(endDate);<tt> </tt> event.setEndDate(startDate);<tt> </tt><tt> </tt> <span class="pt">Set</span>&lt;ConstraintViolation&lt;CalendarEvent&gt;&gt; violations = <tt> </tt> validator.validate(event);<tt> </tt> assertEquals(<span class="i">1</span>, violations.size());<tt> </tt><tt> </tt> ConstraintViolation&lt;CalendarEvent&gt; violation = <tt> </tt> violations.iterator().next();<tt> </tt> assertEquals(<tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k">SpEL expression </span><span class="ch">\&quot;</span><span class="k">startDate &lt; endDate</span><span class="ch">\&quot;</span><span class="k"> didn't evaluate to true.</span><span class="dl">&quot;</span></span>,<tt> </tt> violation.getMessage());<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table></div><p>The complete sources for this post can be found at <a href="https://github.com/gunnarmorling/musings-of-a-programming-addict/blob/master/bv-spel/">GitHub</a>, so don't hesistate to give it a try or use it in your projects if you like. Any feedback or ideas for improvement are warmly welcome.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/12/putting-spell-on-bean-validation-api.htmlnoreply@blogger.com (Anonymous)6tag:blogger.com,1999:blog-1306958590608174053.post-16077222076617642432010年11月17日 16:40:00 +00002010年11月17日T17:40:06.362+01:00JavashellSVNIdentifying files with missing SVN propertiesAt my day job we are using the SVN property <a href="http://svnbook.red-bean.com/nightly/en/svn.advanced.props.special.keywords.html">"svn:keywords"</a> to let SVN replace the string "$Id$" with author name, revision number etc. within each Java source file.<br /> <br /> One can add this property automatically when creating new files with the help of SVN's <a href="http://svnbook.red-bean.com/nightly/en/svn.advanced.props.html#svn.advanced.props.auto">auto props</a> feature. But from time to time someone, e.g. a new developer not knowing about auto props, checks in files without having the "svn:keywords" property set.<br /> <br /> So I wondered how to identify such files in the repository. SVN doesn't provide a command answering that question, you only can retrieve all files having a certain property set.<br /> <br /> But no problem, some shell magic to the rescue:<br /> <br /> <div class="gmCode"><table class="CodeRay"><tbody> <tr> <td class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }" title="click to toggle"><pre>1<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">comm -23 &lt;(sort &lt;(sed 's/\.\///g' &lt;(find . -name "*.java"))) &lt;(sort &lt;(sed 's/ - Id//g' &lt;(svn propget svn:keywords * -R)))<tt> </tt></pre></td> </tr> </tbody></table></div><br /> So what's happening here? The basic idea is to list those files <i>with</i> the svn:keywords property set (<a href="http://svnbook.red-bean.com/nightly/en/svn.ref.svn.c.propget.html"><code>svn propget</code></a>) and compare this to a list with <i>all</i> files (<a href="http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/find.1.html"><code>find</code></a>).<br /> <br /> The outputs of both commands are brought into the same format using <a href="http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sed.1.html"><code>sed</code></a>, <a href="http://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sort.1.html">sorted</a> and then passed as parameters to the <a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/comm.1.html"><code>comm</code></a> command, which compares two input files to each other. The <code>-23</code> parameter causes only those lines to be put out which are only contained in file1 but not in file2, which are exactly the names of those files lacking the "svn:keywords" property.<br /> <br /> I tested the command successfully on Mac OS X, but I think it should work pretty much the same way on other Unix systems, too.http://musingsofaprogrammingaddict.blogspot.com/2010/11/identifying-files-with-missing-svn.htmlnoreply@blogger.com (Anonymous)49tag:blogger.com,1999:blog-1306958590608174053.post-75995166416718997432010年8月15日 18:44:00 +00002010年08月15日T20:45:45.168+02:00EclipseJavaJDTRCPRetrieving JDT sources with Eclipse 3.6<p>While developing an Eclipse plug-in for a small spare-time project of mine I wondered where to find the sources of the <a href="http://www.eclipse.org/jdt/">Java Development Tools</a> (JDT). They used to be part of the Eclipse RCP/plug-in developers package in previous releases, but this isn't the case anymore with <a href="http://www.eclipse.org/helios/">Eclipse 3.6 (Helios)</a>.</p><p>After some googling I found <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=322513">the</a> <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=230357">reason</a>: as of release 3.6 the RCP distribution (now named <a href="http://www.eclipse.org/downloads/packages/eclipse-rcp-and-rap-developers/heliosr">"Eclipse for RCP and RAP Developers"</a>) no longer contains any sources (except those for the actual platform plug-ins) in order to reduce the size of the download package.</p><p>If you want to get the sources for plug-ins such as JDT which are not part of the core platform you have to retrieve them separately. To do so you can use the new <a href="http://archive.eclipse.org/eclipse/downloads/drops/S-3.6M7-201004291549/eclipse-news-M7.html#PDE">plug-in import wizard</a> which allows to fetch source projects corresponding to plug-ins of the target platform directly from the Eclipse CVS.</p><p>Just open the "Plug-ins" view, right-click on the plug-in you want to retrieve (e.g. <code>org.eclipse.jdt.ui</code>) and select "Import as" > "Project from a Repository ...". Confirm the next dialog by clicking "Finish", and the check-out starts. Afterwards the new project will automatically replace the plug-in JAR as dependency in any dependent projects within your workspace. </p>http://musingsofaprogrammingaddict.blogspot.com/2010/08/retrieving-jdt-sources-with-eclipse-36.htmlnoreply@blogger.com (Anonymous)3tag:blogger.com,1999:blog-1306958590608174053.post-65086396027894014112010年7月24日 12:41:00 +00002010年07月25日T17:55:07.718+02:00CDIDependency InjectionJavaJSR 223JSR 299A CDI portable extension for JSR 223 scripting engines<p>In the last couple of days I spent some time experimenting a little bit with <a href="http://jcp.org/en/jsr/detail?id=299">CDI</a>, the standard of the Java EE 6 platform for dependency injection services.</p> <p>I must say that I really like that spec, as it hits the sweet spot between specifying features that provide a value out of the box (<a href="http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/#injection">type-safe DI</a>, <a href="http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/#events">eventing</a>, <a href="http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/#interceptors">interceptor services</a> etc.) and being open enough to allow people to build totally new stuff based on it (using <a href="http://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html_single/#extend">portable extensions</a>).</p> <p>I've got the feeling we're going to see a lot of exciting things based on CDI within the near future. Actually this reminds me a bit of Java annotations. Having been introduced with Java 5, just over time people started using them for more and more use cases that had not been foreseen in the first place.</p> <p>Anyway, to do something practical with CDI I built a small portable extension which allows to retrieve <a href="http://jcp.org/en/jsr/detail?id=223">JSR 223</a> scripting engines using dependency injection.</p> <p>Just annotate any injection points of type <code>javax.script.ScriptEngine</code> with one of the qualifier annotations <code>@Language</code>, <code>@Extension</code> or <code>@MimeType</code>. The following code extract shows an example of a JavaScript engine (for example the <a href="http://www.mozilla.org/rhino/">Rhino</a> engine shipping with Java 6) being injected into some managed bean, where it can be used for arbitrary script evaluations:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@RequestScoped</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">MyBean</span> {<tt> </tt><tt> </tt> <span class="at">@Inject</span> <tt> </tt> <span class="at">@Language</span>(<span class="s"><span class="dl">&quot;</span><span class="k">javascript</span><span class="dl">&quot;</span></span>) <tt> </tt> <span class="di">private</span> ScriptEngine jsEngine;<tt> </tt><tt> </tt> <span class="c">// ...</span><tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> foo() <span class="di">throws</span> ScriptException {<tt> </tt><tt> </tt> <span class="kw">assert</span> <span class="fl">42.0d</span> == (<span class="pt">Double</span>)jsEngine.eval(<span class="s"><span class="dl">&quot;</span><span class="k">2 * 21</span><span class="dl">&quot;</span></span>);<tt> </tt> <span class="c">// ...</span><tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The extension can be found in my <a href="http://gunnarmorling-maven-repo.googlecode.com/svn/repo/de/gmorling/cdi/extensions/scripting-extension/">Maven repository</a>. Just add the following dependency to your POM in order to use it:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="ta">&lt;dependency&gt;</span><tt> </tt> <span class="ta">&lt;groupId&gt;</span>de.gmorling.cdi.extensions<span class="ta">&lt;/groupId&gt;</span><tt> </tt> <span class="ta">&lt;artifactId&gt;</span>scripting-extension<span class="ta">&lt;/artifactId&gt;</span><tt> </tt> <span class="ta">&lt;version&gt;</span>0.1<span class="ta">&lt;/version&gt;</span><tt> </tt><span class="ta">&lt;/dependency&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>In case you want to take a look at the source code (which is just a few lines), you can check it out from <a href="http://github.com/gunnarmorling/scripting-extension/">GitHub</a>. As always, any ideas for improvement or other feedback are highly appreciated.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/07/cdi-portable-extension-for-jsr-223.htmlnoreply@blogger.com (Anonymous)1tag:blogger.com,1999:blog-1306958590608174053.post-49441537898066792012010年6月28日 21:33:00 +00002010年07月12日T00:15:57.036+02:00Bean ValidationHibernate ValidatorJavaJSR 223JSR 303What's new in Hibernate Validator 4.1?<p>I'm very proud to report that today <a href="http://hibernate.org/hibernate/subprojects/validator.html">Hibernate Validator</a> 4.1.0 Final has been <a href="http://in.relation.to/Bloggers/HibernateValidator410FinalHasLanded">released</a>. Besides many bug fixes this release adds also a lot of new features to the code base.</p> <p>The changes fall into four areas, which I'm going to discuss in detail in the following:</p> <ul> <li>New constraint annotations</li> <li>ResourceBundleLocator API</li> <li>API for programmatic constraint creation</li> <li>Constraint annotation processor</li> </ul> <h2>New constraint annotations</h2> <p>In addition to the constraints defined in the Bean Validation spec and those custom constraints already part of HV 4.0, the new release ships with the following new constraint annotations:</p> <ul> <li><a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/constraints/CreditCardNumber.html">@CreditCardNumber</a>: Validates that a given String represents a valid credit card number using the Luhn algorithm. Useful to detect mis-entered numbers for instance.</li> <li><a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/constraints/NotBlank.html">@NotBlank</a>: Validates that a given String is neither null nor empty nor contains only whitespaces.</li> <li><a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/constraints/URL.html">@URL</a>: Validates that a given String is a valid URL. Can be restricted to certain protocols if required: <code>@URL(protocol = "http") private String url;</code></li> <li><a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/constraints/ScriptAssert.html">@ScriptAssert</a>: Allows to use scripting or expression languages for the definition of class-level validation routines.</li> </ul> <p>Let's have a closer look at the @ScriptAssert constraint, which I'm particularly excited about as I have implemented it :-). </p> <p>The intention behind it is to provide a simplified way for expressing validation logic that is based on multiple attributes of a given type. Instead of having to implement dedicated class-level constraints the @ScriptAssert constraint allows to express such validation routines in an ad hoc manner using a wide range of scripting and expression languages.</p> <p>In order to use this constraint an implementation of the Java Scripting API as defined by <a href="http://jcp.org/en/jsr/detail?id=223">JSR 223</a> ("Scripting for the Java<sup>TM</sup> Platform") must be part of the class path. This is automatically the case when running on Java 6. For older Java versions, the JSR 223 RI can be added manually to the class path.</p> <p>As example let's consider a class representing calendar events. The start date of such an event shall always be earlier than the end date. Using JavaScript (for which an engine comes with Java 6) this requirement could be expressed as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@ScriptAssert</span>(lang = <span class="s"><span class="dl">&quot;</span><span class="k">javascript</span><span class="dl">&quot;</span></span>, script = <span class="s"><span class="dl">&quot;</span><span class="k">_this.startDate.before(_this.endDate)</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> endDate;<tt> </tt><tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>So all you have to do is to specify a scripting expression returning true or false within the <code>script</code> attribute. The expression must be implemented in the language given within the <code>language</code> attribute. The language name must be the language's name as registered with the JSR 223 <a href="http://java.sun.com/javase/6/docs/api/javax/script/ScriptEngineManager.html">ScriptEngineManager</a>. Within the expression the annotated element can be accessed using the alias <code>_this</code> by default.</p> <p>The cool thing is that the @ScriptConsert constraint can be used with any other scripting language for which a JSR 223 binding exists. Let's take <a href="http://commons.apache.org/jexl/">JEXL</a> from the Apache Commons project for instance. Using Maven you only have to add the following dependency:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="ta">&lt;dependency&gt;</span><tt> </tt> <span class="ta">&lt;groupId&gt;</span>org.apache.commons<span class="ta">&lt;/groupId&gt;</span><tt> </tt> <span class="ta">&lt;artifactId&gt;</span>commons-jexl<span class="ta">&lt;/artifactId&gt;</span><tt> </tt> <span class="ta">&lt;version&gt;</span>2.0.1<span class="ta">&lt;/version&gt;</span><tt> </tt><span class="ta">&lt;/dependency&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>With JEXL dates can be compared using the "&lt;" operator. Using a shorter alias for the evaluated object the constraint from above therefore can be rewritten as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@ScriptAssert</span>(lang = <span class="s"><span class="dl">&quot;</span><span class="k">jexl</span><span class="dl">&quot;</span></span>, script = <span class="s"><span class="dl">&quot;</span><span class="k">_.startDate &lt; _.endDate</span><span class="dl">&quot;</span></span>, alias = <span class="s"><span class="dl">&quot;</span><span class="k">_</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> endDate;<tt> </tt><tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Very likely one will work with only one scripting language throughout all @ScriptAssert constraints of an application. So let's leverage the power of constraint composition to create a custom constraint which allows for an even more compact notation by setting the attributes <code>lang</code> and <code>alias</code> to fixed values: </p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Target</span>({ TYPE })<tt> </tt><span class="at">@Retention</span>(RUNTIME)<tt> </tt><span class="at">@Constraint</span>(validatedBy = {})<tt> </tt><span class="at">@Documented</span><tt> </tt><span class="at">@ScriptAssert</span>(lang = <span class="s"><span class="dl">&quot;</span><span class="k">jexl</span><span class="dl">&quot;</span></span>, script = <span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span>, alias = <span class="s"><span class="dl">&quot;</span><span class="k">_</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="at">@interface</span> JexlAssert {<tt> </tt><tt> </tt> <span class="pt">String</span> message() <span class="kw">default</span> <span class="s"><span class="dl">&quot;</span><span class="k">{org.hibernate.validator.constraints.ScriptAssert.message}</span><span class="dl">&quot;</span></span>;<tt> </tt><tt> </tt> <span class="pt">Class</span>&lt;?&gt;<span class="ty">[]</span> groups() <span class="kw">default</span> {};<tt> </tt><tt> </tt> <span class="pt">Class</span>&lt;? <span class="di">extends</span> Payload&gt;<span class="ty">[]</span> payload() <span class="kw">default</span> {};<tt> </tt><tt> </tt> <span class="at">@OverridesAttribute</span>(constraint = ScriptAssert.class, name = <span class="s"><span class="dl">&quot;</span><span class="k">script</span><span class="dl">&quot;</span></span>)<tt> </tt> <span class="pt">String</span> value();<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Note how the <code>script</code> attribute of the composing @ScriptAssert constraint is overridden using the <a href="http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/validation/OverridesAttribute.html">@OverridesAttribute</a> meta-annotation. Using this custom constraint the example finally reads as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@JexlAssert</span>(<span class="s"><span class="dl">&quot;</span><span class="k">_.startDate &lt; _.endDate</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="pt">Date</span> endDate;<tt> </tt><tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>As shown the @ScriptAssert constraint allows to define class-level constraints in a very compact way. </p> <p>But there is also a price to pay. As scripting languages are used, compile-time type-safety is lost. When for instance the <code>startDate</code> attribute is renamed the script expression would have to be adapted by hand. Also evaluation performance should be considered when validation logic is getting more complex. </p> <p>So I recommend to try it out and choose what ever fits your needs best.</p> <h2>The ResourceBundleLocator API</h2> <p>The Bean Validation API defines the <a href="http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/validation/MessageInterpolator.html">MessageInterpolator</a> interface which allows to plug in custom strategies for message interpolation and resource bundle loading. </p> <p>As it turned out, most users only want to customize the latter aspect (e.g. in order to load message bundles from a database) but would like to re-use the interpolation algorithm provided by Hibernate Validator.</p> <p>Therefore Hibernate Validator 4.1 introduces the interface <a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/resourceloading/ResourceBundleLocator.html">ResourceBundleLocator</a> which is used by HV's default MessageInterpolator implementation <a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.html">ResourceBundleMessageInterpolator</a> to do the actual resource bundle loading.</p> <p>The interface defines only one method, in which implementations have to return the bundle for a given locale:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">interface</span> <span class="cl">ResourceBundleLocator</span> {<tt> </tt><tt> </tt> <span class="pt">ResourceBundle</span> getResourceBundle(<span class="pt">Locale</span> locale);<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The ResourceBundleLocator to be used can be set when creating a <a href="http://download.oracle.com/docs/cd/E17410_01/javaee/6/api/javax/validation/ValidatorFactory.html">ValidatorFactory</a>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">ValidatorFactory validatorFactory = Validation<tt> </tt> .byProvider(HibernateValidator.class)<tt> </tt> .configure()<tt> </tt> .messageInterpolator(<tt> </tt> <span class="kw">new</span> ResourceBundleMessageInterpolator(<tt> </tt> <span class="kw">new</span> MyCustomResourceBundleLocator()))<tt> </tt> .buildValidatorFactory();<tt> </tt></pre></td> </tr></table> </div> <p>The default ResourceBundleLocator implementation used by Hibernate Validator is <a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/resourceloading/PlatformResourceBundleLocator.html">PlatformResourceBundleLocator</a> which simply loads bundles using <a href="http://java.sun.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String)">ResourceBundle.loadBundle()</a>. Another implementation provided out of the box is <a href="http://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/resourceloading/AggregateResourceBundleLocator.html">AggregateResourceBundleLocator</a>, which allows to retrieve message texts from multiple bundles by merging them into a single aggregate bundle. Let's look at an example:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">HibernateValidatorConfiguration configuration = Validation<tt> </tt> .byProvider(HibernateValidator.class)<tt> </tt> .configure();<tt> </tt><tt> </tt>ValidatorFactory validatorFactory = configuration<tt> </tt> .messageInterpolator(<tt> </tt> <span class="kw">new</span> ResourceBundleMessageInterpolator(<tt> </tt> <span class="kw">new</span> AggregateResourceBundleLocator(<tt> </tt> <span class="pt">Arrays</span>.asList(<span class="s"><span class="dl">&quot;</span><span class="k">foo</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">bar</span><span class="dl">&quot;</span></span>), <tt> </tt> configuration.getDefaultResourceBundleLocator())))<tt> </tt> .buildValidatorFactory();<tt> </tt></pre></td> </tr></table> </div> <p>Here messages from bundles "foo" and "bar" could be used in constraints. In case the same key was contained in both bundles, the value from bundle "foo" would have precedence, as it comes first in the list. If a given key can't be found in any of the two bundles as fallback the default locator (which provides access to the ValidationMessages bundle as demanded by the JSR 303 spec) will be tried.</p> <h2>API for programmatic constraint creation</h2> <p>Using the Bean Validation API constraints can be declared using either annotations and/or XML descriptor files. Hibernate Validator 4.1 introduces a third approach by providing an API for <a href="http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/programmaticapi.html">programmatic constraint declaration</a>. </p> <p>This API can come in handy for instance in dynamic scenarios where constraints change upon runtime or for testing scenarios.</p> <p>As example let's consider two classes, <code>Customer</code> and <code>Order</code>, from a web shop application for which the following constraints shall apply:</p> <ul> <li>each customer must have a name</li> <li>each order must have a customer</li> <li>each order must comprise at least one order line</li> </ul> <p>With help of the programmatic constraint API these constraints can be declared as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">ConstraintMapping mapping = <span class="kw">new</span> ConstraintMapping();<tt> </tt><tt> </tt>mapping<tt> </tt> .type(Order.class)<tt> </tt> .property(<span class="s"><span class="dl">&quot;</span><span class="k">customer</span><span class="dl">&quot;</span></span>, <span class="pt">ElementType</span>.FIELD)<tt> </tt> .constraint(NotNullDef.class)<tt> </tt> .property(<span class="s"><span class="dl">&quot;</span><span class="k">orderLines</span><span class="dl">&quot;</span></span>, <span class="pt">ElementType</span>.FIELD)<tt> </tt> .constraint(SizeDef.class)<tt> </tt> .min(<span class="i">1</span>)<tt> </tt> .message(<span class="s"><span class="dl">&quot;</span><span class="k">An order must contain at least one order line</span><span class="dl">&quot;</span></span>)<tt> </tt> .type(Customer.class)<tt> </tt> .property(<span class="s"><span class="dl">&quot;</span><span class="k">name</span><span class="dl">&quot;</span></span>, <span class="pt">ElementType</span>.FIELD)<tt> </tt> .constraint(NotNullDef.class); <tt> </tt></pre></td> </tr></table> </div> <p>As the listing shows the API is designed in a fluent style with the class <a href="http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/cfg/ConstraintMapping.html">ConstraintMapping</a> as entry point. Constraints are declared by chaining method calls which specify to which property of which type which constraints should be added.</p> <p>The API provides constraint definition classes such as <a href="http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/cfg/defs/NotNullDef.html">NotNullDef</a> etc. for all built-in constraints, which allow to access their properties (min(), message() etc.) in a type-safe way. For custom constraints you could either provide your own constraint definition class or you could make use of <a href="http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/cfg/defs/GenericConstraintDef.html">GenericConstraintDef</a> which allows to identify properties by name.</p> <p>Having created a constraint mapping it has to be registered with the constraint validator factory. As the programmatic API is not part of the Bean Validation spec, we must explicitely specify Hibernate Validator as the BV provider to be used:</p> <div class="gmCode"><table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="pt">Validator</span> validator = Validation<tt> </tt> .byProvider(HibernateValidator.class)<tt> </tt> .configure()<tt> </tt> .addMapping(mapping)<tt> </tt> .buildValidatorFactory()<tt> </tt> .getValidator();<tt> </tt></pre></td> </tr></table> </div> <p>If you now use this validator to validate an <code>Order</code> object which has no customer set a constraint violation will occur, just as if the "customer" property was annotated with @NotNull.</p> <h2>Constraint annotation processor</h2> <p>The Hibernate Validator annotation processor might become your new favourite tool if you find yourself accidentally doing things like</p> <ul> <li>annotating Strings with @Min to specify a minimum length (instead of using @Size)</li> <li>annotating the setter of a JavaBean property (instead of the getter method)</li> <li>annotating static fields/methods with constraint annotations (which is not supported)</li> </ul> <p>Normally you would notice such mistakes only during run-time. The annotation processor helps in saving your valuable time by detecting these and similar errors already upon compile-time by plugging into the build process and raising compilation errors whenever constraint annotations are incorrectly used.</p> <p>The processor can be used in basically every build environment (plain <a href="http://java.sun.com/javase/6/docs/technotes/guides/javac/index.html">javac</a>, Apache <a href="http://ant.apache.org/">Ant</a>, Apache <a href="http://maven.apache.org/">Maven</a>) as well as within all common IDEs. Just be sure to use JDK 6 or later, as the processor is based on the "Pluggable Annotation Processing API" defined by <a href="http://jcp.org/en/jsr/detail?id=269">JSR 269</a> which was introduced with Java 6.</p> <p>The HV reference guide <a href="http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/ch08.html#d0e2816">describes in detail</a> how to integrate the processor into the different environments, so I'll spare you the details here. As example the following screenshot shows some compilation errors raised by the processor within the Eclipse IDE (click to enlarge):</p> <p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBm59GzWM1A03sI9FO5RiCn0nNjlFv4KaLOghqv8rE526G0claoTfcSjmHME1JkpVN0nJ_f1SB33wK4DBdw8Y8ZPAeXc5Itpip9hJMenSCHedrPHUCHswESE4eCykEaWQkruZstqs_2_g/s1600/annotation_processor_eclipse.png"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 285px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBm59GzWM1A03sI9FO5RiCn0nNjlFv4KaLOghqv8rE526G0claoTfcSjmHME1JkpVN0nJ_f1SB33wK4DBdw8Y8ZPAeXc5Itpip9hJMenSCHedrPHUCHswESE4eCykEaWQkruZstqs_2_g/s400/annotation_processor_eclipse.png" border="0" alt="Hibernate Validator Annotation Processor in Eclipse IDE" id="BLOGGER_PHOTO_ID_5492768896123357714" /></a></p> <p>Just for the record it should be noted that the annotation processor already works pretty well but is currently still under development and is considered as an experimental feature as of HV 4.1. If you are facing any problems please report them in <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV/component/10356">JIRA</a>. Some <a href="http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/ch08.html#section-known-issues">known issues</a> are also discussed in the reference guide.</p> <h2>Summary</h2> <p>The focus for Hibernate Validator 4.0 was to provide a feature-complete, production-ready reference implementation of the <a href="http://jcp.org/en/jsr/detail?id=303">Bean Validation</a> spec.</p> <p>While strictly staying spec-compliant, HV 4.1 goes beyond what is defined in JSR 303 and aims at generating even more user value by providing new constraints, new API functionality as well as an annotation processor for compile-time annotation checking.</p> <p>In order to try out the new features yourself just download the release from <a href="https://sourceforge.net/projects/hibernate/files/hibernate-validator/4.1.0.Final">SourceForge</a>. Of course HV 4.1 can also be retrieved from the JBoss Maven <a href="https://repository.jboss.org/nexus/content/repositories/public/org/hibernate/hibernate-validator/4.1.0.Final">repository</a>.</p> <p>If you are already using HV 4.0.x, the new release generally should work as drop-in replacement. The only exception is that we had to move the class <a href="http://docs.jboss.org/hibernate/validator/4.1/api/org/hibernate/validator/messageinterpolation/ResourceBundleMessageInterpolator.html">ResourceBundleMessageInterpolator</a> to the new package <code>messageinterpolation</code>. So if you're directly referencing this class, you'll have to update your imports here.</p> <p>The rationale behind this relocation was to clearly separate those packages which can safely be accessed by HV users from packages intended for internal use only. The public packages are: </p> <ul> <li><code>org.hibernate.validator</code></li> <li><code>org.hibernate.validator.cfg</code></li> <li><code>org.hibernate.validator.constraints</code></li> <li><code>org.hibernate.validator.messageinterpolation</code></li> <li><code>org.hibernate.validator.resourceloading</code></li> </ul> <p>Of course we'd be very happy on any feedback. Questions and comments can be posted in the <a href="http://forum.hibernate.org/viewforum.php?f=9">HV forum</a>, while any issues or feature requests should be reported in <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV">JIRA</a>.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/06/whats-new-in-hibernate-validator-41.htmlnoreply@blogger.com (Anonymous)12tag:blogger.com,1999:blog-1306958590608174053.post-49224068935006019822010年6月12日 21:42:00 +00002010年06月13日T09:31:06.133+02:00OracleXMLXPathOracle's extractValue() Function and XML Namespaces<p>With the help of <a href="http://www.oracle.com/technology/software/products/database/index.html">Oracle's</a> <a href="http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/functions059.htm#SQLRF06173">extractValue()</a> function one can retrieve values from XML documents stored in a database using <a href="http://www.w3.org/TR/xpath/">XPath</a> expressions.</p> <p>Generally this function works as expected but it gave me a hard time when <a href="http://www.w3.org/TR/xml-names/">XML namespaces</a> came into play. As I didn't find much related information on the web I thought a short example might be helpful for others facing the same problem.</p> <p>Let's take the web service from the video rental shop from my recent post <a href="http://musingsofaprogrammingaddict.blogspot.com/2010/02/integrating-jax-ws-with-xmlbeans.html">Integrating JAX-WS with XmlBeans</a> as example. </p> <p>For auditing purposes all requests and responses of the web service might be logged in a database table <code>REQUESTS</code> created as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">CREATE</span> <span class="r">TABLE</span> <span class="">REQUESTS</span> (<tt> </tt> <span class="">ID</span> <span class="">NUMBER</span>(<span class="i">10</span>,<span class="i">0</span>) <span class="r">PRIMARY</span> <span class="r">KEY</span>,<tt> </tt> <span class="">REQUEST</span> <span class="">XMLTYPE</span>,<tt> </tt> <span class="">RESPONSE</span> <span class="">XMLTYPE</span><tt> </tt>);<tt> </tt></pre></td> </tr></table> </div> <p>In reality, logging would probably be implemented using a <a href="https://jax-ws.dev.java.net/articles/handlers_introduction.html">message handler</a>, but for demonstration purposes let's simply insert a sample request of the FindMoviesByDirector() operation using SQL:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">INSERT</span> <span class="r">INTO</span> <span class="">REQUESTS</span> (<span class="">ID</span>, <span class="">REQUEST</span>, <span class="">RESPONSE</span>)<tt> </tt><span class="r">VALUES</span> (<tt> </tt> <span class="i">1</span>,<tt> </tt> <span class="s"><span class="dl">'</span><span class="k">&lt;soapenv:Envelope <tt> </tt> xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; <tt> </tt> xmlns:typ=&quot;http://www.gunnarmorling.de/moapa/videorental/types&quot;&gt;<tt> </tt><tt> </tt> &lt;soapenv:Header/&gt;<tt> </tt> &lt;soapenv:Body&gt;<tt> </tt> &lt;typ:FindMoviesByDirectorRequest&gt;<tt> </tt> &lt;Director&gt;Bryan Singer&lt;/Director&gt;<tt> </tt> &lt;/typ:FindMoviesByDirectorRequest&gt;<tt> </tt> &lt;/soapenv:Body&gt;<tt> </tt> &lt;/soapenv:Envelope&gt;</span><span class="dl">'</span></span>,<tt> </tt> <span class="s"><span class="dl">'</span><span class="k">...</span><span class="dl">'</span></span>;<tt> </tt></pre></td> </tr></table> </div> <p>Here we have two namespace aliases declared, <code>soapenv</code> for the <a href="http://www.w3.org/TR/soap/">SOAP</a> message and <code>typ</code> for the actual message content.</p> <p>The key for accessing values from this document using extractValue() is the pretty poorly documented optional parameter <code>namespace_string</code>, which can be used to declare any namespaces. This has to happen in the form <code>xmlns:alias="URI"</code>, multiple namespaces must be separated by a space character. </p> <p>Knowing that, it's easy to issue a SQL query which retrieves the director name from the request above. Just make sure to qualify the element names with their namespace alias in the XPath expression:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">SELECT</span><tt> </tt> <span class="">req</span>.<span class="">id</span>,<tt> </tt> <span class="">extractValue</span>(<tt> </tt> <span class="">req</span>.<span class="">request</span>,<tt> </tt> <span class="s"><span class="dl">'</span><span class="k">/soapenv:Envelope/soapenv:Body/typ:FindMoviesByDirectorRequest/Director</span><span class="dl">'</span></span>,<tt> </tt> <span class="s"><span class="dl">'</span><span class="k">xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:typ=&quot;http://www.gunnarmorling.de/moapa/videorental/types&quot;</span><span class="dl">'</span></span>) <span class="">Title</span><tt> </tt><span class="r">FROM</span> <tt> </tt> <span class="">requests</span> <span class="">req</span><tt> </tt><span class="r">WHERE</span><tt> </tt> <span class="">id</span> = <span class="i">1</span><tt> </tt>;<tt> </tt><tt> </tt><span class="">ID</span> <span class="">TITLE</span> <tt> </tt>---------------<span class="c">--<tt> </tt>1 Bryan Singer</span><tt> </tt></pre></td> </tr></table> </div>http://musingsofaprogrammingaddict.blogspot.com/2010/06/oracles-extractvalue-function-and-xml.htmlnoreply@blogger.com (Anonymous)28tag:blogger.com,1999:blog-1306958590608174053.post-59741154820437323402010年5月14日 21:10:00 +00002010年05月15日T11:14:44.617+02:00Bean ValidationJavaJoda Time APIJSR 303Providing Bean Validation Support for the Joda Time API<p>Amongst others the <a href="http://jcp.org/en/jsr/detail?id=303">Bean Validation</a> API defines two constraint annotations related to time: <a href="http://java.sun.com/javaee/6/docs/api/javax/validation/constraints/Past.html">@Past</a> and <a href="http://java.sun.com/javaee/6/docs/api/javax/validation/constraints/Future.html">@Future</a>. With the help of these constraints one can validate that a given element is a date either in the past or in the future.</p> <p>As per the BV specification these constraints are allowed for the types <a href="http://java.sun.com/javase/6/docs/api/java/util/Date.html">java.util.Date</a> and <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html">java.util.Calendar</a>. But what if you are working with an alternative date/time library such as the <a href="http://joda-time.sourceforge.net/">Joda Time API</a>? Does that mean you can't use the @Past/@Future constraints?</p> <p>Luckily not, as the Bean Validation API defines a mechanism for adding new validators to existing constraints. Basically all you have to do is to implement a validator for each type to be supported and register it within a constraint mapping file.</p> <p>Note that for the remainder of this post I'll focus on the @Past constraint. Doing the same for @Future is left as an exercise for the reader.</p> <h2>Providing a Validator</h2> <p>So let's start with implementing a validator. The Joda Time API provides a whole bunch of types replacing the JDK date and time types. A good introduction to these types can be found in Joda's <a href="http://joda-time.sourceforge.net/quickstart.html">quickstart guide</a>.</p> <p>All Joda types representing exact points on the time-line implement the interface <a href="http://joda-time.sourceforge.net/api-release/org/joda/time/ReadableInstant.html">ReadableInstant</a>. Providing an @Past validator for that interface will allow the @Past constraint to be used for widely used ReadableInstant implementations such as <a href="http://joda-time.sourceforge.net/api-release/org/joda/time/DateTime.html">DateTime</a> or <a href="http://joda-time.sourceforge.net/api-release/org/joda/time/DateMidnight.html">DateMidnight</a>.</p> <p>Implementing the validator is straight-forward. Obeying the contract defined by @Past the given date is simply compared to a new DateTime instance which represents the current instant in the default time zone:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">PastValidatorForReadableInstant</span> <span class="di">implements</span><tt> </tt> ConstraintValidator&lt;Past, ReadableInstant&gt; {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> initialize(Past constraintAnnotation) {}<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid(ReadableInstant value,<tt> </tt> ConstraintValidatorContext constraintValidatorContext) {<tt> </tt><tt> </tt> <span class="kw">if</span>(value == <span class="kw">null</span>) {<tt> </tt> <span class="kw">return</span> <span class="kw">true</span>;<tt> </tt> }<tt> </tt><tt> </tt> <span class="kw">return</span> value.isBefore(<span class="kw">new</span> DateTime());<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Similar validators could also be written for other Joda types which don't implement ReadableInstant (such as <a href="http://joda-time.sourceforge.net/api-release/org/joda/time/LocalDate.html">LocalDate</a>) but as this is basically the same, it is out of the scope of this post.</p> <h2>Registering the Validator</h2> <p>Having implemented the validator we need to register it within a <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#d0e1957">constraint mapping</a> file:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="ta">&lt;constraint-mappings</span><tt> </tt> <span class="an">xmlns</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/mapping</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xsi:schemaLocation</span>=<tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt><tt> </tt> <span class="ta">&lt;constraint-definition</span> <span class="an">annotation</span>=<span class="s"><span class="dl">&quot;</span><span class="k">javax.validation.constraints.Past</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt> <span class="ta">&lt;validated-by</span> <span class="an">include-existing-validators</span>=<span class="s"><span class="dl">&quot;</span><span class="k">true</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt> <span class="ta">&lt;value&gt;</span>de.gmorling.moapa.joda_bv_integration.PastValidatorForReadableInstant<span class="ta">&lt;/value&gt;</span><tt> </tt> <span class="ta">&lt;/validated-by&gt;</span><tt> </tt> <span class="ta">&lt;/constraint-definition&gt;</span><tt> </tt><tt> </tt><span class="ta">&lt;/constraint-mappings&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>Using the <code>validated-by</code> element we add our new validator to the validators for the @Past constraint. By setting <code>include-existing-validators</code> to true, we ensure that the @Past constraint still can be used at the JDK date types.</p> <p>As demanded by the Bean Validation API we then register the constraint mapping file within the central configuration file validation.xml:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="pp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><tt> </tt><span class="ta">&lt;validation-config</span><tt> </tt> <span class="an">xmlns</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/configuration</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">&quot;</span></span><tt> </tt> <span class="an">xsi:schemaLocation</span>=<span class="s"><span class="dl">&quot;</span><span class="k">http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt><tt> </tt> <span class="ta">&lt;constraint-mapping&gt;</span>META-INF/validation/custom-constraints.xml<span class="ta">&lt;/constraint-mapping&gt;</span><tt> </tt><tt> </tt><span class="ta">&lt;/validation-config&gt;</span><tt> </tt></pre></td> </tr></table> </div> <h2>Trying it out</h2> <p>Now it's time to test how that all works out. To do so, we define an examplary domain class Customer which has an attribute birthday of the Joda type DateMidnight:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">Customer</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">final</span> <span class="ty">String</span> name;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">final</span> DateMidnight birthday;<tt> </tt><tt> </tt> <span class="di">public</span> Customer(<span class="ty">String</span> name, DateMidnight birthday) {<tt> </tt><tt> </tt> <span class="lv">this</span>.name = name;<tt> </tt> <span class="lv">this</span>.birthday = birthday;<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@NotNull</span><tt> </tt> <span class="di">public</span> <span class="ty">String</span> getName() {<tt> </tt> <span class="kw">return</span> name;<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@NotNull</span><tt> </tt> <span class="at">@Past</span><tt> </tt> <span class="di">public</span> DateMidnight getBirthday() {<tt> </tt> <span class="kw">return</span> birthday;<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>A simple test finally shows that creating a customer with a future birthday causes a constraint violation, while a customer with a birthday in the past doesn't:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">CustomerTest</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">static</span> <span class="ty">Validator</span> validator;<tt> </tt><tt> </tt> <span class="at">@BeforeClass</span><tt> </tt> <span class="di">public</span> <span class="di">static</span> <span class="ty">void</span> setUpValidatorAndDates() {<tt> </tt><tt> </tt> ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();<tt> </tt> validator = validatorFactory.getValidator();<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> customerWithFutureBirthdayCausesConstraintViolation() {<tt> </tt><tt> </tt> Customer customer = <span class="kw">new</span> Customer(<span class="s"><span class="dl">&quot;</span><span class="k">Bob</span><span class="dl">&quot;</span></span>, <span class="kw">new</span> DateMidnight(<span class="i">2020</span>, <span class="i">11</span>, <span class="i">3</span>));<tt> </tt> <span class="ty">Set</span>&lt;ConstraintViolation&lt;Customer&gt;&gt; constraintViolations = validator.validate(customer);<tt> </tt><tt> </tt> assertEquals(<span class="i">1</span>, constraintViolations.size());<tt> </tt> assertEquals(<span class="s"><span class="dl">&quot;</span><span class="k">must be in the past</span><span class="dl">&quot;</span></span>, constraintViolations.iterator().next().getMessage());<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> customerWithPastBirthdayCausesNoConstraintViolation() {<tt> </tt><tt> </tt> Customer customer = <span class="kw">new</span> Customer(<span class="s"><span class="dl">&quot;</span><span class="k">Bob</span><span class="dl">&quot;</span></span>, <span class="kw">new</span> DateMidnight(<span class="i">1960</span>, <span class="i">11</span>, <span class="i">3</span>));<tt> </tt> <span class="ty">Set</span>&lt;ConstraintViolation&lt;Customer&gt;&gt; constraintViolations = validator.validate(customer);<tt> </tt><tt> </tt> assertTrue(constraintViolations.isEmpty());<tt> </tt> }<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The complete source code in form of a Maven project can be found in my <a href="http://github.com/gunnarmorling/musings-of-a-programming-addict/tree/master/joda-bv-integration/">Git repository</a>.</p> <p>So just try it out and don't hesistate to post any feedback. It is also <a href="http://opensource.atlassian.com/projects/hibernate/browse/HV-307">planned</a> to add support for the Joda types in an ucoming version of <a href="http://www.hibernate.org/subprojects/validator.html">Hibernate Validator</a>.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/05/providing-bean-validation-support-for.htmlnoreply@blogger.com (Anonymous)2tag:blogger.com,1999:blog-1306958590608174053.post-13754469695618424882010年4月09日 19:52:00 +00002010年04月11日T21:25:42.108+02:00Bean ValidationJavaJPA 2JSR 303Slides from JUG presentation on Bean Validation API available<p>Recently I gave a presentation on <a href="" title="http://jcp.org/en/jsr/detail?id=303">JSR 303</a> ("Bean Validation") at the <a href="http://www.jughh.org/">Java User Group</a> in Hamburg, Germany.</p> <p>I did also some live coding during the talk and mentioned some things only verbally. Nevertheless I thought the slides (which are in German) might be of interest, so I decided to put them online.</p> <div id="__ss_3677549" style="width:425px; margin: 0 auto"> <object height="355" width="425"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=vortragbeanvalidationapi-100409141506-phpapp01&stripped_title=objektvalidierung-mit-dem-bean-validation-api" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed allowfullscreen="true" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=vortragbeanvalidationapi-100409141506-phpapp01&stripped_title=objektvalidierung-mit-dem-bean-validation-api" allowscriptaccess="always" type="application/x-shockwave-flash" height="355" width="425"></embed> </object> </div> <p>You can also find the presentation at <a href="http://www.slideshare.net/gunnarmorling/objektvalidierung-mit-dem-bean-validation-api">slideshare</a>.</p> <p>The talk went pretty well, there were many questions and people generally seemed quite interested :-) One question related to the automatic validation of constraints at <a href="http://jcp.org/en/jsr/detail?id=317">JPA</a> entities I couldn't answer immediately, though. </p> <p>The question was, whether lazy properties are loaded from the database during validation of class-level constraints (for constraint validation at fields/properties lazy attributes must not be loaded, which is ensured by checking each attribute's load state using a <a href="http://java.sun.com/javaee/6/docs/api/javax/validation/TraversableResolver.html">TraversableResolver</a>).</p> <p>Actually this can happen, depending on the attributes accessed by the validator implementation for the class-level constraint. I think, generally this is alright, as the validator needs to know the values of all attributes relevant for the evaluation of the constraint. If in certain scenarios this behavior is not acceptable, one could examine the attributes' load states manually using <a href="http://java.sun.com/javaee/6/docs/api/javax/persistence/PersistenceUtil.html#isLoaded(java.lang.Object,%20java.lang.String)">javax.validation.PersistenceUtil</a> and depending on that access only certain attributes.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/04/slides-from-jug-presentation-on-bean.htmlnoreply@blogger.com (Anonymous)0tag:blogger.com,1999:blog-1306958590608174053.post-80028175952738445722010年3月24日 23:22:00 +00002010年03月25日T00:24:13.421+01:00Bean ValidationJavaJSR 303Latest Bean Validation Links<p>When I <a href="http://musingsofaprogrammingaddict.blogspot.com/2009/01/getting-started-with-jsr-303-beans.html">started</a> blogging on <a href="http://jcp.org/en/jsr/detail?id=303">JSR 303</a> ("Bean Validation"), there was not too much information available on the web concerning the BV API, its usage and its integration with other technologies.</p> <p>In between JSR 303 got approved, BV is part of <a href="http://jcp.org/en/jsr/detail?id=316">Java EE 6</a> and as it generally gains wider adoption, more and more blog posts and other information related to Bean Validation come available.</p> <p>That's why I thought it might be a good idea to collect the most interesting pieces and publish those links here every once in a while.</p> <p>And there you go, here is the first couple of links related to JSR 303:</p> <ul> <li><a href="http://community.jboss.org/docs/DOC-15041"> How to write a custom ConstraintValidatorFactory</a> (Hardy Ferentschik, March 23rd, 2010)</li> <li><a href="http://agoncal.wordpress.com/2010/03/03/bean-validation-with-jpa-1-0/">Bean Validation with JPA 1.0</a> (Antonio Goncalves, March 3rd, 2010)</li> <li><a href="http://blog.smart-java.nl/blog/index.php/2010/03/17/jsf-2-0-clientbehavior-bean-validation-in-javascript/">JSF 2.0 ClientBehavior: Bean Validation in JavaScript</a> (Jan-Kees van Andel, March 17th, 2010)</li> <li><a href="http://blog.smart-java.nl/blog/index.php/2010/03/12/unit-testing-a-bean-validation-constraintvalidator/">Unit Testing a Bean Validation ConstraintValidator</a> (Jan-Kees van Andel, March 12th, 2010)</li> <li><a href="http://blog.inflinx.com/2010/03/10/jsr-303-bean-validation-using-spring-3/">JSR 303 Bean Validation Using Spring 3</a> (Balaji, March 10th, 2010)</li> <li><a href="http://blog.zenika.com/index.php?post/2010/02/24/Wicket-JSR-303-Validators">Wicket JSR-303 Validators</a> (Ophelie Salm, February 25rd, 2010)</li> </ul> <p>I plan to post follow-ups, whenever I gathered some interesting links, so stay tuned.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/03/latest-bean-validation-links.htmlnoreply@blogger.com (Anonymous)2tag:blogger.com,1999:blog-1306958590608174053.post-37319353659561160912010年3月16日 21:04:00 +00002010年03月16日T22:06:16.786+01:00IntelliJ IDEAJavaImport code style settings into IntelliJ IDEA<p>Recently I received an <a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a> code style settings file for <a href="https://www.hibernate.org/412.html">Hibernate Validator</a>, the reference implementation of <a href="http://jcp.org/en/jsr/detail?id=303">JSR 303</a> ("Bean Validation"), to which I'm contributing.</p> <p>I wanted to import it into IntelliJ in order to format any code changes I make in the project's standard style. But as it turned out, there is no functionality within the IDE for importing code style settings. Instead one has to do the following:</p> <ul> <li>Copy the settings XML file to INTELLIJ_SETTINGS_DIR/config/codestyles (where INTELLIJ_SETTINGS_DIR is the folder containing your IntelliJ settings; typically it is situated within your home directory, the name depends on your version of IntelliJ; in my case the complete path is "~/.IdeaIC90/config/codestyles")</li> <li>Start IntelliJ</li> <li>Go to "File" - "Settings" - "Code Style", select "Use global settings" and choose the previously imported style from the drop-down box</li> <li>Optionally apply the style to one project only by clicking on "Copy to Project" and selecting "Use per project settings" afterwards</li> </ul>http://musingsofaprogrammingaddict.blogspot.com/2010/03/import-code-style-settings-into.htmlnoreply@blogger.com (Anonymous)8tag:blogger.com,1999:blog-1306958590608174053.post-70570305058743171532010年3月09日 14:01:00 +00002010年03月10日T12:00:59.439+01:00JavaJAX-WSJMXMetroweb servicesRuntime Configuration of Schema Validation using Metro<p>When working with XML-based web services, it usually a good idea to validate all requests and responses against their associated <a href="http://www.w3.org/XML/Schema">XML schema</a> in order to ensure the integrity of the incoming/outgoing messages.</p> <p>Unfortunately <a href="http://jcp.org/en/jsr/detail?id=224">JAX-WS</a>, the standard API for SOAP-based web services of the Java Platform doesn't specify a standard way for schema validation. Therefore most of the JAX-WS implementations such as <a href="https://metro.dev.java.net/">Metro</a> or <a href="http://cxf.apache.org/">CXF</a> provide a proprietary mechanism to perform that task.</p> <p>Using Metro, this is simply done by annotating the endpoint class with <a href="https://jax-ws.dev.java.net/guide/Schema_Validation.html">@SchemaValidation</a>:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@WebService</span>(endpointInterface = <span class="s"><span class="dl">&quot;</span><span class="k">...</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="at">@SchemaValidation</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">VideoRentalPort</span> <span class="di">implements</span> VideoRentalPortType {<tt> </tt> <span class="c">//...</span><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>This works as expected, but has one big flaw in my eyes: to enable or disable validation for a given endpoint, the application code must be modified, followed by a re-deployment of the application.</p> <p>This is not viable in many scenarios. Taking a large enterprise application with a whole lot of services for example, schema validation might be turned off by default for performance reasons. But for the purpose of bug analysis it might be required to temporarily enable validation for single endpoints. Re-deploying the application is not an option in this scenario.</p> <p>That's why I had a look into Metro's implementation of the validation feature and tried to find a way to make schema validation configurable during runtime.</p> <h2>Custom Metro tubes</h2> <p>Schema validation in Metro is realized using in form of a so-called "tube". Metro tubes are conceptually similar to JAX-WS message handlers, but a magnitude more powerful. Since Metro 2.0 the tubes to be applied for a given application can be configured by providing so-called <a href="http://wikis.glassfish.org/metro/Wiki.jsp?page=DeclarativeTubelineAssemblerOnePager">custom "tubelines"</a>.</p> <p>So the basic idea is to extend the standard validation tube provided by Metro in a way which makes it runtime-configurable and register this customized tube instead of the original one.</p> <p><strong>Note:</strong> Before going into details, it should be said that as of Metro 2.0 the tube-related APIs <a href="http://marek.potociar.net/2009/10/19/custom-metro-tube-interceptor/">are regarded</a> as Metro-internal APIs, so they could be changed in future versions, causing the approach described here not to work anymore. But as we don't write very much code, this shouldn't really scare us.</p> <h2>Managing configuration state</h2> <p>First of all, some sort of data structure is needed, which will store whether validation is enabled for a given web service endpoint. For this purpose a simple map is sufficient (as this class must be accessible for multiple threads at the same time, a <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ConcurrentMap.html">ConcurrentMap</a> is used):</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">ValidationConfigurationHolder</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">static</span> <span class="ty">ConcurrentMap</span>&lt;<span class="ty">String</span>, <span class="ty">Boolean</span>&gt; configuration = <tt> </tt> <span class="kw">new</span> <span class="ty">ConcurrentHashMap</span>&lt;<span class="ty">String</span>, <span class="ty">Boolean</span>&gt;();<tt> </tt><tt> </tt> <span class="di">public</span> <span class="di">static</span> <span class="ty">boolean</span> isValidationEnabled(<span class="ty">String</span> portName) {<tt> </tt><tt> </tt> <span class="ty">Boolean</span> theValue = configuration.get(portName);<tt> </tt> <span class="kw">return</span> theValue != <span class="kw">null</span> ? theValue : <span class="ty">Boolean</span>.TRUE;<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="di">static</span> <span class="ty">void</span> setValidationEnabled(<span class="ty">String</span> portName, <span class="ty">boolean</span> enabled) {<tt> </tt> configuration.put(portName, enabled);<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Next we need a way to manipulate this structure during application runtime. Java's standard API for management purposes as the one at hand is <a href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX</a>. The following listing shows a JMX MBean, which later can be invoked to enable or disable validation for a given port:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt>32<tt> </tt>33<tt> </tt>34<tt> </tt>35<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c">//MBean interface</span><tt> </tt><span class="di">public</span> <span class="ty">interface</span> EndpointConfigurationMBean {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">String</span> getName();<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isSchemaValidationEnabled();<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> setSchemaValidationEnabled(<span class="ty">boolean</span> enabled);<tt> </tt><tt> </tt>}<tt> </tt><tt> </tt><span class="c">//MBean implementation</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">EndpointConfiguration</span> <span class="di">implements</span> EndpointConfigurationMBean {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="ty">String</span> name;<tt> </tt><tt> </tt> <span class="di">public</span> EndpointConfiguration() {}<tt> </tt><tt> </tt> <span class="di">public</span> EndpointConfiguration(<span class="ty">String</span> name) {<tt> </tt> <span class="lv">this</span>.name = name;<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">String</span> getName() {<tt> </tt> <span class="kw">return</span> name;<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isSchemaValidationEnabled() {<tt> </tt> <span class="kw">return</span> ValidationConfigurationHolder.isValidationEnabled(name);<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> setSchemaValidationEnabled(<span class="ty">boolean</span> enabled) {<tt> </tt> ValidationConfigurationHolder.setValidationEnabled(name, enabled);<tt> </tt> }<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <h2>The customized validation tube</h2> <p>The schema validation support of Metro is basically realized in three classes: <a href="http://fisheye5.cenqua.com/browse/jax-ws-sources/jaxws-ri/rt/src/com/sun/xml/ws/util/pipe/AbstractSchemaValidationTube.java">AbstractSchemaValidationTube</a> and <a href="http://fisheye5.cenqua.com/browse/jax-ws-sources/jaxws-ri/rt/src/com/sun/xml/ws/server/ServerSchemaValidationTube.java">ServerSchemaValidationTube</a> resp. <a href="http://fisheye5.cenqua.com/browse/jax-ws-sources/jaxws-ri/rt/src/com/sun/xml/ws/client/ClientSchemaValidationTube.java">ClientSchemaValidationTube</a> which extend the former. To make schema validation configurable on the server side, we create a sub-class of ServerSchemaValidationTube:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt>32<tt> </tt>33<tt> </tt>34<tt> </tt>35<tt> </tt>36<tt> </tt>37<tt> </tt>38<tt> </tt>39<tt> </tt><strong>40</strong><tt> </tt>41<tt> </tt>42<tt> </tt>43<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">ConfigurableServerSchemaValidationTube</span> <span class="di">extends</span> ServerSchemaValidationTube {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="ty">String</span> name;<tt> </tt><tt> </tt> <span class="di">public</span> ConfigurableServerSchemaValidationTube(WSEndpoint&lt;?&gt; endpoint, WSBinding binding,<tt> </tt> SEIModel seiModel, WSDLPort wsdlPort, Tube next) {<tt> </tt><tt> </tt> <span class="lv">super</span>(endpoint, binding, seiModel, wsdlPort, next);<tt> </tt><tt> </tt> name = seiModel.getServiceQName().getLocalPart() + <span class="s"><span class="dl">&quot;</span><span class="k">-</span><span class="dl">&quot;</span></span> + wsdlPort.getName().getLocalPart();<tt> </tt> ValidationConfigurationHolder.setValidationEnabled(name, <span class="kw">true</span>);<tt> </tt> registerMBean();<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">private</span> <span class="ty">void</span> registerMBean() {<tt> </tt><tt> </tt> <span class="ty">MBeanServer</span> mbs = <span class="ty">ManagementFactory</span>.getPlatformMBeanServer(); <tt> </tt><tt> </tt> <span class="kw">try</span> {<tt> </tt> mbs.registerMBean(<tt> </tt> <span class="kw">new</span> EndpointConfiguration(name),<tt> </tt> <span class="kw">new</span> <span class="ty">ObjectName</span>(<span class="s"><span class="dl">&quot;</span><span class="k">de.gmorling.moapa.videorental.jmx:type=Endpoints,name=</span><span class="dl">&quot;</span></span> + name));<tt> </tt> }<tt> </tt> <span class="kw">catch</span> (<span class="ty">Exception</span> e) {<tt> </tt> <span class="kw">throw</span> <span class="kw">new</span> <span class="ty">RuntimeException</span>(e);<tt> </tt> }<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">protected</span> <span class="ty">boolean</span> isNoValidation() {<tt> </tt> <span class="kw">return</span> !ValidationConfigurationHolder.isValidationEnabled(name);<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">protected</span> ConfigurableServerSchemaValidationTube(ConfigurableServerSchemaValidationTube that, TubeCloner cloner) {<tt> </tt> <span class="lv">super</span>(that,cloner);<tt> </tt> <span class="lv">this</span>.name = that.name;<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> ConfigurableServerSchemaValidationTube copy(TubeCloner cloner) {<tt> </tt> <span class="kw">return</span> <span class="kw">new</span> ConfigurableServerSchemaValidationTube(<span class="lv">this</span>, cloner);<tt> </tt> }<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The important thing here is, that the method isNoValidation() is overridden. This method is defined within the base class AbstractSchemaValidationTube and is invoked whenever a request/response is processed by this tube. The implementation of this method simply delegates to the ValidationConfigurationHolder shown above.</p> <p>Within the constructor an instance of the EndpointConfiguration MBean for the given endpoint is registered with the platform MBean server, which later can be used to toggle validation for this endpoint.</p> <h2>Providing a TubeFactory</h2> <p>Metro tubes are instantiated by implementations of the <a href="http://fisheye5.cenqua.com/browse/wsit/wsit/rt/src/com/sun/xml/ws/assembler/TubeFactory.java">TubeFactory</a> interface. The following implementation allows the Metro runtime to retrieve instances of the ConfigurableServerSchemaValidationTube:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">ConfigurableValidationTubeFactory</span> <span class="di">implements</span> TubeFactory {<tt> </tt><tt> </tt> <span class="c">//...</span><tt> </tt><tt> </tt> <span class="di">public</span> Tube createTube(ServerTubelineAssemblyContext context)<tt> </tt> <span class="di">throws</span> WebServiceException {<tt> </tt><tt> </tt> ServerTubeAssemblerContext wrappedContext = context.getWrappedContext();<tt> </tt> WSEndpoint&lt;?&gt; endpoint = wrappedContext.getEndpoint();<tt> </tt> WSBinding binding = endpoint.getBinding();<tt> </tt> Tube next = context.getTubelineHead();<tt> </tt> WSDLPort wsdlModel = wrappedContext.getWsdlModel();<tt> </tt> SEIModel seiModel = wrappedContext.getSEIModel();<tt> </tt><tt> </tt> <span class="kw">if</span> (binding <span class="kw">instanceof</span> SOAPBinding &amp;&amp; binding.isFeatureEnabled(SchemaValidationFeature.class) &amp;&amp; wsdlModel != <span class="kw">null</span>) {<tt> </tt> <span class="kw">return</span> <span class="kw">new</span> ConfigurableServerSchemaValidationTube(endpoint, binding, seiModel, wsdlModel, next);<tt> </tt> } <span class="kw">else</span><tt> </tt> <span class="kw">return</span> next;<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The code resembles Metro's instantiation logic for the standard validation tube, the only difference being that a ConfigurableServerSchemaValidationTube is created. Note that we also check whether the SchemaValidationFeature is enabled or not. That way given endpoints can be completey excluded from validation by simply not annotating them with @SchemaValidation.</p> <p>To register the tube factory with Metro as part of a custom tubeline the configuration file META-INF/metro.xml has to be provided:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="pp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><tt> </tt><span class="ta">&lt;metro</span> <span class="an">xmlns:xsi</span>=<span class="s"><span class="dl">'</span><span class="k">http://www.w3.org/2001/XMLSchema-instance</span><span class="dl">'</span></span><tt> </tt> <span class="an">xmlns</span>=<span class="s"><span class="dl">'</span><span class="k">http://java.sun.com/xml/ns/metro/config</span><span class="dl">'</span></span><tt> </tt> <span class="an">version</span>=<span class="s"><span class="dl">&quot;</span><span class="k">1.0</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt> <span class="ta">&lt;tubelines</span> <span class="an">default</span>=<span class="s"><span class="dl">&quot;</span><span class="k">#custom-tubeline</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt> <span class="ta">&lt;tubeline</span> <span class="an">name</span>=<span class="s"><span class="dl">&quot;</span><span class="k">custom-tubeline</span><span class="dl">&quot;</span></span><span class="ta">&gt;</span><tt> </tt> <span class="ta">&lt;client-side&gt;</span><tt> </tt> ...<tt> </tt> <span class="ta">&lt;/client-side&gt;</span><tt> </tt> <span class="ta">&lt;endpoint-side&gt;</span><tt> </tt> ...<tt> </tt> <span class="ta">&lt;tube-factory</span> <span class="an">className</span>=<span class="s"><span class="dl">&quot;</span><span class="k">com.sun.xml.ws.assembler.jaxws.HandlerTubeFactory</span><span class="dl">&quot;</span></span> <span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;tube-factory</span> <span class="an">className</span>=<span class="s"><span class="dl">&quot;</span><span class="k">de.gmorling.moapa.videorental.metro.ConfigurableValidationTubeFactory</span><span class="dl">&quot;</span></span> <span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;tube-factory</span> <span class="an">className</span>=<span class="s"><span class="dl">&quot;</span><span class="k">com.sun.xml.ws.assembler.jaxws.TerminalTubeFactory</span><span class="dl">&quot;</span></span> <span class="ta">/&gt;</span><tt> </tt> <span class="ta">&lt;/endpoint-side&gt;</span><tt> </tt> <span class="ta">&lt;/tubeline&gt;</span><tt> </tt> <span class="ta">&lt;/tubelines&gt;</span><tt> </tt><span class="ta">&lt;/metro&gt;</span><tt> </tt></pre></td> </tr></table> </div> <p>As suggested in a <a href="http://marek.potociar.net/2009/10/19/custom-metro-tube-interceptor/">post</a> by Metro developer Marek Potociar it's a good idea to take Metro's standard tube configuration, metro-default.xml, as template and adapt it as needed. In our case the ConfigurableValidationTubeFactory is registered instead of the standard ValidationTubeFactory.</p> <h2>Configuring Schema Validation using VisualVM</h2> <p>Having registered the tube factory, it's time to give the whole thing a test run. A complete project containing all sources of this post together with an examplary video rental web service can be found in my Git <a href="http://github.com/gunnarmorling/musings-of-a-programming-addict/tree/master/configurable-schema-validation/">repo</a>. </p> <p>When starting the project's main class the service will be fired up and waiting for requests. Any JMX client such as <a href="https://visualvm.dev.java.net/">VisualVM</a> can now be used to connect to the running JVM. The following screenshot shows VisualVM connected to the video rental application:</p> <p><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqP_tWsrWZ5cOyBLaUSGt0YShPyHUB2qX43A33cqksHNUxO0Cb2IxzfCjetR8mvehSTuelBCKxCcWF_swzmxEwMdEJxTEvjROIljSiCz813N-21ExVDKsCd4Vj0FB21eL37epWskmALp8/s1600-h/2010_03_09_visual_vm.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqP_tWsrWZ5cOyBLaUSGt0YShPyHUB2qX43A33cqksHNUxO0Cb2IxzfCjetR8mvehSTuelBCKxCcWF_swzmxEwMdEJxTEvjROIljSiCz813N-21ExVDKsCd4Vj0FB21eL37epWskmALp8/s400/2010_03_09_visual_vm.png" border="0" id="BLOGGER_PHOTO_ID_5446634090468389634" alt="" style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 245px;" /></a></p> <p>By setting the value of the "SchemaValidationEnabled" property of the MBean named "VideoRentalService-VideoRentalPort", schema validation now can be enabled or disabled for the corresponding endpoint.</p> <p><strong>Update (march 10, 2010):</strong> I created an request for enhancement for that feature: <a href="https://jax-ws.dev.java.net/issues/show_bug.cgi?id=839">Allow schema validation to be enabled/disabled at runtime</a>. So let's see, whether this will be part of some future Metro version :-)</p>http://musingsofaprogrammingaddict.blogspot.com/2010/03/runtime-configuration-of-schema.htmlnoreply@blogger.com (Anonymous)3tag:blogger.com,1999:blog-1306958590608174053.post-79214848002186955992010年2月15日 23:41:00 +00002010年02月19日T21:10:31.509+01:00Bean ValidationJavaJSR 303A generic class-level constraint for the Bean Validation API<p>One question I'm being asked pretty often when talking about the Bean Validation API (<a href="http://jcp.org/en/jsr/detail?id=303">JSR 303</a>) is, how cross-field validation can be done. The canonical example for this is a class representing a calendar event, where the end date of an event shall always be later than the start date.</p> <p>This and other scenarios where validation depends on the values of multiple attributes of a given object can be realized by implementing a <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#d0e328">class-level constraint</a>.</p> <p>That basically works pretty well, nevertheless this is one part of the BV spec, I'm not too comfortable with. This is mainly for two reasons:</p> <ul> <li>You'll probably need a dedicated constraint for every reasonably complex business object. Providing an annotation, a validator implementation and an error message for each can become pretty tedious.</li> <li>Business objects typically know best themselves, whether they are in a consistent, valid state or not. By putting validation logic into a separate constraint validator class, objects have to expose their internal state, which otherwise might not be required.</li> </ul> <p>To circumvent these problems, <a href="(http://musingsofaprogrammingaddict.blogspot.com/2009/08/script-annotation-for-bean-validation.html">I suggested</a> a while ago a generic constraint annotation, which allows the use of script expressions written in languages such as <a href="http://groovy.codehaus.org/">Groovy</a> to implement validation logic directly at the validated class.</p> <p>This approach frees you from the need to implement dedicated constraints for each relevant business object, but also comes at the cost of losing type-safety at compile-time.</p> <p>The constraint presented in the following therefore tries to combine genericity with compile-time type safety. The basic idea is to implement validation logic within the business objects themselves and to invoke this logic from within a generic constraint class. </p> <p>In order to do so let's first define an interface to be implemented by any validatable class:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">interface</span> Validatable {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid();<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Next we define a constraint annotation, @SelfValidating, which we'll use later on to annotate Validatable implementations:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@Target</span>( { TYPE })<tt> </tt><span class="at">@Retention</span>(RUNTIME)<tt> </tt><span class="at">@Constraint</span>(validatedBy = SelfValidatingValidator.class)<tt> </tt><span class="at">@Documented</span><tt> </tt><span class="di">public</span> <span class="at">@interface</span> SelfValidating {<tt> </tt><tt> </tt> <span class="ty">String</span> message() <span class="kw">default</span> <span class="s"><span class="dl">&quot;</span><span class="k">{de.gmorling.moapa.self_validating.SelfValidating.message}</span><span class="dl">&quot;</span></span>;<tt> </tt><tt> </tt> <span class="ty">Class</span>&lt;?&gt;<span class="ty">[]</span> groups() <span class="kw">default</span> {};<tt> </tt><tt> </tt> <span class="ty">Class</span>&lt;? <span class="di">extends</span> Payload&gt;<span class="ty">[]</span> payload() <span class="kw">default</span> {};<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Of course we also need a constraint validator implementation, which is able to evaluate that constraint:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">SelfValidatingValidator</span> <span class="di">implements</span><tt> </tt> ConstraintValidator&lt;SelfValidating, Validatable&gt; {<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">void</span> initialize(SelfValidating constraintAnnotation) {}<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid(Validatable value,<tt> </tt> ConstraintValidatorContext constraintValidatorContext) {<tt> </tt><tt> </tt> <span class="kw">return</span> value.isValid();<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>The implementation is trivial, as nothing is to do in initialize() and the isValid() method just delegates the call to the validatable object itself.</p> <p>Finally we need a properties file named ValidationMessages.properties containing the default error message for the constraint:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">de.gmorling.moapa.self_validating.SelfValidating.message=Validatable object couldn't be validated successfully.<tt> </tt></pre></td> </tr></table> </div> <p>Taking the calendar event example from the beginning, usage of the constraint might look as follows:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt>32<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@SelfValidating</span><tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> <span class="di">implements</span> Validatable {<tt> </tt><tt> </tt> <span class="at">@NotNull</span><tt> </tt> <span class="di">private</span> <span class="di">final</span> <span class="ty">String</span> title;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">final</span> <span class="ty">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">final</span> <span class="ty">Date</span> endDate;<tt> </tt><tt> </tt> <span class="di">public</span> CalendarEvent(<span class="ty">String</span> title, <span class="ty">Date</span> startDate, <span class="ty">Date</span> endDate) {<tt> </tt><tt> </tt> <span class="lv">this</span>.title = title;<tt> </tt> <span class="lv">this</span>.startDate = startDate;<tt> </tt> <span class="lv">this</span>.endDate = endDate;<tt> </tt> }<tt> </tt><tt> </tt> <span class="di">public</span> <span class="ty">boolean</span> isValid() {<tt> </tt> <span class="kw">return</span> startDate == <span class="kw">null</span> || endDate == <span class="kw">null</span> || startDate.before(endDate);<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Override</span><tt> </tt> <span class="di">public</span> <span class="ty">String</span> toString() {<tt> </tt> <span class="ty">DateFormat</span> format = <span class="kw">new</span> <span class="ty">SimpleDateFormat</span>(<span class="s"><span class="dl">&quot;</span><span class="k">dd.MM.yyyy</span><span class="dl">&quot;</span></span>);<tt> </tt><tt> </tt> <span class="kw">return</span> <tt> </tt> title + <tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k"> from </span><span class="dl">&quot;</span></span> + (startDate == <span class="kw">null</span> ? <span class="s"><span class="dl">&quot;</span><span class="k">-</span><span class="dl">&quot;</span></span> : format.format(startDate)) + <tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k"> till </span><span class="dl">&quot;</span></span> + (endDate == <span class="kw">null</span> ? <span class="s"><span class="dl">&quot;</span><span class="k">-</span><span class="dl">&quot;</span></span> : format.format(endDate));<tt> </tt> }<tt> </tt><tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>A short unit test shows that the constraint works as expected:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt>18<tt> </tt>19<tt> </tt><strong>20</strong><tt> </tt>21<tt> </tt>22<tt> </tt>23<tt> </tt>24<tt> </tt>25<tt> </tt>26<tt> </tt>27<tt> </tt>28<tt> </tt>29<tt> </tt><strong>30</strong><tt> </tt>31<tt> </tt>32<tt> </tt>33<tt> </tt>34<tt> </tt>35<tt> </tt>36<tt> </tt>37<tt> </tt>38<tt> </tt>39<tt> </tt><strong>40</strong><tt> </tt>41<tt> </tt>42<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="di">public</span> <span class="ty">class</span> <span class="cl">SelfValidatingTest</span> {<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">static</span> <span class="ty">Validator</span> validator;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">static</span> <span class="ty">Date</span> startDate;<tt> </tt><tt> </tt> <span class="di">private</span> <span class="di">static</span> <span class="ty">Date</span> endDate;<tt> </tt><tt> </tt> <span class="at">@BeforeClass</span><tt> </tt> <span class="di">public</span> <span class="di">static</span> <span class="ty">void</span> setUpValidatorAndDates() {<tt> </tt><tt> </tt> ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();<tt> </tt> validator = validatorFactory.getValidator();<tt> </tt><tt> </tt> startDate = <span class="kw">new</span> <span class="ty">GregorianCalendar</span>(<span class="i">2009</span>, <span class="i">8</span>, <span class="i">20</span>).getTime();<tt> </tt> endDate = <span class="kw">new</span> <span class="ty">GregorianCalendar</span>(<span class="i">2009</span>, <span class="i">8</span>, <span class="i">21</span>).getTime();<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> calendarEventIsValidAsEndDateIsAfterStartDate() {<tt> </tt><tt> </tt> CalendarEvent testEvent = <tt> </tt> <span class="kw">new</span> CalendarEvent(<span class="s"><span class="dl">&quot;</span><span class="k">Team meeting</span><span class="dl">&quot;</span></span>, startDate, endDate);<tt> </tt><tt> </tt> assertTrue(validator.validate(testEvent).isEmpty());<tt> </tt> }<tt> </tt><tt> </tt> <span class="at">@Test</span><tt> </tt> <span class="di">public</span> <span class="ty">void</span> calendarEventIsInvalidAsEndDateIsBeforeStartDate() {<tt> </tt><tt> </tt> CalendarEvent testEvent = <tt> </tt> <span class="kw">new</span> CalendarEvent(<span class="s"><span class="dl">&quot;</span><span class="k">Team meeting</span><span class="dl">&quot;</span></span>, endDate, startDate);<tt> </tt><tt> </tt> <span class="ty">Set</span>&lt;ConstraintViolation&lt;CalendarEvent&gt;&gt; constraintViolations = <tt> </tt> validator.validate(testEvent);<tt> </tt> assertEquals(<span class="i">1</span>, constraintViolations.size());<tt> </tt><tt> </tt> assertEquals(<tt> </tt> <span class="s"><span class="dl">&quot;</span><span class="k">Object couldn't be validated successfully.</span><span class="dl">&quot;</span></span>,<tt> </tt> constraintViolations.iterator().next().getMessage());<tt> </tt> }<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>This works like a charm, only the error message returned by the BV runtime is not yet very expressive. This can easily be solved, as the BV API allows to override error messages within constraint annotations:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="at">@SelfValidating</span>(message=<span class="s"><span class="dl">&quot;</span><span class="k">{de.gmorling.moapa.self_validating.CalendarEvent.message}</span><span class="dl">&quot;</span></span>)<tt> </tt><span class="di">public</span> <span class="ty">class</span> <span class="cl">CalendarEvent</span> <span class="di">implements</span> Validatable {<tt> </tt> ...<tt> </tt>}<tt> </tt></pre></td> </tr></table> </div> <p>Again we need an entry in ValidationMessages.properties for the specified message key:</p> <div class="gmCode"><table class="CodeRay"><tr> <td title="click to toggle" class="line_numbers" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">de.gmorling.moapa.self_validating.CalendarEvent.message=End date of event must be after start date.<tt> </tt></pre></td> </tr></table> </div> <h2>Conclusion</h2> <p>Providing dedicated class-level constraints for all business objects that require some sort of cross-field validation logic can be quite a tedious task as you need to create an annotation type, a validator implementation and an error message text.</p> <p>The @SelfValidating constraint reduces the required work by letting business objects validate themselves. That way all you have to do is implement the Validatable interface, annotate your class with @SelfValidating and optionally provide a customized error message. Furthermore business objects are not required to expose their internal state to make it accessible for an external validator implementation.</p> <p>The complete source code can be found in my GitHub <a href="http://github.com/gunnarmorling/musings-of-a-programming-addict/tree/master/self-validating/">repository</a>. As always I'd be happy about any feedback.</p>http://musingsofaprogrammingaddict.blogspot.com/2010/02/generic-class-level-constraint-for-bean.htmlnoreply@blogger.com (Anonymous)14

AltStyle によって変換されたページ (->オリジナル) /