<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8779314896221625511</id><updated>2012-01-16T07:06:04.864-08:00</updated><title type='text'>Stash's Seriously Named Computing Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>98</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8298653667028599513</id><published>2012-01-14T11:09:00.000-08:00</published><updated>2012-01-14T11:10:27.517-08:00</updated><title type='text'>Xerces Parsing and the Dreaded FWK005 Exception</title><content type='html'>&lt;p&gt;One of the &lt;b&gt;Java/Groovy&lt;/b&gt; applications I'm currently works with multiple threads.  In each thread, an XML service response is marshalled into an object using a method similar to:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;messageObjectFrom&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String responseXml&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def jaxbElement &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;JAXBElement&lt;span style="color: #666666"&gt;)&lt;/span&gt; unmarshaller&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;unmarshal&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;readerFor&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseXml&lt;span style="color: #666666"&gt;))&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;(&lt;/span&gt;MessageType&lt;span style="color: #666666"&gt;)&lt;/span&gt; jaxbElement&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;value&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Under a reasonable amount of load (several threads), the application would report the following exception during an execution of the method:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;    javax.xml.bind.UnmarshalException: null&lt;br /&gt;            at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315) ~[na:1.6.0_24]&lt;br /&gt;    ...&lt;br /&gt;    Caused by: org.xml.sax.SAXException: FWK005 parse may not be called while parsing.&lt;br /&gt;            at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[com.springsource.org.apache.xerces-2.8.1.jar:na]&lt;br /&gt;            at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[com.springsource.org.apache.xerces-2.8.1.jar:na]&lt;br /&gt;            at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211) ~[jaxb-impl-2.1.13.jar:2.1.13]&lt;br /&gt;            ... 43 common frames omitted&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I've seen the error in the past when parsing XML in a multi-threaded application using &lt;b&gt;Xerces&lt;/b&gt;.  Several mailing lists have had questions about this error over the years which point to a concurrency issue in the &lt;b&gt;Xerces&lt;/b&gt; parser.  In my application, the parser is used inside multiple threads.&lt;/p&gt;&lt;p&gt;Most of the postings suggest synchronizing access to the parser.  I attempted to do so by adding the &lt;b&gt;synchronized&lt;/b&gt; keyword to the method signature:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;synchronized&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;messageObjectFrom&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String responseXml&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def jaxbElement &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;JAXBElement&lt;span style="color: #666666"&gt;)&lt;/span&gt; unmarshaller&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;unmarshal&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;readerFor&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseXml&lt;span style="color: #666666"&gt;))&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;(&lt;/span&gt;MessageType&lt;span style="color: #666666"&gt;)&lt;/span&gt; jaxbElement&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;value&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This indeed did resolve the issue as I haven't run into a &lt;b&gt;FWK005&lt;/b&gt; error since.  Its unfortunate the parser itself is not thread-safe, but this solution will do for me for now.&lt;/p&gt;&lt;p&gt;Hope this may help someone else who runs into a similar situation.  Thanks!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8298653667028599513?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8298653667028599513/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2012/01/xerces-parsing-and-dreaded-fwk005.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8298653667028599513'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8298653667028599513'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2012/01/xerces-parsing-and-dreaded-fwk005.html' title='Xerces Parsing and the Dreaded FWK005 Exception'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-277668766615622448</id><published>2011-12-06T19:21:00.000-08:00</published><updated>2011-12-06T19:23:39.399-08:00</updated><title type='text'>Maven JAXB2 Plugin and Configuring Package Names</title><content type='html'>&lt;p&gt;Several &lt;a href="http://maven.apache.org"&gt;Maven&lt;/a&gt; plugins exist for generating classes from a schema definition.  One I have yet to try is the &lt;a href="http://confluence.highsource.org/display/MJIIP/Maven+JAXB2+Plugin"&gt;Maven JAXB2 Plugin&lt;/a&gt;.  Below I'll show the minimum setup for using the plugin in your project, as well as a couple of methods for dealing with the package name assigned to the generated classes.&lt;/p&gt;&lt;p&gt;Given the simplistic schema below, placed in &lt;i&gt;src/main/resources&lt;/i&gt; of the project:&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:schema&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://example.org/message/1.0&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns:xsd=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xsd:string&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:complexType&amp;gt;&lt;/span&gt;    &lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can add the plugin definition to our POM:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.jvnet.jaxb2.maven2&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-jaxb2-plugin&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;0.8.0&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;generate&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The classes generated by the plugin can be found in &lt;i&gt;target/generated-sources/xjc&lt;/i&gt;.  Below is the single class, &lt;i&gt;Message.java&lt;/i&gt;, generated by the plugin (with the imports optimized and comments removed for brevity):&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; generated&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.xml.bind.annotation.*&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@XmlAccessorType&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;XmlAccessType&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;FIELD&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@XmlType&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; propOrder &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;text&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;})&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@XmlRootElement&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Message&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;required &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;protected&lt;/span&gt; String text&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; String &lt;span style="color: #0000FF"&gt;getText&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; text&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;setText&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String value&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; value&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first thing that stands out to me is the package name of &lt;b&gt;generated&lt;/b&gt;.  This might be ok if I wanted to use the generated classes within a single project, but if I wanted to distribute them, a more useful package name might be helpful.&lt;/p&gt;&lt;p&gt;The &lt;a href="http://confluence.highsource.org/display/MJIIP/User+Guide"&gt;user guide&lt;/a&gt; for the plugin describes a list of configuration options.  The one we're looking for might just be &lt;b&gt;generatePackage&lt;/b&gt;, which we can add:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.jvnet.jaxb2.maven2&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-jaxb2-plugin&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;0.8.0&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- ... --&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;generatePackage&amp;gt;&lt;/span&gt;org.example.message&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/generatePackage&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The generated class can now be found in &lt;i&gt;target/generated-sources/xjc/org/example/messsage/Message.java&lt;/i&gt;, with the new package name:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; org&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;example&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;message&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Message&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: #408080; font-style: italic"&gt;// ...&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There are a couple more options for setting the package name, one is to add an annotation to the schema:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:schema&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://example.org/message&amp;quot;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #7D9029"&gt;xmlns:xsd=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt; &lt;br /&gt;      &lt;span style="color: #7D9029"&gt;xmlns:xjc=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://java.sun.com/xml/ns/jaxb/xjc&amp;quot;&lt;/span&gt; &lt;br /&gt;      &lt;span style="color: #7D9029"&gt;xmlns:jaxb=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://java.sun.com/xml/ns/jaxb&amp;quot;&lt;/span&gt; &lt;br /&gt;      &lt;span style="color: #7D9029"&gt;jaxb:version=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;2.0&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:annotation&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:appinfo&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;jaxb:schemaBindings&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;jaxb:package&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;org.example.test&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/jaxb:schemaBindings&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:appinfo&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:annotation&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;message&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- ... --&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you find annotating a schema to for something like &lt;b&gt;JAXB&lt;/b&gt; impurifies the schema, you could influence to derive a namespace by providing a &lt;b&gt;targetNamespace&lt;/b&gt; attribute to the root element of the schema:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;xsd:schema&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://example.org/message&amp;quot;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #7D9029"&gt;targetNamespace=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://example.org/message&amp;quot;&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;...&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- ... --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/xsd:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you have any other suggestions or feel like weighing in on the use of &lt;b&gt;xjc annotations&lt;/b&gt; in schema definitions, please feel free to add a comment below.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-277668766615622448?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/277668766615622448/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/12/maven-jaxb2-plugin-and-configuring.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/277668766615622448'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/277668766615622448'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/12/maven-jaxb2-plugin-and-configuring.html' title='Maven JAXB2 Plugin and Configuring Package Names'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7023710226839134766</id><published>2011-11-15T08:11:00.000-08:00</published><updated>2011-11-15T08:13:25.658-08:00</updated><title type='text'>Logging Thread Names with Log4j</title><content type='html'>&lt;p&gt;In some applications that start multiple threads to handle some concurrent processing, tracking the activity for a particular thread can be tricky.  Take the following log file snippet where a group of JMS messages are sent off all around the same time:&lt;/p&gt;&lt;pre&gt;&lt;br /&gt;2011-11-15 10:08:25,460 INFO  [JmsMessageSender:?] Sending message to queue: UPDATE.REQ.QUEUE&lt;br /&gt;2011-11-15 10:08:25,460 INFO  [JMSMessageReceiver:47] Using JMS Selector JMSCorrelationID='d6d3278b-7f7d-47f5-9d1f-a67e4100d800'&lt;br /&gt;2011-11-15 10:08:25,461 INFO  [JmsMessageSender:?] Sending message to queue: UPDATE.REQ.QUEUE&lt;br /&gt;2011-11-15 10:08:25,489 INFO  [JMSMessageSender:60] Using replyToQueue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:08:25,486 INFO  [JMSMessageReceiver:47] Using JMS Selector JMSCorrelationID='10287b5f-c061-4bba-8d9d-f92dbf6fb239'&lt;br /&gt;2011-11-15 10:08:25,489 INFO  [JMSMessageSender:60] Using replyToQueue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:08:25,505 INFO  [JMSMessageReceiver:47] Using JMS Selector JMSCorrelationID='b7892699-8f26-4260-ae36-62fa8b09c9c3'&lt;br /&gt;2011-11-15 10:08:25,517 INFO  [JmsMessageSender:?] Received message from queue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:08:25,564 INFO  [JmsMessageSender:?] Received message from queue UPDATE.RESP.QUEUE&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;With the case above, where many messages are sent and the responses will be eventually dealt with for further processing (which will also be logged), we could filter the log &lt;br /&gt;Message on the thread to help track where something may have gone wrong.&lt;/p&gt;&lt;p&gt;The &lt;a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html"&gt;logging patterns&lt;/a&gt; for &lt;a href="http://logging.apache.org/log4j/1.2/"&gt;log4j&lt;/a&gt; allow for logging the name of the thread with &lt;i&gt;%t&lt;/i&gt;.  Here is the pattern used to output the messages above:&lt;/p&gt;&lt;pre&gt;log4j.appender.outFile.layout.ConversionPattern=%d{ISO8601} %-5p [%c{1}:%L] %m%n&lt;/pre&gt;&lt;p&gt;We can add the &lt;i&gt;%t&lt;/i&gt; next to the class (&lt;i&gt;%c&lt;/i&gt;):&lt;/p&gt;&lt;pre&gt;log4j.appender.outFile.layout.ConversionPattern=%d{ISO8601} %-5p [%t:%c{1}:%L] %m%n&lt;/pre&gt;&lt;p&gt;The next run produces similar logging statements with the thread name.  As this is a &lt;a href="http://static.springsource.org/spring-batch/"&gt;Spring Batch&lt;/a&gt; application, the threads are named after the class handling their creation, &lt;b&gt;SimpleAsyncTaskExecutor&lt;/b&gt;, with a number appened on as in: &lt;i&gt;SimpleAsyncTaskExecutor-5&lt;/i&gt;&lt;/p&gt;&lt;pre&gt;2011-11-15 10:57:51,643 INFO  [SimpleAsyncTaskExecutor-5:JMSMessageSender:47] Sending to destination queue UPDATE.REQ.QUEUE&lt;br /&gt;2011-11-15 10:57:51,659 INFO  [SimpleAsyncTaskExecutor-5:JMSMessageSender:60] Using replyToQueue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:57:51,665 INFO  [SimpleAsyncTaskExecutor-4:JmsMessageSender:?] Sending message to queue: UPDATE.REQ.QUEUE&lt;br /&gt;2011-11-15 10:57:51,665 INFO  [SimpleAsyncTaskExecutor-4:JMSMessageSender:47] Sending to destination queue UPDATE.REQ.QUEUE&lt;br /&gt;2011-11-15 10:57:51,666 INFO  [SimpleAsyncTaskExecutor-5:JMSMessageReceiver:47] Using JMS Selector JMSCorrelationID='846cc984-84fd-47a5-bfd3-c293da829927'&lt;br /&gt;2011-11-15 10:57:51,687 INFO  [SimpleAsyncTaskExecutor-4:JMSMessageSender:60] Using replyToQueue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:57:51,695 INFO  [SimpleAsyncTaskExecutor-4:JMSMessageReceiver:47] Using JMS Selector JMSCorrelationID='d44087e2-bd19-474a-a8d1-71ac0fc618a7'&lt;br /&gt;2011-11-15 10:57:51,827 INFO  [SimpleAsyncTaskExecutor-5:JmsMessageSender:?] Received message from queue UPDATE.RESP.QUEUE&lt;br /&gt;2011-11-15 10:57:51,831 INFO  [SimpleAsyncTaskExecutor-4:JmsMessageSender:?] Received message from queue UPDATE.RESP.QUEUE&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;Here we can now map request and responses to particular threads if we were to encounter a error (that's logged of course) later in processing.&lt;/p&gt;&lt;p&gt;I'm sure something similar can be done with the newer logging frameworks as well.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7023710226839134766?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7023710226839134766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/11/logging-thread-names-with-log4j.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7023710226839134766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7023710226839134766'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/11/logging-thread-names-with-log4j.html' title='Logging Thread Names with Log4j'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8699862626083381866</id><published>2011-11-13T12:47:00.000-08:00</published><updated>2011-11-13T12:48:39.107-08:00</updated><title type='text'>Groovy: A Look at Mixins</title><content type='html'>&lt;p&gt;&lt;a href="http://groovy.codehaus.org/"&gt;Groovy's&lt;/a&gt; XML support is often cited as one of the bigger attractions of the language.  I often find myself writing classes that parse XML messages of the form:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;MyXmlParser&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def parsedXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; parsed&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            doSomethingWith&lt;span style="color: #666666"&gt;(&lt;/span&gt;parsedXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parsed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;XmlSlurper&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;parseText&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// ...&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One of the things that bothers me about the above pattern is the duplication in the &lt;b&gt;parsed&lt;/b&gt; method that might spread throughout a group of classes.  Using an inheritance scheme here might jump out as a possible solution at first, but that doesn't sit so well with me, as although the classes that use the duplicated method all parse XML, they all may not serve a similar substitutable purpose (one may send parsed results to a database, while another transforms the XML into an object, for example).&lt;/p&gt;&lt;p&gt;A newer feature of Groovy that I've been meaning to look into is the &lt;a href="http://groovy.codehaus.org/Category+and+Mixin+transformations"&gt;Mixin Transformation&lt;/a&gt;.  Here, I could mixin the &lt;b&gt;parsed&lt;/b&gt; method to all my classes that need to parse some XML.  I started with the example linked to the left, but ran into a couple problems doing joint-compilation with &lt;a href="http://maven.apache.org/"&gt;Maven&lt;/a&gt;.  Before we take a look at that, I'll demonstrate the mixin approach I used for this case.&lt;/p&gt;&lt;p&gt;The first step to adding mixins is to ensure the class you want to have make use of the mixin implement a common interface:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;interface&lt;/span&gt; XmlParser &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String xml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;MyXmlParser&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;implements&lt;/span&gt; XmlParser &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def parsedXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; parsed&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            doSomethingWith&lt;span style="color: #666666"&gt;(&lt;/span&gt;parsedXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// ...&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The next step was to define the mixin class using the &lt;a href="http://groovy.codehaus.org/gapi/groovy/lang/Category.html"&gt;@Category&lt;/a&gt; annotation:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Category&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;XmlParser&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;XmlParsing&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parsed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;XmlSlurper&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;parseText&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Finally, we can apply the &lt;a href="http://groovy.codehaus.org/gapi/groovy/lang/Mixin.html"&gt;@Mixin&lt;/a&gt; annotation and remove the &lt;b&gt;parsed&lt;/b&gt; method for our parser:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Mixin&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;XmlParsing&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;MyXmlParser&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;implements&lt;/span&gt; XmlParser &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def parsedXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; parsed&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            doSomethingWith&lt;span style="color: #666666"&gt;(&lt;/span&gt;parsedXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// ... method parsed(String response) removed ...&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;b&gt;MyXmlParser&lt;/b&gt; class now has the &lt;b&gt;parsed&lt;/b&gt; method mixed in and no longer needs to define it.  Is this a worthwile use of a mixin however, i.e. is the overhead worth having the duplication removed?  In a more concrete situation, we might have many more duplicated methods between a group of classes that we would could add to the mixin, improving its worth.&lt;/p&gt;&lt;p&gt;Now back to the compilation problems I referred to earlier.  When I went to compile, the &lt;a href="http://docs.codehaus.org/display/GMAVEN/Home"&gt;GMaven&lt;/a&gt; plugin created the stubs required for joint compilation, and the &lt;a href="http://maven.apache.org/plugins/maven-compiler-plugin/"&gt;Maven Compiler Plugin&lt;/a&gt; stumbled:&lt;/p&gt;&lt;pre&gt;&lt;br /&gt;    [ERROR] /home/prystasj/.../target/generated-sources/groovy-stubs/main/prystasj/groovy/xml/MyXmlParser.java:[..] cannot find symbol&lt;br /&gt;    [ERROR] symbol: variable XmlParsing&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;Everything in the stub looked legitimate, so I was rather stumped.  I remembered running into a &lt;a href="http://prystash.blogspot.com/2011/10/groovy-18-playing-nice-with-jaxb.html"&gt;similar problem&lt;/a&gt; before, so I went ahead and tried fully qualifying the references inside the annotations:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Category&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;XmlParser&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;XmlParsing&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parsed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;XmlSlurper&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;parseText&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Mixin&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;groovy&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;XmlParsing&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;MyXmlParser&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;implements&lt;/span&gt; XmlParser &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;parse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def parsedXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; parsed&lt;span style="color: #666666"&gt;(&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            doSomethingWith&lt;span style="color: #666666"&gt;(&lt;/span&gt;parsedXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// ... method parsed(String response) removed ...&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This resolved the issue and the project compile happily.  If I'm not missing something, maybe this might be something worthwile to bring up with the &lt;b&gt;GMaven&lt;/b&gt; developers?  Thoughts?  Thanks for reading.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8699862626083381866?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8699862626083381866/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/11/groovy-look-at-mixins.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8699862626083381866'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8699862626083381866'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/11/groovy-look-at-mixins.html' title='Groovy: A Look at Mixins'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1315170596954412313</id><published>2011-11-13T10:42:00.000-08:00</published><updated>2011-11-13T10:45:12.913-08:00</updated><title type='text'>Spring Batch: Deadlock Inserting Job Instances</title><content type='html'>&lt;p&gt;I have been working on my first &lt;a href="http://static.springsource.org/spring-batch/"&gt;Spring Batch&lt;/a&gt; project and so far I'm a big fan.  I think I'm pretty far along and the only real issue I've had is random occurrences of deadlock when the app tries to create instances of the same job simultaneously in the job database (in table &lt;b&gt;BATCH_JOB_INSTANCE&lt;/b&gt;):&lt;/p&gt;&lt;pre&gt;  2011-11-13 12:48:18,808 ERROR [AbstractStep:212] Encountered an error executing the step&lt;br /&gt;  org.springframework.dao.DeadlockLoserDataAccessException: PreparedStatementCallback;&lt;br /&gt;  SQL [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]; &lt;br /&gt;  Deadlock found when trying to get lock; try restarting transaction; &lt;br /&gt;  nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:&lt;br /&gt;  Deadlock found when trying to get lock; try restarting transaction&lt;br /&gt;        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:265)&lt;br /&gt;        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)&lt;br /&gt;        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)&lt;/pre&gt;&lt;p&gt;I'm using &lt;a href="http://www.mysql.com/"&gt;MySQL&lt;/a&gt; as the database backing the batch app.  The &lt;a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.html"&gt;Job Repository&lt;/a&gt;, I'm using seemed pretty stock and I never had given it much thought:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;     &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;bean&lt;/span&gt; &lt;span style="color: #7D9029"&gt;id=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;jobRepository&amp;quot;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: #7D9029"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;org.springframework.batch.core.repository.support.JobRepositoryFactoryBean&amp;quot;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:dataSource-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;dataSource&amp;quot;&lt;/span&gt; &lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:transactionManager-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;transactionManager&amp;quot;&lt;/span&gt; &lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:lobHandler-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;lobHandler&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One of the properties on the repository that can be set is &lt;a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.html#setIsolationLevelForCreate%28java.lang.String%29"&gt;isolationLevelForCache&lt;/a&gt;.  I found that setting this value to &lt;b&gt;ISOLATION_READ_UNCOMMITTED&lt;/b&gt; helped my deadlock issue.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;     &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;bean&lt;/span&gt; &lt;span style="color: #7D9029"&gt;id=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;jobRepository&amp;quot;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: #7D9029"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;org.springframework.batch.core.repository.support.JobRepositoryFactoryBean&amp;quot;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:dataSource-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;dataSource&amp;quot;&lt;/span&gt; &lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:transactionManager-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;transactionManager&amp;quot;&lt;/span&gt; &lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:isolationLevelForCache=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;ISOLATION_READ_UNCOMMITTED&amp;quot;&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: #7D9029"&gt;p:lobHandler-ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;lobHandler&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Whether or not this is the solution to use come production time is yet to be seen. For now, I surmise this works because the app is starting enough jobs at the same time, that the framework is ready to proceed with one before the insert to the database table is formally committed.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1315170596954412313?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1315170596954412313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/11/spring-batch-deadlock-inserting-job.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1315170596954412313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1315170596954412313'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/11/spring-batch-deadlock-inserting-job.html' title='Spring Batch: Deadlock Inserting Job Instances'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-2033358497486938299</id><published>2011-11-09T06:19:00.000-08:00</published><updated>2011-11-09T06:20:39.102-08:00</updated><title type='text'>Maven Tomcat Plugin: Selecting an XML Parser</title><content type='html'>&lt;p&gt;The &lt;a href="http://tomcat.apache.org/maven-plugin.html"&gt;Tomcat Maven Plugin&lt;/a&gt; is a great tool for quickly working through the cycle of making and verifying changes in a webapp.  One such app I was working with included an XML parser from &lt;b&gt;Oracle&lt;/b&gt;:&lt;/p&gt;&lt;pre&gt;  oracle:oracle_xmlparserv2:jar:10.2.0.3:compile&lt;/pre&gt;&lt;p&gt;The inclusion of the parser created a problem as it did not take kindly to a document parsed during startup, causing an error:&lt;/p&gt;&lt;pre&gt;  $ mvn tomcat:run &lt;br /&gt;  ...&lt;br /&gt;  Caused by: oracle.xml.parser.schema.XSDException: Duplicated definition for: 'identifiedType'&lt;br /&gt;          at oracle.xml.parser.schema.XSDBuilder.buildSchema(XSDBuilder.java:731)&lt;br /&gt;          at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:435)&lt;br /&gt;          at oracle.xml.parser.schema.XSDValidator.processSchemaLocation(XSDValidator.java:879)&lt;br /&gt;          at oracle.xml.parser.schema.XSDValidator.startElement(XSDValidator.java:496)&lt;br /&gt;          at oracle.xml.parser.v2.NonValidatingParser.parseElement(NonValidatingParser.java:1224)&lt;br /&gt;          at oracle.xml.parser.v2.NonValidatingParser.parseRootElement(NonValidatingParser.java:311)&lt;br /&gt;          at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:278)&lt;br /&gt;         ... 52 more&lt;/pre&gt;&lt;p&gt;A colleague of mine at work pointed at that you can select another parser through system properties, so I wanted to share his solution here:&lt;/p&gt;&lt;pre&gt;  $ mvn tomcat:run \&lt;br /&gt;    -DXMLPARSER=XERCES \&lt;br /&gt;    -DXSLENGINE=XERCES \&lt;br /&gt;    -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl \&lt;br /&gt;    -Djavax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl \&lt;br /&gt;    -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl&lt;/pre&gt;&lt;p&gt;The above selects a &lt;b&gt;JAXP parser&lt;/b&gt; from &lt;b&gt;Apache&lt;/b&gt;, requiring &lt;b&gt;Xerces&lt;/b&gt; and &lt;b&gt;Xalan&lt;/b&gt;:&lt;pre&gt;  xerces:xercesImpl:${xercesImpl.version}&lt;br /&gt;  xalan:xalan:${xalan.version}&lt;/pre&gt;&lt;p&gt;To avoid having to script the above command or have to remember it, you can add the settings to the plugin definition:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.mojo&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;tomcat-maven-plugin&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;port&amp;gt;&lt;/span&gt;${tomcatPort}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/port&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;path&amp;gt;&lt;/span&gt;/${project.artifactId}-${project.version}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/path&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;systemProperties&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;env&amp;gt;&lt;/span&gt;${env}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/env&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;tomcatPort&amp;gt;&lt;/span&gt;${tomcatPort}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/tomcatPort&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;XMLPARSER&amp;gt;&lt;/span&gt;XERCES&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/XMLPARSER&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;XSLENGINE&amp;gt;&lt;/span&gt;XERCES&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/XSLENGINE&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;javax.xml.parsers.DocumentBuilderFactory&amp;gt;&lt;/span&gt;org.apache.xerces.jaxp.DocumentBuilderFactoryImpl&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/javax.xml.parsers.DocumentBuilderFactory&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;javax.xml.transform.TransformerFactory&amp;gt;&lt;/span&gt;org.apache.xalan.processor.TransformerFactoryImpl&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/javax.xml.transform.TransformerFactory&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;javax.xml.parsers.SAXParserFactory&amp;gt;&lt;/span&gt;org.apache.xerces.jaxp.SAXParserFactoryImpl&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/javax.xml.parsers.SAXParserFactory&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/systemProperties&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;xerces&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;xercesImpl&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;${xercesImpl.version}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;xalan&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;xalan&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;${xalan.version}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hope this helps anyone else who may run into a similar situation.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-2033358497486938299?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/2033358497486938299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/11/maven-tomcat-plugin-selecting-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/2033358497486938299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/2033358497486938299'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/11/maven-tomcat-plugin-selecting-xml.html' title='Maven Tomcat Plugin: Selecting an XML Parser'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1683999657732803269</id><published>2011-11-04T08:28:00.000-07:00</published><updated>2011-11-04T08:30:28.885-07:00</updated><title type='text'>Prompted for GNOME Keyring Password During a Subversion Commit</title><content type='html'>&lt;p&gt;Out of the blue when trying a &lt;a href="subversion.tigris.org"/&gt;Subversion&lt;/a&gt; commit, I was presented with a prompt for the password to the &lt;a href="http://en.wikipedia.org/wiki/GNOME_Keyring"&gt;GNOME Keyring&lt;/a&gt;:&lt;/p&gt;&lt;pre&gt;  $ svn ci -m"adding module"&lt;br /&gt;  Password for '(null)' GNOME keyring:&lt;br /&gt;  svn: Commit failed (details follow):&lt;br /&gt;  svn: MKACTIVITY of '/svn/projectr/!svn/act/c5d9f7fb-76ee-4900-9a0b-b55928d418f0': authorization failed:&lt;br /&gt;  Could not authenticate to server: rejected Basic challenge (http://svn.myorginazation.org)&lt;/pre&gt;&lt;p&gt;In my case, the password for my login took care of the problem.  While I do not understand the reasons for the prompt, the same may work for someone else, as by default the login password is what is being asked for.&lt;/p&gt;&lt;p&gt;If anyone understands why the prompt may have occurred (I haven't done much research yet), please leave a comment.  Thanks!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1683999657732803269?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1683999657732803269/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/11/prompted-for-gnome-keyring-password.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1683999657732803269'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1683999657732803269'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/11/prompted-for-gnome-keyring-password.html' title='Prompted for GNOME Keyring Password During a Subversion Commit'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-6706429972239949038</id><published>2011-10-29T14:35:00.000-07:00</published><updated>2011-10-29T14:37:07.565-07:00</updated><title type='text'>Using Guava's LineReader in a Spring Batch Job</title><content type='html'>&lt;p&gt;Reading data from source like flat files and databases are common theme in batch projects.  &lt;a href="http://static.springsource.org/spring-batch/"&gt;Spring Batch&lt;/a&gt; provides a lot of support for reading from these data sources out of the box.  The &lt;a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/item/file/FlatFileItemReader.html"&gt;FlatFileItemReader&amp;lt;T&amp;gt;&lt;/a&gt; provides a great example.  This class extends the abstract &lt;a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/item/support/AbstractItemCountingItemStreamItemReader.html"&gt;AbstractItemCountingItemStreamItemReader&amp;lt;T&amp;gt;&lt;/a&gt;, which for one reason or another, I wanted to implement myself for reading a flat file.  Extensions of this inteface must implement three methods:&lt;ol&gt;&lt;li&gt;doClose()&lt;/li&gt;&lt;li&gt;doOpen()&lt;/li&gt;&lt;li&gt;doRead()&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;Since I was implementing the interface myself, I had to come up with a way to actually read the contents of the file myself.  In the &lt;a href="http://code.google.com/p/guava-libraries/"&gt;Google Guava&lt;/a&gt; project, I found a class to use, &lt;a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/LineReader.html"&gt;LineReader&lt;/a&gt;, that seemed like a good candidate to help alleviate the need for the usual tedious boilerplate code required for reading the contents of the file.&lt;/p&gt;&lt;p&gt;Before describing the use of the LineReader in a Spring Batch project, I wanted to demonstrate an example of using the &lt;b&gt;LineReader&lt;/b&gt; outside of the context of a Spring Batch project.  The &lt;a href="http://maven.apache.org"&gt;Maven&lt;/a&gt; coordinates for the current version of Guava are:&lt;ul&gt;&lt;li&gt;groupId: com.google.guava&lt;/li&gt;&lt;li&gt;artifactId: guava&lt;/li&gt;&lt;li&gt;version: 10.0.1&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;Below is a sample class that follows the same pattern that must be implemenented when extending the &lt;b&gt;AbstractItemCountingItemStreamItemReader&lt;/b&gt; class using a &lt;b&gt;LineReader&lt;/b&gt;.  The class takes the name of a resource to read from and handles making the resource available to the LineReader in the &lt;b&gt;doOpen()&lt;/b&gt; method.  Reading from the resource and closing of the resource are handled by the &lt;b&gt;doRead()&lt;/b&gt; and &lt;b&gt;doClose()&lt;/b&gt; methods respectively.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;sample&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;guava&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;com.google.common.io.LineReader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.InputStream&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.InputStreamReader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.Reader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;ResourceLineReader&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; InputStream inputStream&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; LineReader lineReader&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; String &lt;span style="color: #0000FF"&gt;doRead&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; lineReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readLine&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;doOpen&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String resourceName&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            inputStream &lt;span style="color: #666666"&gt;=&lt;/span&gt; getClass&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClassLoader&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getResourceAsStream&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;resourceName&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;            Reader reader &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; InputStreamReader&lt;span style="color: #666666"&gt;(&lt;/span&gt;inputStream&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;            lineReader &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; LineReader&lt;span style="color: #666666"&gt;(&lt;/span&gt;reader&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;doClose&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            inputStream&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;close&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To test the above class, we'll use a file, &lt;i&gt;tests.txt&lt;/i&gt; that contains three lines:&lt;br /&gt;&lt;pre&gt;  line1&lt;br /&gt;  line2&lt;br /&gt;  line3&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;As the &lt;b&gt;doRead()&lt;/b&gt; method, by way of &lt;b&gt;LineReader.readLine()&lt;/b&gt;, will return &lt;i&gt;null&lt;/i&gt; when the data from the input resource has been exhausted, we can use that fact to know when to call the &lt;b&gt;doClose()&lt;/b&gt; method.   At the end of our test we have a check to verify that three lines were read from the file:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;sample&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;guava&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.junit.Before&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.junit.Test&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.util.ArrayList&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.util.List&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;static&lt;/span&gt; org&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;junit&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;Assert&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;assertEquals&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;static&lt;/span&gt; org&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;junit&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;Assert&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;fail&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;ResourceLineReaderTest&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        ResourceLineReader resourceLineReader&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Before&lt;/span&gt; &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;setup&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            resourceLineReader &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; ResourceLineReader&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Test&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;reads_lines_from_a_classpath_resource&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            List&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;String&lt;span style="color: #666666"&gt;&amp;gt;&lt;/span&gt; itemsRead &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; ArrayList&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;String&lt;span style="color: #666666"&gt;&amp;gt;();&lt;/span&gt;&lt;br /&gt;            String itemRead&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;try&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                resourceLineReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;doOpen&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;test.txt&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;while&lt;/span&gt;&lt;span style="color: #666666"&gt;((&lt;/span&gt;itemRead &lt;span style="color: #666666"&gt;=&lt;/span&gt; resourceLineReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;doRead&lt;/span&gt;&lt;span style="color: #666666"&gt;())&lt;/span&gt; &lt;span style="color: #666666"&gt;!=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    itemsRead&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;itemRead&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;                resourceLineReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;doClose&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;catch&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Exception e&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                fail&lt;span style="color: #666666"&gt;(&lt;/span&gt;e&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;());&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            assertEquals&lt;span style="color: #666666"&gt;(3,&lt;/span&gt; itemsRead&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;size&lt;/span&gt;&lt;span style="color: #666666"&gt;());&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can use the same implementation pattern when extending the the &lt;b&gt;AbstractItemCountingItemStreamItemReader&amp;lt;T&amp;gt;&lt;/b&gt; abstract class as mentioned earlier in this post:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;sample&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;batch&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;com.google.common.io.LineReader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.springframework.batch.item.file.ResourceAwareItemReaderItemStream&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.springframework.core.io.Resource&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.springframework.util.ClassUtils&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.InputStream&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.InputStreamReader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;java.io.Reader&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;BatchFileReader&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; AbstractItemCountingItemStreamItemReader&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;String&lt;span style="color: #666666"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;implements&lt;/span&gt; ResourceAwareItemReaderItemStream&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;String&lt;span style="color: #666666"&gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; InputStream inputStream&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; LineReader lineReader&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; Resource fileResource&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;/** Fulfills execution context requirement that {@code name} is set for the keys prefix. */&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0000FF"&gt;BibNumbersFileReader&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            setName&lt;span style="color: #666666"&gt;(&lt;/span&gt;ClassUtils&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getShortName&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;BibNumbersFileReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;class&lt;/span&gt;&lt;span style="color: #666666"&gt;));&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Override&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;protected&lt;/span&gt; String &lt;span style="color: #0000FF"&gt;doRead&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; lineReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readLine&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Override&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;protected&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;doOpen&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            inputStream &lt;span style="color: #666666"&gt;=&lt;/span&gt; fileResource&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getInputStream&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;            Reader reader &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; InputStreamReader&lt;span style="color: #666666"&gt;(&lt;/span&gt;inputStream&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;            lineReader &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; LineReader&lt;span style="color: #666666"&gt;(&lt;/span&gt;reader&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Override&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;protected&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;doClose&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; Exception &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            inputStream&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;close&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;            inputStream &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@Override&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;setResource&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Resource resource&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;fileResource&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; resource&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above reader can be configured with the below definition to be referenced from within a batch job:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;bean&lt;/span&gt; &lt;span style="color: #7D9029"&gt;id=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;bibNumbersFileReader&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;prystasj.sample.batch.BatchFileReader&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;scope=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;step&amp;quot;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #7D9029"&gt;p:resource=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;file:#{jobParameters[&amp;#39;file&amp;#39;]}&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the above configuration, the scope of &lt;i&gt;step&lt;/i&gt; ensures the bean is reconfigured every time the reader is used within a job.  &lt;a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/expressions.html"&gt;Spring Expression Language&lt;/a&gt; is used to inject the the name of the file resource into the reader from the job's parameters.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-6706429972239949038?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/6706429972239949038/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/10/using-guavas-linereader-in-spring-batch.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6706429972239949038'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6706429972239949038'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/10/using-guavas-linereader-in-spring-batch.html' title='Using Guava&apos;s LineReader in a Spring Batch Job'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-3455228218277568083</id><published>2011-10-21T07:40:00.000-07:00</published><updated>2011-10-28T12:43:07.009-07:00</updated><title type='text'>Groovy 1.8 Playing Nice with JAXB</title><content type='html'>&lt;p&gt;Getting &lt;b&gt;Groovy&lt;/b&gt; and &lt;b&gt;JAXB&lt;/b&gt; to play nice together has been previously discussed many times.  A good solution to the problem is presented here: &lt;a href="http://stackoverflow.com/questions/1161147/how-do-i-get-groovy-and-jaxb-to-play-nice-together"&gt;Stack Overflow&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;However, I started running into some problems when I upgraded to Groovy 1.8. Take the following class definition:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.xml.bind.annotation.*&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlRootElement&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlAccessorType&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;XmlAccessType&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;NONE&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;JobResult&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String status&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String exitDescription&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;During compliation, I received the following error:&lt;/p&gt;&lt;pre&gt;    symbol: variable XmlAccessType&lt;br /&gt;    @javax.xml.bind.annotation.XmlRootElement() @javax.xml.bind.annotation.XmlAccessorType(value=XmlAccessType.NONE) public class JobResult&lt;br /&gt;    /home/prystasj/workspace/project/target/generated-sources/groovy-stubs/main/org/oclc/controlledheading/admin/JobResult.java:[10,106] an enum annotation value must be an enum constant&lt;/pre&gt;&lt;p&gt;Confused, I first tried using a static import:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.xml.bind.annotation.*&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;static&lt;/span&gt; javax&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;bind&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;annotation&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;XmlAccessType&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;NONE&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlRootElement&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlAccessorType&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;NONE&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;JobResult&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String status&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String exitDescription&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Giving me a different error:&lt;/p&gt;&lt;pre&gt;    [ERROR] /home/prystasj/project/target/generated-sources/groovy-stubs/main/prystasj/JobResult.java:[10,93] cannot find symbol&lt;br /&gt;    symbol: variable NONE&lt;br /&gt;    @javax.xml.bind.annotation.XmlRootElement() @javax.xml.bind.annotation.XmlAccessorType(value=NONE) public class JobResult&lt;/pre&gt;&lt;p&gt;A solution I used was to fully qualify the path to field inside the &lt;b&gt;XmlAccessorType&lt;/b&gt; annotation:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.xml.bind.annotation.*&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlRootElement&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@XmlAccessorType&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;javax&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xml&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;bind&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;annotation&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;XmlAccessType&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;NONE&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;JobResult&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String status&lt;br /&gt;        &lt;span style="color: #AA22FF"&gt;@XmlElement&lt;/span&gt; String exitDescription&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-3455228218277568083?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/3455228218277568083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/10/groovy-18-playing-nice-with-jaxb.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3455228218277568083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3455228218277568083'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/10/groovy-18-playing-nice-with-jaxb.html' title='Groovy 1.8 Playing Nice with JAXB'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-3978238443865563991</id><published>2011-09-26T01:50:00.000-07:00</published><updated>2011-09-26T01:53:29.443-07:00</updated><title type='text'>Groovy: Reuse of Helper Test Methods in a Spec</title><content type='html'>&lt;p&gt;There are a lot of message transformers in a lot of the projects I work that work primarily with XML.  I want my tests to take a source XML document and verify the output transformation.  The example input and output messages live on the classpath, say in directory &lt;i&gt;src/test/resources&lt;/i&gt;, and their contents are accessed through the classloader.&lt;/p&gt;&lt;p&gt;Take this example spec written for the &lt;a href="http://code.google.com/p/spock/"&gt;Spock framework&lt;/a&gt; for &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt;:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;XmlTransformerSpec&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; Specification &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        XmlTransformer transformer &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; XmlTransformer&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #6666FF"&gt;&amp;quot;transforms xml messages for some business goal&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #A0A000"&gt;given:&lt;/span&gt;&lt;br /&gt;                def inputXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; textOf&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #6666FF"&gt;&amp;quot;input.xml&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                def expectedOutputXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; textOf&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #6666FF"&gt;&amp;quot;expected-output.xml&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #A0A000"&gt;when:&lt;/span&gt;&lt;br /&gt;                def actualOutputXml &lt;span style="color: #666666"&gt;=&lt;/span&gt; transformer&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;transform&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;inputXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #A0A000"&gt;then:&lt;/span&gt;&lt;br /&gt;                areIdentical&lt;span style="color: #666666"&gt;(&lt;/span&gt;expectedOutputXml&lt;span style="color: #666666"&gt;,&lt;/span&gt; actualOutputXml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;areIdentical&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String expected&lt;span style="color: #666666"&gt;,&lt;/span&gt; String actual&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            XMLUnit&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;setIgnoreWhitespace&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;Diff&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;expected&lt;span style="color: #666666"&gt;,&lt;/span&gt; actual&lt;span style="color: #666666"&gt;).&lt;/span&gt;&lt;span style="color: #7D9029"&gt;identical&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;textOf&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;resource&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            getClass&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClassLoader&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getResourceAsStream&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;resource&lt;span style="color: #666666"&gt;).&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The retrieval of the messages in the form of resources is contained within the &lt;b&gt;textOf()&lt;/b&gt; method where it can be resued by additional tests within the spec.  What if I wanted to reuse it other test classes?  One option I found was to move the method to an abstract class that extends &lt;b&gt;spock.lang.Specification&lt;/b&gt;:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;abstract&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;ResourceAwareSpecification&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; Specification &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;textOf&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;resource&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            getClass&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClassLoader&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getResourceAsStream&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;resource&lt;span style="color: #666666"&gt;).&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now my test spec, along with any others that need to retrieve the contents of classpath resources, can extend the new specification:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;XmlTransformerSpec&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; ResourceAwareSpecification &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// method textOf() has been removed...&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I wonder if others find themselves loading resources in this way and may make use of an approach like this if it were builtin to a framework like Spock in this or some other form, like a mixin. The helper method comparing the XML using &lt;b&gt;XMLUnit&lt;/b&gt; would be another candidate in my mind for such an addition.  Thoughts?&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-3978238443865563991?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/3978238443865563991/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/groovy-reuse-of-helper-test-methods-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3978238443865563991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3978238443865563991'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/groovy-reuse-of-helper-test-methods-in.html' title='Groovy: Reuse of Helper Test Methods in a Spec'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-341526900159573322</id><published>2011-09-20T16:46:00.000-07:00</published><updated>2011-09-21T06:35:41.992-07:00</updated><title type='text'>Openbox: Defining a Keyboard Shortcut</title><content type='html'>&lt;p&gt;The &lt;a href="http://openbox.org/"&gt;Openbox Window Manager&lt;/a&gt; has quickly become my favorite way to use my computer.  It use it almost exclusively on all the machines that I use (&lt;a href="http://www.ubuntu.org"&gt;Ubuntu&lt;/a&gt; at work, &lt;a href="http://linuxmint.com"&gt;Mint&lt;/a&gt; at home).&lt;/p&gt;&lt;p&gt;Here I'll describe how to add a keyboard shortcut so that we do not have to reach for the mouse to do something common.  As I spend most of my time in the terminal, one shortcut I make the most use of is &lt;i&gt;Alt+T&lt;/i&gt; to open a terminal program, &lt;a href="http://materm.sourceforge.net/wiki/pmwiki.php"&gt;mrxvt&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The keyboard shortcuts, along with many other things, are defined in &lt;i&gt;&lt;b&gt;~/.config/openbox/rc.xml&lt;/b&gt;&lt;/i&gt;.  The &lt;i&gt;keyboard&lt;/i&gt; element is a child element of the root &lt;i&gt;openbox&lt;/i&gt; element.  Below is a snippet of an example showing the default shortcut for switching to the desktop to the left.  The shortcut &lt;i&gt;C-A-Left&lt;/i&gt; describes pressing the &lt;i&gt;Ctrl&lt;/i&gt;, &lt;i&gt;Alt&lt;/i&gt;, and left arrow keys with the default mappings:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;openbox_config&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://openbox.org/3.4/rc&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;keyboard&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;chainQuitKey&amp;gt;&lt;/span&gt;C-g&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/chainQuitKey&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- Keybindings for desktop switching --&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;keybind&lt;/span&gt; &lt;span style="color: #7D9029"&gt;key=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;C-A-Left&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;action&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;DesktopLeft&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;dialog&amp;gt;&lt;/span&gt;no&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/dialog&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;wrap&amp;gt;&lt;/span&gt;no&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/wrap&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/keybind&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some keyboard config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/keyboard&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/openbox&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To add our terminal shortcut, we add a &lt;i&gt;keybind&lt;/i&gt; entry with the &lt;i&gt;Execute&lt;/i&gt; action, followed by the command to execute.  The &lt;i&gt;key&lt;/i&gt; of &lt;i&gt;A-t&lt;/i&gt; corresponding to pressing the &lt;i&gt;Alt&lt;/i&gt; key along with the &lt;i&gt;t&lt;/i&gt; key:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;openbox_config&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://openbox.org/3.4/rc&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;keyboard&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;chainQuitKey&amp;gt;&lt;/span&gt;C-g&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/chainQuitKey&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- my keybindings --&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;keybind&lt;/span&gt; &lt;span style="color: #7D9029"&gt;key=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;A-t&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;action&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Execute&amp;quot;&lt;/span&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;execute&amp;gt;&lt;/span&gt;mrxvt&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/execute&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/action&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/keybind&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some keyboard config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/keyboard&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- some config omitted...  --&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #3333FF; font-weight: bold"&gt;&amp;lt;/openbox&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can reload the new configuration from the command line with:&lt;br /&gt;&lt;pre&gt;  $ openbox --reconfigure&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Or by right-clicking on the desktop and selecting &lt;i&gt;Reconfigure&lt;/i&gt; from the Openbox menu.&lt;/p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-qevyl833g38/TnkmpmN1cQI/AAAAAAAAABk/bWsSmLt4e_w/s1600/openbox-menu.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 225px;" src="http://3.bp.blogspot.com/-qevyl833g38/TnkmpmN1cQI/AAAAAAAAABk/bWsSmLt4e_w/s400/openbox-menu.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5654593303215436034" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;hr&gt;&lt;p&gt;&lt;i&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;/i&gt; In order to take the screenshot with the menu shown, I ran &lt;i&gt;gnome-screenshot&lt;/i&gt; with a delay of 5 seconds:&lt;/p&gt;&lt;pre&gt;  $ gnome-screenshot -d 5&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;During the delay, you can right click on the desktop to get the menu in the picture.&lt;/p&gt;&lt;hr&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-341526900159573322?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/341526900159573322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/openbox-defining-keyboard-shortcut.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/341526900159573322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/341526900159573322'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/openbox-defining-keyboard-shortcut.html' title='Openbox: Defining a Keyboard Shortcut'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-qevyl833g38/TnkmpmN1cQI/AAAAAAAAABk/bWsSmLt4e_w/s72-c/openbox-menu.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7101331539414253300</id><published>2011-09-20T08:09:00.001-07:00</published><updated>2011-09-20T08:09:51.691-07:00</updated><title type='text'>Installing Clojure 1.2 on Ubuntu</title><content type='html'>&lt;p&gt;If you're looking to install the latest stable version of &lt;a href="http://clojure.org"&gt;Clojure&lt;/a&gt; on &lt;a href="http://www.ubuntu.org"&gt;Ubuntu&lt;/a&gt; or a derivative, below are a few options.  Currently, I'm running &lt;b&gt;Ubuntu 10.10&lt;/b&gt;, whose package list contains &lt;b&gt;Clojure 1.1.0&lt;/b&gt;.  We can install this version using &lt;b&gt;apt-get&lt;/b&gt;:&lt;/p&gt;&lt;pre&gt;  $ sudo apt-get install clojure&lt;br /&gt;  ...&lt;br /&gt;  $ clojure&lt;br /&gt;  Clojure 1.1.0&lt;br /&gt;  user=&gt; (System/exit 0)&lt;/pre&gt;&lt;p&gt;The most recent stable release is &lt;b&gt;1.2.1&lt;/b&gt; (see &lt;a href="http://clojure.org/downloads"&gt;Clojure Releases&lt;/a&gt;). It looks like while there was a request to have the available package updated to &lt;b&gt;1.2&lt;/b&gt; (&lt;a href="https://bugs.launchpad.net/ubuntu/+source/clojure/+bug/731979"&gt;Ubuntu Bug #731979&lt;/a&gt;), but it looks like it won't be included by default until the &lt;b&gt;11.10&lt;/b&gt; release of Ubuntu.&lt;/p&gt;&lt;p&gt;If we want to use the most recent stable release, the bug report has a link to the package set to be included in &lt;b&gt;11.10&lt;/b&gt;, &lt;a href="http://packages.ubuntu.com/oneiric/clojure1.2"&gt;clojure1.2&lt;/a&gt;, which we can install with &lt;b&gt;dpkg&lt;/b&gt;:&lt;pre&gt;  $ sudo dpkg -i clojure1.2_1.2.1+dfsg-3_all.deb&lt;/pre&gt;&lt;p&gt;This links the &lt;b&gt;clojure.jar&lt;/b&gt; as:&lt;/p&gt;&lt;pre&gt;  $ ls -l /usr/share/java/clojure.jar&lt;br /&gt;  lrwxrwxrwx 1 root root 29 2011-09-20 10:39 /usr/share/java/clojure.jar -&gt; /etc/alternatives/clojure.jar&lt;/pre&gt;&lt;p&gt;Alternatively, we can download the source from the releases page and build it with &lt;b&gt;ant&lt;/b&gt; ourselves: &lt;a href="https://github.com/downloads/clojure/clojure/clojure-1.2.1.zip"&gt;Clojure 1.2.1 Source&lt;/a&gt;.&lt;pre&gt;  $ unzip clojure-1.2.1.zip&lt;br /&gt;  $ cd clojure-1.2.1&lt;br /&gt;  $ ant&lt;br /&gt;  ...&lt;br /&gt;  BUILD SUCCESSFUL&lt;br /&gt;  Total time: 1 minute 57 seconds&lt;/pre&gt;&lt;p&gt;If the build succeeds, we can just copy the built jar to the install location:&lt;/p&gt;&lt;pre&gt;  $ sudo cp clojure.jar /usr/share/java/clojure.jar&lt;br /&gt;  $ clojure&lt;br /&gt;  Clojure 1.2.1&lt;br /&gt;  user=&gt; (System/exit 0)&lt;/pre&gt;&lt;p&gt;This approach should also work if we want to run the most recent development versions as well.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7101331539414253300?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7101331539414253300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/installing-clojure-12-on-ubuntu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7101331539414253300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7101331539414253300'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/installing-clojure-12-on-ubuntu.html' title='Installing Clojure 1.2 on Ubuntu'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8633357755889777062</id><published>2011-09-15T17:55:00.000-07:00</published><updated>2011-09-15T18:03:42.820-07:00</updated><title type='text'>Fantom: Constructors, Required Fields, and More</title><content type='html'>&lt;p&gt;This post is part of a series of sorts where I write about what I find while exploring the &lt;a href="http://fantom.org"&gt;Fantom&lt;/a&gt; language.  This time I'm going to share what I find out about declaring and using classes as I go.  I imagine I will only scratch the surface.&lt;/p&gt;&lt;p&gt;To give you the chance to devote some of your valuable reading time elsewhere, below is a summary of some of the highlights covered below:&lt;/p&gt;&lt;p&gt;&lt;ul&gt;&lt;li&gt;Declaring class fields that are required at object creation and throughout the lifetime of the object&lt;/li&gt;&lt;li&gt;Default class field values&lt;/li&gt;&lt;li&gt;Automatic getters and setters&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;A class can be declared using the &lt;b&gt;class&lt;/b&gt; keyword as we're all probably used to.  Let's declare a (boring) class &lt;b&gt;Person&lt;/b&gt; with a &lt;b&gt;name&lt;/b&gt; field and add a &lt;b&gt;main&lt;/b&gt; method so we can try and create an instance.&lt;/p&gt;&lt;p&gt;Assignments in &lt;b&gt;Fantom&lt;/b&gt; are done via the use of &lt;b&gt;:=&lt;/b&gt; instead of an equals sign.  Here we'll start by simply creating an empty object and printing out to the console that we've succeeded:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #005B9A; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        Str name&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #005B9A"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            Person p &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person created.&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The variable &lt;b&gt;p&lt;/b&gt; is given what looks to be the result of a call to a default constructor.  We can run the above with:&lt;/p&gt;&lt;pre&gt;   $ fan Person.fan&lt;/pre&gt;&lt;p&gt;Our first run gives us the following error:&lt;/p&gt;&lt;pre&gt;   Non-nullable field 'name' must be assigned in constructor 'make'&lt;/pre&gt;&lt;p&gt;The error hints that the call to &lt;b&gt;Person()&lt;/b&gt; results in a call to a constructor named &lt;b&gt;make&lt;/b&gt;.  Also, the error descrption seems to go against what we may be used to in &lt;b&gt;Java&lt;/b&gt;, where class properties can be &lt;b&gt;null&lt;/b&gt;.  Right of the bat to me, this could demonstrate a potential strength of Fantom as, as given the class definition above, a user cannot create an incomplete &lt;b&gt;Person&lt;/b&gt;.  If we want to create a Person without assigning a value to the name field, we can append a '?' to the field declaration:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #005B9A; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        Str&lt;span style="color: #666666"&gt;?&lt;/span&gt; name&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #005B9A"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            Person p &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person created.&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This time we'll get the "success" message indicating we've created a &lt;b&gt;Person&lt;/b&gt;.  Let's keep the name field required and assign a value to it when the Person is constructed.  To do this, we can add a constructor and in the main method create a Person named 'Cosmo':&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #005B9A; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        Str name&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #005B9A"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            Person p &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Cosmo&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person created with name: &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; p&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, the main method now also reports the name of the created &lt;b&gt;Person&lt;/b&gt; via a call to the &lt;b&gt;name&lt;/b&gt; method.  Although it looks like we are accessing the field directly, getters and setters are automatically created for each field which helps us cut down on the amount of boilerplate code our class might contain, not unlike some of the newer JVM langauges.  Running things through the interpreter again gives us:&lt;/p&gt;&lt;pre&gt;    Person created with name: Cosmo&lt;/pre&gt;&lt;p&gt;To demonstrate the generated setter, we can add the following:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    p&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;George&amp;quot;&lt;/span&gt;&lt;br /&gt;    echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person renamed to: &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; p&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The output of our program is now:&lt;/p&gt;&lt;pre&gt;   Person created with name: Cosmo&lt;br /&gt;    Person renamed to: George&lt;/pre&gt;&lt;p&gt;One question I now have is now that we have declared the name field as non-null at construction time, can we assign the value to null after the object has been created? Let's add the lines below to the main method:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    p&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;br /&gt;    echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person with null name?: &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; p&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With the above in place, we now get an error:&lt;/p&gt;&lt;pre&gt;   'null' is not assignable to 'sys::Str'&lt;/pre&gt;&lt;p&gt;Again, this restriction should in practice help keep our class instances safe and remove the usual null-checking concerns.&lt;/p&gt;&lt;p&gt;Now some might say a class with one field might be pretty boring (if not useless).  Lets add another field, age, but not require a user of the class to do a &lt;b&gt;Person's&lt;/b&gt; age at construction time by allowing a default value (since age can be a touchy subject anyway).  Let's try adding another constructor to be used when both the name and the age of the Person are known:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #005B9A; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        Str name&lt;br /&gt;        Int age &lt;span style="color: #666666"&gt;:=&lt;/span&gt; &lt;span style="color: #666666"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;,&lt;/span&gt; Int age&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; age&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #005B9A"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            Person cosmo &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Cosmo&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #666666"&gt;40)&lt;/span&gt;&lt;br /&gt;            Person marshall &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Marshall&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; cosmo&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;, aged &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; cosmo&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; marshall&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;, aged &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; marshall&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here will create two &lt;b&gt;Persons&lt;/b&gt; with an declared age and one with the default of 1, and report on both:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;    Duplicate slot name 'make'&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Ah, we get an error... are we not allowed to have two constructors?  Turns out in Fantom, constructors are treated like methods, with the &lt;b&gt;new&lt;/b&gt; keyword out in front, and can be either be named &lt;b&gt;make&lt;/b&gt; or start with &lt;b&gt;make&lt;/b&gt;.  Since we will likely prefer for our users to fully populate the class fields, let's name the two parameter constructor &lt;b&gt;make&lt;/b&gt; and rename the original:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;makeNamed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;,&lt;/span&gt; Int age&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; age&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here though, how do we differentiate between which constructor is used for each assignment?  The &lt;b&gt;Persons&lt;/b&gt; we create are made through a call to &lt;b&gt;Person(...)&lt;/b&gt;?  Turns out we can call the constructors by name by referring to them through the class definition:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    Person cosmo &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Cosmo&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #666666"&gt;40)&lt;/span&gt;&lt;br /&gt;    Person marshall &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;makeNamed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Marshall&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We cannot it appears make calls to &lt;b&gt;Person(...)&lt;/b&gt; like we did before and have the compiler infer with constructor to use asin:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    Person cosmo &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Cosmo&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #666666"&gt;40)&lt;/span&gt;&lt;br /&gt;    Person marshall &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Marshall&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This results in the error:&lt;/p&gt;&lt;pre&gt;   Invalid args &lt;ctor&gt;(sys::Str, sys::Int), not (sys::Str)&lt;/pre&gt;&lt;p&gt;Back to the explicit calls, our class now reads:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #005B9A; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        Str name&lt;br /&gt;        Int age &lt;span style="color: #666666"&gt;:=&lt;/span&gt; &lt;span style="color: #666666"&gt;1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;makeNamed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #005B9A"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str name&lt;span style="color: #666666"&gt;,&lt;/span&gt; Int age&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; name&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;this&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; age&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #005B9A"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            Person cosmo &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;make&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Cosmo&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #666666"&gt;40)&lt;/span&gt;&lt;br /&gt;            Person marshall &lt;span style="color: #666666"&gt;:=&lt;/span&gt; Person&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;makeNamed&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Marshall&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; cosmo&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;, aged &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; cosmo&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Person &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; marshall&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;name&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;, aged &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; marshall&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Giving this attempt a shot gives us... &lt;/p&gt;&lt;pre&gt;   Person Cosmo, aged 40&lt;br /&gt;    Person Marshall, aged 1&lt;/pre&gt;&lt;p&gt;...output for both &lt;b&gt;Persons&lt;/b&gt;, with the second making use of the default age.&lt;/p&gt;&lt;p&gt;This looks like enough to cover for now in one post.  Looking at the documentation further, there's looks like there's a lot more to cover at a later date.  If I find I misstated any of the terminology or made things more difficult then they need to be, I'll be sure to come back and make corrections.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8633357755889777062?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8633357755889777062/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/fantom-constructors-required-fields-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8633357755889777062'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8633357755889777062'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/fantom-constructors-required-fields-and.html' title='Fantom: Constructors, Required Fields, and More'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-3056869786682728549</id><published>2011-09-13T09:10:00.000-07:00</published><updated>2011-09-13T09:15:35.389-07:00</updated><title type='text'>Creating a Marco in Vim</title><content type='html'>&lt;p&gt;I'm a fan of the &lt;a href="http://www.vim.org/"&gt;vim&lt;/a&gt; text editor, and try to use it as much as possible.  I have hard time though making the effort to learn more than I already know.  While I think I can use it pretty effectively, I know I've truly only scratched the surface.&lt;/p&gt;&lt;p&gt;Often I find myself retyping the same commands over and over to repeatedly accomplish the same task.  I then make a mental note to learn about creating macros, which would of helped me out. I then proceed to forget about immediately.&lt;/p&gt;&lt;p&gt;Not today though.  Today I'm going to create my first macro.&lt;/p&gt;&lt;p&gt;A macro is a series of commands that can be replaced.  Marcos can be assigned to buffers so they can be saved and recalled.  Buffers have single- character names in the ranges:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;0-9&lt;/li&gt;&lt;li&gt;a-z&lt;/li&gt;&lt;li&gt;A-Z&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;To start the definition of a macro in command mode, type 'q' followed by a single character in one of the ranges above.&lt;/p&gt;&lt;p&gt;I'm going to start with the text below and think of some reason to create a macro:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;hello my name is george&lt;br /&gt;hello my name is george&lt;br /&gt;hello my name is george&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;How about we try and replace every occurence with "name" with "first name".  We could do a global substitution by typing a colon followed by 's/name/first name/g' and hitting enter, but I want to try out a macro.&lt;/p&gt;&lt;p&gt;A marco could simply:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Search for the next occurrence of name.&lt;/li&gt;&lt;li&gt;Replace "name" with "first name" (or insert the text "first ")&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;Let's try it out.  With my cursor on the first character in the file, if I:&lt;ol&gt;&lt;li&gt;Type 'q' to declare I want to define a macro&lt;/li&gt;&lt;li&gt;Type 'a' to assign the macro to buffer 'a'&lt;/li&gt;&lt;li&gt;Type '/' followed by "name" then Enter to find the first occurence of "name"&lt;/li&gt;&lt;li&gt;Type 'cw' followed by "first name" then Esc to change the word&lt;/li&gt;&lt;li&gt;Type 'q' to complete the macro definition&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;The text should now have one occurrence of "first name" in place of "name":&lt;/p&gt;&lt;pre&gt;hello my first name is george&lt;br /&gt;hello my name is george&lt;br /&gt;hello my name is george&lt;/pre&gt;&lt;p&gt;Now we can run the macro typing '@a'.  Running it twice will complete the remaning two substitutions, giving us:&lt;pre&gt;hello my first name is george&lt;br /&gt;hello my first name is george&lt;br /&gt;hello my first name is george&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;We could also run the marco a number of times with one command.  Say, given the text above where the first substitution was done, if we wanted to run the marco twice in one shot, instead of invoking it two separate times, we can type '2@a'.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-3056869786682728549?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/3056869786682728549/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/creating-marco-in-vim.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3056869786682728549'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3056869786682728549'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/creating-marco-in-vim.html' title='Creating a Marco in Vim'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7815548339625011427</id><published>2011-09-08T18:37:00.000-07:00</published><updated>2011-09-08T18:41:58.789-07:00</updated><title type='text'>Exceptions for Control Flow: Always Wrong?</title><content type='html'>&lt;p&gt;It is generally accepted that exceptions should never be used for control of flow and should only be used for exceptional conditions.  On face value, that assertion seems to make sense.  For instance, given a method that iterates over an array, the user of the method should not be expected to catch a &lt;b&gt;NoSuchElementException&lt;/b&gt; because the method does not make use of a &lt;b&gt;hasNext()&lt;/b&gt; method.&lt;/p&gt;&lt;p&gt;If a state checking method is available to help determine if another method can be called, we would all assume it should be used.  However, what about cases where a series of methods must be called in sequence to accomplish a larger overall task?  In a scenario I'm thinking off, a series of individual methods must be called, where each in the sequence is called only if the previous call succeeded.&lt;/p&gt;&lt;p&gt;&lt;i&gt;Would disobeying the exceptions rule possibly make for cleaner code in such a case?&lt;/i&gt;&lt;/p&gt;&lt;p&gt;To work out my thoughts, I thought of a home building example.  Let's simplify building a house to three steps: laying a foundation, putting up the framing and walls, and then adding the roof.&lt;/p&gt;&lt;p&gt;Let's also imagine a contractor who's job is to build a house, ensuring its marked as condemned is any of the construction fails.  If the contractor attempts to add the framing without the foundation, or the roof without the framing, a catastrophe would surely happen.  To avoid the catastrophe, the builder must check the house to ensure the previous step completed before moving on to the next.&lt;/p&gt;&lt;p&gt;Like an &lt;b&gt;Iterator&lt;/b&gt; with a &lt;b&gt;hasNext()&lt;/b&gt; method, the &lt;b&gt;House&lt;/b&gt; class has methods or state variables indicating whether it has the requirements for the next phase of construction and something similarly for its overall state: whether or not its livable or condemned.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;House&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; hasFoundation&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; hasFraming&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; hasRoof&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; isCondemned&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;b&gt;Contractor&lt;/b&gt; classs has one public method, &lt;b&gt;buildHouse()&lt;/b&gt;, that returns a &lt;b&gt;House&lt;/b&gt;, livable or not, and a private method for completing each step in the construction process:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Contractor&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; House &lt;span style="color: #0000FF"&gt;buildHouse&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def house &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; House&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            addFoundationTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasFoundation&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                addFramingTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasFraming&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    addRoofTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;                    &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(!&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasRoof&lt;/span&gt;&lt;span style="color: #666666"&gt;())&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                        house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;isCondemend&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #666666"&gt;}&lt;/span&gt;              &lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;isCondemned&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;isCondemned&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            house&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addFoundationTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addFramingTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addRoofTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This contractor is overly cautious as he wants to condemn the house as soon as possible if one of the constuction steps fails to avoid a catastrophe.  Another implementation may wait till the building process is completed before condemning the house:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Contractor&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; House &lt;span style="color: #0000FF"&gt;buildHouse&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def house &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; House&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            addFoundationTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasFoundation&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                addFramingTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasFraming&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    addRoofTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;                            &lt;br /&gt;            &lt;span style="color: #666666"&gt;    }&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(!&lt;/span&gt;house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;hasRoof&lt;/span&gt;&lt;span style="color: #666666"&gt;())&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;isCondemned&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            house&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This contractor only looks at the roof and the end of the process to determine if the house should be condemned, which may lead to a lawsuit if the someone where to change the order in which the steps were executed.  The contractor's check at the end could include looking at the foundation and framing as well, but he is overly trusting of his workers not to proceeded and the extra checks may be costly.&lt;/p&gt;&lt;p&gt;What if a different contractor required a complaint be raised by a worker if any of the home building steps fails and condemns the house if he receives the complaint before attempting the next step?&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Contractor&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; House &lt;span style="color: #0000FF"&gt;buildHouse&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def house &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; House&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;try&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                addFoundationTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                addFramingTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                addRoofTo&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt;                            &lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;catch&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;ConstructionComplaint complaint&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                house&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;isCondemned&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            house&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addFoundationTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; ConstructionComplaint &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addFramingTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; ConstructionComplaint &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; def &lt;span style="color: #0000FF"&gt;addRoofTo&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;house&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; ConstuctionComplaint &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here, a specific, but uniform, exception is given to the builder if any of the steps fail, ensuring the next step in the process is not attempted, avoiding a catastrophe.&lt;/p&gt;&lt;p&gt;An argument for this procedure may include that adding another step, like adding a garge, does not increase the complexity of the building process by adding more evaluation (branching), or require the builder to explicitly check that a garge can be added.  Conversely, this approach may be overly trusting in that it requires a each worker method properly files a complaint.&lt;/p&gt;&lt;p&gt;I think it's safe to say we've all seen complex code as least as complex as the first implentation of the &lt;b&gt;buildHouse()&lt;/b&gt; method.  Is it worth asking that the use of exceptions would make the code cleaner?  On the other hand, such cases usually present a situation where the user is required to code around what may be a poor design.&lt;/p&gt;&lt;p&gt;This topic and discussion may be nothing new, but I think it should help give thought to reasoning about the rule stated earlier regarding using exceptions for only exceptional conditions.  Here were not talking avoiding programmer errors, such as trying to access a non-existent array element, but using exceptions to simplify a process.&lt;/p&gt;&lt;p&gt;While I'm not trying to suggest generally that using exceptions to control flow makes for better programs, I do think it is worthwhlie to consider such rules and understand why they may exist in the first place.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7815548339625011427?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7815548339625011427/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/09/exceptions-for-control-flow-always.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7815548339625011427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7815548339625011427'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/09/exceptions-for-control-flow-always.html' title='Exceptions for Control Flow: Always Wrong?'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8117209772040306568</id><published>2011-08-06T14:38:00.000-07:00</published><updated>2011-08-06T14:48:53.258-07:00</updated><title type='text'>First Steps with Fantom</title><content type='html'>&lt;p&gt;I'm now finally getting around to taking my first steps with &lt;a href="http://fantom.org/"&gt;Fantom&lt;/a&gt;, even though I thought I would months ago.  Where else would I start then "Hello World", and while I'm here, I'll toss in a comparison to the &lt;a href="http://www.java.com/en/"&gt;Java&lt;/a&gt; and &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt; versions for good measure:&lt;/p&gt;&lt;p&gt;Below is your classic Java "Hello World" program:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HelloWorld&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;String&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            System&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;out&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;println&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Here is a &lt;b&gt;Fantom&lt;/b&gt; version of the same program.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HelloWorld&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #0000FF"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;There are a few differences worth noting:&lt;ul&gt;&lt;li&gt;The public keyword is not required.&lt;/li&gt;&lt;li&gt;The void keyword maps to a &lt;b&gt;Void&lt;/b&gt; type.&lt;/li&gt;&lt;li&gt;The equivalent to &lt;b&gt;String&lt;/b&gt; in Java is &lt;b&gt;Str&lt;/b&gt; in &lt;b&gt;Fantom&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;The semicolon ending the singular statement in the Java version is not present in the &lt;b&gt;Fantom&lt;/b&gt; version, in fact, including it would cause an error.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Also, the argument to the main method in the Java version is not required in the &lt;b&gt;Fantom&lt;/b&gt; version:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt; &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HelloWorld&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #0000FF"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Since I spend most of time with &lt;b&gt;Groovy&lt;/b&gt;, I thought I'd consider the Groovy version.  Like the &lt;b&gt;Fantom&lt;/b&gt; version, the public keyword is not required, but the declared argument to the main method is, although we don't have to declare a type:&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HelloWorld&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            println &lt;span style="color: #BA2121"&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Additionally, the parentheses are optional and unlike with the &lt;b&gt;Fantom&lt;/b&gt; version, an ending semicolon is allowed.&lt;/p&gt;&lt;p&gt;Now back to &lt;b&gt;Fantom&lt;/b&gt; and getting the greeting printed out on the screen.  The &lt;b&gt;Fantom&lt;/b&gt; class can be placed in a file and run using the &lt;b&gt;fan&lt;/b&gt; command:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ fan HelloWorld.fan&lt;br /&gt;  Hello World!&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;With &lt;b&gt;Groovy&lt;/b&gt;, the &lt;b&gt;println&lt;/b&gt; call can simply be placed in a file and executed as a script using the &lt;b&gt;groovy&lt;/b&gt; command:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ cat hello.groovy&lt;br /&gt;  println "Hello World!"&lt;br /&gt;&lt;br /&gt;  $ groovy hello.groovy&lt;br /&gt;  Hello World!&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Knowing this, I thought I'd try doing something similar in &lt;b&gt;Fantom&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ cat hello.fan&lt;br /&gt;  echo("Hello World!")&lt;br /&gt;&lt;br /&gt;  $ fan echo.fan&lt;br /&gt;  /home/prystasj/workspace/&lt;b&gt;Fantom&lt;/b&gt;/echo.fan(2,1): Expected 'class', not 'echo'&lt;br /&gt;  ERROR: cannot compile script&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Unfortunately, we don't get the same results, but &lt;b&gt;Fantom&lt;/b&gt; does have an interactive shell we can use, &lt;b&gt;fansh&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ fansh&lt;br /&gt;  Fantom Shell v1.0.59 ('?' for help)&lt;br /&gt;  fansh&amp;gt; echo("Hello World!")&lt;br /&gt;  Hello World!&lt;br /&gt;  fansh&amp;gt; exit&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Next, let's have the &lt;b&gt;Fantom&lt;/b&gt; class make use of the array representing the arguments passed into the main method.  In &lt;b&gt;Fantom&lt;/b&gt;, arrays are also zero-based and accessed in the same way we are all used to:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;   &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HelloWorld&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;static&lt;/span&gt; Void &lt;span style="color: #0000FF"&gt;main&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Str&lt;span style="color: #666666"&gt;[]&lt;/span&gt; args&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            echo&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Hello and &amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; args&lt;span style="color: #666666"&gt;[0])&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Running the progam with a single argument will have the argument included in the output:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ fan HelloWorld.fan goodbye&lt;br /&gt;  Hello and goodbye&lt;/pre&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8117209772040306568?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8117209772040306568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/08/first-steps-with-fantom.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8117209772040306568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8117209772040306568'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/08/first-steps-with-fantom.html' title='First Steps with Fantom'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7618245592093733433</id><published>2011-07-23T10:20:00.000-07:00</published><updated>2011-07-23T10:29:49.732-07:00</updated><title type='text'>Groovy: Reducing Duplication with Closures</title><content type='html'>&lt;p&gt;I've been using &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt; as my primary programming language for a few years now.  One of the features that is commonly discussed and I've read about a lot are &lt;a href="http://groovy.codehaus.org/Closures"&gt;closures&lt;/a&gt;.  While I've appreciated what I've read, I've had a difficult time recognizing good places to use them.  Like I lot of things, if you "dont use it, you lose it".&lt;/p&gt;&lt;p&gt;One situation where I have been able to find uses for closures recently was using them instead of methods to reduce duplication that I can hopefully demonstrate using the following example scenario.  We have a &lt;b&gt;TextAnalyzer&lt;/b&gt; class whose sole responsibility is to analyze a string and report the words separated by commas and semicolons and the counts for said delimiting characters.&lt;/p&gt;&lt;p&gt;The &lt;b&gt;TextAnalyzer.analyze()&lt;/b&gt; method below travels through a given string, aggregating words and counting commas and semicolons and returns an instance of &lt;b&gt;TextAnalysis&lt;/b&gt; that contains the results.&lt;/p&gt;&lt;p&gt;For example, given the string:&lt;/p&gt;&lt;pre&gt;    groovy,is,a,language;enjoyable&lt;/pre&gt;&lt;p&gt;The result is:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;    TextAnalysis(words:[groovy, is, a, language, enjoyable], commas:3, semicolons:1)&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Here's the first version of the code.  Of note here to me is the duplication that exists when either a comma or semicolon is found.  In both clauses, the current word is pushed onto the words list and is reset in preparation for the next word.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def text &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;groovy,is,a,language;enjoyable&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalyzer&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@groovy.transform.ToString&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;includeNames&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalysis&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        List words&lt;br /&gt;        &lt;span style="color: #B00040"&gt;int&lt;/span&gt; commas&lt;br /&gt;        &lt;span style="color: #B00040"&gt;int&lt;/span&gt; semicolons&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalyzer&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def words &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[]&lt;/span&gt;&lt;br /&gt;            def word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #B00040"&gt;int&lt;/span&gt; commas &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #B00040"&gt;int&lt;/span&gt; semicolons &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;each&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; c &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    commas&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                    words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #0000FF"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    semicolons&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                    words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;+=&lt;/span&gt; c&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;word&lt;span style="color: #666666"&gt;)&lt;/span&gt; words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalysis&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;words:&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;commas:&lt;/span&gt;commas&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;semicolons:&lt;/span&gt;semicolons&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One option to remove the duplication is to add another method that would handle the push and reset of the current word, named &lt;b&gt;add&lt;/b&gt; below:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalyzer&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; &lt;span style="color: #0000FF"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; word&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;            &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            def words &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[]&lt;/span&gt;&lt;br /&gt;            def word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #B00040"&gt;int&lt;/span&gt; commas &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #B00040"&gt;int&lt;/span&gt; semicolons &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;each&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; c &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    commas&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;=&lt;/span&gt; add&lt;span style="color: #666666"&gt;(&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; word&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #0000FF"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    semicolons&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;=&lt;/span&gt; add&lt;span style="color: #666666"&gt;(&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; word&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    word &lt;span style="color: #666666"&gt;+=&lt;/span&gt; c&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;word&lt;span style="color: #666666"&gt;)&lt;/span&gt; words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;            &lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalysis&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;words:&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;commas:&lt;/span&gt;commas&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;semicolons:&lt;/span&gt;semicolons&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;     &lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This works and produces the same result, however a couple of things bother me about it.  First, the method is doing two things, it's adding the current word to the list, and return a value to reset the word.  The user is required to know that it needs to assign the return value to the current word.&lt;/p&gt;&lt;p&gt;We could also used a boolean to indicate a word was complete that could of been evaluated at the end of each character evaluation, but that would add a bit more complexity to the &lt;b&gt;analyze&lt;/b&gt; method through additional branching.&lt;/p&gt;&lt;p&gt;Another approach that sits better with me is to use a closure that's internal to the &lt;b&gt;analyze&lt;/b&gt; method.  Let's call it &lt;b&gt;completeWord&lt;/b&gt;:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalyzer&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def words &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[]&lt;/span&gt;&lt;br /&gt;        def word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #B00040"&gt;int&lt;/span&gt; commas &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #B00040"&gt;int&lt;/span&gt; semicolons &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        def completeWord &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;br /&gt;            words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;            word &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;each&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; c &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                commas&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                completeWord&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #0000FF"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;c &lt;span style="color: #666666"&gt;==&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                semicolons&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;                completeWord&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                word &lt;span style="color: #666666"&gt;+=&lt;/span&gt; c&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;word&lt;span style="color: #666666"&gt;)&lt;/span&gt; words &lt;span style="color: #666666"&gt;&amp;lt;&amp;lt;&lt;/span&gt; word&lt;br /&gt;        &lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalysis&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;words:&lt;/span&gt;words&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;commas:&lt;/span&gt;commas&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;semicolons:&lt;/span&gt;semicolons&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There are a few reasons I like this better besides the reduced duplication.  Since the closure is defined within the method, it has access to the variables holding the current word and the list of words, so there's no need to pass the variables around.  Also, since there are no other uses for either the previous method or the new closure, the code reads better having the logic to complete a word inside the method itself.&lt;/p&gt;&lt;p&gt;Of course, a really concise version of the &lt;b&gt;analyze&lt;/b&gt; method probably would not iterate over the characters in the string at all (I was thinking I could of simply started with this version, but found the iteration helped illustrate things a bit more):&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalyzer&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalysis&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;words:&lt;/span&gt; text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;split&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;,|;&lt;/span&gt;&lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;),&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;commas:&lt;/span&gt; text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;count&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;),&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;semicolons:&lt;/span&gt; text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;count&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above though still has some duplication in the call to &lt;b&gt;String.count()&lt;/b&gt;.  We remove it using a closure that counts:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;TextAnalyzer&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def &lt;span style="color: #0000FF"&gt;analyze&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            def count &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; c &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;count&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;c&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;TextAnalysis&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;words:&lt;/span&gt; text&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;split&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;,|;&lt;/span&gt;&lt;span style="border: 1px solid #FF0000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;),&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;commas:&lt;/span&gt; count&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;),&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #A0A000"&gt;semicolons:&lt;/span&gt; count&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: #666666"&gt;}&lt;/span&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;While we have not seen any huge wins here, in a more complex scenario, defining closures within methods could help make our code more readable and easier to maintain, which is always a win.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7618245592093733433?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7618245592093733433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/07/groovy-reducing-duplication-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7618245592093733433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7618245592093733433'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/07/groovy-reducing-duplication-with.html' title='Groovy: Reducing Duplication with Closures'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-6932565114078129357</id><published>2011-07-05T18:02:00.000-07:00</published><updated>2011-07-05T18:04:37.048-07:00</updated><title type='text'>Spock: Varying Results from a Mocked Method</title><content type='html'>&lt;p&gt;In the &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt; world, I'm a big fan of the &lt;a href="http://code.google.com/p/spock/"&gt;Spock&lt;/a&gt; specification framework and have written about it before.  One little challenge I ran into was to setup an interaction for a mock to return a different result depending on how many times its been called.  To illustrate what I'm talking about, I'll set things up with the following little scenario.&lt;/p&gt;&lt;p&gt;Let's say we have a class, &lt;b&gt;RecordProcessor&lt;/b&gt; whose job is to read records from a source until there are no more records to be read.  A collaborator of the class, &lt;b&gt;RecordReader&lt;/b&gt;, is responsible for the actual record reading, and is the class whose behavior we want to mock.&lt;/p&gt;&lt;p&gt;The method to be called on the &lt;b&gt;RecordReader&lt;/b&gt; takes as a parameter, the number of records to read and reports whether or not there are more records left in the source to be read.  For simplicities sake, let's assume the responsibility of the &lt;b&gt;RecordProcessor&lt;/b&gt; is to simply invoke the &lt;b&gt;RecordReader&lt;/b&gt; until it reports that there are no more records to read.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;RecordProcessor&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    RecordReader recordReader&lt;br /&gt;    &lt;span style="color: #B00040"&gt;int&lt;/span&gt; threshold&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;process&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; isProcessingComplete &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; recordsRemaining &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;while&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;recordsRemaining&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            recordsRemaining &lt;span style="color: #666666"&gt;=&lt;/span&gt; recordReader&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readRecords&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;threshold&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #408080; font-style: italic"&gt;// would do more processing evaluation here... &lt;/span&gt;&lt;br /&gt;        processingComplete&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We want our unit test to determine whether or not the class under test can detect when there are no more records to read and report that processing was completed.  Here is the base of the &lt;b&gt;Spock specification&lt;/b&gt; where the mocking setup is left to a method:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;RecordProcessorSpec&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; Specification &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def threshold &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;5&lt;/span&gt;&lt;br /&gt;    RecordProcessor recordProcessor &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; RecordProcessor&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;threshold:&lt;/span&gt; threshold&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #BA2121"&gt;&amp;quot;can process all the records accessible by a record reader&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;given:&lt;/span&gt;&lt;br /&gt;            recordProcessorHasMockRecordReader&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;when:&lt;/span&gt;&lt;br /&gt;            isProcessingComplete &lt;span style="color: #666666"&gt;=&lt;/span&gt; recordProcessor&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;process&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;then:&lt;/span&gt;&lt;br /&gt;            isProcessingComplete&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The wiki page describing mocking and interactions can be found My first pass at setting up the mock looked like the below, where the first two calls to &lt;b&gt;readRecords&lt;/b&gt; method would return true and the third would return false (for more on how mocking is done with Spock, see &lt;a href="http://code.google.com/p/spock/wiki/Interactions"&gt;Interactions&lt;/a&gt;).&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def &lt;span style="color: #0000FF"&gt;recordProcessorHasMockRecordReader&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def mock &lt;span style="color: #666666"&gt;=&lt;/span&gt; Mock&lt;span style="color: #666666"&gt;(&lt;/span&gt;RecordReader&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;2&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; mock&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readRecods&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;threshold&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;1&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; mock&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readRecods&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;threshold&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;false&lt;/span&gt;&lt;br /&gt;        recordProcessor&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;recordReader&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; mock&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This didn't do the trick however as 3 invocations were detected for the first definition of how the method should behave:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  Too many invocations for:&lt;br /&gt;&lt;br /&gt;  2 * mock.readFrom(threshold) &amp;gt;&amp;gt; true   (3 invocations)&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I found one answer in using a closure to define the return value of the method invocation that had a bound variable to tracking the number of reads that were asked for:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def &lt;span style="color: #0000FF"&gt;recordProcessorHasMockRecordReader&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def mock &lt;span style="color: #666666"&gt;=&lt;/span&gt; Mock&lt;span style="color: #666666"&gt;(&lt;/span&gt;RecordReader&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        def readsAttempted &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;0&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;*&lt;/span&gt; mock&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;readRecods&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;threshold&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            readsAttempted&lt;span style="color: #666666"&gt;++&lt;/span&gt;&lt;br /&gt;            readsAttepted &lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #666666"&gt;3&lt;/span&gt; &lt;span style="color: #666666"&gt;?&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt; &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;false&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        recordProcessor&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;recordReader&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; mock&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With each invocation of the &lt;b&gt;readRecords&lt;/b&gt; method by the &lt;b&gt;RecordProcessor&lt;/b&gt;, the &lt;b&gt;readsAttempted&lt;/b&gt; variable defined outside of the closure is incremented.  With the first two calls, the &lt;b&gt;RecordProcessor&lt;/b&gt; is told there are more records to process, and with the third call, it is told that all the records have been read.&lt;/p&gt;&lt;p&gt;Are there any other approaches anyone has found to approach a similar problem?  Thanks.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-6932565114078129357?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/6932565114078129357/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/07/spock-varying-results-from-mocked.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6932565114078129357'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6932565114078129357'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/07/spock-varying-results-from-mocked.html' title='Spock: Varying Results from a Mocked Method'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1635961432790645328</id><published>2011-06-16T15:58:00.000-07:00</published><updated>2011-06-16T17:31:03.704-07:00</updated><title type='text'>Experimenting with a Groovy Template Engine and PermGen</title><content type='html'>&lt;p&gt;I had a &lt;b&gt;Groovy&lt;/b&gt; application that was using a &lt;a href="http://groovy.codehaus.org/Groovy+Templates"&gt;SimpleTemplateEngine&lt;/a&gt; was running out of &lt;b&gt;PermGen&lt;/b&gt; space after running for a long period of time.  The app was using version &lt;b&gt;1.7.10&lt;/b&gt; and was running in a &lt;b&gt;Tomcat 6&lt;/b&gt; container.&lt;/p&gt;&lt;p&gt;Load tests consistently showed a linear increase in the &lt;b&gt;PermGen&lt;/b&gt; consumption (monitored by &lt;b&gt;JConsole&lt;/b&gt;).  Originally, the template engine was used to create the template from the template text with every request.  Also, the template was injected as a resource to the class using the engine.&lt;/p&gt;&lt;p&gt;After switching the class to create and cache the created template, we were still experiencing an &lt;b&gt;OutOfMemoryError&lt;/b&gt; during the load test.  Only after removing the use of the engine in its entirety, where we able to observe the &lt;b&gt;PermGen&lt;/b&gt; leveling off.&lt;/p&gt;&lt;p&gt;A run of &lt;b&gt;jmap -permstat PID&lt;/b&gt; against the application process showed a large number of entries of the form of:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  0x00000000f96bf800      24      233928  0x00000000f7f19558      dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000000fd784720&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;To investigate things, I thought I would try to see if I can reproduce the behavior using only a script run by &lt;b&gt;Maven&lt;/b&gt;.  As I go, I'll record what I see below.  To start, I'll use &lt;b&gt;Groovy&lt;/b&gt; &lt;b&gt;1.8.0&lt;/b&gt;.&lt;p&gt;The script will loop and complete a template with every iteration.  The first version of the script will use a singe instance of &lt;b&gt;SimpleTemplateEngine&lt;/b&gt;, but will call the &lt;b&gt;createTemplate&lt;/b&gt; method with every iteration:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  def runs = 5&lt;br /&gt;  def templateText = 'Run number: ${runNumber}'&lt;br /&gt;  def engine = new groovy.text.SimpleTemplateEngine()&lt;br /&gt;&lt;br /&gt;  runs.times { i -&amp;gt;&lt;br /&gt;      def binding = [runNumber:"$i"]&lt;br /&gt;      engine.createTemplate(templateText).make(binding)&lt;br /&gt;  }&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;We'll start the script with verbose class loading on so we see what classes are loaded into &lt;b&gt;PermGen&lt;/b&gt; during execution:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ export MAVEN_OPTS="-verbose:class"&lt;br /&gt;  $ mvn -o exec:java -Dexec.mainClass=TemplateEngine | tee /tmp/out&lt;br /&gt;  $ grep SimpleTemplateScript /tmp/out&lt;br /&gt;  [Loaded SimpleTemplateScript1 from file:/groovy/shell]&lt;br /&gt;  [Loaded SimpleTemplateScript2 from file:/groovy/shell]&lt;br /&gt;  [Loaded SimpleTemplateScript3 from file:/groovy/shell]&lt;br /&gt;  [Loaded SimpleTemplateScript4 from file:/groovy/shell]&lt;br /&gt;  [Loaded SimpleTemplateScript5 from file:/groovy/shell]&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;We might suspect that behavior since not only are reading the template everytime through the loop.  A new version of the script below again uses the same &lt;b&gt;SimpleTemplateEngine&lt;/b&gt; instance and only creates the template once:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  def runs = 5&lt;br /&gt;  def engine = new groovy.text.SimpleTemplateEngine()&lt;br /&gt;  def createdTemplate = engine.createTemplate(templateText)&lt;br /&gt;&lt;br /&gt;  runs.times { i -&amp;gt;&lt;br /&gt;      def binding = [runNumber:"$i"]&lt;br /&gt;      createdTemplate.make(binding)&lt;br /&gt;  }&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;With this version, we only see one class load:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ grep SimpleTemplateScript /tmp/out&lt;br /&gt;  [Loaded SimpleTemplateScript1 from file:/groovy/shell]&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;This is encouraging, but I want to make sure &lt;b&gt;PermGen&lt;/b&gt; truly doesn't increase linerally over time.  To do so, I'll add a sleep at the beginning of the script so I can attach &lt;b&gt;JConsole&lt;/b&gt; to the &lt;b&gt;Maven&lt;/b&gt; process and set the number of runs to a really high number:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  sleep(30000)&lt;br /&gt;  println "Starting..."&lt;br /&gt;  def runs = 50000&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The &lt;b&gt;PermGen&lt;/b&gt; graph is pretty jagged in the case where the &lt;b&gt;createTemplate&lt;/b&gt; method is called each time through the loop.  However, it appears that space was recovered as well so we do not end up running out of space:&lt;/p&gt;&lt;img src="http://2.bp.blogspot.com/-n-8Q_cDRMII/TfqeEl070SI/AAAAAAAAABE/_9NRPSp3h2s/s320/permGenNewInstances.png"/&gt;&lt;p&gt;To test the version where we only create the template once, I'm going to up the number of runs to 5,000,000 as execution is much faster without the repeated calls to the &lt;b&gt;createTemplate&lt;/b&gt; method.  The &lt;b&gt;PermGen&lt;/b&gt; graph rises quickly but levels off:&lt;/p&gt;&lt;img src="http://3.bp.blogspot.com/-T2oV7kmmu-E/TfqetQgBB6I/AAAAAAAAABM/oCzSnTEvvXs/s320/permGenOneInstance.png"/&gt;&lt;p&gt;So far, based on the verbose class loading and graph results, it looks like things are a definite improvement over what I was seeing in the original applcation.  Just to be sure, I'm going to same experiment creating the template only once with version &lt;b&gt;1.7.0.&lt;/b&gt; as well.&lt;/p&gt;&lt;p&gt;With the single instance approach, the &lt;b&gt;PermGen&lt;/b&gt; increased at a very slow rate.  Using the approach where we call the &lt;b&gt;createTemplate&lt;/b&gt; method with each iteration, the graph is again jagged:&lt;/p&gt;&lt;img  src="http://3.bp.blogspot.com/-1Ovu230xcX0/Tfqe89OvRyI/AAAAAAAAABU/Esy_6UyI56A/s320/permGenNewInstances17.png"/&gt;&lt;p&gt;At the end of this little experiment, I don't see any significant differences between &lt;b&gt;Groovy 1.7&lt;/b&gt; and &lt;b&gt;1.8&lt;/b&gt;.  My work application was running in a &lt;b&gt;Tomcat&lt;/b&gt; container which may also factor into things, along with a slightly different &lt;b&gt;JVM&lt;/b&gt; version.  Outside of the container, while the &lt;b&gt;PermGen&lt;/b&gt; does rise with every load of a &lt;b&gt;SimpleTemplateScript&lt;/b&gt; class, the memory is recovered.&lt;/p&gt;&lt;p&gt;I'll likely have to do some more experimentation to understand the behavior I was seeing in the original applcation.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1635961432790645328?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1635961432790645328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/06/experimenting-with-groovy-template.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1635961432790645328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1635961432790645328'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/06/experimenting-with-groovy-template.html' title='Experimenting with a Groovy Template Engine and PermGen'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-n-8Q_cDRMII/TfqeEl070SI/AAAAAAAAABE/_9NRPSp3h2s/s72-c/permGenNewInstances.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5540591046466894550</id><published>2011-04-27T11:50:00.000-07:00</published><updated>2011-04-27T11:51:07.984-07:00</updated><title type='text'>JMeter Plugin: NonGuiDriver NullPointerException &amp; SOAPException</title><content type='html'>&lt;p&gt;When using the &lt;a href="http://www.ronniealleva.org/index.php/maven-jmeter-plugin/"&gt;Maven JMeter Plugin&lt;/a&gt; to try and send web service requests, I ran into the following exception:&lt;pre&gt;  Error in NonGUIDriver java.lang.NullPointerException&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;My &lt;b&gt;jmeter.log&lt;/b&gt; presented the following:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;2011/04/27 14:20:46 ERROR - jmeter.save.SaveService: Conversion error com.thoughtworks.xstream.converters.ConversionException: org/apache/soap/SOAPException : org/apache/sp/SOAPException&lt;br /&gt;---- Debugging information ----&lt;br /&gt;message             : org/apache/soap/SOAPException&lt;br /&gt;cause-exception     : java.lang.NoClassDefFoundError&lt;br /&gt;cause-message       : org/apache/soap/SOAPException&lt;br /&gt;first-jmeter-class  : org.apache.jmeter.save.converters.TestElementConverter.unmarshal(TestElementConverter.java:108)&lt;br /&gt;class               : org.apache.jmeter.save.ScriptWrapper&lt;br /&gt;required-type       : org.apache.jmeter.protocol.http.sampler.WebServiceSampler&lt;br /&gt;path                : /jmeterTestPlan/hashTree/hashTree/hashTree[2]/WebServiceSampler&lt;br /&gt;line number         : 741&lt;br /&gt;-------------------------------&lt;br /&gt;2011/04/27 14:20:46 ERROR - jmeter.JMeter:  java.lang.NullPointerException&lt;br /&gt;        at org.apache.jmeter.gui.tree.JMeterTreeModel.addSubTree(JMeterTreeModel.java:91)&lt;br /&gt;        at org.apache.jmeter.JMeter.run(JMeter.java:728)&lt;br /&gt;        at org.apache.jmeter.JMeter.startNonGui(JMeter.java:706)&lt;br /&gt;        ...&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The solution was to add a dependency on &lt;b&gt;soap:soap:2.3&lt;/b&gt; to my plugin definition:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.jmeter&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-jmeter-plugin&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;reportDir&amp;gt;&lt;/span&gt;${reportDir}&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/reportDir&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;          &lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;soap&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;soap&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.3&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;After adding the dependency, the tests ran fine:&lt;pre&gt;  Starting the test @ Wed Apr 27 14:42:57 EDT 2011 (1303929777586)&lt;br /&gt;  Tidying up ...    @ Wed Apr 27 14:42:59 EDT 2011 (1303929779839)&lt;br /&gt;  ... end of run&lt;/pre&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5540591046466894550?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5540591046466894550/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/04/jmeter-plugin-nonguidriver.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5540591046466894550'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5540591046466894550'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/04/jmeter-plugin-nonguidriver.html' title='JMeter Plugin: NonGuiDriver NullPointerException &amp; SOAPException'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-6360554272709240271</id><published>2011-04-17T16:25:00.000-07:00</published><updated>2011-04-17T16:26:46.018-07:00</updated><title type='text'>Fantom: Getting FWT to run on Linux 64-bit</title><content type='html'>&lt;p&gt;Today I decided to start playing around with the &lt;a href="http://fantom.org/"&gt;Fantom&lt;/a&gt; language.  After working through a couple of examples on the &lt;a href="http://fantom.org/doc/docIntro"&gt;Hello World&lt;/a&gt; page, I ran into a problem running the example that uses the &lt;a href="http://fantom.org/doc/fwt/index.html"&gt;Fantom Widget Toolkit&lt;/a&gt; on my 64-bit machine.&lt;/p&gt;&lt;p&gt;After following the instructions from the &lt;a href="http://fantom.org/doc/docTools/Setup.html"&gt;Fantom Setup Page&lt;/a&gt;, I set my &lt;b&gt;FAN_HOME&lt;/b&gt; in my &lt;b&gt;.bash_profile&lt;/b&gt; to: &lt;pre&gt;  FAN_HOME=/opt/fantom-1.0.58&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I later tried running the &lt;a href="http://fantom.org/doc/docIntro/HelloWorld.html#fwt"&gt;Hello FWT&lt;/a&gt; example below:&lt;pre&gt;  using fwt&lt;br /&gt;  class FwtHello : Test {&lt;br /&gt;    Void main() {&lt;br /&gt;      Window { Label { text = "Hello world" }, }.open&lt;br /&gt;    }&lt;br /&gt;  }&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Which produced the following error:&lt;pre&gt;  hello $ fan hello::FwtHello&lt;br /&gt;  sys::Err: java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I found a &lt;b&gt;swt.jar&lt;/b&gt; here:&lt;pre&gt;  /opt/fantom-1.0.58/lib/java/ext/linux-x86/swt.jar&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The setup page recommended running &lt;b&gt;fan&lt;/b&gt; to get a read on the platform:&lt;pre&gt; $ fan -version | grep platform&lt;br /&gt;   fan.platform:    linux-x86_64&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I created a directory, &lt;b&gt;linux-x86_64&lt;/b&gt;, under &lt;b&gt;java/ext&lt;/b&gt;, and copied the &lt;b&gt;swt.jar&lt;/b&gt; downloaded from the &lt;a href="http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6.2-201102101200/swt-3.6.2-gtk-linux-x86_64.zip"&gt;Eclipse site&lt;/a&gt; to it.&lt;/p&gt;&lt;p&gt;Afterwhich, the example ran fine.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-6360554272709240271?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/6360554272709240271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/04/fantom-getting-fwt-to-run-on-linux-64.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6360554272709240271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/6360554272709240271'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/04/fantom-getting-fwt-to-run-on-linux-64.html' title='Fantom: Getting FWT to run on Linux 64-bit'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5310560045150988372</id><published>2011-04-12T17:47:00.000-07:00</published><updated>2011-04-12T17:48:09.975-07:00</updated><title type='text'>Linux Mint 10 &amp; Intel Centrino Wireless Card</title><content type='html'>&lt;p&gt;Today, I got a new laptop and chose to install &lt;a href="http://www.linuxmint.com/"&gt;Linux Mint 10&lt;/a&gt;.  While the wired connection worked after the install, I had a little trouble with the wireless card, a &lt;b&gt;Intel Centrino Advanced-N  6200&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;For my Google searches, I verified the type of card using:&lt;br /&gt;&lt;pre&gt;  $ lspci -v&lt;br /&gt;  ...&lt;br /&gt;  01:00.0 Network controller: Intel Corporation Device 008a (rev 34)&lt;br /&gt;          Subsystem: Intel Corporation Device 5325&lt;br /&gt;          Flags: bus master, fast devsel, latency 0, IRQ 48&lt;br /&gt;          Memory at d1600000 (64-bit, non-prefetchable) [size=8K]&lt;br /&gt;          Capabilities: &amp;lt;access denied&amp;gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;In short, to fix the problem I had to upgrade to kernel version &lt;b&gt;2.6.38&lt;/b&gt; from &lt;b&gt;2.6.35&lt;/b&gt; and install a driver.&lt;/p&gt;&lt;p&gt;To upgrade my kernel, I followed the helpful instructions here: &lt;a href="http://www.ramoonus.nl/2011/03/linux-kernel-2-6-38-installation-guide-for-ubuntu-linux/"&gt;Upgrade Instructions&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;After a reboot, the splash screen and &lt;b&gt;dmesg&lt;/b&gt; presented the following complaint:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  [   15.131819] iwlagn 0000:01:00.0: request for firmware file 'iwlwifi-6000g2b-5.ucode' failed.&lt;br /&gt;  [   15.131870] iwlagn 0000:01:00.0: no suitable firmware found!&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;To resolve this issue, I download the driver, &lt;a href="http://www.intellinuxwireless.org/iwlwifi/downloads/iwlwifi-6000g2b-ucode-17.168.5.2.tgz"&gt;iwlwifi-6000g2b-ucode-17.168.5.2&lt;/a&gt; from &lt;a href="http://www.intellinuxwireless.org"&gt;Intel Wireless WiFi Link drivers for Linux&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;After unpacking the tar, I copied the driver to &lt;b&gt;/lib/firmware&lt;/b&gt;:&lt;br /&gt;&lt;pre&gt;  iwlwifi-6000g2b-ucode-17.168.5.2 $ sudo cp iwlwifi-6000g2b-5.ucode /lib/firmware&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;After another reboot, I was able to use my wireless card.  Hopefully, this post will help anyone else in a similar situation.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5310560045150988372?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5310560045150988372/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/04/linux-mint-10-intel-centrino-wireless.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5310560045150988372'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5310560045150988372'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/04/linux-mint-10-intel-centrino-wireless.html' title='Linux Mint 10 &amp; Intel Centrino Wireless Card'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-3924520430643928024</id><published>2011-04-06T18:26:00.000-07:00</published><updated>2011-04-06T18:32:46.449-07:00</updated><title type='text'>Groovy 1.8: Playing with the new @Canonical Transformation</title><content type='html'>&lt;p&gt;&lt;a href="http://groovy.codehaus.org"&gt;Groovy 1.8&lt;/a&gt; introduces some new transformations through the use of annotations.  One I came across that I wanted to investigate was &lt;b&gt;@Canonical&lt;/b&gt; which gives you an implementation of &lt;b&gt;equals()&lt;/b&gt;, &lt;b&gt;hashCode()&lt;/b&gt;, and &lt;b&gt;toString()&lt;/b&gt;, along with tuple constructors.&lt;/p&gt;&lt;p&gt;These transformations can also be applied individually through the &lt;b&gt;@EqualsHashCode&lt;/b&gt;, &lt;b&gt;@ToString()&lt;/b&gt;, and &lt;b&gt;@TupleConstructors&lt;/b&gt; respectively.&lt;/p&gt;&lt;p&gt;As I have no experience with tuple constructors,  I hope to take a look at that one later.  On the other hand, I have written a fair share of &lt;b&gt;equals()&lt;/b&gt; and &lt;b&gt;toString()&lt;/b&gt; methods (some better than others) so I'm always looking for a good shortcut for both.&lt;/p&gt;&lt;p&gt;To start, I took a look at &lt;b&gt;@EqualsAndHashCode&lt;/b&gt;.  I wrote the simplest of classes that has one property and tried to see if comparing two instances with the same value would work.  Since the class does not override &lt;b&gt;equals()&lt;/b&gt;, this failed as one would expected:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  def p1 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30)&lt;/span&gt;&lt;br /&gt;  def p2 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;assert&lt;/span&gt; p1 &lt;span style="color: #666666"&gt;==&lt;/span&gt; p2&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The failure presented by running the script:&lt;/p&gt;&lt;pre&gt;  Assertion failed: &lt;br /&gt;&lt;br /&gt;  assert p1 == p2&lt;br /&gt;         |  |  |&lt;br /&gt;         |  |  Person@3040c5&lt;br /&gt;         |  false&lt;br /&gt;         Person@1ec459b&lt;/pre&gt;&lt;p&gt;Adding a simple &lt;b&gt;equals()&lt;/b&gt; method does the trick for now as the assertion passes:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;    &lt;br /&gt;      &lt;span style="color: #B00040"&gt;boolean&lt;/span&gt; &lt;span style="color: #0000FF"&gt;equals&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;o&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;          age &lt;span style="color: #666666"&gt;==&lt;/span&gt; o&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;age&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can use the &lt;b&gt;@EqualsAndHashCode&lt;/b&gt; annotation and remove our override of &lt;b&gt;equals()&lt;/b&gt;.  Here the assertion will stll pass:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;groovy.transform.EqualsAndHashCode&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@EqualsAndHashCode&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age    &lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The next question I had was whether or not there was a way to exclude some properties from taking part in the comparsion.  Turns out we set the annotation to ignore certain properties using &lt;b&gt;excludes&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;Let's add a second property, &lt;b&gt;ssn&lt;/b&gt;, and give both our &lt;b&gt;Person&lt;/b&gt; instances different values:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #AA22FF"&gt;@EqualsAndHashCode&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;excludes&lt;span style="color: #666666"&gt;=&lt;/span&gt;'ssn'&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;      String ssn&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  def p1 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  def p2 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;2&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;assert&lt;/span&gt; p1 &lt;span style="color: #666666"&gt;==&lt;/span&gt; p2&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;No failures here.  Turns out the same can be accomplished by declaring the &lt;b&gt;ssn&lt;/b&gt; as &lt;b&gt;private&lt;/b&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #AA22FF"&gt;@EqualsAndHashCode&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;private&lt;/span&gt; String ssn&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now let's add &lt;b&gt;@ToString&lt;/b&gt; into the mix:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;groovy.transform.EqualsAndHashCode&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;groovy.transform.ToString&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@EqualsAndHashCode&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@ToString&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;      String ssn&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  def p1 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  def p2 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;assert&lt;/span&gt; p1 &lt;span style="color: #666666"&gt;==&lt;/span&gt; p2&lt;br /&gt;  println p1&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above script prints out:&lt;/p&gt;&lt;pre&gt;  Person(30, 1)&lt;/pre&gt;&lt;p&gt;This is definitely more helpful than the default we get if we don't override &lt;b&gt;toString()&lt;/b&gt;:&lt;/p&gt;&lt;pre&gt;  Person@1e092&lt;/pre&gt;&lt;p&gt;But it could be improved perhaps if we tell the annotation to include the name of the properties in the created String:&lt;/p&gt;&lt;pre&gt;  @ToString(includeNames=true)&lt;/pre&gt;&lt;p&gt;We now get the more helpful:&lt;/p&gt;&lt;pre&gt;  Person(age:30, ssn:1)&lt;/pre&gt;&lt;p&gt;Now let's see what happens if we replace both annotations with &lt;b&gt;@Canoncial&lt;/b&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #AA22FF"&gt;@Canonical&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;      String ssn&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  def p1 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  def p2 &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Person&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #A0A000"&gt;age:&lt;/span&gt;&lt;span style="color: #666666"&gt;30,&lt;/span&gt; &lt;span style="color: #A0A000"&gt;ssn:&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;#39;1&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;assert&lt;/span&gt; p1 &lt;span style="color: #666666"&gt;==&lt;/span&gt; p2&lt;br /&gt;  println p1&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The assertion passes, but we now are reverted to the String representation that didn't include the field names.  An attempt to add the &lt;b&gt;includeNames&lt;/b&gt; setting failed with an exception:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  'includeNames'is not part of the annotation groovy.transform.Canonical&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I found that if added the &lt;b&gt;@ToString&lt;/b&gt; annotation back, we get the display we want.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #AA22FF"&gt;@Canonical&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #AA22FF"&gt;@ToString&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;includeNames&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Person&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #B00040"&gt;int&lt;/span&gt; age&lt;br /&gt;      String ssn&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So it appears that &lt;b&gt;@Canonical&lt;/b&gt; applies both the &lt;b&gt;@EqualsAndHashCode&lt;/b&gt; and &lt;b&gt;@ToString&lt;/b&gt; annotations, but applies the defaults for each.  To be more selective in the behavior of the individual annotations, we need include them individually.&lt;/p&gt;&lt;p&gt;There look's like there are more options for each of the transformations we've used so far.  I found the &lt;b&gt;Javadoc&lt;/b&gt; for  version &lt;b&gt;1.8-rc-3&lt;/b&gt; in the &lt;a href="http://dist.groovy.codehaus.org/distributions/groovy-src-1.8.0-rc-3.zip"&gt;source release&lt;/a&gt; to be very helpful.  A link should be made availabe on the &lt;a href="http://groovy.codehaus.org/Download"&gt;Groovy downloads&lt;/a&gt; page when &lt;b&gt;1.8.0&lt;/b&gt; is released.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-3924520430643928024?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/3924520430643928024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/04/groovy-18-playing-with-new-canonical.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3924520430643928024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3924520430643928024'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/04/groovy-18-playing-with-new-canonical.html' title='Groovy 1.8: Playing with the new @Canonical Transformation'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8668484337387699617</id><published>2011-04-06T17:07:00.000-07:00</published><updated>2011-04-06T17:08:21.155-07:00</updated><title type='text'>Installing Groovy 1.8: NoClassDefFoundError: GroovyStarter</title><content type='html'>&lt;p&gt;Today, I tried installing &lt;a href="http://groovy.codehaus.org/Download"&gt;Groovy 1.8-rc-3&lt;/a&gt; so I could play with some of the new features of the language.  On my &lt;a href="www.ubuntu.com"&gt;Ubuntu&lt;/a&gt; machine, the current version I had installed from the repositories was &lt;b&gt;1.7.6&lt;/b&gt;, which in itself is pretty recent:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ groovy --version&lt;br /&gt;  Groovy Version: 1.7.6 JVM: 1.6.0_13&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Since I installed &lt;b&gt;Groovy&lt;/b&gt; through the Ubunutu repositories using &lt;b&gt;apt-get&lt;/b&gt;, the &lt;b&gt;groovy&lt;/b&gt; executable was already on my path:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ which groovy&lt;br /&gt;  /usr/bin/groovy&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;As I did not want to mess with the default install, to get &lt;b&gt;1.8&lt;/b&gt; into the picture, I would have to install things manually.  After unzipping the install, I expected to be able to run it easily, but I ran into an exception:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ /opt/groovy-1.8.0-rc-3/bin/groovy --version&lt;br /&gt;   Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/tools/GroovyStarter&lt;br /&gt;   Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.tools.GroovyStarter&lt;br /&gt;           at java.net.URLClassLoader$1.run(URLClassLoader.java:200)&lt;br /&gt;           ...&lt;br /&gt;   Could not find the main class: org.codehaus.groovy.tools.GroovyStarter.  Program will exit.&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Of course by instinct, I went to Google, but before I went to far, it hit me to check &lt;b&gt;GROOVY_HOME&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ echo $GROOVY_HOME&lt;br /&gt;  /usr/share/groovy&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;After changing the value to my new install, I was able to verify the install:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  $ export $GROOVY_HOME='/opt/groovy-1.8.0-rc-3'&lt;br /&gt;  $ /opt/groovy-1.8.0-rc-3/bin/groovy --version&lt;br /&gt;  Groovy Version: 1.8.0-rc-3 JVM: 1.6.0_13&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Nothing groundbreaking here, but hopefully I won't forget about this the next time and jump to a search page.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8668484337387699617?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8668484337387699617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/04/installing-groovy-18.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8668484337387699617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8668484337387699617'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/04/installing-groovy-18.html' title='Installing Groovy 1.8: NoClassDefFoundError: GroovyStarter'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-625701930362846712</id><published>2011-02-26T09:30:00.001-08:00</published><updated>2011-02-26T09:32:29.132-08:00</updated><title type='text'>Spring JMS &amp; Groovy: Sending and Receiving Messages</title><content type='html'>&lt;p&gt;I recently wrote some code using &lt;a href="http://static.springsource.org/spring/docs/3.0.x/reference/jms.html"&gt;Spring JMS&lt;/a&gt; to send a JMS text message to a queue and to receive a response.  The examples in the Spring documentation use Java of course to send and receive the messages.  Here I wanted to demonstrate how the code can be simplified using &lt;a href="http://groovy.codehaus.org"&gt;Groovy&lt;/a&gt; .&lt;/p&gt;&lt;p&gt;In order to send and receive the messages, a &lt;a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jms/core/JmsTemplate.html"&gt;JmsTemplate&lt;/a&gt; object is required.  In a typical case, the template would be configured thru Spring and injected.  Here,  I'm going to omit the creation process here for brevity's sake, especially given any example would be specific to a particular JMS provider, but the &lt;a href="http://static.springsource.org/spring/docs/3.0.x/reference/jms.html"&gt;documentation&lt;/a&gt; does a great job explaining how to accomplish the creation of a template using &lt;b&gt;Spring&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;When sending a text message, we'll need a list of parameters:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;The text the message should contain.&lt;/li&gt;&lt;li&gt;The name of the queue to place the request on.&lt;/li&gt;&lt;li&gt;The name of the queue to have the response sent to.&lt;/li&gt;&lt;li&gt;A correlation ID so that we can receive the response created for the message being sent.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;Since its simple to get out of the way here, a correlation ID can be created by creating a unique &lt;b&gt;String&lt;/b&gt; using:&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def &lt;span style="color: #0000FF"&gt;createCorrelationId&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; UUID&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;randomUUID&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;toString&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;/p&gt;&lt;p&gt;We'll also need a method to create a &lt;a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jms/core/MessageCreator.html"&gt;MessageCreator&lt;/a&gt; to give to the &lt;b&gt;JmsTemplate&lt;/b&gt; when sending the mesage. A Java example of doing so my look like this using an inner class:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;JmsTemplate jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; String text&lt;span style="color: #666666"&gt;,&lt;/span&gt; String requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; String responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; String correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; JMSException &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; MessageCreator&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;public&lt;/span&gt; Message &lt;span style="color: #0000FF"&gt;createMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Session session&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; JMSException &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                TextMessage message &lt;span style="color: #666666"&gt;=&lt;/span&gt; session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; message&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;});&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;This example can be made easier on the eyes by using &lt;b&gt;as&lt;/b&gt; in Groovy since &lt;b&gt;MessageCreator&lt;/b&gt; is an interface (we also get the benefit of not needing the &lt;b&gt;throws&lt;/b&gt; declarations):&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; session &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; &lt;br /&gt;            session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt; as MessageCreator&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Or we can store the &lt;b&gt;MessageCreator&lt;/b&gt; in a variable:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        def messageCreator &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; session &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; &lt;br /&gt;            session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt; as MessageCreator&lt;br /&gt;&lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; messageCreator&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;In order to set the response queue and correlationID though, we'll need access to the text message created by the &lt;b&gt;Session&lt;/b&gt; provided by the &lt;b&gt;JmsTemplate&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def correlationId &lt;span style="color: #666666"&gt;=&lt;/span&gt; createCorrelationId&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        def messageCreator &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; session &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; &lt;br /&gt;            session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;            message&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;with&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                setText request&lt;br /&gt;                setJMSCorrelationID correlationId&lt;br /&gt;                setJMSReplyTo session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createQueue&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt; as MessageCreator&lt;br /&gt;&lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; messageCreator&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Since the &lt;b&gt;MessageCreator&lt;/b&gt; is defined by a closure, we can extract a method out that the closure will have access to when its executed to create the text message:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def correlationId &lt;span style="color: #666666"&gt;=&lt;/span&gt; createCorrelationId&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;createMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;session&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def message &lt;span style="color: #666666"&gt;=&lt;/span&gt; session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;        message&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;with&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            setText text&lt;br /&gt;            setJMSCorrelationID correlationId&lt;br /&gt;            setJMSReplyTo session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createQueue&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        message&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        def messageCreator &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; session &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; &lt;br /&gt;            createMessage&lt;span style="color: #666666"&gt;(&lt;/span&gt;session&lt;span style="color: #666666"&gt;,&lt;/span&gt; request&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt; as MessageCreator&lt;br /&gt;&lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; messageCreator&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now that we have a method for sending the request message, we need a way to grab the response.  A Java example:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;receiveResponse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;JmsTemplate jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; String responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; String correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throws&lt;/span&gt; JMSException &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        String selector &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;JMSCorrelationID=&amp;#39;&amp;quot;&lt;/span&gt; &lt;span style="color: #666666"&gt;+&lt;/span&gt; correlationId &lt;span style="color: #666666"&gt;+&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;#39;&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        Message message &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;TextMessage&lt;span style="color: #666666"&gt;)&lt;/span&gt; jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;receiveSelected&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;parameters&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getRetrieveFromQueue&lt;/span&gt;&lt;span style="color: #666666"&gt;(),&lt;/span&gt; selector&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;        String response&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;message &lt;span style="color: #666666"&gt;!=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            response &lt;span style="color: #666666"&gt;=&lt;/span&gt; message&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getText&lt;/span&gt;&lt;span style="color: #666666"&gt;();&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; response&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;A Groovier way:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def &lt;span style="color: #0000FF"&gt;receiveResponse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;     def selector &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;JMSCorrelationID=&amp;#39;$correlationId&amp;#39;&amp;quot;&lt;/span&gt; &lt;br /&gt;     def message &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;TextMessage&lt;span style="color: #666666"&gt;)&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;receiveSelected&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; selector&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;     message&lt;span style="color: #666666"&gt;?.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getText&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;In both reception examples, the fact we may not get a response or text in the response.  This is something the caller can deal with or we can modify the examples to throw an exception:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;   def &lt;span style="color: #0000FF"&gt;receiveResponse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def selector &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;JMSCorrelationID=&amp;#39;$correlationId&amp;#39;&amp;quot;&lt;/span&gt;&lt;br /&gt;        def message &lt;span style="color: #666666"&gt;=&lt;/span&gt; jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;receiveSelected&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; selector&lt;span style="color: #666666"&gt;)&lt;/span&gt; as TextMessage&lt;br /&gt;        def response &lt;span style="color: #666666"&gt;=&lt;/span&gt; message&lt;span style="color: #666666"&gt;?.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getText&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(!&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;throw&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000FF"&gt;Exception&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Got nothing!&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #408080; font-style: italic"&gt;// in practice would likely throw a more apt or custom exception&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        response&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;To put it all together, the code examples above can be aggregated to a class:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;JmsMessageSender&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;createCorrelationId&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; UUID&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;randomUUID&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;toString&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;createMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;session&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def message &lt;span style="color: #666666"&gt;=&lt;/span&gt; session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createTextMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;text&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;br /&gt;        message&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;with&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            setText text&lt;br /&gt;            setJMSCorrelationID correlationId&lt;br /&gt;            setJMSReplyTo session&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createQueue&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        message&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; &lt;br /&gt;        def messageCreator &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; session &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; &lt;br /&gt;            createMessage&lt;span style="color: #666666"&gt;(&lt;/span&gt;session&lt;span style="color: #666666"&gt;,&lt;/span&gt; request&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt; as MessageCreator&lt;br /&gt;&lt;br /&gt;        jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;send&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; messageCreator&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;createSelectorFrom&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #BA2121"&gt;&amp;quot;JMSCorrelationID=&amp;#39;$correlationId&amp;#39;&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;receiveResponse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def selector &lt;span style="color: #666666"&gt;=&lt;/span&gt; createSelectorFrom&lt;span style="color: #666666"&gt;(&lt;/span&gt;correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        def message  &lt;span style="color: #666666"&gt;=&lt;/span&gt; jmsTemplate&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;receiveSelected&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; selector&lt;span style="color: #666666"&gt;)&lt;/span&gt; as TextMessage&lt;br /&gt;        def response &lt;span style="color: #666666"&gt;=&lt;/span&gt; message&lt;span style="color: #666666"&gt;?.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getText&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;(!&lt;/span&gt;response&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;throw&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Exception&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;Got nothing!&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        response&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;A client code example using the above class through three calls:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    JmsTemplate jmsTemplate &lt;span style="color: #408080; font-style: italic"&gt;// injected&lt;/span&gt;&lt;br /&gt;    def correlationId &lt;span style="color: #666666"&gt;=&lt;/span&gt; messageSender&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;createCorrelationId&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;    messageSender&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;sendMessage&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; text&lt;span style="color: #666666"&gt;,&lt;/span&gt; requestQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    def response &lt;span style="color: #666666"&gt;=&lt;/span&gt; messageSender&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;receiveResponse&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;jmsTemplate&lt;span style="color: #666666"&gt;,&lt;/span&gt; responseQueue&lt;span style="color: #666666"&gt;,&lt;/span&gt; correlationId&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Thanks for reading!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-625701930362846712?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/625701930362846712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/02/spring-jms-groovy-sending-and-receiving.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/625701930362846712'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/625701930362846712'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/02/spring-jms-groovy-sending-and-receiving.html' title='Spring JMS &amp; Groovy: Sending and Receiving Messages'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-2323171524125622073</id><published>2011-01-28T07:59:00.001-08:00</published><updated>2011-01-28T07:59:55.568-08:00</updated><title type='text'>Perl: Retrieve URLs with LWP and LWP::Simple</title><content type='html'>&lt;p&gt;With &lt;b&gt;Perl&lt;/b&gt; there are many ways to make requests over the web.  One method is to use the &lt;a href="http://search.cpan.org/dist/libwww-perl/lib/LWP.pm"&gt;LWP&lt;/a&gt; module.  Below is an example of using it grab the contents of a web page:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;use&lt;/span&gt; Carp;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;use&lt;/span&gt; LWP;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$url&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;http://prystash.blogspot.com&amp;#39;&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$contents&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; get_contents_from(&lt;span style="color: #19177C"&gt;$url&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;print&lt;/span&gt; &lt;span style="color: #19177C"&gt;$contents&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;sub &lt;/span&gt;&lt;span style="color: #0000FF"&gt;get_contents_from&lt;/span&gt; {&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; (&lt;span style="color: #19177C"&gt;$url&lt;/span&gt;) &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #19177C"&gt;@_&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$agent&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;LWP::&lt;/span&gt;UserAgent&lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$request&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;HTTP::&lt;/span&gt;Request&lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt;(GET &lt;span style="color: #666666"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #19177C"&gt;$url&lt;/span&gt;);&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$response&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #19177C"&gt;$agent&lt;/span&gt;&lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;request(&lt;span style="color: #19177C"&gt;$request&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; (&lt;span style="color: #666666"&gt;!&lt;/span&gt;&lt;span style="color: #19177C"&gt;$response&lt;/span&gt;&lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;is_success) {&lt;br /&gt;        croak &lt;span style="color: #BA2121"&gt;&amp;quot;Could not get URL &amp;#39;$url&amp;#39;&amp;quot;&lt;/span&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #19177C"&gt;$response&lt;/span&gt;&lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;content&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;Another simpler method is to use the &lt;a href="http://search.cpan.org/~gaas/libwww-perl-5.837/lib/LWP/Simple.pm"&gt;LWP::Simple&lt;/a&gt; module:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;use&lt;/span&gt; Carp;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;use&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;LWP::&lt;/span&gt;Simple;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$url&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;#39;http://prystash.blogspot.com&amp;#39;&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$contents&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; get_contents_from(&lt;span style="color: #19177C"&gt;$url&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;sub &lt;/span&gt;&lt;span style="color: #0000FF"&gt;get_contents_from&lt;/span&gt; {&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; (&lt;span style="color: #19177C"&gt;$url&lt;/span&gt;) &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #19177C"&gt;@_&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;my&lt;/span&gt; &lt;span style="color: #19177C"&gt;$contents&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; get(&lt;span style="color: #19177C"&gt;$url&lt;/span&gt;) &lt;span style="color: #AA22FF; font-weight: bold"&gt;or&lt;/span&gt; croak &lt;span style="color: #BA2121"&gt;&amp;quot;Could not get URL &amp;#39;$url&amp;#39;&amp;quot;&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #19177C"&gt;$contents&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-2323171524125622073?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/2323171524125622073/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/01/perl-retrieve-urls-with-lwp-and.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/2323171524125622073'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/2323171524125622073'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/01/perl-retrieve-urls-with-lwp-and.html' title='Perl: Retrieve URLs with LWP and LWP::Simple'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-8370284770934931527</id><published>2011-01-27T14:06:00.000-08:00</published><updated>2011-01-28T06:15:09.568-08:00</updated><title type='text'>Removing Files Older than a Certain Number of Days</title><content type='html'>Using the &lt;b&gt;find&lt;/b&gt; command, we can remove files that have not been modified in a certain number of days old using the &lt;b&gt;mtime&lt;/b&gt; option:&lt;br /&gt;&lt;pre&gt;  -mtime n&lt;br /&gt;    File's data was last modified n*24 hours ago.  See the comments for -atime &lt;br /&gt;    to understand how rounding affects  the  inter‐pretation of file modification times.&lt;/pre&gt;To delete files that are older than 6 months, we can use:&lt;pre&gt;  $ find . -type f -mtime +180 | xargs rm&lt;/pre&gt;Or alternatively:&lt;pre&gt;  $ find . -type f -mtime +180 -exec rm {} \;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-8370284770934931527?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/8370284770934931527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/01/removing-files-older-than-certain.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8370284770934931527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/8370284770934931527'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/01/removing-files-older-than-certain.html' title='Removing Files Older than a Certain Number of Days'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1987338292909489583</id><published>2011-01-25T16:33:00.000-08:00</published><updated>2011-01-25T16:36:19.391-08:00</updated><title type='text'>GMaven: A Couple Early Problems Building a Plugin</title><content type='html'>&lt;p&gt;My first attempt to build a &lt;a href="http://apache.maven.org"&gt;Maven plugin&lt;/a&gt; using &lt;a href="http://docs.codehaus.org/display/GMAVEN/Implementing+Maven+Plugins"&gt;GMaven&lt;/a&gt; got off to a rough start.  I don't want to use this space to complain by any means, but I would to share what I learned in case anyone else runs into something similar.&lt;/p&gt;&lt;p&gt;My first problem had to deal with my use of a newer version of &lt;b&gt;GMaven&lt;/b&gt;:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.gmaven&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;gmaven-plugin&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.3&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;providerSelection&amp;gt;&lt;/span&gt;1.7&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/providerSelection&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;extensions&amp;gt;&lt;/span&gt;true&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/extensions&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;inherited&amp;gt;&lt;/span&gt;true&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/inherited&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;generateStubs&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;compile&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;generateTestStubs&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;testCompile&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;During the build of the plugin, I was given a deprecation warning stating that no mojo descriptors were found in the project:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;[WARNING] Deprecation Alert:&lt;br /&gt;[WARNING] No mojo descriptors were found in this project which has a packaging type of maven-plugin.&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I found that reason for the warning was that the stub generation was not retaining the &lt;b&gt;Javadoc annotations&lt;/b&gt; used to mark a &lt;b&gt;Mojo&lt;/b&gt;.  By downgrading to version &lt;b&gt;1.2&lt;/b&gt; of &lt;b&gt;GMaven&lt;/b&gt; and changing the &lt;b&gt;providerSelection&lt;/b&gt; to &lt;b&gt;1.6&lt;/b&gt;, the warning went away.&lt;/p&gt;&lt;p&gt;Next when trying to use the plugin in another build, I was present with something ilke:&lt;p&gt;&lt;p&gt;&lt;pre&gt;This realm = plexus.core&lt;br /&gt;urls[0] = file:/opt/apache-maven-2.2.1/lib/maven-2.2.1-uber.jar&lt;br /&gt;Number of imports: 10&lt;br /&gt;import: org.codehaus.classworlds.Entry@a6c57a42&lt;br /&gt;import: org.codehaus.classworlds.Entry@12f43f3b&lt;br /&gt;import: org.codehaus.classworlds.Entry@20025374&lt;br /&gt;import: org.codehaus.classworlds.Entry@f8e44ca4&lt;br /&gt;import: org.codehaus.classworlds.Entry@92758522&lt;br /&gt;import: org.codehaus.classworlds.Entry@ebf2705b&lt;br /&gt;import: org.codehaus.classworlds.Entry@bb25e54&lt;br /&gt;import: org.codehaus.classworlds.Entry@bece5185&lt;br /&gt;import: org.codehaus.classworlds.Entry@3fee8e37&lt;br /&gt;import: org.codehaus.classworlds.Entry@3fee19d8&lt;br /&gt;-----------------------------------------------------&lt;br /&gt;[INFO] ------------------------------------------------------------------------&lt;br /&gt;[ERROR] BUILD ERROR&lt;br /&gt;[INFO] ------------------------------------------------------------------------&lt;br /&gt;[INFO] Internal error in the plugin manager executing goal 'org.prystasj.plugins:jms-testing:1.0-SNAPSHOT:hello': Unable to find the mojo 'hello' (or one of its required components) in the plugin 'org.prystasj.plugins:jms-testing'&lt;br /&gt;org.codehaus.groovy.runtime.GroovyCategorySupport.getCategoryNameUsage(Ljava/lang/String;)Ljava/util/concurrent/atomic/AtomicInteger;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I found the solution to this problem was to exclude the &lt;b&gt;groovy-all-minimal&lt;/b&gt; jar, version &lt;b&gt;1.5.7&lt;/b&gt; with:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;   &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.groovy.maven&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;gmaven-mojo&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;      &lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;exclusions&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;exclusion&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.groovy&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;groovy-all-minimal&lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/exclusion&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/exclusions&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I also had another that was similar error related to class &lt;b&gt;CallSiteArray&lt;/b&gt; that was alleviated by ensuring I was using &lt;b&gt;Groovy 1.6&lt;/b&gt; everywhere.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1987338292909489583?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1987338292909489583/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/01/gmaven-couple-early-problems-building.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1987338292909489583'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1987338292909489583'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/01/gmaven-couple-early-problems-building.html' title='GMaven: A Couple Early Problems Building a Plugin'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-3364222008755448721</id><published>2011-01-10T12:23:00.000-08:00</published><updated>2011-01-10T12:27:22.342-08:00</updated><title type='text'>Groovy: Sorting a Map by Values</title><content type='html'>&lt;p&gt;Here's a real quick &lt;a href="http://groovy.codehaus.org/"&gt;Groovy&lt;/a&gt; snippet demonstrating one way to sort a &lt;b&gt;Map&lt;/b&gt; by the values stored in its entries (mostly so I don't forget how to do it):&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;def map &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;ghi&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;:6,&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;abc&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;:4&lt;/span&gt; &lt;span style="color: #666666"&gt;,&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;def&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;:5]&lt;/span&gt;&lt;br /&gt;def sortedByValue &lt;span style="color: #666666"&gt;=&lt;/span&gt; map&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;sort&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; a&lt;span style="color: #666666"&gt;,&lt;/span&gt;b &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; a&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;value&lt;/span&gt; &lt;span style="color: #666666"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; b&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;value&lt;/span&gt; &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;println sortedByValue&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;keySet&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;The output from this snippet is:&lt;pre&gt;[abc, def, ghi]&lt;/pre&gt;Anyone have any other methods for doing the same thing?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-3364222008755448721?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/3364222008755448721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/01/groovy-sorting-map-by-values.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3364222008755448721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/3364222008755448721'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/01/groovy-sorting-map-by-values.html' title='Groovy: Sorting a Map by Values'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-336335978574295491</id><published>2011-01-08T15:20:00.000-08:00</published><updated>2011-03-11T07:23:49.580-08:00</updated><title type='text'>Using Maven to Publish and Verify Schemas</title><content type='html'>&lt;p&gt;We use &lt;a href="http://maven.apache.org"&gt;Maven&lt;/a&gt; to publish schemas and other documents, like WSDLs, that we would like to share across projects. This makes publishing schema releases, along with schemas that are in-development, easy for consumption by clients.  Using &lt;b&gt;Maven&lt;/b&gt; also allows to easily to publish the schema together with example documents as a resource bundle in addition to validating the examples messages against the schema during the build so we know the schema and what we would expect a message to look like are in sync.&lt;/p&gt;&lt;p&gt;Here I'm going to demonstrate our base project structure and the minimum POM we use for our schema projects.  I'll follow that up by adding in additional plugins to make the project more worthwhile.&lt;/p&gt;&lt;p&gt;To start, we'll use a real simple schema, &lt;b&gt;people.xsd&lt;/b&gt;, describing a list of people with each person being described with a first name, last name, and his/her age:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns:xs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;elementFormDefault=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;qualified&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;people&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;minOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;maxOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;person&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;person&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;firstName&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;lastName&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;age&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/xs:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;An example document containing two people:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;people&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;person&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;firstName&amp;gt;&lt;/span&gt;George&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/firstName&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;lastName&amp;gt;&lt;/span&gt;Costanza&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/lastName&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;age&amp;gt;&lt;/span&gt;40&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/age&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/person&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;person&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;firstName&amp;gt;&lt;/span&gt;Cosmo&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/firstName&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;lastName&amp;gt;&lt;/span&gt;Kramer&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/lastName&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;age&amp;gt;&lt;/span&gt;42&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/age&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/person&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/people&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;At the root of the project, we of course have the &lt;b&gt;pom.xml&lt;/b&gt;.  The schema and example documents are kept in the &lt;b&gt;src/main/resources&lt;/b&gt; directory:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  pom.xml&lt;br /&gt;  src/main/resources/people.xsd&lt;br /&gt;  src/main/resources/twoPeople.xml&lt;br /&gt;  src/main/resources/onePerson.xml&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The &lt;b&gt;POM&lt;/b&gt; itself is relatively simple.  By default, &lt;b&gt;Maven&lt;/b&gt; will look to package a &lt;b&gt;JAR&lt;/b&gt;.  We have no classes in this project, but the files in the &lt;b&gt;src/main/resources&lt;/b&gt; directory will be packaged.  Since we want to publish the schema as a separate artifact, we can use the &lt;a href="http://mojo.codehaus.org/build-helper-maven-plugin/"&gt;Build Helper&lt;/a&gt; plugin to attach the schema to the build for publishing.  The location of the schema is defined by the &lt;b&gt;schema&lt;/b&gt; property, which we will reuse later:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;project&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/POM/4.0.0&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns:xsi=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;modelVersion&amp;gt;&lt;/span&gt;4.0.0&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/modelVersion&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.prystasj.schemas&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;people&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0-SNAPSHOT&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;People Schema&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;properties&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;schema&amp;gt;&lt;/span&gt;src/main/resources/${artifactId}.xsd&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/schema&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/properties&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.mojo&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;build-helper-maven-plugin&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.5&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;package&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;attach-artifacts&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;attach-artifact&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifacts&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifact&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;file&amp;gt;&lt;/span&gt;${schema}&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/file&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;type&amp;gt;&lt;/span&gt;xsd&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/type&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifact&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifacts&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/project&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now we can run the &lt;b&gt;install&lt;/b&gt; phase and see that the schema is installed to our local repository:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;$ mvn install&lt;br /&gt;[INFO] Scanning for projects...&lt;br /&gt;...&lt;br /&gt;[INFO] [install:install {execution: default-install}]&lt;br /&gt;[INFO] Installing /home/prystasj/workspace/prystasj/writing/maven-xsd/target/people-1.0-SNAPSHOT.jar to /home/prystasj/.m2/repository/org/prystasj/schemas/people/1.0-SNAPSHOT/people-1.0-SNAPSHOT.jar&lt;br /&gt;[INFO] Installing /home/prystasj/workspace/prystasj/writing/maven-xsd/src/main/schemas/people.xsd to /home/prystasj/.m2/repository/org/prystasj/schemas/people/1.0-SNAPSHOT/people-1.0-SNAPSHOT.xsd&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The schema, along with the JAR, are installed separately to the local repository, and of course we can deploy it for public consumption by running the &lt;b&gt;deploy&lt;/b&gt; phase.&lt;/p&gt;&lt;p&gt;Since we have a couple of sample messages, we can add a step to the build to validate them against the schema to help insulate us against any inconsistencies if the schema ever changes.  To do just that, we'll add an execution of the &lt;a href="http://mojo.codehaus.org/xml-maven-plugin/"&gt;XML Maven Plugin&lt;/a&gt;:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.codehaus.mojo&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;xml-maven-plugin&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0-beta-3&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;validate&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;validationSets&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;validationSet&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;systemId&amp;gt;&lt;/span&gt;${schema}&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/systemId&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;dir&amp;gt;&lt;/span&gt;src/main/resources&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/dir&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;excludes&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;exclude&amp;gt;&lt;/span&gt;${artifactId}.xsd&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/exclude&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/excludes&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/validationSet&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/validationSets&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The &lt;b&gt;systemId&lt;/b&gt; property is used to define the location of the schema we want to validate against relative to the base of the project.  Here I'm using the value of the &lt;b&gt;schema&lt;/b&gt; property from the original version of the POM.  The &lt;b&gt;dir&lt;/b&gt; property describes the directory containing the instance documents to validate.  I've added an &lt;b&gt;exclude&lt;/b&gt; element to ensure the schema itself is not used.&lt;/p&gt;&lt;p&gt;When we run the &lt;b&gt;install phase&lt;/b&gt; now, we should see the following build output if the validation succeeds:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;[INFO] [surefire:test {execution: default-test}]&lt;br /&gt;[INFO] No tests to run.&lt;br /&gt;[INFO] [xml:validate {execution: default}]&lt;br /&gt;[INFO] [jar:jar {execution: default-jar}]&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;To make sure things are setup correctly, let's add an instance document that we know should be considered invalid, &lt;b&gt;src/main/resources/invalidPerson.xml&lt;/b&gt;, which defines one person without the required &lt;b&gt;age&lt;/b&gt; element:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;people&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;person&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;firstName&amp;gt;&lt;/span&gt;George&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/firstName&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;lastName&amp;gt;&lt;/span&gt;Costanza&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/lastName&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/person&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/people&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now our build fails, citing:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;[INFO] ------------------------------------------------------------------------&lt;br /&gt;[ERROR] BUILD ERROR&lt;br /&gt;[INFO] ------------------------------------------------------------------------&lt;br /&gt;[INFO] While parsing /home/prystasj/workspace/prystasj/writing/maven-xsd/src/main/resources/invalidPerson.xml, at file:/home/prystasj/workspace/prystasj/writing/maven-xsd/src/main/resources/invalidPerson.xml, line 5,  column 12: cvc-complex-type.2.4.b: The content of element 'person' is not complete. One of '{age}' is expected.  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Finally, we can add the &lt;a href="http://maven.apache.org/plugins/maven-remote-resources-plugin/"&gt;Remote Resources Plugin&lt;/a&gt; to create a resource bundle for clients to take advantage of.  We do have the JAR that is built that contains both the schema and example documents that client can unpack using the &lt;a href="http://maven.apache.org/plugins/maven-dependency-plugin/"&gt;Dependency Plugin&lt;/a&gt;, but providing a resource bundle makes things easier, as the contents of the bundle are unpacked as a result of a depending on the bundle.  An example use case for a client would be ensuring the creation of messages to be sent are valid.&lt;/p&gt;&lt;p&gt;The resource bundle can be created by adding the plugin:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-remote-resources-plugin&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.1&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;bundle&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;includes&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;include&amp;gt;&lt;/span&gt;**/*.xml&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/include&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;include&amp;gt;&lt;/span&gt;**/*.xsd&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/include&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/includes&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The build will now create a file describing the resources to be included in the bundle:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;remoteResourcesBundle&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/plugins/maven-remote-resources-plugin/remote-resources/1.1.0 http://maven.apache.org/plugins/maven-remote-resources-plugin/xsd/remote-resources-1.1.0.xsd&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/plugins/maven-remote-resources-plugin/remote-resources/1.1.0&amp;quot;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #7D9029"&gt;xmlns:xsi=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/span&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;remoteResources&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;remoteResource&amp;gt;&lt;/span&gt;twoPeople.xml&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/remoteResource&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;remoteResource&amp;gt;&lt;/span&gt;people.xsd&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/remoteResource&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;remoteResource&amp;gt;&lt;/span&gt;onePerson.xml&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/remoteResource&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/remoteResources&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/remoteResourcesBundle&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;A client can now depend on the bundle using the same plugin:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.maven.plugins&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-remote-resources-plugin&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;process&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;resourceBundles&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;resourceBundle&amp;gt;&lt;/span&gt;org.prystasj.schemas:people:1.0-SNAPSHOT&lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/resourceBundle&amp;gt;&lt;/span&gt;             &lt;br /&gt;              &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/resourceBundles&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #000044; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;With the addition of the plugin in the client build, the schema and example documents will be available on the classpath:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;target/maven-shared-archive-resources/twoPeople.xml&lt;br /&gt;target/maven-shared-archive-resources/people.xsd&lt;br /&gt;target/maven-shared-archive-resources/onePerson.xml&lt;br /&gt;target/classes/twoPeople.xml&lt;br /&gt;target/classes/people.xsd&lt;br /&gt;target/classes/onePerson.xml&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Hope this helps demonstrate some potential uses for Maven that might be a little outside-the-box, but useful none the less.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-336335978574295491?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/336335978574295491/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2011/01/using-maven-to-publish-and-verify.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/336335978574295491'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/336335978574295491'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2011/01/using-maven-to-publish-and-verify.html' title='Using Maven to Publish and Verify Schemas'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1918228949001026601</id><published>2010-11-08T06:34:00.000-08:00</published><updated>2010-11-08T07:33:39.751-08:00</updated><title type='text'>Griffon: Signing an Application</title><content type='html'>&lt;p&gt;Last week, I tried using &lt;a href="http://griffon.codehaus.org/"&gt;Griffon&lt;/a&gt; to help investigate the possibility of creating a &lt;a href="http://www.oracle.com/technetwork/java/javase/overview-137531.html"&gt;Java Web Start&lt;/a&gt; application.  Everything went smoothly with the exception of some trouble I had signing the application for the "production" environment.  To help those in a similar situation, I'll summarize the steps I needed to get things singed below signed below.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt;I used this &lt;a href="http://markmail.org/message/sbyiumlw5dpyywu3#query:+page:1+mid:h55qo2hdgfbgd7gb+state:results"&gt;thread on Markmail&lt;/a&gt; to and the &lt;a href="http://griffon.codehaus.org/Quick+Start"&gt;Griffon Quick Start&lt;/a&gt; guide to help me out.&lt;/p&gt;&lt;p&gt;I created my application as the guide suggests with:&lt;pre&gt;  $ griffon create-app DemoConsole&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Now the guide offers some suggestions for making the resulting application more useful, but here I'm just going to do the bare minimum I know to get the application deployed.&lt;/p&gt;&lt;p&gt;Next, in order to sign the jars in the application for production use, we need to create a keystore:&lt;pre&gt;  $ keytool -genkey -alias GriffonKey&lt;br /&gt;&lt;br /&gt;  Enter keystore password:  &lt;br /&gt;  Re-enter new password: &lt;br /&gt;  What is your first and last name?&lt;br /&gt;    [Unknown]:  John Prystash&lt;br /&gt;  ...&lt;br /&gt;  Enter key password for &amp;lt;GriffonKey&amp;gt;&lt;br /&gt; (RETURN if same as keystore password):&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I used &lt;b&gt;prystasj&lt;/b&gt; for both the keystore and key password, this being information we'll need to know later. The resulting keystore file was created at: &lt;i&gt;/home/prystasj/.keystore&lt;/i&gt;&lt;/p&gt;&lt;p&gt;The signing and key information is determined from the file &lt;i&gt;griffon-app/conf/BuildConfig.groovy&lt;/i&gt;.  Below is the relevant file information in its original form:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;// key signing information&lt;/span&gt;&lt;br /&gt;environments &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;    production &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        signingkey &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            params &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                sigfile &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;GRIFFON&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                keystore &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;CHANGE ME&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                alias &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;CHANGE ME&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #408080; font-style: italic"&gt;// NOTE: for production keys it is more secure to rely on key prompting&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #408080; font-style: italic"&gt;// no value means we will prompt //storepass = &amp;#39;BadStorePassword&amp;#39;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #408080; font-style: italic"&gt;// no value means we will prompt //keypass   = &amp;#39;BadKeyPassword&amp;#39;&lt;/span&gt;&lt;br /&gt;                lazy &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;false&lt;/span&gt; &lt;span style="color: #408080; font-style: italic"&gt;// sign, regardless of existing signatures&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        griffon &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            jars &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                sign &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                pack &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                destDir &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;${basedir}/staging&amp;quot;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            webstart &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                codebase &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;CHANGE ME&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;We'll need to point the production configuration at the keystore we created earlier and set the alias to &lt;b&gt;GriffonKey&lt;/b&gt;.  We'll also add the password we gave to the &lt;b&gt;keytool&lt;/b&gt; program, which is &lt;b&gt;prystasj&lt;/b&gt; in both cases.  Finally, we'll be publishing the app to a webserver, whose location we set in the &lt;b&gt;codebase&lt;/b&gt; property:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;// key signing information&lt;/span&gt;&lt;br /&gt;environments &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #408080; font-style: italic"&gt;//...&lt;/span&gt;&lt;br /&gt;    production &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        signingkey &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            params &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                sigfile &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;GRIFFON&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                keystore &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #666666"&gt;/&lt;/span&gt;home&lt;span style="color: #666666"&gt;/&lt;/span&gt;prystasj&lt;span style="color: #666666"&gt;/.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;keystore&lt;/span&gt;&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                alias &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;GriffonKey&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                storepass &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;prystasj&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                keypass &lt;span&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;prystasj&lt;span&gt;&amp;#39;&lt;/span&gt;&lt;br /&gt;                lazy &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;false&lt;/span&gt; &lt;span style="color: #408080; font-style: italic"&gt;// sign, regardless of existing signatures&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666667"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        griffon &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            jars &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                sign &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                pack &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;true&lt;/span&gt;&lt;br /&gt;                destDir &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;${basedir}/staging&amp;quot;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;            webstart &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                codebase &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span&gt;&amp;#39;&lt;/span&gt;&lt;span style="color: #A0A000"&gt;http:&lt;/span&gt;&lt;span style="color: #408080; font-style: italic"&gt;//myhost.org/prystasj/democonsole/&amp;#39;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now we can build the app with: &lt;pre&gt;  $ griffon prod package webstart&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The result is a &lt;b&gt;zip&lt;/b&gt; file at &lt;i&gt;dist/webstart&lt;/i&gt;.  Taking this zip we can unpack on the webserver at the location we set in the &lt;b&gt;codebase&lt;/b&gt; property.&lt;/p&gt;&lt;p&gt;Next, we can download the application and run it from: &lt;i&gt;http://myhost.org/prystasj/democonsole/application.jnlp&lt;/i&gt;.&lt;/p&gt;&lt;p&gt;Hope this helps anyone else looking to get a &lt;b&gt;Griffon&lt;/b&gt; application signed.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1918228949001026601?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1918228949001026601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/11/griffon-signing-application.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1918228949001026601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1918228949001026601'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/11/griffon-signing-application.html' title='Griffon: Signing an Application'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-4838443146744358586</id><published>2010-10-20T08:18:00.000-07:00</published><updated>2010-11-02T09:58:40.867-07:00</updated><title type='text'>Groovy: Retrieving the Value of Multiple XML Elements</title><content type='html'>&lt;p&gt;Yesterday, I ran into an interesting case at work where some code was parsing XML using &lt;a href="http://groovy.codehaus.org"&gt;Groovy's&lt;/a&gt; &lt;a href="http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper"&gt;XmlSlurper&lt;/a&gt; to retrieve the value of an element and treat it as a &lt;b&gt;String&lt;/b&gt;. Something along the likes of:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def xml &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;xml&amp;gt;&amp;lt;character&amp;gt;a&amp;lt;/character&amp;gt;&amp;lt;/xml&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;  def node &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; XmlSlurper().parseText(xml)&lt;br /&gt;  &lt;span style="color: #008000"&gt;String&lt;/span&gt; result &lt;span style="color: #666666"&gt;=&lt;/span&gt; node.character&lt;br /&gt;  println result&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Which simply prints out &lt;b&gt;a&lt;/b&gt;.  The code was expected to only return value of the first element found, but when a second element is added:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def xml &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;xml&amp;gt;&amp;lt;character&amp;gt;a&amp;lt;/character&amp;gt;&amp;lt;character&amp;gt;b&amp;lt;/character&amp;gt;&amp;lt;/xml&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;  def node &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; XmlSlurper().parseText(xml)&lt;br /&gt;  &lt;span style="color: #008000"&gt;String&lt;/span&gt; result &lt;span style="color: #666666"&gt;=&lt;/span&gt; node.character&lt;br /&gt;  println result&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The resulting output is &lt;b&gt;ab&lt;/b&gt;, which broke a new test case.&lt;/p&gt;&lt;p&gt;One solution to the issue is to grab the first element with:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #008000"&gt;String&lt;/span&gt; result &lt;span style="color: #666666"&gt;=&lt;/span&gt; node.character[&lt;span style="color: #666666"&gt;0&lt;/span&gt;]&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Another interesting point is that the result of &lt;b&gt;node.character[0]&lt;/b&gt; is a &lt;b&gt;NodeChild&lt;/b&gt;, not a &lt;b&gt;String&lt;/b&gt;.  Since the type of the &lt;b&gt;result&lt;/b&gt; variable is declared, the right side of the assignment was coerced to a &lt;b&gt;String&lt;/b&gt;.  If that were not the case, and we had:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def result &lt;span style="color: #666666"&gt;=&lt;/span&gt; node.character[&lt;span style="color: #666666"&gt;0&lt;/span&gt;]&lt;br /&gt;  println result.getClass().getName()&lt;br /&gt;  println result&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The output would be:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  groovy.util.slurpersupport.NodeChildren&lt;br /&gt;  a&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt;We need to use &lt;b&gt;getClass()&lt;/b&gt; as &lt;b&gt;result.class&lt;/b&gt; would return the &lt;b&gt;class&lt;/b&gt; node and not the &lt;b&gt;Class&lt;/b&gt; object itself.&lt;/p&gt;&lt;p&gt;Alternatively, we can use the &lt;b&gt;text()&lt;/b&gt; method of &lt;b&gt;NodeChildren&lt;/b&gt; (which also exists for &lt;b&gt;NodeChild&lt;/b&gt;) to ensure we get a &lt;b&gt;String&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def result &lt;span style="color: #666666"&gt;=&lt;/span&gt; node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;[0].&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println result&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClass&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getName&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println result&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Giving us:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  java.lang.String&lt;br /&gt;  a&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;To summarize, putting all the methods discussed so far to use with:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;  def xml &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;xml&amp;gt;&amp;lt;character&amp;gt;a&amp;lt;/character&amp;gt;&amp;lt;character&amp;gt;b&amp;lt;/character&amp;gt;&amp;lt;/xml&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;  def node &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; XmlSlurper&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;parseText&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;xml&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClass&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getName&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println  node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClass&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getName&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;[0]&lt;/span&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;[0].&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClass&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getName&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;[0].&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println node&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;character&lt;/span&gt;&lt;span style="color: #666666"&gt;[0].&lt;/span&gt;&lt;span style="color: #7D9029"&gt;text&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getClass&lt;/span&gt;&lt;span style="color: #666666"&gt;().&lt;/span&gt;&lt;span style="color: #7D9029"&gt;getName&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;  println&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;We get:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;  ab&lt;br /&gt;  groovy.util.slurpersupport.NodeChildren&lt;br /&gt;&lt;br /&gt;  ab&lt;br /&gt;  java.lang.String&lt;br /&gt;&lt;br /&gt;  a&lt;br /&gt;  groovy.util.slurpersupport.NodeChild&lt;br /&gt;&lt;br /&gt;  a&lt;br /&gt;  java.lang.String&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;While the original gotcha might not be all that hard to resolve, hopefully this gives a little insight to those who might explore things a little bit further.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-4838443146744358586?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/4838443146744358586/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/10/groovy-retrieving-value-of-multiple.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4838443146744358586'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4838443146744358586'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/10/groovy-retrieving-value-of-multiple.html' title='Groovy: Retrieving the Value of Multiple XML Elements'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-1727404155552483925</id><published>2010-10-19T17:28:00.000-07:00</published><updated>2010-10-19T17:36:06.013-07:00</updated><title type='text'>Bash: Learning about Arrays</title><content type='html'>&lt;p&gt;Last year, I wrote about a little &lt;a href="http://groovy.codehaus.org"&gt;Groovy script&lt;/a&gt; to help me validate XML documents against a schema.  A ended up with a little script to call to help me from having to recall how to run it using &lt;a href="http://maven.apache.org"&gt;Maven&lt;/a&gt;.   The script takes a schema location and an example instance document as arguments:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;  #!/bin/bash&lt;/span&gt;&lt;br /&gt;  mvn -o &lt;span style="color: #008000"&gt;exec&lt;/span&gt;:java -Dexec.mainClass&lt;span style="color: #666666"&gt;=&lt;/span&gt;Validator -Dexec.args&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$1 $2&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Which I can call with:&lt;pre&gt;  $ ./validate.sh order.xsd order.xml&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;We store example XML documents along with our internal schemas, and sometimes I find myself running the same script multiple times, once for each document in the project, so I thought having a script to loop through the documents in a directory and report the results would also be helpful.&lt;/p&gt;&lt;p&gt;In order to report both the 'good' instances and the 'bad' instances at the end of the scripts run, I needed to learn a little about &lt;b&gt;bash arrays&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;In found that in &lt;b&gt;bash&lt;/b&gt;, there are multiple ways to create an array.  You can start by simply assigning a value to a yet unused array:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;  #!/bin/bash&lt;/span&gt;&lt;br /&gt;  good&lt;span style="color: #666666"&gt;[&lt;/span&gt;0&lt;span style="color: #666666"&gt;]=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;my.xml&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  echo&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${&lt;/span&gt;&lt;span style="color: #19177C"&gt;good&lt;/span&gt;[0]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;Or you can declare an array using:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #408080; font-style: italic"&gt;  #!/bin/bash&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  declare&lt;/span&gt; -a &lt;span style="color: #19177C"&gt;good&lt;/span&gt; &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;my.xml&amp;quot;&lt;/span&gt;, &lt;span style="color: #BA2121"&gt;&amp;quot;your.xml&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  echo&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${&lt;/span&gt;&lt;span style="color: #19177C"&gt;good&lt;/span&gt;[0]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  echo&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${&lt;/span&gt;&lt;span style="color: #19177C"&gt;good&lt;/span&gt;[1]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;In my case, I will be iterating through the files and adding them to the appropriate array, so I won't be able to declare the array or its size up front.  I used this method to first check if the array is empty in an &lt;b&gt;if&lt;/b&gt; statement, and if so declare and initialize it.  If the array does in fact exist, I append an element to in the &lt;b&gt;else&lt;/b&gt; clause, by using &lt;b&gt;@&lt;/b&gt; to get the length of the array:&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #408080; font-style: italic"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${#&lt;/span&gt;&lt;span style="color: #19177C"&gt;bad&lt;/span&gt;[0]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt; -eq 0 &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;    &lt;/span&gt;&lt;span style="color: #008000"&gt;declare&lt;/span&gt; -a &lt;span style="color: #19177C"&gt;bad&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$i&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;    &lt;/span&gt;&lt;span style="color: #19177C"&gt;bad&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;${bad[@]}&amp;quot;&lt;/span&gt;, &lt;span style="color: #19177C"&gt;$i&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;  fi&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;To roll everything up, my new scripts takes the schema location and a directory as arguments.  For every, directory listing in the directory, I run the &lt;b&gt;Groovy&lt;/b&gt; code against it.  If the validation failed, setting &lt;b&gt;$?&lt;/b&gt; to &lt;b&gt;1&lt;/b&gt;, I add the file to the 'bad' list.  Otherwise, it goes to the good list:&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #408080; font-style: italic"&gt;#!/bin/bash&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #19177C"&gt;xsd&lt;/span&gt;&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #19177C"&gt;$1&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #19177C"&gt;dir&lt;/span&gt;&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #19177C"&gt;$2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;for &lt;/span&gt;i in &lt;span style="color: #BA2121"&gt;`&lt;/span&gt;ls &lt;span style="color: #19177C"&gt;$dir&lt;/span&gt;&lt;span style="color: #BA2121"&gt;`&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000; font-weight: bold"&gt;do&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;      &lt;/span&gt;&lt;span style="color: #008000"&gt;echo&lt;/span&gt; -n &lt;span style="color: #BA2121"&gt;&amp;quot;$i...&amp;quot;&lt;/span&gt;&lt;br /&gt;      mvn -o &lt;span style="color: #008000"&gt;exec&lt;/span&gt;:java -Dexec.mainClass&lt;span style="color: #666666"&gt;=&lt;/span&gt;Validator -Dexec.args&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$1 $2/$i&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: #008000; font-weight: bold"&gt;if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; &lt;span style="color: #19177C"&gt;$?&lt;/span&gt; -eq 1 &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;          if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${#&lt;/span&gt;&lt;span style="color: #19177C"&gt;bad&lt;/span&gt;[0]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt; -eq 0 &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;              &lt;/span&gt;&lt;span style="color: #008000"&gt;declare&lt;/span&gt; -a &lt;span style="color: #19177C"&gt;bad&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$i&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;              &lt;/span&gt;&lt;span style="color: #19177C"&gt;bad&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;${bad[@]}&amp;quot;&lt;/span&gt;, &lt;span style="color: #19177C"&gt;$i&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;fi&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;      else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;          if&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${#&lt;/span&gt;&lt;span style="color: #19177C"&gt;good&lt;/span&gt;[0]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt; -eq 0 &lt;span style="color: #666666"&gt;]&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;              &lt;/span&gt;&lt;span style="color: #008000"&gt;declare&lt;/span&gt; -a &lt;span style="color: #19177C"&gt;good&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;$i&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;              &lt;/span&gt;&lt;span style="color: #19177C"&gt;good&lt;/span&gt;&lt;span style="color: #666666"&gt;=(&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;${good[@]}&amp;quot;&lt;/span&gt;, &lt;span style="color: #19177C"&gt;$i&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #008000; font-weight: bold"&gt;fi&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;      fi&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;  done&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;  &lt;/span&gt;&lt;span style="color: #008000"&gt;echo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  echo&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;Good files: &amp;quot;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000"&gt;echo&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;${good[@]}&amp;quot;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000"&gt;echo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;  echo&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;Failed files: &amp;quot;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000"&gt;echo&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;${&lt;/span&gt;&lt;span style="color: #19177C"&gt;bad&lt;/span&gt;[@]&lt;span style="color: #008000; font-weight: bold"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #008000"&gt;echo&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;At the end of script, I simply &lt;b&gt;echo&lt;/b&gt; both lists.  An example of the output for the report:&lt;pre&gt;  Good files: &lt;br /&gt;  good1.xml good2.xml good3.xml&lt;br /&gt;&lt;br /&gt;  Failed files: &lt;br /&gt;  bad1.xml bad2.xml bad3.xml&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;I'm sure I could remove some of the duplication, but I since I have only two arrays and this is just a helper, I think I'll leave things be for me.  If you have any more array advice, please leave a comment!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-1727404155552483925?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/1727404155552483925/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/10/bash-playing-with-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1727404155552483925'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/1727404155552483925'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/10/bash-playing-with-arrays.html' title='Bash: Learning about Arrays'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-396187426485402373</id><published>2010-10-01T09:00:00.000-07:00</published><updated>2010-10-01T09:01:27.638-07:00</updated><title type='text'>Trang: Creating Schemas from XML</title><content type='html'>&lt;p&gt;Does anyone like writing XML schemas?  Sometimes they can be frustrating, and yet always ends up feeling simple when you're done.  When given the choice, it always feels good to me to start writing a schema from an example instance document, and of course there are plenty of tools to help.&lt;/p&gt;&lt;p&gt;While a lot of tools are available for a price, &lt;a href="http://www.thaiopensource.com/relaxng/trang.html"&gt;Trang&lt;/a&gt; however is free, and helps me with the writer's block I tend to get when I'm handed an XML document and asked to make a schema from scratch.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Trang&lt;/b&gt; is a &lt;a href="http://www.java.com/"&gt;Java&lt;/a&gt; app that can be &lt;a href="http://code.google.com/p/jing-trang/downloads/list"&gt;downloaded&lt;/a&gt; in zip form.  Luckily (for me at least), it was available in the &lt;a href="http://www.ubuntu.com/"&gt;Ubuntu&lt;/a&gt; package repositores:&lt;pre&gt;$ sudo apt-get install trang&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Now that we have &lt;b&gt;Trang&lt;/b&gt; installed, let's generate a schema from a simple XML document, &lt;i&gt;languages.xml&lt;/i&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #220000"&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39; encoding=&amp;#39;UTF-8&amp;#39;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;languages&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Groovy&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;platform&amp;gt;&lt;/span&gt;JVM&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/platform&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;appeared&amp;gt;&lt;/span&gt;2003&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/appeared&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Scala&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;platform&amp;gt;&lt;/span&gt;JVM&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/platform&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;appeared&amp;gt;&lt;/span&gt;2003&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/appeared&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Boo&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;platform&amp;gt;&lt;/span&gt;CLR&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/platform&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;appeared&amp;gt;&lt;/span&gt;2003&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/appeared&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/languages&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now let's tell &lt;b&gt;Trang&lt;/b&gt;, gratefully, to make us a schema:&lt;pre&gt;$ trang languages.xml languages.xsd&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;And a schema is generated for us:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #220000"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span style="color: #3300FF"&gt;xmlns:xs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;elementFormDefault=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;qualified&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;languages&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;maxOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;language&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;language&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;appeared&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;name&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:NCName&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:NCName&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;appeared&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:integer&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;This is a real good start, but I feel like I should make a couple changes.  To make things a little easier to understand for consumers, I think I'll change the uses of &lt;a href="http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#NCName"&gt;NCName&lt;/a&gt; to &lt;a href="http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#string"&gt;string&lt;/a&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;name&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:NCName&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:NCName&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;And require at least one &lt;i&gt;language&lt;/i&gt; element for the document to be valid:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;minOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;maxOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;language&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;My edited schema becomes:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #220000"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span style="color: #3300FF"&gt;xmlns:xs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;elementFormDefault=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;qualified&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;languages&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;minOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;maxOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;language&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;language&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;ref=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;appeared&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;name&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;platform&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #3300FF"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;appeared&amp;quot;&lt;/span&gt; &lt;span style="color: #3300FF"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:integer&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/xs:schema&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Even with this simple example, &lt;b&gt;Trang&lt;/b&gt; has saved me a lot of typing.  &lt;b&gt;Trang&lt;/b&gt; also has the options to also create &lt;a href="http://www.relaxng.org/"&gt;RELAX NG&lt;/a&gt; and &lt;a href="http://www.w3.org/TR/REC-xml/#dt-doctype"&gt;DTD&lt;/a&gt; documents if you need them.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-396187426485402373?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/396187426485402373/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/10/trang-creating-schemas-from-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/396187426485402373'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/396187426485402373'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/10/trang-creating-schemas-from-xml.html' title='Trang: Creating Schemas from XML'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7349602096806770391</id><published>2010-09-21T14:41:00.001-07:00</published><updated>2010-09-21T14:44:26.492-07:00</updated><title type='text'>Perl &amp; Bash: Mass Substitutions from the Command Line</title><content type='html'>&lt;p&gt;Today I found myself in a situation where I had to do the same global substitution for every file in a directory.  I considered writing a script to handle it, but I wanted to try using a &lt;a href="http://www.perl.org"&gt;Perl&lt;/a&gt; one-liner to get the job done.&lt;/p&gt;&lt;p&gt;Given a directory containing a file with the following contents:&lt;/p&gt;&lt;pre&gt;  This is a XML file&lt;br /&gt;  This is a XML file&lt;/pre&gt;I can substitute every occurence of &lt;i&gt;XML&lt;/i&gt; with &lt;i&gt;text&lt;/i&gt; using:&lt;div class="highlight"&gt;&lt;pre&gt;  &lt;span style="color: #19177C"&gt;$ &lt;/span&gt;perl -i -pe &lt;span style="color: #BA2121"&gt;&amp;#39;s/XML/text/g&amp;#39;&lt;/span&gt; file1.xml&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;Afterwards, the file now reads:&lt;pre&gt;  This is a text file&lt;br /&gt;  This is a text file&lt;/pre&gt;If I wanted to do the substitution for every file in the current directory, I can wrap the command in a &lt;b&gt;bash for loop&lt;/b&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;  $ &lt;span style="color: #008000; font-weight: bold"&gt;for &lt;/span&gt;i in &lt;span style="color: #BA2121"&gt;`&lt;/span&gt;ls&lt;span style="color: #BA2121"&gt;`&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;do &lt;/span&gt;perl -i -pe &lt;span style="color: #BA2121"&gt;&amp;#39;s/XML/text/g&amp;#39;&lt;/span&gt; &lt;span style="color: #19177C"&gt;$i&lt;/span&gt;; &lt;span style="color: #008000; font-weight: bold"&gt;done&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;This isn't revolutionary by any means, but its harder for me to forget this technique if I post it here.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7349602096806770391?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7349602096806770391/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/09/perl-bash-mass-substitutions-from.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7349602096806770391'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7349602096806770391'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/09/perl-bash-mass-substitutions-from.html' title='Perl &amp; Bash: Mass Substitutions from the Command Line'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5298679360837125380</id><published>2010-08-14T16:41:00.000-07:00</published><updated>2010-08-14T16:45:22.567-07:00</updated><title type='text'>Maven &amp; soapUI: Testing a Service</title><content type='html'>&lt;p&gt;In a &lt;a href="http://prystash.blogspot.com/2010/07/groovy-spock-adding-number.html"&gt;previous post&lt;/a&gt;, I demonstrated the creation of the business logic for an adding web service, that when given a list of numbers, returns their sum. The goal for me was to create a simple service so I can try out automated testing of the service using &lt;a href="http://soapui.org"&gt;soapUI&lt;/a&gt; from &lt;a href="http://maven.apache.org"&gt;Maven&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;In this post, I'll show how I can run the test suite for the service. In the next post, hopefully I'll demonstrate how we can expose the service in &lt;a href="http://tomcat.apache.org/"&gt;Tomcat&lt;/a&gt; using &lt;a href="http://www.mulesoft.com/"&gt;Mule&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The created a test suite, when run, produces the following results as demonstrated by the service's log:&lt;pre&gt;INFO  [http-8080-1][2010-07-07 11:23:34,981] - Sum of: [2, 2] = 4&lt;br /&gt;INFO  [http-8080-1][2010-07-07 11:23:35,109] - Sum of: [1, 2, 3] = 6&lt;br /&gt;INFO  [http-8080-1][2010-07-07 11:23:35,133] - Sum of: [1, 2, 3, 4] = 10&lt;br /&gt;INFO  [http-8080-1][2010-07-07 11:23:35,148] - Sum of: [0, 1] = 1&lt;br /&gt;INFO  [http-8080-1][2010-07-07 11:23:35,167] - Sum of: [0, 0] = 0&lt;br /&gt;INFO  [http-8080-1][2010-07-07 11:23:35,221] - Sum of: [] = 0&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;In the &lt;b&gt;adder&lt;/b&gt; service project, I exported the project containing a test suite to a new integration testing module, &lt;b&gt;adder-it&lt;/b&gt;, as: &lt;pre&gt;src/test/resources/adder-soapui-project.xml&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;The &lt;b&gt;POM&lt;/b&gt; for the &lt;b&gt;adder-it&lt;/b&gt; module contains the following:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #BC7A00"&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;project&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/POM/4.0.0&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xmlns:xsi=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/span&gt; &lt;span style="color: #7D9029"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&amp;quot;&lt;/span&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;modelVersion&amp;gt;&lt;/span&gt;4.0.0&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/modelVersion&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;parent&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.prystasj.adder&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;adder&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0-SNAPSHOT&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/parent&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;adder-it&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;adder Integration Testing&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;description&amp;gt;&lt;/span&gt;Integration testing with soapUI.&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/description&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;build&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;eviware&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-soapui-plugin&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.5.1&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;projectFile&amp;gt;&lt;/span&gt;src/test/resources/adder-soapui-project.xml&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/projectFile&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;host&amp;gt;&lt;/span&gt;localhost&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/host&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;port&amp;gt;&lt;/span&gt;8080&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/port&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;soap-integration-test&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;integration-test&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;test&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/project&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To run the tests, with the service up and running locally on port &lt;b&gt;8080&lt;/b&gt;, invoke &lt;b&gt;Maven&lt;/b&gt; with:&lt;pre&gt;$ mvn integration-test&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Below is some of the build output statements from a test run:&lt;pre&gt;[INFO] [soapui:test {execution: soap-integration-test}]&lt;br /&gt;soapUI 3.5.1 Maven2 TestCase Runner&lt;br /&gt;19:31:31,085 INFO  [WsdlProject] Loaded project from [file:/home/prystasj/workspace/prystasj/mule/adder/adder-it/src/test/resources/adder-soapui-project.xml]&lt;br /&gt;19:31:31,713 INFO  [SoapUITestCaseRunner] Running soapUI tests in project [adder]&lt;br /&gt;...&lt;br /&gt;19:31:31,734 INFO  [SoapUITestCaseRunner] Running soapUI testcase [Add_2_to_2]&lt;br /&gt;19:31:31,750 INFO  [SoapUITestCaseRunner] running step [Add_2_to_2]&lt;br /&gt;19:31:33,305 INFO  [SoapUITestCaseRunner] Assertion [SOAP Response] has status VALID&lt;br /&gt;19:31:33,306 INFO  [SoapUITestCaseRunner] Assertion [Contains] has status VALID&lt;br /&gt;19:31:33,306 INFO  [SoapUITestCaseRunner] Finished running soapUI testcase [Add_2_to_2], time taken: 1543ms, status: FINISHED&lt;br /&gt;...&lt;br /&gt;19:31:33,307 INFO  [SoapUITestCaseRunner] Running soapUI testcase [Add_1_to_2_to_3]&lt;br /&gt;...&lt;br /&gt;19:31:33,385 INFO  [SoapUITestCaseRunner] Project [adder] finished with status [FINISHED] in 1664ms&lt;/pre&gt;&lt;p&gt;To access the &lt;b&gt;soapUI Maven&lt;/b&gt; plugin, add the following plugin repository to your &lt;b&gt;settings.xml&lt;/b&gt;:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;pluginRepository&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;eviwarePluginRepository&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;url&amp;gt;&lt;/span&gt;http://www.eviware.com/repository/maven2/&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/url&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #330099; font-weight: bold"&gt;&amp;lt;/pluginRepository&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5298679360837125380?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5298679360837125380/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/08/maven-soapui-testing-service.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5298679360837125380'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5298679360837125380'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/08/maven-soapui-testing-service.html' title='Maven &amp; soapUI: Testing a Service'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5062157828196609642</id><published>2010-07-06T19:33:00.000-07:00</published><updated>2010-07-06T19:38:42.497-07:00</updated><title type='text'>Groovy &amp; Spock: Adding Numbers Together</title><content type='html'>&lt;p&gt;Lately, I've been playing around with embedding &lt;a href="http://www.mulesoft.com/"&gt;Mule&lt;/a&gt; in &lt;a href="http://tomcat.apache.org/"&gt;Tomcat&lt;/a&gt;.  To get going, I wanted to start with creating an extermely simple service, and along the way I decided to try out the &lt;a href="http://code.google.com/p/spock/"&gt;Spock specification framework&lt;/a&gt; to test the logic I'd be using in my service.  I liked what I found so much, I thought I'd take a little detour and write about my first use of the framework, comparing it to more a common test class, and save the Tomcat posting for another day.&lt;/p&gt;&lt;p&gt;The simple service that will eventually be developed will take a list of numbers and add them up.  Here is the class that will ultimately do the math:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;Adder&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;    Integer &lt;span style="color: #0000FF"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;List&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;Integer&lt;span style="color: #666666"&gt;&amp;gt;&lt;/span&gt; numbers&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        println &lt;span style="color: #BA2121"&gt;&amp;quot;adding: $numbers&amp;quot;&lt;/span&gt;&lt;br /&gt;        numbers&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;inject&lt;/span&gt;&lt;span style="color: #666666"&gt;(0)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; sum&lt;span style="color: #666666"&gt;,&lt;/span&gt; item &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt; sum &lt;span style="color: #666666"&gt;+&lt;/span&gt; item &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;b&gt;add&lt;/b&gt; method takes a list of numbers and returns their sum.  I'm using &lt;b&gt;Integer&lt;/b&gt; as the return type and &lt;b&gt;List&lt;/b&gt; on the parameter declaration instead of the &lt;b&gt;def&lt;/b&gt; keyword to aid in the generation of the service WSDL down the line.  While the &lt;b&gt;println&lt;/b&gt; won't make into the final version of the class, it might help us out demonstrating what is being sent to the method during testing.&lt;/p&gt;&lt;p&gt;With &lt;b&gt;Spock&lt;/b&gt; we'll be writing a specification that describes the behavior of the &lt;b&gt;Adder&lt;/b&gt; class:&lt;ul&gt;&lt;li&gt;Given a list of numbers, they should be added together to produce a sum.&lt;/li&gt;&lt;li&gt;Given a list containg no numbers, zero should be returned.&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;The framework's documentation does a great job explaining how to write a spec, so I will not to try and reproduce much of it here.  The spec is pretty much self-explanatory, each of the two requirements manifests itself in a method:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;spock.lang.*&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;AdderSpec&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;extends&lt;/span&gt; Specification &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def adder &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Adder&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #BA2121"&gt;&amp;quot;numbers should be added together to produce a sum&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;expect:&lt;/span&gt;&lt;br /&gt;            sum &lt;span style="color: #666666"&gt;==&lt;/span&gt; adder&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;numbers&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;where:&lt;/span&gt;&lt;br /&gt;            sum &lt;span style="color: #666666"&gt;|&lt;/span&gt; numbers&lt;br /&gt;            &lt;span style="color: #666666"&gt;4&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[2,&lt;/span&gt; &lt;span style="color: #666666"&gt;2]&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;6&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[1,&lt;/span&gt; &lt;span style="color: #666666"&gt;2,&lt;/span&gt; &lt;span style="color: #666666"&gt;3]&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;10&lt;/span&gt;  &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[1,&lt;/span&gt; &lt;span style="color: #666666"&gt;2,&lt;/span&gt; &lt;span style="color: #666666"&gt;3,&lt;/span&gt; &lt;span style="color: #666666"&gt;4]&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;1&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[0,&lt;/span&gt; &lt;span style="color: #666666"&gt;1]&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[0,&lt;/span&gt; &lt;span style="color: #666666"&gt;0]&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #BA2121"&gt;&amp;quot;summing no numbers at all should return zero&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;expect:&lt;/span&gt;&lt;br /&gt;            sum &lt;span style="color: #666666"&gt;==&lt;/span&gt; adder&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;numbers&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #A0A000"&gt;where:&lt;/span&gt;&lt;br /&gt;            sum &lt;span style="color: #666666"&gt;|&lt;/span&gt; numbers&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #666666"&gt;[]&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;   &lt;span style="color: #666666"&gt;|&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;  &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;b&gt;where&lt;/b&gt; clauses resemble a table, with each entry describing the input to the add() method on the right, and the expected result on the left. The first entry in the first method would have the values for &lt;b&gt;sum&lt;/b&gt; and &lt;b&gt;numbers&lt;/b&gt; substituted into the &lt;b&gt;expect&lt;/b&gt; clause so that it would read:&lt;/p&gt;&lt;pre&gt;4 == adder.add([2, 2])&lt;/pre&gt;&lt;p&gt;The output from the test run helps illustrate how things are added during a run of the spec:&lt;/p&gt;&lt;pre&gt;Running org.prystasj.services.adder.AdderSpec&lt;br /&gt;adding: [2, 2]&lt;br /&gt;adding: [1, 2, 3]&lt;br /&gt;adding: [0, 1]&lt;br /&gt;adding: []&lt;br /&gt;adding: null&lt;br /&gt;Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.091 sec&lt;/pre&gt;&lt;p&gt;If the value for &lt;b&gt;numbers&lt;/b&gt; instead read &lt;b&gt;[2, 3]&lt;/b&gt;, we would get the following failure when we run the test as 2+3 does not equal 4:&lt;/p&gt;&lt;pre&gt;Condition not satisfied:&lt;br /&gt;&lt;br /&gt;sum == adder.add(numbers)&lt;br /&gt;|   |  |     |   |&lt;br /&gt;4   |  |     5   [2, 3]&lt;br /&gt;    |  org.prystasj.services.adder.Adder@969c29&lt;br /&gt;        false&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;While I could of used a helper method to remove the redunduncy between the two &lt;b&gt;expect&lt;/b&gt; clauses, I liked this output such much, I decided to keep it, as using a method here would modify the resulting failure description. For example:&lt;/p&gt;&lt;pre&gt;check(sum, numbers)&lt;br /&gt;|     |    |&lt;br /&gt;false 4    [2, 3]&lt;/pre&gt;&lt;p&gt;Compare the above spec class to a more traditional unit test and see what you think:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.junit.Before&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;org.junit.Test&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;static&lt;/span&gt; org&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;junit&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;Assert&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;assertEquals&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;AdderTest&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def underTest&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Before&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;setUp&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        underTest &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;new&lt;/span&gt; Adder&lt;span style="color: #666666"&gt;()&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    def &lt;span style="color: #0000FF"&gt;verifySumsFor&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;data&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        data&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;each&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt; sum&lt;span style="color: #666666"&gt;,&lt;/span&gt; numbers &lt;span style="color: #666666"&gt;-&amp;gt;&lt;/span&gt;&lt;br /&gt;            assertEquals &lt;span style="color: #BA2121"&gt;&amp;quot;$sum &amp;lt;= $numbers&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt; sum&lt;span style="color: #666666"&gt;,&lt;/span&gt; underTest&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;add&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;numbers&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Test&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;test_numbers_added_together_produce_a_sum&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def data &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;4&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[2,&lt;/span&gt; &lt;span style="color: #666666"&gt;2],&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;6&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[1,&lt;/span&gt; &lt;span style="color: #666666"&gt;2,&lt;/span&gt; &lt;span style="color: #666666"&gt;3],&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;10&lt;/span&gt; &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[1,&lt;/span&gt; &lt;span style="color: #666666"&gt;2,&lt;/span&gt; &lt;span style="color: #666666"&gt;3,&lt;/span&gt; &lt;span style="color: #666666"&gt;4],&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;1&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[0,&lt;/span&gt; &lt;span style="color: #666666"&gt;1],&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[0,&lt;/span&gt; &lt;span style="color: #666666"&gt;0],&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;]&lt;/span&gt;&lt;br /&gt;        verifySumsFor&lt;span style="color: #666666"&gt;(&lt;/span&gt;data&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Test&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #B00040"&gt;void&lt;/span&gt; &lt;span style="color: #0000FF"&gt;test_summing_no_numbers_at_all_should_return_zero&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def data &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #666666"&gt;[&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #666666"&gt;[],&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #666666"&gt;0&lt;/span&gt;  &lt;span style="color: #666666"&gt;:&lt;/span&gt; &lt;span style="color: #008000; font-weight: bold"&gt;null&lt;/span&gt;&lt;span style="color: #666666"&gt;,&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #666666"&gt;]&lt;/span&gt;&lt;br /&gt;        verifySumsFor&lt;span style="color: #666666"&gt;(&lt;/span&gt;data&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Which version do you like better?  Thanks for reading.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5062157828196609642?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5062157828196609642/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/07/groovy-spock-adding-number.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5062157828196609642'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5062157828196609642'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/07/groovy-spock-adding-number.html' title='Groovy &amp; Spock: Adding Numbers Together'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5068361408187765951</id><published>2010-06-26T15:35:00.000-07:00</published><updated>2010-06-26T15:36:40.467-07:00</updated><title type='text'>Mule: Transport of Larger Responses in CXF</title><content type='html'>Upon calling a remote web service via a &lt;a href="http://cxf.apache.org/"&gt;CXF&lt;/a&gt; endpoint in &lt;a href="http://mulesource.org"&gt;Mule&lt;/a&gt; I was presented with the following error:&lt;pre&gt;Caused by: javax.xml.bind.UnmarshalException&lt;br /&gt; - with linked exception:&lt;br /&gt;[com.ctc.wstx.exc.WstxIOException: Connection reset]&lt;br /&gt;        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:426)&lt;br /&gt;        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)&lt;br /&gt;        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339)&lt;br /&gt;        at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:537)&lt;br /&gt;        ... 96 more&lt;/pre&gt;&lt;p&gt;At first, I thought either my request or the response from the remote server was malformed XML since the exception class and stack trace seems to suggest a marshalling problem.  On the other hand, the exception mentions a connection reset.&lt;/p&gt;&lt;p&gt;To help spare you kind readers the details of my investigation, it turns out the above problem only manifested itself when a large amount of data was being transferred between the services.&lt;/p&gt;&lt;p&gt;The &lt;a href="http://www.mulesoft.org/documentation/display/MULE2USER/CXF+Transport+Configuration+Reference"&gt;Mule CXF Transport&lt;/a&gt; documentation lists an attribute, &lt;b&gt;mtomEnabled&lt;/b&gt;, that can be enabled on the definition of the outbound endpoint for the remote service:&lt;br /&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #008000; font-weight: bold"&gt;&amp;lt;cxf:endpoint&lt;/span&gt; &lt;span style="color: #7D9029"&gt;name=&lt;/span&gt;&lt;span style="color: #000088"&gt;&amp;quot;remoteService&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;address=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;http://constanza.com/service/architectureService&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;clientClass=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;com.costanza.ArchitectureService&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;wsdlPort=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;ArchitectureServicePort&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;wsdlLocation=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;http://constanza.com/service/architectureService?wsdl&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;operation=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;getBlueprint&amp;quot;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #660000"&gt;mtomEnabled=&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #008000; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The attribute turns on the &lt;a href="http://www.w3.org/TR/soap12-mtom/"&gt;SOAP Message Transmission Optimization Mechanism&lt;/a&gt; which encodes the response payload for travel between the services.  The Mule documentation refers to this allowing for data to be sent as an attachment.  At the very least it tells CXF to be on the look out or to handle a potentially large response.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5068361408187765951?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5068361408187765951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/06/mule-transport-of-larger-responses-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5068361408187765951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5068361408187765951'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/06/mule-transport-of-larger-responses-in.html' title='Mule: Transport of Larger Responses in CXF'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-7408990242376334295</id><published>2010-06-22T07:36:00.000-07:00</published><updated>2010-06-22T07:39:24.398-07:00</updated><title type='text'>WSDLs and Message Parts not recognized</title><content type='html'>&lt;p&gt; I recently ran into an interesting problem consuming a web service where a call to the service produced the following error message:&lt;/p&gt;&lt;pre&gt;Message part {http://myservice.com/}getSets was not recognized.  (Does it exist in service WSDL?)&lt;/pre&gt;&lt;p&gt;I originally tested the service using &lt;a href="http://www.eviware.com/"&gt;soapUI&lt;/a&gt; with no problem, but when I tried to invoke the service from elsewhere I was presented with the error.&lt;/p&gt;&lt;p&gt;My service WSDL was auto-generated by &lt;a href="http://cxf.apache.org/"&gt;CXF&lt;/a&gt;. Here I found I had to tweak it a little to help it better conform to the doc/literal style.  The message part declaration for the &lt;b&gt;getSets&lt;/b&gt; operation originally looked something like this (nothing special):&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;wsdl:message&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;getSets&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;wsdl:part&lt;/span&gt; &lt;span style="color: #660066"&gt;element=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;tns:getSets&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;parameters&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/wsdl:part&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/wsdl:message&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The definition of the referenced &lt;b&gt;getSets&lt;/b&gt; element is where the problem was hiding.  The &lt;b&gt;element&lt;/b&gt; declaration contained a reference to a &lt;b&gt;complexType&lt;/b&gt; instead of having the &lt;b&gt;complexType&lt;/b&gt; defined within, or as part of, the element declaration.&lt;/p&gt;&lt;p&gt;The original element defintion:&lt;br /&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;getSets&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;tns:getSets&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:complexType&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;getSets&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #660066"&gt;minOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;shipment&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The new definition that allows calls from both &lt;b&gt;soapUI&lt;/b&gt; and another client to work:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;getSets&amp;quot;&lt;/span&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style="color: #660066"&gt;minOccurs=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;shipment&amp;quot;&lt;/span&gt; &lt;span style="color: #660066"&gt;type=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt; &lt;span style="color: #003333; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #003333; font-weight: bold"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Hopefully this can help someone out who runs into something similar.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-7408990242376334295?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/7408990242376334295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/06/wsdls-and-message-parts-not-recognized.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7408990242376334295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/7408990242376334295'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/06/wsdls-and-message-parts-not-recognized.html' title='WSDLs and Message Parts not recognized'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-441799062200176047</id><published>2010-05-06T12:49:00.000-07:00</published><updated>2010-05-10T06:51:05.805-07:00</updated><title type='text'>Mule: Defining Properties at Runtime</title><content type='html'>&lt;p&gt;I've had the desire for sometime to investigate telling &lt;a href="http://www.mulesoft.org/display/COMMUNITY/Home"&gt;Mule&lt;/a&gt; at run (or start-up) time what set of properties to use for a particular environment as in development, QA, or production, instead of deciding at build or deploy time what the target environment will be.  So below I've come up with a rather simplistic service that uses a system property to define which set of properties to use at startup.&lt;/p&gt;&lt;p&gt;We will use the following Mule configuration element to define the environment properties file (explained in more detail &lt;a href="http://www.mulesoft.org/display/MULE2USER/Configuring+Properties"&gt;here&lt;/a&gt;).&lt;/p&gt;&lt;pre&gt;  &amp;lt;context:property-placeholder location="classpath:default.properties,classpath:${env}.properties" /&amp;gt;&lt;/pre&gt;&lt;p&gt;With the above line included in our Mule configuration, the &lt;i&gt;default.properties&lt;/i&gt; file will be loaded first, followed by the environment-specific properties file.  The latter will have the opportunity to override anything defined by the former.&lt;/p&gt;&lt;p&gt;The service will report the current season.  A greeter bean configured in the Mule context will have two properties to be set via injection: &lt;ul&gt;&lt;li&gt;greetee - who the greeting will be addressed to&lt;/li&gt;&lt;li&gt;season - the current season (a true property of the environment)&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;The greeting will take the form of:&lt;pre&gt;  "Hello ${greetee}, it is ${season}"&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;A default properties file will be used with the hope that we can eliminate having to repeat the property definitions &lt;br /&gt;whose values would be common for every environment.  We'll also investigate the ability to override this property in&lt;br /&gt;case a particular environment would need to do so.&lt;/p&gt;&lt;p&gt;The default properties file will hold the 'greetee'.&lt;pre&gt;    # default.properties&lt;br /&gt;    greetee = Friend&lt;/pre&gt;We'll have two season-defining properties files, one for &lt;b&gt;spring&lt;/b&gt; and one for &lt;b&gt;winter&lt;/b&gt;.  The winter properties file will simply define the season:&lt;pre&gt;    # winter.properties&lt;br /&gt;    season = winter&lt;/pre&gt;The spring properties file will define the &lt;b&gt;season&lt;/b&gt; as well as override the &lt;b&gt;greetee&lt;/b&gt; property set in &lt;b&gt;default.properties&lt;/b&gt;:&lt;pre&gt;    # spring.properties&lt;br /&gt;    greetee = Chap&lt;br /&gt;    season = spring&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Since I'm using &lt;a href="http://maven.apache.org/"&gt;Maven 2&lt;/a&gt; to build my service, we'll place the properties file in the &lt;b&gt;src/main/resources&lt;/b&gt; directory.  By convention, Maven will place all files found in this directory in the JAR file produced by the build.  To make the files available to Mule on the classpath, we can place the JAR in hte &lt;b&gt;lib/user&lt;/b&gt; directory of our Mule deployment so that they are available on the classpath as requested by the &lt;b&gt;property-placeholder&lt;/b&gt; element.&lt;/p&gt;&lt;p&gt;The &lt;b&gt;SeasonReporter&lt;/b&gt; class will have the two properties injected and will be used as a service component in our Mule model:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #000066; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;SeasonReporter&lt;/span&gt; {&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #0000FF"&gt;greetee&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #0000FF"&gt;season&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;def&lt;/span&gt; &lt;span style="color: #0000FF"&gt;reportSeason&lt;/span&gt;() { &lt;span style="color: #BB6622"&gt;&amp;quot;Hello $greetee, it is $season&amp;quot;&lt;/span&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Our Mule configuration will have an inbound HTTP endpoint that will reply synchronously with the environment-specific greeting.  The component that is invoked will be implemented by a &lt;b&gt;SeasonReporter&lt;/b&gt; class that takes the two properties used to create the seasonal greeting.&lt;/p&gt;&lt;p&gt;We'll direct Mule to use the &lt;b&gt;reportSeason&lt;/b&gt; method with no arguments (by default the HTTP transport will provide a payload of one argument, &lt;i&gt;/season&lt;/i&gt; here):&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;mule&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;context:property-placeholder&lt;/span&gt; &lt;span style="color: #444444"&gt;location=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;classpath:default.properties,classpath:${env}.properties&amp;quot;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;model&lt;/span&gt; &lt;span style="color: #444444"&gt;name=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;seasonModel&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #444444"&gt;name=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;seasonService&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;http:inbound-endpoint&lt;/span&gt; &lt;span style="color: #444444"&gt;address=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;http://localhost:8080/season&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;component&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;no-arguments-entry-point-resolver&amp;gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;include-entry-point&lt;/span&gt; &lt;span style="color: #444444"&gt;method=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;reportSeason&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/no-arguments-entry-point-resolver&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;spring-object&lt;/span&gt; &lt;span style="color: #444444"&gt;bean=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;seasonReporter&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/component&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/model&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;spring:bean&lt;/span&gt; &lt;span style="color: #444444"&gt;name=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;seasonReporter&amp;quot;&lt;/span&gt; &lt;span style="color: #444444"&gt;class=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;SeasonReporter&amp;quot;&lt;/span&gt; &lt;span style="color: #444444"&gt;scope=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;prototype&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;spring:property&lt;/span&gt; &lt;span style="color: #444444"&gt;name=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;greetee&amp;quot;&lt;/span&gt; &lt;span style="color: #444444"&gt;value=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;${greetee}&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;spring:property&lt;/span&gt; &lt;span style="color: #444444"&gt;name=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;season&amp;quot;&lt;/span&gt;  &lt;span style="color: #444444"&gt;value=&lt;/span&gt;&lt;span style="color: #BB6622"&gt;&amp;quot;${season}&amp;quot;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/spring:bean&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #000066; font-weight: bold"&gt;&amp;lt;/mule&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To start Mule using the &lt;b&gt;winter&lt;/b&gt; properties file, we can pass in the &lt;b&gt;env&lt;/b&gt; property when starting Mule on the command line with:&lt;pre&gt;  $ mule/bin/mule start -M-Denv=winter&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;After starting Mule and hitting URL &lt;i&gt;http://localhost:8080/season&lt;/i&gt; in a browser, we are presented with:&lt;pre&gt;  Hello Friend, it is winter&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Now we can try starting Mule up for season &lt;b&gt;spring&lt;/b&gt;.  The greeting should now include the overridden greetee property of 'Chap':&lt;pre&gt;  $ mule/bin/mule start -M-Denv=spring&lt;/pre&gt;Giving us:&lt;pre&gt;  Hello Chap, it is spring&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;Thanks for reading!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-441799062200176047?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/441799062200176047/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/05/mule-defining-properties-at-runtime.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/441799062200176047'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/441799062200176047'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/05/mule-defining-properties-at-runtime.html' title='Mule: Defining Properties at Runtime'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-4153311320241467776</id><published>2010-04-20T14:32:00.000-07:00</published><updated>2010-04-21T05:22:07.318-07:00</updated><title type='text'>Mule &amp; CXF: Multiple Operations and Groovy Components</title><content type='html'>&lt;p&gt;When I picture a web service in Mule, there is usually a component backing the endpoint.  Here I have a case where I would want the service fielding the web service request to delegate its work to another service.  To the client, the fact that Mule will pass the actual workload for a request to another service will be hidden.&lt;/p&gt;&lt;p&gt;In this example, we have a mock "warehouse", where a client can store and retrieve a box.  A box is uniquely identified by an ID (a String).  Each operation will result in the invocation of an additional, but separate, service to do the actual work.&lt;/p&gt;&lt;p&gt;Here's the interface defined for our warehouse:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #660099; font-weight: bold"&gt;package&lt;/span&gt; prystasj&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #0000A0"&gt;warehouse&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #660099; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.jws.WebParam&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #660099; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.jws.WebResult&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #660099; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;javax.jws.WebService&lt;/span&gt;&lt;span style="color: #666666"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #AA22FF"&gt;@WebService&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #660099; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #660099; font-weight: bold"&gt;interface&lt;/span&gt; Warehouse &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@WebResult&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;receipt&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    List&lt;span style="color: #666666"&gt;&amp;lt;&lt;/span&gt;String&lt;span style="color: #666666"&gt;&amp;gt;&lt;/span&gt; retrieve&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #AA22FF"&gt;@WebParam&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;boxId&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; String boxId&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@WebResult&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;box&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt;&lt;br /&gt;    String &lt;span style="color: #0000FF"&gt;store&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;&lt;span style="color: #AA22FF"&gt;@WebParam&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;name&lt;span style="color: #666666"&gt;=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;boxId&amp;quot;&lt;/span&gt;&lt;span style="color: #666666"&gt;)&lt;/span&gt; String boxId&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The inteface defines both the operations.  To determine which operation the client wishes to invoke, we can inspect the &lt;b&gt;cxf_property&lt;/b&gt; of the &lt;b&gt;Mule message&lt;/b&gt; created by the &lt;a href="http://www.mulesoft.org/display/MULE2USER/CXF+Transport"&gt;CXF Transport&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;A request indicating the &lt;b&gt;store&lt;/b&gt; operation will be routed to a &lt;b&gt;storageService&lt;/b&gt;, while a &lt;b&gt;retrieve&lt;/b&gt; operation will be routed to a &lt;b&gt;retrievalService&lt;/b&gt;:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;model&lt;/span&gt; &lt;span style="color: #0000A0"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;warehouse&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #0000A0"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;warehouseService&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;cxf:inbound-endpoint&lt;/span&gt; &lt;span style="color: #0000A0"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;http://localhost:8080/warehouse&amp;quot;&lt;/span&gt;&lt;br /&gt;                                 &lt;span style="color: #0000A0"&gt;serviceClass=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;prystasj.warehouse.Warehouse&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;filtering-router&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #0000A0"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;retrieval&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;expression-filter&lt;/span&gt; &lt;span style="color: #0000A0"&gt;evaluator=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;groovy&amp;quot;&lt;/span&gt; &lt;br /&gt;                        &lt;span style="color: #0000A0"&gt;expression=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;message.getProperty(&amp;#39;cxf_operation&amp;#39;).getLocalPart() == &amp;#39;retrieve&amp;#39;&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;filtering-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;filtering-router&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #0000A0"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;storage&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;expression-filter&lt;/span&gt; &lt;span style="color: #0000A0"&gt;evaluator=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;groovy&amp;quot;&lt;/span&gt; &lt;br /&gt;                        &lt;span style="color: #0000A0"&gt;expression=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;message.getProperty(&amp;#39;cxf_operation&amp;#39;).getLocalPart() == &amp;#39;store&amp;#39;&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;filtering-router&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;/model&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;To test the processing flow out, we can script a couple of components with &lt;a href="http://groovy.codehaus.org/"&gt;Groovy&lt;/a&gt;.  A couple of println's will help us pick out that the operations were invoked when viewing the Mule log.  With a scripting component, we'll have access to a variable &lt;b&gt;payload&lt;/b&gt;, which in each operation will contain the web parameters (a &lt;b&gt;boxId&lt;/b&gt; in both cases) as defined in the &lt;b&gt;Warehouse interface&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #0000A0"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;retrievalService&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #0000A0"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;retrieval&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:component&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:script&lt;/span&gt; &lt;span style="color: #0000A0"&gt;engine=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;groovy&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                println &amp;quot;Operation: retrieve; Box ID: $payload&amp;quot;&lt;br /&gt;                new Box(payload)&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:script&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:component&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #0000A0"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;storageService&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #0000A0"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;storage&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:component&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:script&lt;/span&gt; &lt;span style="color: #0000A0"&gt;engine=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;groovy&amp;quot;&lt;/span&gt;&lt;span style="color: #660099; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                println &amp;quot;Operation: store; Box ID: $payload&amp;quot;&lt;br /&gt;                new Receipt(payload)&lt;br /&gt;            &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:script&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;script:component&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #660099; font-weight: bold"&gt;&amp;lt;service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The store operation returns an instance of a &lt;b&gt;Box&lt;/b&gt; and the retrieve operation returns a &lt;b&gt;Receipt&lt;/b&gt;.  As both services are invoked synchronously, the result of the component invocations will be returned to the calling &lt;b&gt;Warehouse&lt;/b&gt; service, which will pass them through to the client.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-4153311320241467776?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/4153311320241467776/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/04/mule-cxf-multiple-operations-and-groovy.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4153311320241467776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4153311320241467776'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/04/mule-cxf-multiple-operations-and-groovy.html' title='Mule &amp; CXF: Multiple Operations and Groovy Components'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-5723579855097493074</id><published>2010-03-29T08:29:00.000-07:00</published><updated>2010-03-29T08:30:18.740-07:00</updated><title type='text'>XMLUnit: Simpler Asserts</title><content type='html'>&lt;p&gt;&lt;a href="http://prystash.blogspot.com/2008/07/xml-unit-testing-with-groovy-and.html"&gt;Quite a while ago&lt;/a&gt;, I wrote about using &lt;a href="http://xmlunit.sourceforge.net/"&gt;XMLUnit&lt;/a&gt; in my unit tests to compare XML results.&lt;/p&gt;&lt;p&gt;The method used to create &lt;b&gt;Diffs&lt;/b&gt; has been useful, but the project offers a simpler way to compare XML:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #008000; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;static&lt;/span&gt; org&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;custommonkey&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;xmlunit&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;XMLAssert&lt;/span&gt;&lt;span style="color: #666666"&gt;.&lt;/span&gt;&lt;span style="color: #7D9029"&gt;assertXMLEqual&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000FF; font-weight: bold"&gt;AssertXmlEqualTest&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Test&lt;/span&gt; &lt;span style="color: #B00040"&gt;void&lt;/span&gt; test_xml_is_similar&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        def expected &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;hi&amp;gt;bye&amp;lt;/hi&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;        def actual &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;hi&amp;gt;bye&amp;lt;/hi&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;        assertXMLEqual expected&lt;span style="color: #666666"&gt;,&lt;/span&gt; actual&lt;br /&gt;    &lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #666666"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;The test passes.  If I change the value of &lt;b&gt;actual&lt;/b&gt; to something different, say:&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;    def actual &lt;span style="color: #666666"&gt;=&lt;/span&gt; &lt;span style="color: #BA2121"&gt;&amp;quot;&amp;lt;hi&amp;gt;goodbye&amp;lt;/hi&amp;gt;&amp;quot;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The test fails as the XML documents are no longer similar:&lt;pre&gt;junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff&lt;br /&gt;[different] Expected text value 'bye' but was 'goodbye' - &lt;br /&gt;  comparing &amp;lt;hi ...&amp;gt;bye&amp;lt;hi&amp;gt; at /hi[1]/text()[1] to &amp;lt;hi ...&amp;gt;goodbye&amp;lt;hi&amp;gt; at /hi[1]/text()[1]&lt;/pre&gt;Since I have no reason in this simple case to be inspecting the &lt;b&gt;Diff&lt;/b&gt; object created by &lt;b&gt;XMLUnit&lt;/b&gt; as described in the previous posting, this method should serve as a nice substitute.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-5723579855097493074?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/5723579855097493074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/03/xmlunit-simpler-asserts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5723579855097493074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/5723579855097493074'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/03/xmlunit-simpler-asserts.html' title='XMLUnit: Simpler Asserts'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-4431191392024739134</id><published>2010-03-07T12:00:00.001-08:00</published><updated>2010-03-08T08:19:11.872-08:00</updated><title type='text'>Mule: Service Composition with Dependencies</title><content type='html'>&lt;p&gt;&lt;a href="http://prystash.blogspot.com/2010/02/mule-example-request-chaining-and.html"&gt;Last time around&lt;/a&gt;, I did a little work with service composition in hopes to come up with an approach I could use in a real-world project.  The approach I came up with last time appeared to fit the bill, until another requirement was added: The response from the first service would be needed to make the request to the second.&lt;/p&gt;&lt;p&gt;The new requirement definitely forced me to rethink my previous approach, as I could no longer simply send the intial request off to be routed to the two services that needed to be consumed, aggregating the responses for a request to the business logic.&lt;/p&gt;&lt;p&gt;For this example, I'll try to come up with a similar problem for a service to solve that follows a similar processing flow:  The goal of this new service, given an order number, is to respond with a report about the customer containing their information and order history.&lt;/p&gt;&lt;p&gt;In order to accomplish this task, this new service will need to make use of two remote services:&lt;ol&gt;&lt;li&gt;A customer service: Returns the information for a customer who placed a particular order.&lt;/li&gt;&lt;li&gt;An order history service: Returns the order history for a particular customer.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;The information contained in both service responses is used in the business logic contained within this composite service to produce a report, let's say to relate the ordering of certain products to different demographic profiles.&lt;/p&gt;&lt;p&gt;Now at this point, one might say: "Why can't the order history service return the customer information its provided in its request in its response?".&lt;/p&gt;&lt;p&gt;A good question, but some would argue that a service should not respond with data the caller already has or knows.  My job here would be a lot of easier if the order history service could be changed to do just that, I could just pass the order history response to my business logic and not need to keep the customer service response around. On the other hand, the order history service should just be charged with returning the information it was requested to produce and not concern itself with how its response will ultimately be used, as long as that response is usable and correct.  Not all clients might make use of the customer information returned in the order history response, and we should not request that type of overhead.&lt;/p&gt;&lt;p&gt;Ok, enough talking and more Mule-ing.  For now I can start with the two things I know for certain, I will need to make requests to both the customer service and order history service, and they need to happen in order, so let's start there.  For the sake of brevity, I'll omit any needed transformations to make the processing paths easier to follow.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;orderService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;jms:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;queue=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;order.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;history.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The incoming request will be routed to a VM queue where a listening internal service will route it to the remote customer service:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customerService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.queue&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;pass-through-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;jms:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;queue=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.service.request.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/pass-through-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The response from the customer service will be provided to the order history service, which is defined similarly.&lt;/p&gt;&lt;p&gt;Now the next problem here is getting both responses to the same place so a request for my business logic can be created and the request carried out.  From here, I'll add the internal service containing the business logic:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;reportService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;responses.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;custom-inbound-router&lt;/span&gt; &lt;span style="color: #660099"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;ResponseAggregator&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;component&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;spring-object&lt;/span&gt; &lt;span style="color: #660099"&gt;bean=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;reportingComponent&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/component&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The inbound router is an extension of Mule's &lt;a href="http://www.mulesoft.org/docs/site/2.2.1/apidocs/org/mule/routing/inbound/AbstractEventAggregator.html"&gt;AbstractEventAggregator&lt;/a&gt; class that waits for a group of 2 responses (to avoid a context switch I'll omit the implementation details for now).  It's job is to take both service responses and create a request for the &lt;b&gt;reportingComponent&lt;/b&gt;.  Now that we have that defined, how do we get the service responses to its VM queue?&lt;/p&gt;&lt;p&gt;Getting the order history response to the queue is easy, we just add another outbound endpoint to the chaining router:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;orderService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;jms:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;queue=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;order.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;history.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;responses.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Now we need to get the customer service response to the order history service &lt;i&gt;and&lt;/i&gt; to the same &lt;b&gt;response.queue&lt;/b&gt;.  A "customer response routing service" might work here.  This new service would be added as an endpoint in the chaining router between the routings to the customer and history services.  This new internal service needs to route its request to the VM queue for the report service and return the request to the chaining router:&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customerRoutingService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.routing.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;multicasting-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;responses.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;async.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/multicasting-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;async.queue&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;single-async-reply-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #408080; font-style: italic"&gt;&amp;lt;!-- we want to return the message that maps to the correlation ID found on the original request --&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;expression-message-info-mapping&lt;/span&gt; &lt;span style="color: #660099"&gt;messageIdExpression=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;#[header:JMSCorrelationID]&amp;quot;&lt;/span&gt;&lt;br /&gt;                                             &lt;span style="color: #660099"&gt;correlationIdExpression=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;#[header:JMSCorrelationID]&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/single-async-reply-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Unlike the other internal services that respond synchronously, this service is asynchronous.  To the caller, the chaining router, the request will have been handled synchronously, following the &lt;a href="http://www.mulesoft.org/display/MULE2USER/Mule+Messaging+Styles#MuleMessagingStyles-AsyncRequestResponse"&gt;Async Request Response style&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The request received (the customer information), will be sent to the desired VM queue and to the &lt;b&gt;async.queue&lt;/b&gt; where a reply router will return the customer information response it recevied on its inbound endpoint to the chaining router.&lt;/p&gt;&lt;p&gt;Now we place the routing to the &lt;b&gt;customerRoutingService&lt;/b&gt; into the chain:&lt;br /&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;orderService&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;jms:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;queue=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;order.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;customer.routing.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;history.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;responses.queue&amp;quot;&lt;/span&gt; &lt;span style="color: #660099"&gt;synchronous=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Since the last endpoint in the chain will be the second message placed on &lt;b&gt;responses.queue&lt;/b&gt;, the inbound router for the &lt;b&gt;reportService&lt;/b&gt; will be invoked, in turn invoking the component.  The response from the component will be returned to the chain, completing it.  The chaining router will then return the response to the caller of the &lt;b&gt;orderService&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;To sum up the processing flow:&lt;ol&gt;&lt;li&gt;A request is sent to the &lt;b&gt;order service&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;The request is forwarded to the remote &lt;b&gt;customer service&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;The customer service response is sent to the &lt;b&gt;customer response routing service&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;The routing service sends the request to the &lt;b&gt;responses queue&lt;/b&gt; (event #1) and to an async reply queue.&lt;/li&gt;&lt;li&gt;A listener on the async reply queue returns the customer service response to the chaining router&lt;/li&gt;&lt;li&gt;The customer service response is forwarded to the remote &lt;b&gt;order history service&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;The order history response is sent to the &lt;b&gt;responses queue&lt;/b&gt; (event #2).&lt;/li&gt;&lt;li&gt;The aggreagation router listening to the &lt;b&gt;responses queue&lt;/b&gt;, having both events, is invoked, creating the reporting request.&lt;/li&gt;&lt;li&gt;The &lt;b&gt;reporting component&lt;/b&gt; is invoked creating the service response.&lt;/li&gt;&lt;li&gt;The response is returned to the chaining router, completing the chain.&lt;/li&gt;&lt;li&gt;The response is returned to the caller.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;This could one of several approaches to the stated problem, alternatives are welcome!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8779314896221625511-4431191392024739134?l=prystash.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://prystash.blogspot.com/feeds/4431191392024739134/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://prystash.blogspot.com/2010/03/mule-service-composition-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4431191392024739134'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8779314896221625511/posts/default/4431191392024739134'/><link rel='alternate' type='text/html' href='http://prystash.blogspot.com/2010/03/mule-service-composition-with.html' title='Mule: Service Composition with Dependencies'/><author><name>John Prystash</name><uri>http://www.blogger.com/profile/16707064951271867131</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://4.bp.blogspot.com/_-vY4TuPFyCo/SuN167NHqaI/AAAAAAAAAAM/tnnko5_jVWo/S220/DSCF7097.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8779314896221625511.post-973846622463688711</id><published>2010-02-13T09:34:00.000-08:00</published><updated>2010-02-13T09:39:54.052-08:00</updated><title type='text'>Mule Example: Request Chaining and Response Aggregation</title><content type='html'>&lt;p&gt;With a better understanding of the processing options available in &lt;a href="http://www.mulesoft.org/display/COMMUNITY/Home"&gt;Mule&lt;/a&gt; after reading &lt;a href="http://www.manning.com/dossot/"&gt;Mule in Action&lt;/a&gt;, I found a place where some of the routing and aggregation examples would be of help in a current project.  I came up with a prototype that models the messaging flow I was interested in before proceeding with work on my real-world project.&lt;p&gt;&lt;p&gt;The processing model I wanted to prototype involved:&lt;br /&gt;  &lt;ol&gt;&lt;li&gt;A message is received on a inbound endpoint.&lt;/li&gt;&lt;li&gt;The message is transformed and forwarded to each of two services.&lt;/li&gt;&lt;li&gt;The responses of each service is collected and used to create a request for the business logic.&lt;/li&gt;&lt;li&gt;The business logic is invoked and the response returned to the caller.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;In my real-world project, I would of course need to consider the transformation of messages to meet the expected request formats by each service and the transports, &lt;b&gt;JMS&lt;/b&gt; in my case, by which the services are invoked.  In order to keep this example manageable, I decided to use simple components and the VM transport.  This means the stand-ins for the services I want to invoke are defined in the same model as the service handling the business concerns.&lt;/p&gt;&lt;p&gt;In this example, I use the stock &lt;b&gt;log-component&lt;/b&gt; element which provides an implementation of &lt;i&gt;org.mule.api.component.simple.LogService&lt;/i&gt;.  This component simply logs what is received on the inbound endpoint.  If no component is declared, the inbound message is implicitly bridged to the outbound definition.  This approach would fit just fine here for some of the services defined in the model, but I want to log the messages so we can more easily track the message flow.&lt;/p&gt;&lt;p&gt;Originally this example used &lt;b&gt;echo-components&lt;/b&gt;, hence the use of "echo" instead of "log" throughout, but all &lt;i&gt;log-components&lt;/i&gt; could be substitued with an &lt;i&gt;echo-component&lt;/i&gt; to produce the same results with respect to the final response message.&lt;/p&gt;&lt;p&gt;Below is one possible implementation of the logical layout described above:&lt;br /&gt;  &lt;ol&gt;&lt;li&gt;A file is read of the file endpoint, directory &lt;i&gt;/tmp/input&lt;/i&gt;.&lt;li&gt;The contents of the file is logged and are sent to an outbound chaining router.&lt;/li&gt;&lt;li&gt;The chaining router first routes to the &lt;b&gt;echo-router&lt;/b&gt; service by a VM endpoint.&lt;/li&gt;&lt;li&gt;The request is multi-cast to the &lt;b&gt;echo-one&lt;/b&gt; and &lt;b&gt;echo-two&lt;/b&gt; services.&lt;/li&gt;&lt;li&gt;Each response is sent to the &lt;b&gt;echo-response&lt;/b&gt; endpoint where there are aggregated and returned to the chaining router.&lt;/li&gt;&lt;li&gt;The chaining router forwards the aggreagated response to the &lt;b&gt;main-logic&lt;/b&gt; endpoint where the business logic is invoked.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;p&gt;At the start of processing a message is received by the &lt;b&gt;echo-start&lt;/b&gt; service:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-start&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;file:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;/tmp/input&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-requests&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;main-request&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The file contents are first sent to the &lt;b&gt;echo-requests&lt;/b&gt; endpoint. The goal here is to have the service listening to the endpoint invoke the other two response and send the aggreage response back, where the chaining router would forward it to the &lt;b&gt;main-request&lt;/b&gt; endpoint where the business logic of the real-world service would accept it.&lt;/p&gt;&lt;p&gt;The &lt;b&gt;echo-router&lt;/b&gt; service receives the request on the &lt;b&gt;echo-requests&lt;/b&gt; endpoint, where the request is sent to the endpoint for each service we want to invoke:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;multicasting-router&lt;/span&gt; &lt;span style="color: #660099"&gt;enableCorrelation=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;ALWAYS&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-one-request&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-two-request&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;reply-to&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-responses&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/multicasting-router&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As the target services are invoked asynchronously, the &lt;b&gt;enableCorrelation&lt;/b&gt; attribute is needed so any response can be related to the request.  Additionally, we need to tell the router where to send the responses from each service with the &lt;b&gt;reply-to&lt;/b&gt; element.&lt;/p&gt;&lt;p&gt;The responses for the "echo" services (the implemenations of which will be provided later) as sent to the &lt;b&gt;echo-responses&lt;/b&gt; endpoint where a custom aggregator is listening:&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-responses&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;custom-async-reply-router&lt;/span&gt; &lt;span style="color: #660099"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;prystasj.mule.EchoResponseAggregator&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The response from the aggregator is given back to the chaining-router and forwarded to the &lt;b&gt;main-request&lt;/b&gt; endpoint where it is simply logged.  Below is the entire model.  Since it's rather lengthy, I promise to descibe the custom aggregator and an example invocation soon after.&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;model&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echoAggregationModel&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-start&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;file:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;/tmp/input&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-requests&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;main-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/chaining-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-router&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-requests&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;multicasting-router&lt;/span&gt; &lt;span style="color: #660099"&gt;enableCorrelation=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;ALWAYS&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-one-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:outbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-two-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;reply-to&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-responses&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/multicasting-router&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-responses&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;custom-async-reply-router&lt;/span&gt; &lt;span style="color: #660099"&gt;class=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;prystasj.mule.EchoResponseAggregator&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/async-reply&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;main-logic&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;path=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;main-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-one&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-one-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;service&lt;/span&gt; &lt;span style="color: #660099"&gt;name=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;echo-two&amp;quot;&lt;/span&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;vm:inbound-endpoint&lt;/span&gt; &lt;span style="color: #660099"&gt;address=&lt;/span&gt;&lt;span style="color: #BA2121"&gt;&amp;quot;vm://echo-two-request&amp;quot;&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;log-component&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;&amp;lt;/model&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The custom aggregator simply concatenates the responses it sees.  If the original request file contained "HELLO", the response from the aggregator would be "HELLOHELLO" since the responses from the service that are invoked are simply echoes of what they've received.&lt;p&gt;&lt;p&gt;The custom aggregator extends the &lt;i&gt;org.mule.routing.response.ResponseCorrelationAggregator&lt;/i&gt; class since correlation was enabled to map the responses from the echo services to the originating request.&lt;/p&gt;&lt;p&gt;Here is the code for the aggregator, for brevity, I've ommitted any guards and checks:&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #0000A0; font-weight: bold"&gt;public&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;class&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;EchoResponseAggregator&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;extends&lt;/span&gt; ResponseCorrelationAggregator &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #AA22FF"&gt;@Override&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000A0; font-weight: bold"&gt;protected&lt;/span&gt; EventCorrelatorCallback &lt;span style="color: #0000A0"&gt;getCorrelatorCallback&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000A0; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000A0"&gt;CollectionCorrelatorCallback&lt;/span&gt;&lt;span style="color: #666666"&gt;()&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000A0; font-weight: bold"&gt;public&lt;/span&gt; MuleMessage &lt;span style="color: #0000A0"&gt;aggregateEvents&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;EventGroup events&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;throws&lt;/span&gt; AggregationException &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #0000A0; font-weight: bold"&gt;try&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #0000A0; font-weight: bold"&gt;return&lt;/span&gt; &lt;span style="color: #0000A0"&gt;aggregateResponsesFromEvents&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;events&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: #666666"&gt;}&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;catch&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;Exception e&lt;span style="color: #666666"&gt;)&lt;/span&gt; &lt;span style="color: #666666"&gt;{&lt;/span&gt;&lt;br /&gt;                    &lt;span style="color: #0000A0; font-weight: bold"&gt;throw&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;new&lt;/span&gt; &lt;span style="color: #0000A0"&gt;AggregationException&lt;/span&gt;&lt;span style="color: #666666"&gt;(&lt;/span&gt;events&lt;span style="color: #666666"&gt;,&lt;/span&gt; &lt;span style="color: #0000A0; font-weight: bold"&gt;null&lt;/span&gt; &lt;span style="color: #666666"&gt;,&lt;/span&gt;e&lt;span style="color: #666666"&gt;);&lt;/span&gt;&lt;br /&gt;                &lt;span styl
