<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DUUFRHs7fip7ImA9WhBaE0U.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852</id><updated>2013-05-24T10:20:15.506+01:00</updated><category term="web socket" /><category term="proxy" /><category term="sdo" /><category term="jenkins" /><category term="tyrus" /><category term="soa" /><category term="junit" /><category term="maven" /><category term="jersey" /><category term="java xquery jsr225" /><category term="wsdl" /><category term="http" /><category term="osx" /><category term="preflight" /><category term="dogfood" /><category term="asynchronous webservices" /><category term="jdeveloper" /><category term="sentilla" /><category term="iphone" /><category term="javaone" /><category term="spring" /><category term="jsr356" /><category term="hudons" /><category term="WeblogicSCA" /><category term="sca" /><category term="oauth" /><category term="HATEOAS" /><category term="jps" /><category term="motes" /><category term="review" /><category term="jsr331" /><category term="cometd" /><category term="jax-b" /><category term="annotation processor" /><category term="jax-ws" /><category term="wls" /><category term="promotion" /><category term="ramble" /><category term="abbot" /><category term="robomow" /><category term="jsf" /><category term="personal" /><category term="java" /><category term="otn" /><category term="REST" /><category term="webservices" /><category term="security" /><category term="wadl" /><category term="death to boilerplate" /><category term="effectivejava" /><category term="jax-rs" /><category term="apt" /><category term="hudson" /><category term="bluemotion" /><category term="policies" /><category term="oracle" /><category term="ukoug09" /><category term="adf" /><category term="x" /><category term="wlst" /><category term="http analyzer" /><category term="ukoug08" /><category term="polo" /><category term="jobs" /><category term="appengine" /><category term="json-schema" /><category term="lombok" /><category term="weblogic" /><category term="project-dogfood" /><category term="jazoon" /><category term="costello" /><category term="json" /><title>Gerard Davison's Blog</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://kingsfleet.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>235</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/KingsfleetBlog" /><feedburner:info uri="kingsfleetblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DUYMQHw-cSp7ImA9WhBaE0U.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-4188536195760906685</id><published>2013-05-24T10:19:00.000+01:00</published><updated>2013-05-24T10:19:41.259+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-24T10:19:41.259+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><title>wadl 1.1.5 released</title><content type="html">I should have done this a while ago but last month we released a new version of the wadl2java tools, primarily to deal with issues seen whilst writing a &lt;a href="/2013/04/building-simple-java-client-for.html"&gt;previous blog&lt;/a&gt;. The headline change was a move to the latest version of the jsonschema2pojo in order to resolve some class generation issues. Otherwise if you are just using XML Schema based WADL there is nothing new in this release.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/o5TKoju73Do" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/4188536195760906685/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=4188536195760906685" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4188536195760906685?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4188536195760906685?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/o5TKoju73Do/wadl-115-released.html" title="wadl 1.1.5 released" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/05/wadl-115-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUUFRHs5fCp7ImA9WhBaE0U.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-9017883169411941694</id><published>2013-05-23T15:43:00.001+01:00</published><updated>2013-05-24T10:20:15.524+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-24T10:20:15.524+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Persistent breakpoints every java developer should have</title><content type="html">When a developer is working in Java there are a few failure cases you always want to know about even if you were trying to debug something else. Here is a list of the persistent breakpoints that should be enabled in every IDE. (IMHO of course)&lt;br /&gt;
&lt;br /&gt;
Yes in theory you should be able to just get this from a good logging implementation; but often these problems are deep down in somebody else's library caused by changes that are beyond your control.....  or just hidden in a large number of currently executing threads.&lt;br /&gt;
&lt;br /&gt;
Java these days doesn't have that much of an overhead to be running in debug mode, generally I never run anything I am working on without a debugger attached as HotSwap is such a productivity boost. &lt;br /&gt;
&lt;br /&gt;
I would be interested to heard of other people's must have breakpoints. They should be failures that should never come up in the normal running of your application that you would want to know about right away.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Deadlock detection&lt;/h3&gt;&lt;br /&gt;
The VM is getting better and better at recognising deadlocks from low level java contracts, you debugger should have an option to break on deadlock. You should make sure it is turned on by default.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.ExceptionInInitializer&lt;/h3&gt;&lt;br /&gt;
This one can be a right pain to track down because further down the line it can become a ClassNotFoundException and often if it is more than a few levels down there is no obvious reason why the indirectly referencing class has failed to load. &lt;br /&gt;
&lt;br /&gt;
Often caused by people initialising public/final/static variables with methods/constructors that throw RuntimeExceptions. If you are not sure use a static{} block with proper error handling, never do something in there that depends on something external to the current jar file.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.NoClassDefFoundError&lt;/h3&gt;&lt;br /&gt;
Always a popular when working in OSGi, normally you have forgotten a dependant jar file and you see this type of failure. Interestingly you sometimes only get a cause for the first time this exception is thrown for a particular class. So if you have a breakpoint then you can more quickly track this down.&lt;br /&gt;
&lt;br /&gt;
I have found that sometimes that the breakpoint is not hit when behind some reflective code. In that case I often have breakpoints in the constructor of this class just to make sure. &lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.NoSuchMethodError&lt;/h3&gt;&lt;br /&gt;
You will of course generally see this if you haven't built locally properly or you are using miss-matched versions of libraries. You always want to know about this as soon as it happens.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.LinkageError&lt;/h3&gt;&lt;br /&gt;
This normally only turns up when things really go bad, for example when you have two versions of the same class hanging about even though they are from the same jar file. (Gotta love multiple class loaders) Be thankful these don't turn up very often, normally timed for a day when I have a really bad headache already.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.OutOfMemoryError / java.lang.StackOverflowException&lt;/h3&gt;&lt;br /&gt;
If you are very lucky the VM will breakpoint for these; but unfortunately as a lot of time this will happen in native code you will just have to deal with the fallout.&lt;br /&gt;
&lt;br /&gt;
Getting the full stack for the latter is a bit of a pain; but not impossible. See this older post on getting the &lt;a href="/2012/06/catch-stackoverflowerror-by-its-tail.html"&gt;full stack trace&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;On java.lang.AssertionError&lt;/h3&gt;&lt;br /&gt;
A surprising number of people use assertions and then get upset when you complain about there exceptions when debugging or running tests. (You are doing this with -ea aren't you?)&lt;br /&gt;
&lt;br /&gt;
The downside is that you want to turn this off when debugging certain testing frameworks, I am looking at you JUnit, as it is not entirely helpful to hit a breakpoint when you just want to run all of your tests.&lt;br /&gt;
&lt;br /&gt;
Finally every now and again I will run across the following code, which causes an exception just to test if assertions are enabled.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;boolean assertionsEnabled = false;
    try
    { assert false; }
    catch (AssertionException ae)
    { assertionsEnabled = true }
&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Update:&lt;/b&gt; It has been pointed out by a helpful colleague at that I should have provided some alternative suggestions as to what this code should be replaced with. The first option would be to call &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#desiredAssertionStatus()"&gt;Class.desiredAssertionStatus&lt;/a&gt; but if you want to be totally sure you can use the following code that uses assignment to capture the assertion status without throwing an exception.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;boolean assertionsEnabled = false;

    assert isAssertionsEnabled = true;
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/ES4bWCmHgyM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/9017883169411941694/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=9017883169411941694" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/9017883169411941694?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/9017883169411941694?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/ES4bWCmHgyM/persistent-breakpoints-every-java.html" title="Persistent breakpoints every java developer should have" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/05/persistent-breakpoints-every-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMCSXYzfyp7ImA9WhBaEk8.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-1892521672450490759</id><published>2013-05-22T14:14:00.002+01:00</published><updated>2013-05-22T14:14:28.887+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-22T14:14:28.887+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="web socket" /><title>A little command line Web Socket client tool from Tyrus</title><content type="html">The&lt;a href="https://tyrus.java.net"&gt; Tyrus project&lt;/a&gt;, the RI implementation of the Web Socket JSR, has a little sub project added by Pavel Bucek that provides a very useful command line testing tool. (Along with a couple of bug fixes of my own) This blog post gives a quick introduction.&lt;br /&gt;
&lt;br /&gt;
To get hold of the tool at the moment you are going to have to do a little bit of work with svn and maven. So you need to check out the head of &lt;code&gt;https://svn.java.net/svn/tyrus~source-code-repository/trunk&lt;/code&gt; and then perform a &lt;code&gt;mvn package&lt;/code&gt; on the etc/client-cli sub project. Depending on the state of the maven repository you might need to do a &lt;code&gt;mvn install -DskipTests=true&lt;/code&gt; on the root project first.&lt;br /&gt;
&lt;br /&gt;
You can now find the executable jar in &lt;code&gt;.../tyrus/ext/client-clit/target&lt;/code&gt; called something like &lt;code&gt;tyres-client-client....jar&lt;/code&gt;. This has been shaded so it contains all of the required deps to run. Simples.&lt;br /&gt;
&lt;br /&gt;
The tool currently allows you to send text and ping messages. It can receive binary messages but it just displays the hex values of up to the first 1024 bytes.&lt;br /&gt;
&lt;br /&gt;
So you can either pass in the ws:// URL as the single command line argument or you can connect using the open command once the tool is running. Here are some very simple commands along with a response from a simple echo web socket service.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="code"&gt;gdavison@gbr10460 ~]$ java -jar ~/software/tyrus-client-cli-1.0-SNAPSHOT.jar ws://localhost:7101/Project1/echo
# Connecting to ws://localhost:7101/Project1/echo...
# Connected in session 689607b4-7bd8-465a-8d84-e0dca54fa3e8
session 6896...a3e8&gt; 
session 6896...a3e8&gt; send Hello!
# text-message: Message out Hello!
session 6896...a3e8&gt; send
# End multiline message with . on own line
send...&gt; Some
send...&gt; Long
send...&gt; Message
send...&gt; .
# text-message: Message out Some
# Long
# Message
# 
session 6896...a3e8&gt; close
# closed: CloseReason[1000,no reason given]
# Session closed
tyrus-client&gt; quit


gdavison@gbr10460 ~]$ java -jar ~/software/tyrus-client-cli-1.0-SNAPSHOT.jar 

tyrus-client&gt; 
tyrus-client&gt; open ws://localhost:7101/Project1/echo
# Connecting to ws://localhost:7101/Project1/echo...
# Connected in session 59e501a9-a793-4321-ba6a-1c73c13bd822
session 59e5...d822&gt;
session 59e5...d822&gt; ping
# pong-message
session 59e5...d822&gt; help
# 
    open uri : open a connection to the web socket uri
    close : close a currently open web socket session
    send message : send a text message
    send : send a multiline text message teminated with a .
    ping : send a ping message
    quit | exit : exit this tool
    help : display this message
    
session 59e5...d822&gt; 
&lt;/pre&gt;&lt;br /&gt;
I can see this being very helpful when trying to debug a web socket service and you want to remove the web browser out of the equation. It is implemented using &lt;a href="https://github.com/jline/jline2"&gt;JLine2&lt;/a&gt; so it behaves like a native command line tool so is pleasant to use.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/ipDE2j8oy4I" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/1892521672450490759/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=1892521672450490759" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1892521672450490759?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1892521672450490759?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/ipDE2j8oy4I/a-little-command-line-web-socket-client.html" title="A little command line Web Socket client tool from Tyrus" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/05/a-little-command-line-web-socket-client.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEcDQH4-fip7ImA9WhBUE0g.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-4976002370887401407</id><published>2013-04-30T14:07:00.001+01:00</published><updated>2013-04-30T22:07:51.056+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-30T22:07:51.056+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="web socket" /><category scheme="http://www.blogger.com/atom/ns#" term="REST" /><category scheme="http://www.blogger.com/atom/ns#" term="tyrus" /><category scheme="http://www.blogger.com/atom/ns#" term="json" /><category scheme="http://www.blogger.com/atom/ns#" term="jsr356" /><title>Using Java WebSockets, JSR 356,  and JSON mapped to POJO's</title><content type="html">So I have been playing around with &lt;a href="http://tyrus.java.net"&gt;Tyrus&lt;/a&gt;, the reference implementation of the JSR 356 WebSocket for Java spec. Because I was looking at test tooling I was interested in running both the client and the server side in Java. So no HTML5 in this blog post I am afraid.&lt;br /&gt;
&lt;br /&gt;
In this example we want to sent JSON back and forth and because I am old fashioned like that I want to be able to bind to a POJO object. I am going to use Jackson for this so my maven file looks like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name"code" class="xml"&gt;&amp;lt;dependencies&gt;
    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;javax.websocket&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;javax.websocket-api&amp;lt;/artifactId&gt;
        &amp;lt;version&gt;1.0-rc3&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;org.glassfish.tyrus&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;tyrus-client&amp;lt;/artifactId&gt;
        &amp;lt;version&gt;1.0-rc3&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;org.glassfish.tyrus&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;tyrus-server&amp;lt;/artifactId&gt;
        &amp;lt;version&gt;1.0-rc3&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
        &amp;lt;groupId&gt;org.glassfish.tyrus&amp;lt;/groupId&gt;
        &amp;lt;artifactId&gt;tyrus-container-grizzly&amp;lt;/artifactId&gt;
        &amp;lt;version&gt;1.0-rc3&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;
    

    &amp;lt;dependency&gt;
      &amp;lt;groupId&gt;com.fasterxml.jackson.core&amp;lt;/groupId&gt;
      &amp;lt;artifactId&gt;jackson-databind&amp;lt;/artifactId&gt;
      &amp;lt;version&gt;2.2.0&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
      &amp;lt;groupId&gt;com.fasterxml.jackson.core&amp;lt;/groupId&gt;
      &amp;lt;artifactId&gt;jackson-annotations&amp;lt;/artifactId&gt;
      &amp;lt;version&gt;2.2.0&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

    &amp;lt;dependency&gt;
      &amp;lt;groupId&gt;com.fasterxml.jackson.core&amp;lt;/groupId&gt;
      &amp;lt;artifactId&gt;jackson-core&amp;lt;/artifactId&gt;
      &amp;lt;version&gt;2.2.0&amp;lt;/version&gt;
    &amp;lt;/dependency&gt;

  &amp;lt;/dependencies&gt;
