Archive for the ‘Java’ Category

TDD With JackRabbit

Published by Rob on June 29th, 2007 - in jackrabbit, Java

… tips on Apache JackRabbit configuration for Test Driven Development. 

As AJ mentioned, we are currently working with Apache JackRabbit for an internal project.  It has been an interesting voyage of discovery, with some very interesting stops along the way. One of the first steps was to build some tests around the JCR.  I started with some simple exploratory tests around the API.  These helped build my understanding of the JCR and how it works.

The next step was to begin really working out our persistence API with TDD.  As a part of this, I wanted to be able to have a predominantly memory based repository to work with.  This is possible to do, as per the attached memoryRepository.xml file.

The key is to use a org.apache.jackrabbit.core.fs.mem. MemoryFileSystem for all file system elements, and a org.apache.jackrabbit.core.persistence.mem. InMemPersistenceManager for all persistence manager elements.  Unfortunately, with the attached config the file system is still used to store Lucene search indexes.  This means that the file system is still used, and needs to be cleaned up in the tests.

The attached JcrRepositoryTestBase.java file has the setup and teardown code that handles the cleanup of the search index directories.  This combination of code combines to allow the 35 JCR tests that we have run in about 10 seconds, and gives 100% coverage of all non exception handling code1.

1 – i.e. not including code that does nothing but catch (Exception e) {throw Wrapper(e) }, which while is testable is not currently tested by us.

First Rule of Enterprise Java Development

Published by Rob on June 28th, 2007 - in jackrabbit, Java

The first rule of Enterprise Java Development is:

……

any unexpected behaviour is caused by a classpath problem

Todays instance of this rule is a particularly fun one, given the line of code that exposed the problem.

I am using Apache Jackrabbit  as an embedded JCR repository, mostly following the Jackrabbit embedded mode instructions. I saw mostly because the Jackrabbit website doesn't actually match the 1.3 released version of JackRabbit (this is most annoying when following the "First Hops", but that is a digression).

So I've got a nice little webapp with an embedded repository that is accessed via JNDI.  The repository is responsible for shutting itself down which it does via a shutdown hook (BindableRepositories do this).  Unfortunately the shutdown hook gets called after the web app class loader has started unloading, and things are beginning to cleanup.  The first symptom of this problem is a null pointer exception when logging a message12. After isolating that the log message (in org.apache.jackrabbit.core.RepositoryImpl.doShutdown) really was causing a NPE, I commented out he line of code, redeployed and confirmed the nasty problems, getting the following exception:

1
2
org.apache.catalina.loader.WebappClassLoader loadClass<br />
       INFO: Illegal access: this web application instance has been stopped already. Could not load org.apache.commons. collections.map.AbstractReferenceMap$ReferenceValuesIterator. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.

So while I am not yet in a position to report a fix this problem, I have been able to isolate it, and identify yet another instance of the first rule of enterprise development.

1 – When I first saw that this line of code was causing the problem, I thought that it would end badly — either the source code I had didn't match the binary or I was getting a null pointer exception while logging.

2 – This is particularly ironic given the raison d’être3 of SL4J is to remove problems like this.

3 – like my attempt at culture?4  

4 – sorry about the footnote overkill.

Example of a Good Open Source Project Website

Published by Rob on June 13th, 2007 - in Development, Java

Open Source projects are known for dodgy websites, and rubbish documentation. I was overjoyed to see a project that got it right (URL Rewrite Filter). There are a number of things that I really appreciated with this site, and in the hope that people might take notice, I’m going to list the good bits here.

The landing home page has a nice concise description of what the project is all about. When I get to the front page, I can read the overview of the project, and get an understanding of what problems it was created for, and the goals of it. The front page told me enough to want to see more, but didn’t overwhelm me. I got a good feeling from the front page.

The other nice thing about the front page is the navigation structure. The side navigation bar provides the links which I am most interested in. One click to get to a download link (which has a direct link to the files). One click to the source code page (with instructions on svn access via http and the commandline). One click to the documentation. One click to get to the community support.

The documentation is excellent, and provides a good baseline for what documentation of a project this size should look like.

There are good examples of what the configuration file looks like, annotated in a way that makes it easy to understand what the code is there for.

The documentation provides a good reference for how to do the configuration.

It is a great site and provides a good example of what most open source project websites should look like.

Pretty URLs for any J2EE framework

Published by Rob on June 13th, 2007 - in Development, Java

Making the browser location work like a command prompt is a good goal for any web application, and is possible for any J2EE web framework, even Struts 1.

To do this, having Rails style friendly urls is required. Traditionally J2EE frameworks and applications have not done a good job of providing friendly urls. It is easy to wander around the net and guess at what framework people used by the urls (e.g. anything .do is probably struts, .action’s are probably webwork etc.)

Of course one doesn’t have to be tied to this world. Pretty much anything is possible with Apache and mod-rewrite. This is handy when you are behind Apache, but that isn’t always the case. From time-to-time you may want to dot his completely within your J2EE application, making it independant of where you are running. The Servlet spec is powerful enough to make this possible, but for the lazy (like me) it is nice to know that someone has already done the hard work. The URL Rewrite library provides a filter which does the heavy lifting for you. There is a simple XML configuration file, which provides a rich language for customising your redirects using the Java Regex library, or a familiar directory traversal wildcard style syntax.