&lt;/pre&gt;&lt;br /&gt;
So the first things we need to do is to define an implementations of the Encode/Decoder interfaces to do this work for us. This is going to do some simple reflection to workout what the bean class is. Like with JAX-WS it is easier to put them on the same class. Note that we use the streaming version of the interface and are only handling text content. (Ignoring the ability to send binary data for the moment)&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package websocket;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

public abstract class JSONCoder&amp;lt;T&gt;
  implements Encoder.TextStream&amp;lt;T&gt;, Decoder.TextStream&amp;lt;T&gt;{


    private Class&amp;lt;T&gt; _type;
    
    // When configured my read in that ObjectMapper is not thread safe
    //
    private ThreadLocal&amp;lt;ObjectMapper&gt; _mapper = new ThreadLocal&amp;lt;ObjectMapper&gt;() {

        @Override
        protected ObjectMapper initialValue() {
            return new ObjectMapper();
        }
    };
    

    @Override
    public void init(EndpointConfig endpointConfig) {
    
        ParameterizedType $thisClass = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type $T = $thisClass.getActualTypeArguments()[0];
        if ($T instanceof Class) {
            _type = (Class&amp;lt;T&gt;)$T;
        }
        else if ($T instanceof ParameterizedType) {
            _type = (Class&amp;lt;T&gt;)((ParameterizedType)$T).getRawType();
        }
    }

    @Override
    public void encode(T object, Writer writer) throws EncodeException, IOException {
        _mapper.get().writeValue(writer, object);
    }

    @Override
    public T decode(Reader reader) throws DecodeException, IOException {
        return _mapper.get().readValue(reader, _type);
    }

    @Override
    public void destroy() {
        
    }

}
&lt;/pre&gt;&lt;br /&gt;
The bean class is really quite simple with a static subclass of the Coder that we can use later.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package websocket;

public class EchoBean {
    
    
    public static class EchoBeanCode extends
       JSONCoder&amp;lt;EchoBean&gt; {
        
    }
    
    
    private String _message;
    private String _reply;


    public EchoBean() {
        
    }

    public EchoBean(String _message) {
        super();
        this._message = _message;
    }


    public void setMessage(String _message) {
        this._message = _message;
    }

    public String getMessage() {
        return _message;
    }


    public void setReply(String _reply) {
        this._reply = _reply;
    }

    public String getReply() {
        return _reply;
    }

}
&lt;/pre&gt;&lt;br /&gt;
So new we need to implement our server endpoint, you can go one of two way either annotating a POJO or extending Endpoint. I am going with the first for the server and the second for the client. Really all this service does is to post the message back to the client. Note the registration of the encode and decoder. The same class in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package websocket;

import java.io.IOException;

import javax.websocket.EncodeException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import static java.lang.System.out;

@ServerEndpoint(value="/echo",
                encoders = {EchoBean.EchoBeanCode.class},
                decoders = {EchoBean.EchoBeanCode.class})
public class EchoBeanService
{
    
    @OnMessage
    public void echo (EchoBean bean, Session peer) throws IOException, EncodeException {
        //
        bean.setReply("Server says " + bean.getMessage());
        out.println("Sending message to client");
        peer.getBasicRemote().sendObject(bean);
    }

    @OnOpen
    public void onOpen(final Session session, EndpointConfig endpointConfig) {
        out.println("Server connected "  + session + " " + endpointConfig);
    }
}
&lt;/pre&gt;&lt;br /&gt;
Lets look at a client bean, this time extending the standard Endpoint class and adding a specific listener for a message. In this case when the message is received the connection is simply closed to make our test case simple. In the real world managing this connection would obviously be more complicated.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package websocket;

import java.io.IOException;

import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.EncodeException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

import static java.lang.System.out;

@ClientEndpoint(encoders = {EchoBean.EchoBeanCode.class},
                decoders = {EchoBean.EchoBeanCode.class})
public class EchoBeanClient 
  extends Endpoint
{
    public void onOpen(final Session session, EndpointConfig endpointConfig) {

        out.println("Client Connection open "  + session + " " + endpointConfig);
        
        // Add a listener to capture the returning event
        //
        
        session.addMessageHandler(new MessageHandler.Whole&lt;echobean&gt;() {

            @Override
            public void onMessage(EchoBean bean) {
                out.println("Message from server : " + bean.getReply());
                
                out.println("Closing connection");
                try {
                    session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "All fine"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        
        // Once we are connected we can now safely send out initial message to the server
        //
        
        out.println("Sending message to server");
        try {
            EchoBean bean = new EchoBean("Hello");
            session.getBasicRemote().sendObject(bean);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EncodeException e) {
            e.printStackTrace();
        }

    }
}
&lt;/pre&gt;&lt;br /&gt;
Now running the WebSocket standalone is really quite straightforward with Tyrus, you simple instantiate a Server and start it. Be aware this starts daemon threads so you need to make sure if this is in a main method that you do something to keep the JVM alive.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;import org.glassfish.tyrus.server.Server;

Server server = new Server("localhost", 8025, "/", EchoBeanService.class);
server.start();

&lt;/pre&gt;&lt;br /&gt;
So the client is relatively simple; but as we are doing the declarative method we need to explicitly register the encoders and decoders when registering the client class.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;import javax.websocket.ClientEndpointConfig;
import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.Session;

import org.glassfish.tyrus.client.ClientManager;


// Right now we have to create a client, which will send a message then close
// when it has received a reply
//

ClientManager client = ClientManager.createClient();
EchoBeanClient beanClient = new EchoBeanClient();

Session session = client.connectToServer(
        beanClient, 
        ClientEndpointConfig.Builder.create()
         .encoders(Arrays.&amp;lt;Class&amp;lt;? extends Encoder&gt;&gt;asList(EchoBean.EchoBeanCode.class))
         .decoders(Arrays.&amp;lt;Class&amp;lt;? extends Decoder&gt;&gt;asList(EchoBean.EchoBeanCode.class))
         .build(),
        URI.create("ws://localhost:8025/echo"));
        
        
// Wait until things are closed down
        
while (session.isOpen()) {
    out.println("Waiting");
    TimeUnit.MILLISECONDS.sleep(10);
}
&lt;/pre&gt;&lt;br /&gt;
Now the output of this looks like the following:&lt;br /&gt;
&lt;br /&gt;
&lt;pre nme="code"&gt;Server connected SessionImpl{uri=/echo, id='e7739cc8-1ce5-4c26-ad5f-88a24c688799', endpoint=EndpointWrapper{endpointClass=null, endpoint=org.glassfish.tyrus.core.AnnotatedEndpoint@1ce5bc9, uri='/echo', contextPath='/'}} javax.websocket.server.DefaultServerEndpointConfig@ec120d
Waiting
Client Connection open SessionImpl{uri=ws://localhost:8025/echo, id='7428be2b-6f8a-4c40-a0c4-b1c8b22e1338', endpoint=EndpointWrapper{endpointClass=null, endpoint=websocket.EchoBeanClient@404c85, uri='ws://localhost:8025/echo', contextPath='ws://localhost:8025/echo'}} javax.websocket.DefaultClientEndpointConfig@15fdf14
Sending message to server
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Sending message to client
Message from server : Server says Hello
Closing connection
Waiting
&lt;/pre&gt;&lt;br /&gt;
Interestingly the first time this is run the there is a pause, I suspect this is due to Jackson setting itself up but I haven't had time to profile. I did find that this long delay on occurred on the first post - although obviously this is going to be slower than just passing plain text messages in general. Whether the different is significant to you depends on your application.&lt;br /&gt;
&lt;br /&gt;
It would be interesting to compare the performance of the plain text with a JSON stream API such as that provided by the new JSR and of course the version that binds those values to a JSON POJO. Something for another day perhaps.&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/18iE6RDGz54" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/4976002370887401407/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=4976002370887401407" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4976002370887401407?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4976002370887401407?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/18iE6RDGz54/using-java-websockets-jsr-356-and-json.html" title="Using Java WebSockets, JSR 356,  and JSON mapped to POJO's" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/04/using-java-websockets-jsr-356-and-json.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMDSX47eip7ImA9WhBVGEo.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-2752827970142534169</id><published>2013-04-25T09:27:00.000+01:00</published><updated>2013-04-25T09:27:58.002+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-25T09:27:58.002+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="REST" /><category scheme="http://www.blogger.com/atom/ns#" term="jersey" /><category scheme="http://www.blogger.com/atom/ns#" term="proxy" /><title>Building a simple java client for forecast.io</title><content type="html">I like to be outdoors when life allows so weather is always an interest for me. So i was very pleases to see &lt;a href="http://forecast.io"&gt;forecast.io&lt;/a&gt; becoming available with global coverage after being jealous of the US only "DarkSkys" app. It was also nice to see that there is a &lt;a href="http://developer.forecast.io"&gt;nice REST api&lt;/a&gt; for the service which I could play with.&lt;br /&gt;
&lt;br /&gt;
Unfortunately there was only a textual description of service, nothing I could consume with the Java client tools I have been working with. But I though it would be an interesting experiment to try to document an existing service from scratch.&lt;br /&gt;
&lt;br /&gt;
The first thing I did was to get an example of the data that the service would respond with and run it through a JSON-&gt;JSON Schema tool. I used &lt;a href="http://www.jsonschema.net"&gt;JSONSchema.net&lt;/a&gt; which gave me a rough v3 JSON Schema; but it wasn't able to take into account elements that are reused. A little bit of hand editing later I ended up with three JSON Schema files, one for the &lt;a href="https://github.com/kingsfleet/rest-metadata/blob/master/forecast.io/forecast.json"&gt;forecast&lt;/a&gt; itself and &lt;a href="https://github.com/kingsfleet/rest-metadata/blob/master/forecast.io/datablock.json"&gt;one for a list of weather details&lt;/a&gt; and and one for each particular &lt;a href="https://github.com/kingsfleet/rest-metadata/blob/master/forecast.io/datapoint.json"&gt;data point&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
From there is was pretty simple to read the developer documentation and write up a &lt;a href="https://github.com/kingsfleet/rest-metadata/blob/master/forecast.io/forecast.wadl"&gt;simple WADL&lt;/a&gt; to describe the two resources provided by the service. Luckily Jason LaPort from the DarkSkys was on hand to fix a few mistake and generally verify my description.&lt;br /&gt;
&lt;br /&gt;
Once I had this all in place I of course found a couple of bugs in my own code, so it was time to update and fix &lt;a href="http://wadl.java.net"&gt;wadl.java.net&lt;/a&gt; and release a 1.1.5 version with a revised depdency on the &lt;a href="https://code.google.com/p/jsonschema2pojo/"&gt;jsonschema2pojo&lt;/a&gt; project. This is why a little bit of adhoc expert testing pay dividends.&lt;br /&gt;
&lt;br /&gt;
So taking the WADL from github it is possible to generate a nice clean Java API using the &lt;a href="http://wadl.java.net"&gt;wadl.java.net&lt;/a&gt; generator. I won't r&lt;a href="https://wadl.java.net/wadl2java.html"&gt;eproduce the steps to generate the client&lt;/a&gt; here, but this is the end result and the kind of code you can get out of it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;// Create a Jersey client and setup for POJO JSON mapping

    ClientConfig cc = new DefaultClientConfig();
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    Client c = Client.create(cc);


    // Get current weather for location and check for any alerts 
    
    Forecast f = ApiForecastIo_Forecast
        .apikey(c, "xxxxxxx")                  // Get own key from developer.forecast.io
        .latitudeLongitude(51.32d, -1.244d)    // Some location near where I live
           .getAsForecast(
              null,                            // Not JSONP
              Units.SI,                        // Return values in SI units
              "hourly,minutely,daily,flags");  // Just return current weather and any alerts
    
    // Output a summary

    System.out.println(
        f.getCurrently().getSummary() + " " +
        f.getCurrently().getTemperature() + " C");

    // Output any alerts
  
    List&amp;lt;Alert&gt; alerts = f.getAlerts();
    for (Alert altert : alerts) {
        System.out.println(
            altert.getTitle() + " " + altert.getUri());
    }
&lt;/pre&gt;&lt;br /&gt;
The nice thing here was just how pleasantly easy describing a service that already exists was, yes I could have written it by hand in Java; but then when I wanted to move to Jersey 2.x I would have to re-write by hand rather that just regenerating from the metadata to a new technology target. (Also did the same for a &lt;a href="https://github.com/kingsfleet/rest-metadata/blob/master/bbc.co.uk/bbc-schedule.wadl"&gt;BBC REST Schedule API&lt;/a&gt;, that is in the same git project)&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/t6HzH3PztPM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/2752827970142534169/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=2752827970142534169" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2752827970142534169?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2752827970142534169?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/t6HzH3PztPM/building-simple-java-client-for.html" title="Building a simple java client for forecast.io" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/04/building-simple-java-client-for.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEERX8yeCp7ImA9WhBQGUs.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-9035374079148807186</id><published>2013-03-22T16:05:00.003Z</published><updated>2013-03-22T16:10:04.190Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-03-22T16:10:04.190Z</app:edited><title>wadl2java 1.1.4 released</title><content type="html">We have released a new version of the &lt;a href="http://wadl.java.net"&gt;wadl2java&lt;/a&gt; tool today with a bunch of new features and bug fixes. Again thanks for Pavel Bucek from the Jersey team for his continual help, and for the contribution from my JDeveloper team mate Michael Bachand.&lt;br /&gt;
&lt;br /&gt;
New Features:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Added support for generating binding classes for WADL &amp; JSON Schema decribed services.&lt;/li&gt;
&lt;li&gt;Refactored upgrade transform to wadl-xslt module and added a WADL-&gt;HTML transform.&lt;/li&gt;
&lt;li&gt;Added support for constructors that take a URI, this require use of an internal class UriTemplate in Jersey 2.x to have this work in the RS 2.0 version breaking general compatibility.&lt;/li&gt;
&lt;li&gt;Fixes with regards to the requirement to use a later version of JAX-B than provided with JDK 6, some examples now required endorsed directory to run / build.&lt;/li&gt;
&lt;li&gt;The default message for this exception with just a status was just an empty string, so we generate some boilerplate to fix this. (See JAX_RS_SPEC-312)&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
WADL Bugs:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;WADL-65 Exception if non-required query parameters are passed in as null.&lt;/li&gt;
&lt;li&gt;WADL-66 If the XML Schema didn't start with &amp;lt;?xml it would be treated as a JSON Schema.&lt;/li&gt;
&lt;li&gt;WADL-52 UriBuilder impl dependency missing.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Oracle Bugs:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;16234340 matrix parameters were not showing up in the test URL because of inconsistent usage of the resourceTypes and the resourceObjects structures.&lt;/li&gt;
&lt;li&gt;16429792, where you would get duplicate methods if there were matrix parameters. These are dealt with like template parameters and as such are dealt with differently and needed to be filtered from the optional list.&lt;/li&gt;
&lt;li&gt;16069855 we are modifying the WADL stylesheet (wadl_2009-02.xsl) since it's resulting XHTML document is now properly depicting non ascii characters (such as a french accent) the fix involves adding a element specifying UTF-8 as the charset.&lt;/li&gt;
&lt;li&gt;16061180 Cannot process WADL with regex in template parameters, templates needed to be take out for code gen.&lt;/li&gt;
&lt;li&gt;15878417 Generate template method to make it easier for IDE tooling to customize client creation&lt;/li&gt;
&lt;li&gt;14831111 The client was no longer throwing an exception for a server side failure.&lt;/li&gt;
&lt;li&gt;14710107 where the generated javadoc would would not be rendered properly do a XML copying issue.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Future Work:&lt;br /&gt;
&lt;br /&gt;
Going forward we are looking to generate "java.lang.reflect.Proxy" based clients based on the Jersey 2.x module or a future part of the JAX-RS 2.x specification. We are also going to look at transforming other REST service descriptions into XML, for example Swagger, so that they can be fed into the client generation tool.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/e06pvekvFpg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/9035374079148807186/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=9035374079148807186" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/9035374079148807186?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/9035374079148807186?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/e06pvekvFpg/wadl2java-114-released.html" title="wadl2java 1.1.4 released" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/03/wadl2java-114-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04MQXY_cCp7ImA9WhBSFU4.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7710647991690276122</id><published>2013-02-22T12:24:00.001Z</published><updated>2013-02-22T12:26:20.848Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-22T12:26:20.848Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="REST" /><category scheme="http://www.blogger.com/atom/ns#" term="jersey" /><title>Using UriTemplate to change path parameters in a URI </title><content type="html">I have been recently working on a client generator in the wadl.java.net project and I was solving a bug where I wanted to change path parameter on a URI. This turned out to be quite easy with existing classes that are part of the Jersey project.&lt;br /&gt;
&lt;br /&gt;
First of all you need a instance of UriTemplate, depending on the very of Jersey this is in a slightly different package - you IDE's automatic import will do the work for you.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;  String template = "http://example.com/name/{name}/age/{age}";
  UriTemplate uriTemplate = new UriTemplate(template);

&lt;/pre&gt;&lt;br /&gt;
Then you use match(...) to extract them:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;  String uri = "http://example.com/name/Bob/age/47";
  Map&amp;lt;String, String&gt; parameters = new HashMap&amp;lt;&gt;();

  // Not this method returns false if the URI doesn't match, ignored
  // for the purposes of the this blog.
  uriTemplate.match(uri, parameters);
&lt;/pre&gt;&lt;br /&gt;
Then you can replace the parameters at will and rebuild the URI:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;  parameters.put("name","Arnold");

  UriBuilder builder = UriBuilder.fromPath(template);
  URI output = builder.build(parameters);
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/3sPEaXeb0Hw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7710647991690276122/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7710647991690276122" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7710647991690276122?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7710647991690276122?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/3sPEaXeb0Hw/using-uritemplate-to-change-path.html" title="Using UriTemplate to change path parameters in a URI " /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/02/using-uritemplate-to-change-path.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkAGQHc8eyp7ImA9WhBSFU4.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-1935563794397545569</id><published>2013-02-22T12:05:00.000Z</published><updated>2013-02-22T12:05:21.973Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-22T12:05:21.973Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="junit" /><category scheme="http://www.blogger.com/atom/ns#" term="abbot" /><title>A little temporal feedback when running tests</title><content type="html">I have been working a lot recently with a bunch of test jobs that are intermittently getting stuck. Now it is possible to work out the point where the problem is occurring by looking at the date labels on the LOG output; but it is hard visually to pick these discontinuities out.&lt;br /&gt;
&lt;br /&gt;
In order improve the output in a way that my brain can really quickly parse and identify I thought I would try my hand a bit of simple ASCII animation. I wrote a simple fixture that writes out a line every 1 minute while the test is running, here is a very simple implementation - I am sure there are better ways to do the animation!&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class TickTockFixture
{
   private Timer timer;
   

   @Before
   public void setUp()
   {
      // Clean out any old timers
      tearDown();
      
      // Start a new timer
      
      timer = new Timer();
      timer.scheduleAtFixedRate(new TimerTask()
      {
         int counter = 0;
         
         @Override
         public void run()
         {
            StringBuilder sb = new StringBuilder(". TickTock : ");
            int animation = counter++ % 10;
            boolean direction = animation &lt; 5;
            int count = direction ?  animation : 9 - animation;
            sb.append("     ", 0, count);
            sb.append(direction ? "/" : "\\");
            sb.append("     ",0, 5-count);
            sb.append(counter);
            sb.append(" min(s)");
            System.err.println(sb);
         }
      }, 0, TimeUnit.MINUTES.toMillis(1));
      
   }

   @After
   public void tearDown()
   {
      if (timer!=null)
      {
         timer.cancel();
         timer = null;
      }
   }
}

&lt;/pre&gt;

So does this help, well take a look at the - made up example - output below and try to spot the section of the log output where everything slows down. It is a lot easier with the ticking output which I think sticks out better because of the animation.

&lt;pre name="code"&gt;22-Feb-2013 11:54:30 Items in drs, going to tidy up
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/weblogic.xml
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/web.xml
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/project1/Hello.class
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/project1/
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/.module_marker
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/META-INF/weblogic-application.xml
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/META-INF/application.xml
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/META-INF/
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/.adrs-module.properties
22-Feb-2013 11:54:40 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/
22-Feb-2013 11:54:53 oracle.jdevimpl.webservices.tcpmonitor.config.AnalyzerInstance stop
WARNING: AnalyzerInstance aborting a stop as already appears to be stopped
22-Feb-2013 11:54:53 weblogic.logging.ServerLoggingHandler publish
WARNING: Container weblogic.wsee.jaxws.WLSContainer$BasicContainer@1232e17 doesn't support class com.sun.xml.ws.api.server.Module
22-Feb-2013 11:54:53 weblogic.logging.ServerLoggingHandler publish
WARNING: Container weblogic.wsee.jaxws.WLSContainer$BasicContainer@1232e17 doesn't support class com.sun.xml.ws.api.server.Module
trigger seeding of SecureRandom
. TickTock :  \    9 min(s)
. TickTock : \     10 min(s)
. TickTock : /     11 min(s)
. TickTock :  /    12 min(s)
. TickTock :   /   13 min(s)
22-Feb-2013 11:59:50 done seeding SecureRandom
22-Feb-2013 11:59:53 weblogic.logging.ServerLoggingHandler publish
WARNING: Container weblogic.wsee.jaxws.WLSContainer$BasicContainer@1232e17 doesn't support class com.sun.xml.ws.api.server.Module
22-Feb-2013 12:00:01 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/project1/Hello.class
22-Feb-2013 12:00:01 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/project1/
22-Feb-2013 12:00:01 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/classes/
22-Feb-2013 12:00:01 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/WEB-INF/
22-Feb-2013 12:00:01 Removing file:/scratch/gdavison/view_storage/gdavison_lt/oracle/jdeveloper/system12.1.2.0.40.65.92/o.j2ee/drs/Application1/Project1WebApp.war/
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/eVgnf83qXvQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/1935563794397545569/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=1935563794397545569" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1935563794397545569?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1935563794397545569?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/eVgnf83qXvQ/a-little-temporal-feedback-when-running.html" title="A little temporal feedback when running tests" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/02/a-little-temporal-feedback-when-running.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkAASHg5cCp7ImA9WhBTFUQ.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7549062609231188552</id><published>2013-02-11T16:05:00.002Z</published><updated>2013-02-11T16:05:49.628Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-02-11T16:05:49.628Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="http" /><category scheme="http://www.blogger.com/atom/ns#" term="http analyzer" /><category scheme="http://www.blogger.com/atom/ns#" term="proxy" /><title>Proxying a request to localhost in Java</title><content type="html">It can be very convenient when developing to server based application to run them using "localhost" in order to maintain consistency between developer machines. This is normally a good idea but there is a small case where this can cause problems.&lt;br /&gt;
&lt;br /&gt;
Consider if you are running a local http proxy on your machine in order to capture your HTTP traffic. (Cough perhaps even the one in JDeveloper I work on). Then you might run into &lt;a href="http://bugs.sun.com/view_bug.do?bug_id=6737819"&gt;Java bug 6737819&lt;/a&gt;. Basically by default JDK 1.6 was hard coded not to send any request to localhost via a proxy which of course was a bit of a pain. Luckily a workaround was put in where you could put the string "~localhost" in your nonProxyHosts entry to turn of this feature:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;java -client -classpath classes -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8099 -Dhttp.nonProxyHosts=~localhost -Dhttps.proxyHost=localhost -Dhttps.proxyPort=8099 client.Example
&lt;/pre&gt;&lt;br /&gt;
Now moving forward to JDK 1.7 this workaround no longer works; but you need to take care to define nonProxyHosts as an empty string:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;java -client -classpath classes -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8099 -Dhttp.nonProxyHosts= -Dhttps.proxyHost=localhost -Dhttps.proxyPort=8099 client.Example
&lt;/pre&gt;&lt;br /&gt;
If you define this any anything other than an empty string the &lt;tt&gt;DefaultProxySelector&lt;/tt&gt; though beware because internally it will append / or use the &lt;tt&gt;http.nonProxyHosts&lt;/tt&gt; value from &lt;tt&gt;../jre/lib/net.properties"&lt;/tt&gt;.&lt;br /&gt;
&lt;br /&gt;
Just a minor complication that is not obvious from the published API.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/TtYaBIIC_sg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7549062609231188552/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7549062609231188552" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7549062609231188552?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7549062609231188552?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/TtYaBIIC_sg/proxying-request-to-localhost-in-java.html" title="Proxying a request to localhost in Java" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2013/02/proxying-request-to-localhost-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEHQ3s7eCp7ImA9WhNQFUo.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-2174851242853071040</id><published>2012-11-19T15:00:00.000Z</published><updated>2012-11-22T10:03:52.500Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-11-22T10:03:52.500Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="json-schema" /><category scheme="http://www.blogger.com/atom/ns#" term="json" /><category scheme="http://www.blogger.com/atom/ns#" term="jersey" /><title>JSON-Schema generation in Jersey</title><content type="html">So in my &lt;a href="/2012/11/json-schema-in-wadl.html"&gt;previous post&lt;/a&gt; I talked about a proposal to allow the use of JSON-Schema in a WADL, this post looks at how to get this working with a recently build of Jersey. You are going to have to download / reference 1.16SNAPSHOT until 1.16 is released.&lt;br /&gt;
&lt;br /&gt;
If you are using Maven this should be quite straight forward to update your dependencies assuming you already have jersey and jersey-json. You are just going to need to add a dependency on the "jersey-wadl-json-schema" artefact from the "com.sun.jersey.contribs" group to get the new feature.&lt;br /&gt;
&lt;br /&gt;
If you are outside of Maven the easiest thing to do would be to download the latest &lt;a href="https://maven.java.net/content/repositories/snapshots/com/sun/jersey/jersey-archive/1.16-SNAPSHOT/"&gt;jersey-archive&lt;/a&gt; and then the &lt;a href="https://maven.java.net/content/repositories/snapshots/com/sun/jersey/contribs/jersey-wadl-json-schema/1.16-SNAPSHOT/"&gt;jersey-wadl-json-schema&lt;/a&gt; jar. How you deploy these is tool specific, but if you are using WLS then here are &lt;a href="http://docs.oracle.com/cd/E24329_01/web.1211/e24983/version.htm"&gt;some specific notes&lt;/a&gt; on how to upgrade the version of Jersey.&lt;br /&gt;
&lt;br /&gt;
Once you have this working, you need to create a WadlGeneratorConfig class in order to enable this new grammar generation:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package jersey;

import com.sun.jersey.api.wadl.config.WadlGeneratorConfig;
import com.sun.jersey.api.wadl.config.WadlGeneratorDescription;
import com.sun.jersey.wadl.generators.json.WadlGeneratorJSONGrammarGenerator;

import java.util.List;

public class JsonGeneratorConfig extends WadlGeneratorConfig {
 
    @Override
    public List&lt;wadlgeneratordescription&gt; configure() {
        return generator(WadlGeneratorJSONGrammarGenerator.class).descriptions();
    }
}
&lt;/pre&gt;&lt;br /&gt;
This can then be registered in a variety of ways, here is an example using a servlet init param. Note also that to make this example simple that we are using the Jersey POJO mapping; but whilst writing this blog I noticed that this setting affected the format of the &lt;a href="http://java.net/jira/browse/JERSEY-1593"&gt;JSON version of the WADL&lt;/a&gt; in case you try this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;?xml version = '1.0' encoding = 'ISO-8859-1'?&gt;
&amp;lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0"&gt;
  &amp;lt;servlet&gt;
    &amp;lt;servlet-name&gt;jersey&amp;lt;/servlet-name&gt;
    &amp;lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&amp;lt;/servlet-class&gt;
    &amp;lt;init-param&gt;
      &amp;lt;param-name&gt;com.sun.jersey.config.property.WadlGeneratorConfig&amp;lt;/param-name&gt;
      &amp;lt;param-value&gt;jersey.JsonGeneratorConfig&amp;lt;/param-value&gt;
    &amp;lt;/init-param&gt;
    &amp;lt;init-param&gt;
      &amp;lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&amp;lt;/param-name&gt;
      &amp;lt;param-value&gt;true&amp;lt;/param-value&gt;
    &amp;lt;/init-param&gt;
    &amp;lt;load-on-startup&gt;1&amp;lt;/load-on-startup&gt;
  &amp;lt;/servlet&gt;
  &amp;lt;servlet-mapping&gt;
    &amp;lt;servlet-name&gt;jersey&amp;lt;/servlet-name&gt;
    &amp;lt;url-pattern&gt;/resources/*&amp;lt;/url-pattern&gt;
  &amp;lt;/servlet-mapping&gt;
&amp;lt;/web-app&gt;
&lt;/pre&gt;&lt;br /&gt;
So I put together a really simple echo service, just to check this is all working:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;package jersey;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/echo")
public class EchoResource {
     

    @GET
    @Produces("application/json")
    public EchoBean echo() {
        EchoBean bean = new EchoBean();
        bean.setMessage("Hello");
        return bean;
    }

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public EchoBean echo(EchoBean echo) {
        return echo;
    }
    
}

and

package jersey;

public class EchoBean {
    public EchoBean() {
        super();
    }
    
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }


}
&lt;/pre&gt;&lt;br /&gt;
This very simple example results in the following WADL with the JSON-Schema elements referenced:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;?xml version = '1.0' encoding = 'UTF-8'?&gt;
&amp;lt;ns0:application xmlns:ns0="http://wadl.dev.java.net/2009/02"&gt;
   &amp;lt;ns0:doc xmlns:ns1="http://jersey.java.net/" ns1:generatedBy="Jersey: 1.16-SNAPSHOT 11/19/2012 12:59 AM"/&gt;
   &amp;lt;ns0:grammars/&gt;
   &amp;lt;ns0:resources base="http://localhost:7103/Jersey/resources/"&gt;
      &amp;lt;ns0:resource path="/echo"&gt;
         &amp;lt;ns0:method id="echo" name="GET"&gt;
            &amp;lt;ns0:response&gt;
               &amp;lt;ns0:representation mediaType="application/json" xmlns:ns2="http://wadl.dev.java.net/2009/02/json-schema" ns2:describedby="application.wadl/echoBean"/&gt;
            &amp;lt;/ns0:response&gt;
         &amp;lt;/ns0:method&gt;
         &amp;lt;ns0:method id="echo" name="POST"&gt;
            &amp;lt;ns0:request&gt;
               &amp;lt;ns0:representation mediaType="application/json" xmlns:ns3="http://wadl.dev.java.net/2009/02/json-schema" ns3:describedby="application.wadl/echoBean"/&gt;
            &amp;lt;/ns0:request&gt;
            &amp;lt;ns0:response&gt;
               &amp;lt;ns0:representation mediaType="application/json" xmlns:ns4="http://wadl.dev.java.net/2009/02/json-schema" ns4:describedby="application.wadl/echoBean"/&gt;
            &amp;lt;/ns0:response&gt;
         &amp;lt;/ns0:method&gt;
      &amp;lt;/ns0:resource&gt;
   &amp;lt;/ns0:resources&gt;
&amp;lt;/ns0:application&gt;
&lt;/pre&gt;&lt;br /&gt;
The URI application.wadl/echoBean contains this simple JSON-Schema definition:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="javascript"&gt;{
    "type" : "object",
    "properties" : {
        "message" : {
            "type" : "string"
        }
    },
    "name" : "echoBean"
}
&lt;/pre&gt;&lt;br /&gt;
Now there are a number of limitations in the current design, not least that the generated schema doesn't take into account any &lt;a href="http://java.net/jira/browse/JERSEY-1594"&gt;notation settings&lt;/a&gt;. But I thought this would be enough to provoke feedback on whether this feature would be useful generally. There appears to be a growing interest in JSON-Schema both around the net and internally to Oracle, so it would be interesting to see whether this description becomes more common. &lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/ByM2O6r3CKM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/2174851242853071040/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=2174851242853071040" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2174851242853071040?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2174851242853071040?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/ByM2O6r3CKM/json-schema-generation-in-jersey.html" title="JSON-Schema generation in Jersey" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/11/json-schema-generation-in-jersey.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUEDR386eSp7ImA9WhBWF0o.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-4893621513559953017</id><published>2012-11-16T13:01:00.000Z</published><updated>2013-04-12T15:41:16.111+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2013-04-12T15:41:16.111+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="json-schema" /><category scheme="http://www.blogger.com/atom/ns#" term="json" /><title>JSON-Schema in WADL</title><content type="html">In between other jobs I have been recently been reviewing the WADL specification with a view to fixing some documentation problems with a view to producing an updated version. One of the things that because apparent was the lack of any grammar support for languages other than XML - yes you can use a mapping from JSON&lt;-&gt;XML Schema but this would be less than pleasant for a JSON purist.&lt;br /&gt;
&lt;br /&gt;
So I began to look at how one would go about attaching a &lt;a href="http://json-schema.org"&gt;JSON-Schema&lt;/a&gt; grammar of a JSON document in a WADL description of a service. This isn't a specification yet; but a proposal of how it might work consistently.&lt;br /&gt;
&lt;br /&gt;
Now I work with Jersey mostly, so lets consider what Jersey will current generate for a service that returns both XML and JSON. So the service here is implemented using the JAX-B binding so they both use a similar structure as defined by the XML-Schema reference by the include. &lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&amp;gt;
&amp;lt;application xmlns="http://wadl.dev.java.net/2009/02"&amp;gt;
    &amp;lt;doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.16-SNAPSHOT 10/26/2012 09:28 AM"/&amp;gt;
    &amp;lt;grammars&amp;gt;
        &amp;lt;include href="xsd0.xsd"&amp;gt;
            &amp;lt;doc title="Generated" xml:lang="en"/&amp;gt;
        &amp;lt;/include&amp;gt;
    &amp;lt;/grammars&amp;gt;
    &amp;lt;resources base="http://localhost/"&amp;gt;
        &amp;lt;resource path="/root"&amp;gt;
            &amp;lt;method id="hello" name="PUT"&amp;gt;
                &amp;lt;request&amp;gt;
                    &amp;lt;representation xmlns:m="urn:message" element="m:requestMessage"  mediaType="application/json" /&amp;gt;
                    &amp;lt;representation xmlns:m="urn:message" element="m:requestMessage" mediaType="application/xml" /&amp;gt;
                &amp;lt;/request&amp;gt;
                &amp;lt;response&amp;gt;
                    &amp;lt;representation xmlns:m="urn:message" element="m:responseMessage" mediaType="application/json"/&amp;gt;
                    &amp;lt;representation xmlns:m="urn:message" element="m:responseMessage" mediaType="application/xml" /&amp;gt;
                &amp;lt;/response&amp;gt;
            &amp;lt;/method&amp;gt;
        &amp;lt;/resource&amp;gt;
    &amp;lt;/resources&amp;gt;
&amp;lt;/application&amp;gt; 
&lt;/pre&gt;&lt;br /&gt;
So the first thing we considered was re-using the existing element property, which is defined as a QName, on the representation element to reference an imported JSON-Schema. It is shown here both with and another an arbitrary namespace to it can be told apart from XML elements without a namespace.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;grammars&amp;gt;
        &amp;lt;include href="xsd0.xsd" /&amp;gt;
        &amp;lt;include href="application.wadl/responseMessage" /&amp;gt;
    &amp;lt;/grammars&amp;gt;


    &amp;lt;representation element="responseMessage" mediaType="application/json"/&amp;gt;

Or
    xmlns:json="http://wadl.dev.java.net/2009/02/json" 

    &amp;lt;representation 
        element="json:responseMessage" mediaType="application/json" /&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
The problem is that the JSON-Schema specification as it stands doesn't have a concept of a "name" property, so each JSON-Schema is uniquely identified by it's URI. Also from my read of the specification each JSON-Schema contains the definition for at most one document - not the multiple types / documents that can be contained in XML-Schema. &lt;br /&gt;
&lt;br /&gt;
So the next best suggestion would be to just use the "filename" part of the URI as a proxy for the URI; but of course that won't necessarily be unique. I could see for example the US government and Yahoo both publishing there own "address" micro format.&lt;br /&gt;
&lt;br /&gt;
The better solution to this problem is to introduce a new attribute, luckily the WADL spec was designed with this in mind, that is of type URI that can be used to directly reference the JSON-Schema definitions. So rather than the direct import in the previous example we have a URI property on the element itself. The "describedby" attribute name comes from the &lt;a href="http://tools.ietf.org/html/draft-zyp-json-schema-03#section-4"&gt;JSON-Schema proposal&lt;/a&gt; and is consistent with the rel used on atom links in the spec.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="xml"&gt;xmlns:json="http://wadl.dev.java.net/2009/02/json-schema" 
    xmlns:m="urn:message" 


    &amp;lt;grammars&amp;gt;
        &amp;lt;include href="xsd0.xsd" /&amp;gt;
    &amp;lt;/grammars&amp;gt;

    &amp;lt;representation 
        mediaType="application/json"
        element="m:responseMessage" 
        json:describedby="application.wadl/responseMessage" /&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
The has the secondary advantage in that this format is backwardly compatible with tooling that was relying on the XML-Schema grammar. Although this is probably only of interesting to people who work in tooling / testing tools like myself.&lt;br /&gt;
&lt;br /&gt;
Once you have the JSON-Schema definition then some users are going to want to do away with the XML all together, so finally here is a simple mapping of the WADL to a JSON document that contains just the JSON-Schema information. It has been suggested by &lt;a href="http://sberyozkin.blogspot.co.uk"&gt;Sergey Breyozkin&lt;/a&gt; the JSON mapping would only show the json grammars and I am coming around to that way of thinking. I would be interested to hear of a usecase for the JSON mapping that would want access to the XML Schema.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="javascript"&gt;{
   "doc":{
      "@generatedBy":"Jersey: 1.16-SNAPSHOT 10/26/2012 09:28 AM"
   },
   "resources":{
      "@base":"http://localhost/",
      "resource":{
         "@path":"/root",
         "method":{
            "@id":"hello",
            "@name":"PUT",
            "request":{
               "representation":[
                  {
                     "@mediaType":"application/json",
                     "@describedby":"application.wadl/requestMessage"
                  }
               ]
            },
            "response":{
               "representation":[
                  {
                     "@mediaType":"application/json",
                     "@describedby":"application.wadl/responseMessage"
                  }
               ]
            }
         }
      }
   }
}
&lt;/pre&gt;&lt;br /&gt;
I am currently using the mime type of "application/vnd.sun.wadl+json" for this mapping to be consistent with the default WADL mime type. I suspect we would want to change this in the future; but it will do for starters.&lt;br /&gt;
&lt;br /&gt;
So this is all very interesting but you can't play with it unless you have an example implementation. I have something working for both the server side and for a Java client generator in Jersey and wadl2java respectively and that will be the topic of my next post. I have been working with &lt;a href="https://twitter.com/pavel_bucek"&gt;Pavel Bucek&lt;/a&gt; on the Jersey team on these implementations and the WADL proposal, thanks very much to him for putting up with me.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/YPDFWabZC6A" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/4893621513559953017/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=4893621513559953017" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4893621513559953017?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4893621513559953017?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/YPDFWabZC6A/json-schema-in-wadl.html" title="JSON-Schema in WADL" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/11/json-schema-in-wadl.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UHRXk8fSp7ImA9WhNTGUU.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-3690807368080844242</id><published>2012-10-11T11:29:00.000+01:00</published><updated>2012-10-23T10:40:34.775+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-23T10:40:34.775+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="jenkins" /><category scheme="http://www.blogger.com/atom/ns#" term="hudson" /><title>Using Hudson/Jenkins to diagnose that intermittent failure</title><content type="html">&lt;br /&gt;
I have been working on one of those intermittent bugs that just won't reproduce on my machine; but will reproduce intermittently on other machines while they are running automated testing. I filled the code with trace statements, now I suspect the problem is in code that I don't control and doesn't appear to have much in the way of diagnostics in the place I am working on.&lt;br /&gt;
&lt;br /&gt;
So I did the obvious thing which is to run the tests on a loop on my machine overnight, 12 hours later and 8 test iterations later, no test failures and I am no further forward.&lt;br /&gt;
&lt;br /&gt;
Since the tests are failing in the Hudson farm, it make sense to try to connect the debugger up to those jobs; but I don't want to hang around to attach the remove debugger to each. Thankfully there is a workaround that will allow me to set suitable breakpoints and manage the debugger connection for me.&lt;br /&gt;
&lt;br /&gt;
First of all you need to configure you IDE to accept incoming debugger connections, here are some notes on configuring &lt;a href="/2010/09/getting-more-out-of-your-debugger-with.html"&gt;&lt;span id="goog_263730183"&gt;&lt;/span&gt;JDeveloper&lt;span id="goog_263730184"&gt;&lt;/span&gt;&lt;/a&gt;&amp;nbsp;for a listening debugger, in Netbeans you need to use the Debug-&amp;gt;Attach menu item and select "SocketListen" as the connector and configure as per JDeveloper. In Eclipse you need to configure the debug type as &lt;a href="http://www.ibm.com/developerworks/opensource/library/os-eclipse-javadebug/index.html"&gt;"Socket Listen"&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The second step is modifying your build system so that there is a target you can call that will start the test cases in debug mode. This is an example of the parameters for one of our CI jobs that passes in the right information. Note of course the blacked out text the the name of then machine you are trying to connect back to.&amp;nbsp;(The java tests are started with the parameter&amp;nbsp;&lt;span style="background-color: white; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 14px; margin: 0px; padding: 0px; text-align: left;"&gt;-agentlib:jdwp=transport=dt_socket,address=xxxx.oracle.com:&lt;/span&gt;&lt;span class="number" style="background-color: white; border: none; color: #c00000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 14px; margin: 0px; padding: 0px; text-align: left;"&gt;5000&lt;/span&gt;&lt;span style="background-color: white; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 14px; margin: 0px; padding: 0px; text-align: left;"&gt;,server=n)&amp;nbsp;&lt;/span&gt;Make sure that you don't have any firewalls running on that machine that will block the in-coming connections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-FawLzxbUrLc/UHaQ4_MF5KI/AAAAAAAAAWY/UNGb2SLNSuI/s1600/Screen+Shot+2012-10-11+at+10.23.19.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-FawLzxbUrLc/UHaQ4_MF5KI/AAAAAAAAAWY/UNGb2SLNSuI/s1600/Screen+Shot+2012-10-11+at+10.23.19.jpeg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
You probably will want to run to run multiple jobs at the same time if you have the nodes available, so consider checking this concurrent build box. Always a good idea to bring cakes / cookies into the office if you are going to tie up all the preflight nodes for the day.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-ct-QNvKIchA/UHaROZbkVPI/AAAAAAAAAWg/Uoi6z6UMcyY/s1600/Screen+Shot+2012-10-11+at+10.28.04.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-ct-QNvKIchA/UHaROZbkVPI/AAAAAAAAAWg/Uoi6z6UMcyY/s1600/Screen+Shot+2012-10-11+at+10.28.04.jpeg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
And then all that remains is to run a bunch of jobs and wait for your breakpoint to be hit, might take a little while; but it is going be quicker than running these jobs in series on your own machine. And if your farm is Heterogeneous so much the better for reproducing intermittent failures.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-A-DC-DzIxvo/UHaP3OYYVTI/AAAAAAAAAWQ/ejIX05qaI_A/s1600/Screen+Shot+2012-10-11+at+10.20.29.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-A-DC-DzIxvo/UHaP3OYYVTI/AAAAAAAAAWQ/ejIX05qaI_A/s1600/Screen+Shot+2012-10-11+at+10.20.29.jpeg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;You can sit back and then wait for your code to fail..... may I suggest some &lt;a href="https://blogs.oracle.com/java/entry/javaone_content_available_for_free"&gt;sessions from JavaOne&lt;/a&gt; while you wait?&lt;/div&gt;&lt;span id="goog_1695569767"&gt;&lt;/span&gt;&lt;span id="goog_1695569768"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/VSeZlPDpNeI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/3690807368080844242/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=3690807368080844242" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/3690807368080844242?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/3690807368080844242?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/VSeZlPDpNeI/using-hudsonjenkins-to-diagnose-that.html" title="Using Hudson/Jenkins to diagnose that intermittent failure" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-FawLzxbUrLc/UHaQ4_MF5KI/AAAAAAAAAWY/UNGb2SLNSuI/s72-c/Screen+Shot+2012-10-11+at+10.23.19.jpeg" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/10/using-hudsonjenkins-to-diagnose-that.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8FR30-fCp7ImA9WhJaE0k.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-8171205757133214348</id><published>2012-10-04T11:42:00.001+01:00</published><updated>2012-10-04T11:50:16.354+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-04T11:50:16.354+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="jax-rs" /><title>wadl2java 1.1.3 released</title><content type="html">So after a little bit of a pause we now have a new release of the &lt;a href="http://wadl.java.net/"&gt;wadl2java client generation&lt;/a&gt; tool. This is a wide range of improvements in this release; but the main feature is the new support for generating JAX-RS 2.0 client code.&lt;br /&gt;
&lt;br /&gt;
This generated code is nearly identical to the Jersey 1.x interface so most simple code should compile straight after a swap without too many problems. If the client is making use of ClientResponse these references will have to be replaced with Response; but otherwise the interface will be consistent. I would welcome suggestions as to how I could improve what is generated.&lt;br /&gt;
&lt;br /&gt;
So here is a list of items of changes in the release:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://java.net/jira/browse/WADL-25"&gt;WADL-25&lt;/a&gt;, a patch to allow the passing in of argument to xjc, as provided by Brian Chapman thanks for that..&lt;/li&gt;
&lt;li&gt;There is a problem in when there was not response content type, this type of method now returns the relevant Response object.&lt;/li&gt;
&lt;li&gt;Classes that are @XmlType were being wrapped incorrectly with JAXBElement and the information was being taken from the return class.&lt;/li&gt;
&lt;li&gt;The Proxy objects are now immutable, modifying any property created a new instance.&lt;/li&gt;
&lt;li&gt;There is a now a programmatic interface to override the base URI at each level. (Suggested by Luigi Tagliamonte)&lt;/li&gt;
&lt;li&gt;Fix for Oracle Bug 13804542, generation would fail if inner and outer class names match&lt;/li&gt;
&lt;li&gt;JAX-RS 2.0 generation support available in &lt;a href="http://wadl.java.net/wadl2java.html"&gt;all tool modes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Fix for Oracle Bug 14534583, where a fault element was being incorrectly upgraded from a '2006 WADL. This was causing problems with the examples SOAPUI were providing.&lt;/li&gt;
&lt;li&gt;Fix for Oracle Bug 1462282, where the generated exception classes were not actually used in the generated code. All exceptions now extend WebAppilcationException to make migration from 1.x to 2.x easier.&lt;/li&gt;
&lt;li&gt;Improved method generation, removed "application" from media types, removed duplication when method takes and returns the same type (putApplicationXmlAsApplicationXml -&amp;gt; putXml), moved "As" to correct location when we have a type, removed excessive method combinations where we have symmetrical media types. (eg xml,json-&amp;gt;xml,json)&lt;/li&gt;
&lt;li&gt;Initial work looking at how to support JSON-Schema type generation, non functional&lt;/li&gt;
&lt;li&gt;Adding functional testing for generated clients as was previously only examining the generated classes. Now they are run for both 1.x and 2.x type clients.&lt;/li&gt;
&lt;/ul&gt;
&amp;nbsp;Thanks again as usual help from Pavel Bucek in getting this software out the door.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/il9_y6Onel8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/8171205757133214348/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=8171205757133214348" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/8171205757133214348?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/8171205757133214348?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/il9_y6Onel8/wadl2java-113-released.html" title="wadl2java 1.1.3 released" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/10/wadl2java-113-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEICRnoyeyp7ImA9WhJbEUg.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7569671888112261382</id><published>2012-09-20T16:55:00.000+01:00</published><updated>2012-09-20T16:56:07.493+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-20T16:56:07.493+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="jdeveloper" /><category scheme="http://www.blogger.com/atom/ns#" term="http analyzer" /><title>So what credentials do you need?</title><content type="html">One of the features that I am very proud of in the HTTP Analyzer in JDeveloper is the ability to test a SOAP service with the minimum of fuss. Rather than having to configure security headers and understand WS-Policy we make use of the WLS JAX-WS client to read and interpret the policies defined in the WSDL and then apply abstracted credentials.&lt;br /&gt;
&lt;br /&gt;
It turns out that this feature is very popular; but there is some confusion about just what information you might need to provide for a particular policy. So here is a simple summary of what is required for what type of policy.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Username Token&lt;/h3&gt;
&lt;br /&gt;
The simplest page just ask for a username and password. You are going to need this for policies that contains the phrase usernametoken, username, UNT or even HttpBasic and HttpDigest. The policy dictates where the values end up whether they be in an HTTP header or a WS-Security SOAP header.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-mnUTIOfQ3xc/UFsx6K1xdmI/AAAAAAAAAVg/jlflsZZ_3Gk/s1600/Screen+Shot+2012-09-20+at+16.13.32.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="227" src="http://2.bp.blogspot.com/-mnUTIOfQ3xc/UFsx6K1xdmI/AAAAAAAAAVg/jlflsZZ_3Gk/s320/Screen+Shot+2012-09-20+at+16.13.32.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
X509 Certificates&lt;/h3&gt;
&lt;br /&gt;
The X509 panel is the most powerful and the most misunderstood. On this panel you can define a set of certificates and keys for both the client and the server.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-fMr_clYhsjs/UFsyD6caUAI/AAAAAAAAAVo/IdH6jkUnBx0/s1600/Screen+Shot+2012-09-20+at+16.14.28.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="226" src="http://1.bp.blogspot.com/-fMr_clYhsjs/UFsyD6caUAI/AAAAAAAAAVo/IdH6jkUnBx0/s320/Screen+Shot+2012-09-20+at+16.14.28.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
The simplest version of this panel would be one that just contains the server key store. This is sufficient in most policy to be able to send a server an encrypted message and receive a message back. (In the most common case the server certificate is used to encrypt a symmetrical key, most likely AES, &amp;nbsp;that can be used to decrypt the message being received.)&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
You will also need this to verify messages that are signed by the server.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
If you need to server to trust that you send the message with signing then you have to provide a identity in the form of a Public/Private key that can be used to encrypt the outgoing hashes used to sign the message.&amp;nbsp;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
This client identity can also be used in leu of the username token described earlier. In the most common policy I have worked with the CN of the public certificate is used as the user identity.&amp;nbsp;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
Http Keystores&lt;/h3&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
Some policies will make use of a Https channel to send messages, and like the x509 certificate panel the settings come in both client and server side store.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
In the simplest case this panel will just be configured with a "Trusted Certificates Keystore", so this will list with certificates or certificate signing authorities are going to be trusted by the analyser. In order to make this as easy as possible by default we append the "CA" certificates that come with Java so you can by default connect to most publish websites without any further configuration.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-cQzYmW0UaqA/UFsyW68XlXI/AAAAAAAAAVw/NWPZQ0jcefU/s1600/Screen+Shot+2012-09-20+at+16.15.36.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="228" src="http://2.bp.blogspot.com/-cQzYmW0UaqA/UFsyW68XlXI/AAAAAAAAAVw/NWPZQ0jcefU/s320/Screen+Shot+2012-09-20+at+16.15.36.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
This will normally be enough unless the policy is defined as 2-Way HTTPS, most of the OWSM https policies I have worked with are 2-Way by default. The WLS ones tend to specify this explicitly. In the 2-Way case you again need to prove the identity of the client, and the only way of doing this is going to be with a Public/Private key that the server trusts.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
STS Configuration&lt;/h3&gt;
&lt;div&gt;
The final panel regards working with policies that require SAML tokens; JDeveloper is unable to issue SAML tokens itself so instead it need to get hold of a token from a STS store that is trusted by the server you are in contact with.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In this case you need to specify the URL of the endpoint of the STS service along with a security policy that will match that on the sever side. This is unfortunately a static list and has to be figured out manually. Perhaps this is something that can be improved on in a later release.&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-U8-VR-2E8PU/UFsydzScjeI/AAAAAAAAAV4/SnMpLiXmINY/s1600/Screen+Shot+2012-09-20+at+16.16.14.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="230" src="http://2.bp.blogspot.com/-U8-VR-2E8PU/UFsydzScjeI/AAAAAAAAAV4/SnMpLiXmINY/s320/Screen+Shot+2012-09-20+at+16.16.14.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
On important feature to note is that when connecting to the STS store the analyser will be using values from the other three panels that we have discussed. This might be important if you are having trouble setting up a connection.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/K0W-FaPo-Po" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7569671888112261382/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7569671888112261382" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7569671888112261382?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7569671888112261382?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/K0W-FaPo-Po/so-what-credentials-do-you-need.html" title="So what credentials do you need?" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-mnUTIOfQ3xc/UFsx6K1xdmI/AAAAAAAAAVg/jlflsZZ_3Gk/s72-c/Screen+Shot+2012-09-20+at+16.13.32.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/09/so-what-credentials-do-you-need.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8GSHwzcSp7ImA9WhJaE0k.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7520501499269771338</id><published>2012-09-05T15:09:00.001+01:00</published><updated>2012-10-04T11:50:29.289+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-04T11:50:29.289+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Changing delay, and hence the order, in a DelayQueue</title><content type="html">So I was looking at building a simple object cache that expires the objects after a given time. The obvious mechanism for this is the use the &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/DelayQueue.html"&gt;DelayedQueue&lt;/a&gt; class from the concurrency package in Java; but I wanted to know if it way possible to update the delay after an object has been added to the queue. Looking at the &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Delayed.html"&gt;Delayed&lt;/a&gt; interface there didn't seem to be a good reason not to in the docs so I thought it was time to experiment.&lt;br /&gt;
&lt;br /&gt;
So first of all you need to to create an instance of Delayed, this is a very simple implementation that with the switch of a flag you can basically invert the timeout order in the list. (And add a suitable offset so things happen in the right order)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;static int COUNT=100;


   class DelayedSwap implements Delayed, Comparable&amp;lt;Delayed&amp;gt; {

       int index = 0;
       volatile boolean swap = false;
       long starttime;

       public DelayedSwap(int index, long starttime) {
           super();
           this.index = index;
           this.starttime = starttime;
       }

       private long getDelay() {
           return (swap ? starttime + (2*COUNT - index) * 100 :
               starttime + index * 100) - System.currentTimeMillis();
       }


       public String toString()
       {
           return index + " swapped " + swap + " delay " + getDelay();
       }

       @Override
       public long getDelay(TimeUnit unit) {
           return unit.convert(getDelay(), TimeUnit.MILLISECONDS);
       }

       @Override
       public int compareTo(Delayed delayed) {
           if (delayed == this)
               return 0;

           return (int)(getDelay(TimeUnit.MILLISECONDS) - delayed.getDelay(TimeUnit.MILLISECONDS));
       }
   }

&lt;/pre&gt;
&lt;br /&gt;
So to test this I created a method that would create a bunch of the DelayedSwap objects and half way through processing the list switch the flag so altering the order of expiration. &lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;public static void main(String[] args) throws InterruptedException {

       long start = System.currentTimeMillis();
       final List&lt;delayedswap&gt; delayed = new ArrayList&lt;delayedswap&gt;();
       for (int i = 1; i &amp;lt; COUNT; i++) {
           delayed.add(new DelayedSwap(i, start));
       }

       final DelayQueue dq = new DelayQueue();
       dq.addAll(delayed);

       new Thread(new Runnable() {

           @Override
           public void run() {
               try {
                   TimeUnit.SECONDS.sleep(5);
               } catch (InterruptedException e) {
               }
               for (DelayedSwap d : delayed) {
                   d.swap = true;
               }
           }
       }).start();

       while (!dq.isEmpty()) {
           System.out.println(dq.take());
       }

   }
&lt;/delayedswap&gt;&lt;/delayedswap&gt;&lt;/pre&gt;
&lt;br /&gt;
So what I was expecting was the elements 1-50 ish written out in the correct order but instead after the swap over the elements are coming out in an arbitrary order quite far away from the request delay time.  &lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;1 swapped false delay -19
2 swapped false delay -4
3 swapped false delay -4
4 swapped false delay -4
5 swapped false delay -4
6 swapped false delay -4
7 swapped false delay -4
8 swapped false delay -4
9 swapped false delay -4
10 swapped false delay -4
11 swapped false delay -4
12 swapped false delay -4
13 swapped false delay -4
14 swapped false delay -4
15 swapped false delay -4
16 swapped false delay -4
17 swapped false delay -4
18 swapped false delay -4
19 swapped false delay -4
20 swapped false delay -4
21 swapped false delay -4
22 swapped false delay -4
23 swapped false delay -4
24 swapped false delay -4
25 swapped false delay -4
26 swapped false delay -4
27 swapped false delay -4
28 swapped false delay -4
29 swapped false delay -4
30 swapped false delay -4
31 swapped false delay -4
32 swapped false delay -4
33 swapped false delay -4
34 swapped false delay -4
35 swapped false delay -4
36 swapped false delay -4
37 swapped false delay -4
38 swapped false delay -4
39 swapped false delay -5
40 swapped false delay -4
41 swapped false delay -4
42 swapped false delay -5
43 swapped false delay -4
44 swapped false delay -5
45 swapped false delay -5
46 swapped false delay -5
47 swapped false delay -5
48 swapped false delay -5
49 swapped false delay -5
50 swapped false delay -5
51 swapped true delay -6
94 swapped true delay -4306
96 swapped true delay -4506
87 swapped true delay -3606
91 swapped true delay -4006
97 swapped true delay -4606
95 swapped true delay -4406
98 swapped true delay -4706
92 swapped true delay -4106
82 swapped true delay -3106
80 swapped true delay -2906
90 swapped true delay -3906
93 swapped true delay -4206
74 swapped true delay -2306
99 swapped true delay -4806
70 swapped true delay -1906
69 swapped true delay -1806
66 swapped true delay -1506
83 swapped true delay -3206
62 swapped true delay -1107
61 swapped true delay -1007
58 swapped true delay -707
71 swapped true delay -2007
89 swapped true delay -3807
85 swapped true delay -3407
78 swapped true delay -2707
86 swapped true delay -3507
81 swapped true delay -3007
88 swapped true delay -3707
84 swapped true delay -3307
79 swapped true delay -2807
76 swapped true delay -2507
72 swapped true delay -2107
68 swapped true delay -1707
65 swapped true delay -1407
60 swapped true delay -907
57 swapped true delay -608
55 swapped true delay -408
75 swapped true delay -2408
77 swapped true delay -2608
73 swapped true delay -2208
63 swapped true delay -1208
67 swapped true delay -1608
64 swapped true delay -1308
59 swapped true delay -808
56 swapped true delay -508
54 swapped true delay -308
53 swapped true delay -208
52 swapped true delay -108
Process exited with exit code 0.
&lt;/pre&gt;
&lt;br /&gt;
So the trick is when you know you are going to modify the delay is to remove and then re-add the element to the queue.  &lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;// Replacement swap loop
    for (DelayedSwap d : delayed) {
        if (dq.remove(d))
        {
            d.swap = true;
            dq.add(d);
        }
    }
&lt;/pre&gt;
&lt;br /&gt;
This run produces a more sensible set of results:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;1 swapped false delay -4
2 swapped false delay -8
3 swapped false delay -14
4 swapped false delay -8
5 swapped false delay -4
6 swapped false delay -4
7 swapped false delay -4
8 swapped false delay -4
9 swapped false delay -4
10 swapped false delay -4
11 swapped false delay -4
12 swapped false delay -4
13 swapped false delay -4
14 swapped false delay -4
15 swapped false delay -4
16 swapped false delay -4
17 swapped false delay -4
18 swapped false delay -8
19 swapped false delay -4
20 swapped false delay -4
21 swapped false delay -4
22 swapped false delay -4
23 swapped false delay -4
24 swapped false delay -4
25 swapped false delay -4
26 swapped false delay -4
27 swapped false delay -4
28 swapped false delay -4
29 swapped false delay -4
30 swapped false delay -4
31 swapped false delay -4
32 swapped false delay -4
33 swapped false delay -4
34 swapped false delay -4
35 swapped false delay -4
36 swapped false delay -4
37 swapped false delay -4
38 swapped false delay -4
39 swapped false delay -5
40 swapped false delay -5
41 swapped false delay -5
42 swapped false delay -4
43 swapped false delay -4
44 swapped false delay -5
45 swapped false delay -5
46 swapped false delay -5
47 swapped false delay -5
48 swapped false delay -5
49 swapped false delay -5
50 swapped false delay -5
99 swapped true delay -5
98 swapped true delay -5
97 swapped true delay -11
96 swapped true delay -1
95 swapped true delay -5
94 swapped true delay -9
93 swapped true delay -5
92 swapped true delay -5
91 swapped true delay -5
90 swapped true delay -5
89 swapped true delay -5
88 swapped true delay -5
87 swapped true delay -5
86 swapped true delay -5
85 swapped true delay -5
84 swapped true delay -5
83 swapped true delay -5
82 swapped true delay -5
81 swapped true delay -5
80 swapped true delay -5
79 swapped true delay -5
78 swapped true delay -5
77 swapped true delay -5
76 swapped true delay -5
75 swapped true delay -5
74 swapped true delay -5
73 swapped true delay -5
72 swapped true delay -6
71 swapped true delay -5
70 swapped true delay -5
69 swapped true delay -5
68 swapped true delay -5
67 swapped true delay -5
66 swapped true delay -5
65 swapped true delay -5
64 swapped true delay -5
63 swapped true delay -6
62 swapped true delay -5
61 swapped true delay -6
60 swapped true delay -6
59 swapped true delay -6
58 swapped true delay -6
57 swapped true delay -6
56 swapped true delay -6
55 swapped true delay -6
54 swapped true delay -6
53 swapped true delay -6
52 swapped true delay -6
51 swapped true delay -6
Process exited with exit code 0.
&lt;/pre&gt;
&lt;br /&gt;
I don't think this is a bug in the object itself, as you wouldn't expect a HashTable to orders it's self when the key changes, but I was a little bit surprise by the behaviour.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/Xy-yDkJPwgM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7520501499269771338/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7520501499269771338" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7520501499269771338?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7520501499269771338?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/Xy-yDkJPwgM/changing-delay-and-hence-order-in.html" title="Changing delay, and hence the order, in a DelayQueue" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/09/changing-delay-and-hence-order-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkIARnc8fSp7ImA9WhJSGUw.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7790305631675468090</id><published>2012-06-28T11:57:00.000+01:00</published><updated>2012-07-10T09:55:47.975+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-10T09:55:47.975+01:00</app:edited><title>Catch a StackOverFlowError by its tail</title><content type="html">One of the more annoying situations you might have to deal with when working with a Java program is a StackOverFlowError, if you have a nice producible test case then there are few options with regard to playing with the &lt;a href="http://stackoverflow.com/questions/3700459/how-to-increase-to-java-stack-size"&gt;stack size&lt;/a&gt;, or setting a &lt;a href="http://stackoverflow.com/a/5165797"&gt;conditional breakpoint / trace of some kind&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
But if you have a test case that might fail once in a 100 times, perhaps a race condition in AWTMulticaster as in my case, then you want to improve the diagnostics. The problem is that by default the VM won't any elements in a stack trace after the first 1024 entries. (At least for JDK 6) So if you run the following trivial example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;package other;

public class Overflow {


   public static final void call(double a, double b, double c, double d) {
       call(a,b,c,d);
   }


   public static void main(String[] args) {
       call(0,0,0,0);
   }

}
&lt;/pre&gt;&lt;br /&gt;
The output will stop before you get to the cause of the problem, making it very hard to resolve the issue.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;&amp;gt; java other.Overflow 
Exception in thread "main" java.lang.StackOverflowError
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
        [ lots of lines removed ]
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
Process exited with exit code 1.
&lt;/pre&gt;&lt;br /&gt;
The good news is that this limit is one of the many &lt;a href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html"&gt;official&lt;/a&gt; and &lt;a href="http://stas-blogspot.blogspot.co.uk/2011/07/most-complete-list-of-xx-options-for.html"&gt;unofficial&lt;/a&gt; things you can tweak when starting a VM.                                                              &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;&amp;gt; java -XX:MaxJavaStackTraceDepth=1000000 other.Overflow 
Exception in thread "main" java.lang.StackOverflowError
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
        [ lots of lines removed ]
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.call(Overflow.java:7)
 at other.Overflow.main(Overflow.java:12)
Process exited with exit code 1.
&lt;/pre&gt;&lt;br /&gt;
Notice that end of the stack now contains the root of the problem which will be incredibly useful when trying to diagnose the issue.&lt;br /&gt;
&lt;br /&gt;
You probably don't want to leave this property set long term on a production servers because I am not entirely sure about the impact of it. Looking at the C++ code it appears that this is just a maximum value and won't affect most other stack traces as they are allocated in a linked list in segments of 32 entries.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/A2pIfQQCPIg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7790305631675468090/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7790305631675468090" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7790305631675468090?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7790305631675468090?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/A2pIfQQCPIg/catch-stackoverflowerror-by-its-tail.html" title="Catch a StackOverFlowError by its tail" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/06/catch-stackoverflowerror-by-its-tail.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEMRX88cCp7ImA9WhVbE0U.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-1972784296820834426</id><published>2012-05-30T15:35:00.000+01:00</published><updated>2012-05-30T15:38:04.178+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-30T15:38:04.178+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>try/with/resources refactoring that isn't</title><content type="html">&lt;br /&gt;
So I was looking at refactoring some code to use the new try/with/resources construct and found that perhaps it is not as simple as it first appears.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;class Close implements AutoCloseable
   {
       @Override
       public void close() {
           System.out.println("Close");
       }
   }


   System.out.println("Normal version");


   Close close = new Close();
   try {
       throw new Error();
   }
   catch (Error e) {
       System.out.println("Catch");
   }
   finally {
       close.close();
   }
&lt;/pre&gt;&lt;br /&gt;
So the output for this is pretty easy to predict; but lets write it out just to be clear.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Normal version
Catch
Close
&lt;/pre&gt;&lt;br /&gt;
So this is pretty easy, lets throw some JDK 7 goodness on the code, this is an obvious refactoring right?&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;System.out.println("JDK 7 version");

   try (Close close2 = new Close()) {
       throw new Error();
   }
   catch (Error e) {
       System.out.println("Catch");
   }
&lt;/pre&gt;&lt;br /&gt;
So you can be pretty sure that given I am writing this up that the output is not going to be the same. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;JDK 7 version
Close
Catch
&lt;/pre&gt;&lt;br /&gt;
So in this case the close is called before the catch, this might not be a problem but in some cases the catch block might need to make use of the resource before everything is shut down. If you want to perform a "refactoring" the code must behave the same before and after the changes. The following code is equivalent to the JDK 7 version without the try/with/resources&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;System.out.println("JDK 6 equivalent of JDK 7 version");

   Close close3 = new Close();
   try {
       try {
           throw new Error();
       }
       finally {
           close3.close();
       }
   }
   catch (Error e) {
       System.out.println("Catch");
   }
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Just for completeness here is one possible refactoring of the original code which is functionally equivalent. Note quite as pretty I am afraid.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code" class="java"&gt;System.out.println("JDK 7 equivalent of original try/catch/finally");

   try (Close close4 = new Close()) {
       try {
         throw new Error();
       }
       catch (Error e) {
           System.out.println("Catch");
       }
   }
&lt;/pre&gt;&lt;br /&gt;
Now I ran this past my colleague Mark Warner and like me was surprised by the result. Mark pointed out that this nuance is noted in the &lt;a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html"&gt;java tutorial&lt;/a&gt;....&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;b&gt;Note:&lt;/b&gt; A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
...but it does feel like this rather important distinction is part of the small print that some people might at first glance assumed is related to the SQL example above it. (Yeah okay I didn't read the SQL example down to the end)&lt;br /&gt;
&lt;br /&gt;
Again not something that will be a problem all the time; but it might be an important consideration in some quite important use cases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/TDTu2qNVDNA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/1972784296820834426/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=1972784296820834426" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1972784296820834426?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/1972784296820834426?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/TDTu2qNVDNA/trywithresources-refactoring-that-isnt.html" title="try/with/resources refactoring that isn't" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/05/trywithresources-refactoring-that-isnt.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMEQHo7fip7ImA9WhVbE0U.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-609766980167610130</id><published>2012-05-30T14:48:00.001+01:00</published><updated>2012-05-30T15:00:01.406+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-30T15:00:01.406+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>try/with/resource context class loaders</title><content type="html">&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;For various reasons I seem to end up writing a lot of code that fiddles with the context class loader in order to get non-module code running in the OSGi environment that JDeveloper runs in. This leads to a whole bunch of code that looks like this:&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;pre class="java" name="code"&gt; public void doSomething() {

       ClassLoader context = Thread.currentThread().getContextClassLoader();

       try {
           Thread.currentThread().setContextClassLoader(Example.class.getClassLoader());

           // Class class that required the context class loader
           Endpoint.publish(null, null);            
       }
       finally {
           Thread.currentThread().setContextClassLoader(context);

       }  
  }
&lt;/pre&gt;&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Now it occurred to me that the try/with/resources feature in JDK 7 isn't just for the nasty things in life, well resources, you can use it for any operation that might previously used a try/finally for.&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;   public void doSomething() {

       try (CloseableContext c = contextClassLoader(Example.class)) {

           // Class class that required the context class loader
           Endpoint.publish(null, null);            
       }
   }
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
It would have been nice to just call the method an not to allocate any variables as in the following example but it isn't allowable &lt;a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3"&gt;in the spec&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;pre class="java" name="code"&gt;   public void doSomething() {

       try (contextClassLoader(Example.class)) { // Compilation errors

           // Class class that required the context class loader
           Endpoint.publish(null, null);            
       }
   }
&lt;/pre&gt;&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Still the implementation of this is rather trivial and does still tidy up the original code. The only wrinkle is the &amp;nbsp;need to have a public class/ interface subtype of AutoCloseable so that we can narrow the throws clause on the close() operation. If you don't do this then the original code has to deal with "throws Exception".&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;/div&gt;&lt;pre class="java" name="code"&gt;   public static CloseableContext contextClassLoader(Class loader) {
       return contextClassLoader(loader.getClassLoader());
   }

   public static CloseableContext contextClassLoader(ClassLoader loader) {
       final Thread currentThread = Thread.currentThread();
       final ClassLoader ocl = currentThread.getContextClassLoader();
       currentThread.setContextClassLoader(loader);
       return new CloseableContext(currentThread, ocl);
   }

   public static class CloseableContext implements AutoCloseable {
       private Thread _currentThread;
       private ClassLoader _ocl;

       private CloseableContext(Thread currentThread, ClassLoader ocl) {
           this._currentThread = currentThread;
           this._ocl = ocl;
       }

       @Override
       public void close() {
           this._currentThread.setContextClassLoader(this._ocl);
       }
   }
&lt;/pre&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/j0HVeCUVIVE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/609766980167610130/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=609766980167610130" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/609766980167610130?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/609766980167610130?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/j0HVeCUVIVE/trywithresource-context-class-loaders.html" title="try/with/resource context class loaders" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/05/trywithresource-context-class-loaders.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkIDRH8-eCp7ImA9WhVbEEg.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-3148440137880740524</id><published>2012-05-26T17:42:00.001+01:00</published><updated>2012-05-26T17:42:55.150+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-26T17:42:55.150+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="jenkins" /><category scheme="http://www.blogger.com/atom/ns#" term="hudson" /><title>Ignoring the return value of a command in Hudson/Jenkins sh task</title><content type="html">So I was was trying to run pylint and nosetest running on a Jenkins instance; but these python commands tend to return a non zero status code if they see a non zero return code. This is a problem as it means that any further steps are ignored. The most obvious thing to try was this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code"&gt;pylint src/openhea -f parseable ; true
&lt;/pre&gt;&lt;br /&gt;
The problem is that both statements are treated as separate steps so when the first one fails it never does the return true. Instead, and pointed out by a helpful chap in the #efdhack2012 room was to do the following&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code"&gt;pylint src/openhea -f parseable || true
&lt;/pre&gt;&lt;br /&gt;
This correctly ignores the false status code, the only wrinkle is that you need to be careful when piping the output, it has to be to the left of the bars.&lt;br /&gt;
&lt;br /&gt;
&lt;pre name="code"&gt;pylint src/openhea -f parseable &gt; output.report || true
&lt;/pre&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/G8DJ1CEBGaI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/3148440137880740524/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=3148440137880740524" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/3148440137880740524?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/3148440137880740524?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/G8DJ1CEBGaI/ignoring-return-value-of-command-in.html" title="Ignoring the return value of a command in Hudson/Jenkins sh task" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/05/ignoring-return-value-of-command-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUABRX89fCp7ImA9WhVVFkk.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-8765023105517368257</id><published>2012-05-10T12:02:00.000+01:00</published><updated>2012-05-10T12:02:34.164+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-10T12:02:34.164+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Common try-with-resources idiom won't clean up properly</title><content type="html">So I was re-reading some blogs on the JDK 7 language changes and I noticed the following idiom seems &lt;a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html"&gt;popular&lt;/a&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;//
try (BufferedReader br = new BufferedReader(
        new FileReader("/tmp/click.xml"))) {
   System.out.println(br.readLine());
}
&lt;/pre&gt;
&lt;br /&gt;
That seems all well and good; but consider what happens should something goes wrong constructing the BufferedReader. The contract for try-with-resources will only try to close and tidy up the declared field, if something goes wrong then FileReader is never closed.&lt;br /&gt;
&lt;br /&gt;
So consider this example where the constructor of BufferedReader throws an Error, it could well be an OutOfMemoryError or any number of other failure modes.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;//
package client;

import java.io.*;

public class BufferedFileExample {

    public static void main(String[] args) throws FileNotFoundException, 
                                                  IOException {

        try (BufferedReader br = new MyBufferedReader(
            new MyFileReader("/tmp/click.xml"))) {
            System.out.println(br.readLine());
        }
    }
    
    public static class MyFileReader extends FileReader 
    {

        public MyFileReader(String in) throws FileNotFoundException {
            super(in);
        }
        
        public void close() throws IOException {
            super.close();
            System.out.println("Close called");
        }
    }
    
    
    
    public static class MyBufferedReader extends BufferedReader 
    {

        public MyBufferedReader(Reader in) {
            super(in);
            throw new Error();
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;
This gets us the following output and as such the close is not performed on the FileReader which until the finaliser is called. You are stuck until the gc wakes up in the future to tidy it up.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Exception in thread "main" java.lang.Error
 at client.BufferedFileExample$MyBufferedReader.&lt;init&gt;(BufferedFileExample.java:38)
 at client.BufferedFileExample.main(BufferedFileExample.java:12)
Process exited with exit code 1.
&lt;/init&gt;&lt;/pre&gt;
&lt;br /&gt;
Luckily there is an easy fix for this, the language change supports multiple entries in try statement, so you can modify this code to be:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="java" name="code"&gt;//
try (FileReader fr = new MyFileReader("/tmp/click.xml");
     BufferedReader br = new MyBufferedReader(
        fr)) {
   System.out.println(br.readLine());
}
&lt;/pre&gt;
&lt;br /&gt;
So if you run the code with the modification above then you find that close is now called:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Close called
Exception in thread "main" java.lang.Error
 at client.BufferedFileExample$MyBufferedReader.&lt;init&gt;(BufferedFileExample.java:38)
 at client.BufferedFileExample.main(BufferedFileExample.java:12)
Process exited with exit code 1.
&lt;/init&gt;&lt;/pre&gt;
&lt;br /&gt;
So problem solved, well unfortunately there is another wrinkle here, if we remove the code that throws the Error then we will see the following output:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Close called
Close called
Process exited with exit code 0.
&lt;/pre&gt;
&lt;br /&gt;
So this is fine for classes that implement the existing &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/io/Closeable.html"&gt;java.io.Closeble&lt;/a&gt; interface because calling close for a second time is fine; but the more &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html"&gt;generic interface&lt;/a&gt; that is used for other closeable resources unfortunately doesn't have this restriction, so quote the spec&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;
Note that unlike the close method of Closeable, this close method is not required to be idempotent. In other words, &lt;b&gt;calling this close method more than once may have some visible side effect&lt;/b&gt;, unlike Closeable.close which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.&lt;/blockquote&gt;
&lt;br /&gt;
So in order to using a split-try-with-resources idiom you first need to make sure that the resources have a idempotent close method. I suspect that most API will do this; but you need to check first and perhaps need to further wrap your objects to make this the case.&lt;br /&gt;
&lt;br /&gt;
One final point with try-with-resource is the handling of exceptions when the close and construction fails. JDK 7 introduced the concept of suppressed exceptions, so you it make the split version of the code throw an exception both in the constructor or the MyBufferedReader and in the close of MyFileReader you get to see the following novel stack trace.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Close called
Exception in thread "main" java.lang.Error
 at client.BufferedFileExample$MyBufferedReader.&lt;init&gt;(BufferedFileExample.java:38)
 at client.BufferedFileExample.main(BufferedFileExample.java:12)
 Suppressed: java.lang.Error: close
  at client.BufferedFileExample$MyFileReader.close(BufferedFileExample.java:27)
  at client.BufferedFileExample.main(BufferedFileExample.java:14)
Process exited with exit code 1.
&lt;/init&gt;&lt;/pre&gt;
&lt;br /&gt;
This might not be what you calling code is expecting - certainly it might differ slightly from what the try/finally code you are replacing would produce, worth know thing that exceptions are handled slightly differently. For example if there is just an Error thrown during the close method would would see this odd self-referential stack trace.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;Close called
Close called
Exception in thread "main" java.lang.Error: close
 at client.BufferedFileExample$MyFileReader.close(BufferedFileExample.java:28)
 at java.io.BufferedReader.close(BufferedReader.java:517)
 at client.BufferedFileExample.main(BufferedFileExample.java:15)
 Suppressed: java.lang.Error: close
  at client.BufferedFileExample$MyFileReader.close(BufferedFileExample.java:28)
  ... 1 more
Process exited with exit code 1.
&lt;/pre&gt;
&lt;br /&gt;
So the lesson here is that the impact of some of the new JDK 7 constructs are going to a little bit subtler than we might expect. Many thanks to my valued colleague Mark Warner for talking this one through with me.&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/WIQoHQXQxqo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/8765023105517368257/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=8765023105517368257" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/8765023105517368257?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/8765023105517368257?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/WIQoHQXQxqo/common-try-with-resources-idiom-wont.html" title="Common try-with-resources idiom won't clean up properly" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>4</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/05/common-try-with-resources-idiom-wont.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4AR38zeCp7ImA9WhVQFU4.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7707077849386502040</id><published>2012-04-03T18:00:00.004+01:00</published><updated>2012-04-04T11:05:46.180+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-04T11:05:46.180+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="jenkins" /><category scheme="http://www.blogger.com/atom/ns#" term="preflight" /><category scheme="http://www.blogger.com/atom/ns#" term="hudons" /><title>Off-loading test execution using Hudson part 3, skipping the queue</title><content type="html">&lt;p&gt;Previously I had looked at preflighting, (&lt;a href="/2011/07/off-loading-your-test-running-using.html"&gt;1&lt;/a&gt;, &lt;a href="/2012/01/off-loading-test-execution-using-hudson.html"&gt;2&lt;/a&gt;) with Hudson (And Jenkins)  but forgot to mention a very important plugin that is essential if you want to convince your developer to preflight there work. The problem is that is the slave allotment/smallholder/farm is busy running other jobs then they are forced to wait before merging.&lt;/p&gt;

&lt;p&gt;The easy solution to this problem is the &lt;a href="http://wiki.hudson-ci.org/display/HUDSON/Priority+Sorter+Plugin"&gt;priority sorter plugin&lt;/a&gt;. This allows you to specify that some jobs are more or less important then others. The default priority is 100, so seeing your preflight job to 1000 should see it jump to the front of the queue.&lt;/p&gt;

&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-CGysw0uCAFo/T3wb1T7QxnI/AAAAAAAAAVA/MDj8X_O5CSE/s1600/Screen%2BShot%2B2012-04-04%2Bat%2B11.02.14.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 260px;" src="http://4.bp.blogspot.com/-CGysw0uCAFo/T3wb1T7QxnI/AAAAAAAAAVA/MDj8X_O5CSE/s400/Screen%2BShot%2B2012-04-04%2Bat%2B11.02.14.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5727483428804281970" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is interesting how the length of tests affects my commuting behaviour, now that is take around an hour to run the long tests just for our components. It makes sense to start a work at home, kick of a job and you should have you nice shiny test results by the time we get into work. One day I might even get a blue ball if I am so very lucky with our dependencies. :-) &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/inHG5FARSGA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7707077849386502040/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7707077849386502040" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7707077849386502040?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7707077849386502040?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/inHG5FARSGA/off-loading-test-execution-using-hudson.html" title="Off-loading test execution using Hudson part 3, skipping the queue" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-CGysw0uCAFo/T3wb1T7QxnI/AAAAAAAAAVA/MDj8X_O5CSE/s72-c/Screen%2BShot%2B2012-04-04%2Bat%2B11.02.14.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/04/off-loading-test-execution-using-hudson.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8BR3o6cSp7ImA9WhVSGUw.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-2122506521765112088</id><published>2012-03-16T09:55:00.004Z</published><updated>2012-03-16T15:14:16.419Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-03-16T15:14:16.419Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="jersey" /><title>Catalog support for WADL client generation</title><content type="html">&lt;p&gt;I have just been fixing a few bugs in the WADL client generator that I have been working on &lt;a href="http://java.net/projects/wadl/sources/svn/revision/327"&gt;recently&lt;/a&gt;. In particular one bug relates to being able to control the server the client uses at deploy time. (&lt;a href="http://java.net/jira/browse/WADhttp://www.blogger.com/img/blank.gifL-44"&gt;WADL-44&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;So for example you have a standard packaged ?ar file that contains a client to a production service, in order to run automated testing you want to be able to use the same archive; but redirect the request to an internal server. Using a deployment plan, &lt;a href="http://kingsfleet.blogspot.com/2008/12/controlling-what-service-proxy-uses-at.html"&gt;for example&lt;/a&gt;, you can update or introduce an extra deployment descriptor called META-INF/jax-rs-catalog.xml.&lt;/p&gt;

&lt;p&gt;Only the base URI for each root resource in the WADL is updated; but this should be enough for most cases. (Would be interested to hear where people though this was limiting). This simple example redirects the client from a production service to a qa server.&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
&amp;lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"&gt;
  &amp;lt;uri name="http://production.example.com/resource"
          uri="http://qa.example.com/resource"/&gt;
&amp;lt;/catalog&gt;
&lt;/pre&gt;

&lt;p&gt;If you want to play with this you are going to have to check out the tip of the &lt;a href="http://wadl.java.net/"&gt;WADL client&lt;/a&gt; project for the moment; but in the coming weeks it will be part of the 1.1.2 release. Feedback always welcome.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/TY55Ier6k1M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/2122506521765112088/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=2122506521765112088" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2122506521765112088?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/2122506521765112088?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/TY55Ier6k1M/catalog-support-for-wadl-client.html" title="Catalog support for WADL client generation" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/03/catalog-support-for-wadl-client.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE4HRX8zfip7ImA9WhVQFU4.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-4121732416066127312</id><published>2012-01-23T09:36:00.007Z</published><updated>2012-04-04T10:48:54.186+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-04T10:48:54.186+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="jenkins" /><category scheme="http://www.blogger.com/atom/ns#" term="preflight" /><category scheme="http://www.blogger.com/atom/ns#" term="hudson" /><title>Off-loading test execution using Hudson part 2</title><content type="html">&lt;p&gt;Previously I have written about using Hudson to perform &lt;a href="/2011/07/off-loading-your-test-running-using.html"&gt;pre-flights using branches&lt;/a&gt; on source control systems; but sometimes you just have a patch file that you want to run your tests against.&lt;/p&gt;

&lt;p&gt;It turns out this can be quite simple, you just need to make use of a job parameter that takes a File as it's input, this is then written to the specified location in the workspace. In my example I have the following two parameters, the Title allow me to name the job and this property is picked up later by the job &lt;a href="http://wiki.hudson-ci.org/display/HUDSON/Description+Setter+Plugin"&gt;Description&lt;/a&gt; plugin.&lt;/p&gt;

&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-LlAecYyycjI/Tx0r1qzYaQI/AAAAAAAAAUU/HGszkrYUTeo/s1600/Screen%2BShot%2B2012-01-23%2Bat%2B09.39.49.jpeg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 167px;" src="http://2.bp.blogspot.com/-LlAecYyycjI/Tx0r1qzYaQI/AAAAAAAAAUU/HGszkrYUTeo/s400/Screen%2BShot%2B2012-01-23%2Bat%2B09.39.49.jpeg" border="0" alt=""id="BLOGGER_PHOTO_ID_5700760904343775490" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you know where the patch is located you can just apply the patch before you perform the required build steps, here is an example using SVN so we use the unix patch command, git users can use the git command instead for this.&lt;/p&gt;

&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-BzfKZoNSp2g/Tx0shsWvHnI/AAAAAAAAAUg/dKc3iupE5Vc/s1600/Screen%2BShot%2B2012-01-23%2Bat%2B09.42.18.jpeg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 153px;" src="http://1.bp.blogspot.com/-BzfKZoNSp2g/Tx0shsWvHnI/AAAAAAAAAUg/dKc3iupE5Vc/s400/Screen%2BShot%2B2012-01-23%2Bat%2B09.42.18.jpeg" border="0" alt=""id="BLOGGER_PHOTO_ID_5700761660674743922" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there you are, you can test a user submitted patch for testing without tying up your development machine. You can also run the tests more than once to check for any intermittent tests which can be very valuable in concurrent environments.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/pd6NXTDR40U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/4121732416066127312/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=4121732416066127312" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4121732416066127312?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4121732416066127312?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/pd6NXTDR40U/off-loading-test-execution-using-hudson.html" title="Off-loading test execution using Hudson part 2" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-LlAecYyycjI/Tx0r1qzYaQI/AAAAAAAAAUU/HGszkrYUTeo/s72-c/Screen%2BShot%2B2012-01-23%2Bat%2B09.39.49.jpeg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2012/01/off-loading-test-execution-using-hudson.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEUNR3k7eSp7ImA9WhRREk4.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-4264181106105244459</id><published>2011-11-25T11:28:00.007Z</published><updated>2011-11-25T14:44:56.701Z</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-25T14:44:56.701Z</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="x" /><category scheme="http://www.blogger.com/atom/ns#" term="jax-rs" /><category scheme="http://www.blogger.com/atom/ns#" term="jersey" /><title>x!, a very simple xml library</title><content type="html">&lt;p&gt;A couple of weeks ago, having gotten annoyed about the amount of boilerplate you need in Java in order to do some very simple XML actions I decided to put something in place that would get me close to the fluency of the Groovy and JScript XML extensions with a very small interface. Indeed the original proposal can be contained in a &lt;a href="http://is.gd/xspec"&gt;tweet&lt;/a&gt; and the only methods I have added since are parent() and prefix() which doesn't quite fit in the standard 140 characters.&lt;/p&gt;

&lt;p&gt;So there is basically only one class to worry about X, this allows you to read and write values, select children and perform the normal input and output operations. Everything throws unchecked exceptions, and where possible Attr and Elements are treated the same so for example set(val) and get() will work for both in a consistent manner.&lt;/p&gt;

&lt;p&gt;You can find simpler examples in the unit tests in the &lt;a href="https://github.com/kingsfleet/x-"&gt;github project&lt;/a&gt;, but the most interesting use cases for me are when used with JAX-RS to access and program REST resources that accept XML. XPath makes Duck Typing easy and if you are careful your code can ignore minor schema changes, this is in stark contrast to static JAX-B binding for example.&lt;/p&gt;

&lt;p&gt;Take this example, it looks at a remote resource and sets it as being offline using the Jersey client. In the simplest case you need to explicitly tell Jersey about the X message body readers and writers; but other than that the code is quite straight forward.&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
   ClientConfig clientConfig = new DefaultClientConfig(
      XMessageBodyWriter.class,XMessageBodyReader.class);    
   Client client = Client.create(clientConfig);
   client.addFilter(new HTTPBasicAuthFilter(...));
        
   WebResource resource = client.resource("http://hudson...");

   resource.put(
      resource.get(X.class)
         .set("//n:offline", "true"));
        
   System.out.println(resource.get(X.class).get("//n:offline"));
&lt;/pre&gt;

&lt;p&gt;Note in the last line we use the pre-defined namespace prefix n which is always populated by the first node in the tree. You can also perform the select, or selectList for a list, explicitly to get hold of the value:&lt;/p&gt; 

&lt;pre name="code" class="java"&gt;
   resource.put(
      resource.get(X.class)
         .select("//n:offline").set("true"));
        
   System.out.println(resource.get(X.class)
     .select("//n:offline").get());
&lt;/pre&gt;


&lt;p&gt;You can also use x! in JAX-RS resources, here is a very simple hello world example using the same message body readers and writers as before. Since they are marked as @Provider your container should be able to pick them up for you.&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
@Path("/hello")
public class HelloMessage {
    @POST
    @Produces("text/xml")
    @Consumes("text/xml")
    public X hello(X input) {

        String name = input.select("//name").get();

        X response = X.in("http://www.example.com", "message");
        response
            .children().create("name")
                .set(name).parent()  // Think of .parent() like a CR
            .children().create("message")
                .set("Hello " + name)
                .set("@lang", "en");

        return response;
    }
}
&lt;/pre&gt;

&lt;p&gt;I am not entirely sure about the flow to create new objects, feedback is always appreciated of course. The use of @ to set attributes is quick as internally this doesn't result in a XPath query. Currently you can only work on direct children when creating new attributes, for more complex paths you need to select the direct parent to create new attributes. You can still set values on attributes that do exist with complex xpath expressions though.&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
    // @attr exists
    x.select("....@attr").set("value");
    or 
    s.set("...@attr", "value");
&lt;/pre&gt;

&lt;p&gt;Here is a simple request and response from this service just for comparison you can get the code to reflect the xml quite closely.&lt;/p&gt;

&lt;pre name="code" class="xml"&gt;
// Example input message
&amp;lt;message&gt;
   &amp;lt;name&gt;Bob&gt;&amp;lt;/name&gt;
&amp;lt;/message&gt;

// Example response

&amp;lt;message xmlns="http://www.example.com"&gt;
   &amp;lt;name&gt;Bob&gt;&amp;lt;/name&gt;
   &amp;lt;message lang="en"&gt;Hello Bob&gt;&amp;lt;/message&gt;
&amp;lt;/message&gt;
&lt;/pre&gt;

&lt;p&gt;This really was a thought experiment that got out of control; but I would welcome feedback, suggestions, and of course as it is on &lt;a href="https://github.com/kingsfleet/x-"&gt;GitHub patches&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One interesting direction is replacing the current DOM implementation with a streaming version which might be possible for certain read and write options depending on the ordering of the data.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/rGr8szUFiw4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/4264181106105244459/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=4264181106105244459" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4264181106105244459?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/4264181106105244459?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/rGr8szUFiw4/x-or-very-simple-xml-library_25.html" title="x&amp;#33;, a very simple xml library" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2011/11/x-or-very-simple-xml-library_25.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkMGSX07fyp7ImA9WhdaEE8.&quot;"><id>tag:blogger.com,1999:blog-2648073517459434852.post-7349895584163656768</id><published>2011-10-19T13:25:00.005+01:00</published><updated>2011-10-19T13:47:08.307+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T13:47:08.307+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="wadl" /><category scheme="http://www.blogger.com/atom/ns#" term="REST" /><category scheme="http://www.blogger.com/atom/ns#" term="hudson" /><title>A little script to update job descriptions on Matrix jobs</title><content type="html">&lt;p&gt;We have started to use Matrix jobs in Hudson to try and reduce the amount of clutter on the front page; but we have run into problems in that the &lt;a href="http://wiki.hudson-ci.org/display/HUDSON/Description+Setter+Plugin"&gt;job description&lt;/a&gt; plugin we used to add the LABEL name to the job id only works for the &lt;a href="http://issues.hudson-ci.org/browse/HUDSON-9046"&gt;subordinate tasks&lt;/a&gt;. This leaves the top level job without a description.&lt;/p&gt;

&lt;p&gt;There are of course other ways you can do this; but since I was playing with the new REST api yesterday, I am going to use this. You could of course use the old RESTy API or indeed the command line tool to perform similar feats. This week I mostly just have a hammer.&lt;/p&gt;

&lt;p&gt;Before you use the proxy classes you need to first create a client with the right security credentials configured:&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
   public static Client createClient() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("xxxxx@oracle.com","xxxxxxx"));
       return client;
   }
&lt;/pre&gt;

&lt;p&gt;The first part of the job is going to be to create proxies for both the parent project and of the child variant:&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
   public static void main(String[] args) {

       if (args.length!=2) {
           System.err.println("Expecting two parameters project and variant");
       }

       String project = args[0];
       String variant = project + "%2F" + args[1]; 

       System.out.printf("Will copy missing description from %s to %s\n", variant, project);

       // Create a Jersey client
       Client client = createClient();

       // Create a suitable proxy using the client we configured
       Projects hudsonProjects = projects(client);

       // Handles to the right resources
       Projects.ProjectNameBuilds projectBuilds = hudsonProjects.projectNameBuilds(project);
       Projects.ProjectNameBuilds variantBuilds = hudsonProjects.projectNameBuilds(variant);
&lt;/pre&gt;

&lt;p&gt;Then we can make a quick map of the variant build number to descriptions by getting the first build object:&lt;/p&gt;

&lt;pre name="code" class="java"&gt;
       // Copy the descriptions out from the variant
       Map&amp;lt;Integer, String&gt; variantToDescription = new HashMap&amp;lt;Integer,String&gt;();
       BuildsDTO variantBuildsDTO = variantBuilds.getAsApplicationXml(BuildsDTO.class);
       for (BuildDTO build : variantBuildsDTO.getBuild()) {
           if (build.getDescription()!=null)
           {
               variantToDescription.put(
                   build.getNumber(), 
                   build.getDescription());
           }
       }
&lt;/pre&gt;

&lt;p&gt;Now it turns out that the current version of the API doesn't allow you to update build descriptions; but we can easily read them using this API and then update them using a simple form POST using the same client.&lt;/p&gt;

&lt;pre name="code" class="java"&gt;

       // Update the main project descriptions
       BuildsDTO projectBuildsDTO = projectBuilds.getAsApplicationXml(BuildsDTO.class);
       for (BuildDTO build : projectBuildsDTO.getBuild()) {

           String description = build.getDescription();
           // Update description if it has not already been set
           if (description == null || description.length()==0) {

               String variantDesc = variantToDescription.get(build.getNumber());
               if (variantDesc!=null &amp;&amp; variantDesc.length()!=0) {

                   // We need to set the description; but nothing in the REST API
                   // so we can use the url property to perform a form submit that will
                   // update the description for us.

                   MultivaluedMap map = new Form();
                   map.add("description", variantDesc);
                   map.add("Submit", "Submit");
                   WebResource buildResource = client.resource(
                       build.getUrl() + "submitDescription");
                   ClientResponse response = buildResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
                       .post(ClientResponse.class, map);
                   System.out.printf("Updating build %d with description %s gave response %d\n",
                                     build.getNumber(), variantDesc, response.getStatus());

               }
           }
       }
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript"&gt;
_uacct = "UA-2176345-1";
urchinTracker();
&lt;/script&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KingsfleetBlog/~4/50tZiIcXfo4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://kingsfleet.blogspot.com/feeds/7349895584163656768/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2648073517459434852&amp;postID=7349895584163656768" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7349895584163656768?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2648073517459434852/posts/default/7349895584163656768?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KingsfleetBlog/~3/50tZiIcXfo4/little-script-to-update-job.html" title="A little script to update job descriptions on Matrix jobs" /><author><name>Gerard Davison</name><uri>http://www.blogger.com/profile/16521924431312439170</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="28" height="32" src="http://1.bp.blogspot.com/_wg73kpMXUVw/SR2BnEFpCHI/AAAAAAAAAGU/lvJNPVj958I/s1600-R/10680.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://kingsfleet.blogspot.com/2011/10/little-script-to-update-job.html</feedburner:origLink></entry></feed>