The library makes it possible to easily map urls like: products/1 to products.do?productId=1 or from: /australia/queensland/brisbane to: worldMap.jspa?country=australia&state=queensland&city=brisbane. There is a rich syntax for specifying the url rewrite conditions, as described in the comprehensive manual, and shown in the samples.

Automated Building and Releasing With Ant

Published by Rob on May 1st, 2007 - in Java

This blog post was initally started in January 2005.  I've learnt some more since then, and I'm including some of the lessons I've learnt from automation at Ephox and InvoicePlace in this post.

It is well documented that having your build automated is a good thing.

I would extend this, saying that  the automated deployment of applications is important too. The big benefits in my experience are repeatabilty (that is ensuring that deployments always happen in the same way), and consistency of deployments. Automating the process helps to fulfill the import goal of laziness as well.

Often getting this right can take a while. The hard work starts by analyzing the deployment process,  iteratively looking for bits of work that can be automated.

This can usually be done with a combination of custom ant tasks, external program calls, and console utilities. For all ant's wrinkles, it is a relatively useful tool, and there are a number of useful tasks available.

Here are some of the tasks that I have found useful in getting the build working.

The ant SQL task is great. It makes it possible to build/update databases, from simple flat files containing SQL statements.

The CVS task is good. In combination with Paegent from Putty, repositories requiring ssl access can be accessed easily as well.

IBM has a custom version of ant that can be used to deploy to WebSphere. I have found that with a combination of ant tasks, and tcl scripts any task that is required for deployment can be performed. This is best done in a build file that is seperate to the main build.xml, isolating the Websfear dependencies.  

A combination of ant (exec task), wget (command line http client), and phpmyadmin(php based admin tool for mysql databases) can be mangled together to interact with mysql databases in ways that aren't easily possible with the ant sql task (the sql exported by myphpadmin isn't compatible with the sql task).

The buildnumber task is useful. It gets the current build number from a properties file, makes it available as a property, then increments the property.

The moral of the story is that it is there is no good reason not to automate building and deployment.  It possible to do many great things with the large number of ant tasks available.  Writing your own isn't that hard, and calling out to external programs is easy as well.  Really it is hard to find any excuses for not automating a build.  The only excuse is time and effort, and the savings gained can almost certainly offset this.

Memory Constipation (not memory leaks)

Published by Rob on April 30th, 2007 - in Development, Java

This post is the first in a series of efforts to clean up my list of drafts.  This particular post is probably about a year old.  It is somewhat fitting that it is the first in my effort to clean out my drafts ;) .  Before making each post in this series I'll edit the post to clean it up, and add new comments in italics, or footnotes).

I often spend time talking about different things with my wife, Suzanne. We both like learning, and sharing from our different experiences (in fact I'd almost call Suzanne a compulsive learner). 

I was recently struggling with an application with memory leaks1. When explaining this to Suzanne (who is currently working in a hospital setting treating a lot of kids with enuresis and encopresis)2, she pointed out that it is probably more of a case of memory constipation.

Especially in an environment with Garbage collection. The issue is that the rubbish ain’t getting out of the system.

1 - That was two years ago, in a Swing fat client.  Two years later at Ephox we getting OutOfMemoryExceptions in Java Applets

2 - actually now she is a full time Mum, and doing some Enjo demos on the side.

 

Ruby File Generation

Published by Rob on April 9th, 2007 - in Development, Java

Join me on a journey through using ruby to generate java, learning how to use the built in ruby templating tool erb, and some of the rubyish techniques that erb leverages.

 Our adventure starts with a need to generate a large number of very simple java classes in order to have a nice simple test case for the boys at sun.  The requirements for the classes are:

  1. they are some how different
  2. it is easy to tell when each class is loaded by the class loader
  3. it is easy to tell when each class is instantiated

These requirements would be painful to meet (when creating 1000+ classes) even in the most powerful java IDE.  An automated technique for generating code.  In steps Ruby and erb.

A basic ruby script which has a simple template to base the files on, and creates the java classes.

1
require

1
&#39;erb&#39; #we are using the erb library.<br />

1
# create an erb template using the multiline string starting after EOF,

#

1
and finishing at the EOF below.

1
2
template=ERB.<strong>new</strong> &#60;&#60;<strong>EOF</strong><br />
       <strong>package</strong>
1
ephox;&#160;

1
public class

1
2
3
4
5
6
7
8
9
SimpleClass&#60;%=class_number%&#62; {<br />
       &#160;&#160;&#160;&#160;<strong>static</strong> {<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.<em>out</em>.println(&quot;static SimpleClass&#60;%=class_number%&#62;&quot;);<br />
       &#160;&#160;&#160;&#160;}<br />
       &#160;&#160;&#160;&#160;<strong>public</strong> SimpleClass&#60;%=class_number%&#62;() {<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>super</strong>();<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;System.<em>out</em>.println(&quot;init SimpleClass&#60;%=class_number%&#62;&quot;);<br />
       &#160;&#160;&#160;&#160;}<br />
       }
1
&#160;

1
EOF

1
<br />

1
2
3
5.times <strong>do</strong> |counter| # perform code in this loop five times, remembering the counter.<br />
       &#160;&#160;&#160;&#160;class_number=counter+1<br />
       &#160;&#160;&#160;&#160;File.open &quot;src/ephox/SimpleClass#{class_number}.java&quot; ,&#39;w&#39; <strong>do</strong> |file|

        

1
2
# open the file for writing.<br />
       &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;file.puts template.result(binding)

                   #merge the template into the file, passing in the current binding.

1
2
3
<br />
       &#160;&#160;&#160;&#160;<strong>end</strong><br />
       <strong>end</strong>

 

In looking back over the example, you can see some interesting rubyisms.  These are some of the spots where ruby is revealed. Note the following:

  • the use of closures (File.open, and 5.times),
  • everything really is an object (the method times being called on the object literal 5),
  • heredoc notation (<<EOF )
  • the ease of templating with erb (which keeps normal ruby syntax in the template),

The statement I made above about everything being an object is pushed to its limits when looking at the method call:

1
template.result(binding)

.  The binding method is available as a part of the Kernel, a mixin(providing methods for use) that is a part of Object, the mighty superclass of everything in Ruby.  This method will return a Binding object, which erb uses to get data to merge into the template.

The File.open line opens a file in write mode, overriding the contents of the file.  For more examples of how to do this, look at the PLEAC site, which has a good set of ruby file examples, and quite a complete set of ruby examples.  This is an excellent resource for learning a language.

In all, Ruby does a good job of working with files, and templating.  Erb is a great tool for templating, and forms the backbone of many well known ruby programs.

Migrating to FreeMarker

Published by Rob on March 29th, 2007 - in Ephox, Java

… or Adding the red button

AJ mentioned that we have automated the build and release process. After spending much of the morning hearing everyone talking about wanting to push the “big red button” for a release, and hearing how it was “too hard”, to update the html, I decided to go ahead and work out what needed to happen. What an interesting world I discovered.

The webapp in need of the red button is basically a servlet which calls out to the various parts of the world (our various build ). This works fine, but sometimes gets a bit hard to maintain (ok so some things are very hard to maintain). So (rather than doing a huge change), I figured that I could drop in a templating engine, and use the servlet to merge the template. I knew that Velocity is a bit old hat now, so went for a bit of a google, and (after a short diversion into another non-maintained option), I found FreeMarker. Having heard of it in the past, and knowing that it was the tool of choice for WebWork, I thought I’d give it a go.

My first test was to create a hello world web app from scratch.

I ended up with a very basic servlet that I’ve attached.

Basically it creates a configuration:

1
configuration = new Configuration();

tells the config that templates are found in the templates sub directory of the current class.

1
configuration.setClassForTemplateLoading(this.getClass(), &quot;templates&quot;);

retrieves the template:

 

1
template = configuration.getTemplate(&quot;start.html&quot;);

creates a map of data to merge with the template: 

1
Map freeMarkerData = new HashMap();

1
freeMarkerData.put(&quot;user&quot;, &quot;Mr Man&quot;);

1
&#160;and finally merges the data with the template:&#160;

1
&#160;template.process(freeMarkerData, response.getWriter());

The hello world spike done (at home), all I needed to do was repeat the process at work with the real servlet. Replacing the html from the magic servlet, one template at a time.

Of course the first template to be created on our deployment applet was the one with the Big Red button.

InvoicePlace.com

Published by Rob on March 22nd, 2007 - in Development, Java

I was pleasantly surprised to see a blog entry pop up in my feed reader mentioning subscription accounts for InvoicePlace. As I was one of the guys who helped write the code that is behind InvoicePlace (using a Java Web stack), I take pride in seeing it move closer to being a great success. InvoicePlace provides a great, simple, easy to use tool to help enter and keep track of invoices. It works really well for people and businesses send invoices, and want to be able to manage these electronically.

I've been sipping the InvoicePlace Champagne for quite a while now (it tastes much better than dog food), starting to use InvoicePlace to bill Growthbase before the name InvoicePlace had formed more than a glimmer in Scott's eye. I found InvoicePlace really useful, and expect that it will continue to grow and evolve in positive ways. Some of the features that I was able to exploit to help manage my invoicing needs in my development version were nice, and I expect that in due time they will be released and made available to the public.

I definitely have fond memories of my time working on the project, and the late nights spent adding functionality, and working hard at trying to push the test coverage and quality up. I look forward to following the continuing progress of InvoicePlace, and will use this site whenever I need to issue invoices.

Java tool to touch files (update the last modified date)

Published by Rob on March 8th, 2007 - in Development, Java

As the title implies, this utility updates a file (files) and sets the last modified date:

It’s free, and (being Java) it’s cross platform……

ok so there aren’t really many platforms apart from Windows that don’t have “touch” built in, but it is still kinda cool ;) .

© Rob@Rojotek