magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

Posts from the ‘Java’ category

I hope to get a rough performance comparison of an enterprise application using entity beans for persistance compared to using Hibernate soon, in the meantime I thought that a brief comparison of the development effort involved in using the two approaches might be in order. Comparisons are based on JBoss 3.2.2 running MySQL.
Continue reading…

I decided to spend my Sunday afternoon giving “Seraph”:http://opensource.atlassian.com/seraph/ a go. Seraph is “Atlassian’s”:http://www.atlassian.com/ opensource web application securty framework. Upon downloading I found that it depends on “OpenSymphony’s”:http://www.opensymphony.com/ (website tagline: “Quality Components”) “OSCore”:http://www.opensymphony.com/oscore/, and “OSUser”:http://www.opensymphony.com/osuser/, nether of which it includes in its downloads (despite the fact that specific version numbers are mentioned in the “dependencies”:http://opensource.atlassian.com/seraph/dependencies.html). No problem I think, I’ll just pop over to OpenSymphony to grab the latest OSCore and OSUser jars. Unfotunately you can’t. No download links, nothing. I eventually find in the OpenSymphony Wiki that the only way to actually get them is via CVS (and I only found this page via a Google search). Already I’m a little concerned – Seraph seems to be at a relatively early stage of development and is dependant on two apparently immature libraries (if all you provide is a CVS source download, you aren’t what I would call ready for prime time). Having already spent a little time at this I thought I should go one step further, so I fire up SmartCVS and download the repositories. Both OSUser and OSCore build easily enough, and not knowing much about either I decide I should look for some documentaion. OSUser contains little – the online API docs are broken, there does’t appear to be any docs in CVS, and the example app doesn’t build, and there is nothing like a quickstart guide (which was so helpful with Hibernate) so I have no way of knowing how the bloody things works.

Anyway trusting that the versions of OSUser and OSCore in CVS will work with Seraph, I’ll spend a little longer seeing if its worth going any further with it. I don’t hold out much hope…

Well, I managed to get “JBoss”:http://www.jboss.org/ and “MySQL”:http://www.mysql.org/ working thanks to a kind friend who dug his config files out. Getting “Hibernate”:http://www.hibernate.org/ and JBoss working together was fairly simple, although I hate the fact that the Hibernate libraries have to be stuck in JBoss’s own lib directory thanks to one of its infamous class loader problems that never seem to get fixed. A minor hiccup was when JBoss forced the MySQL driver into an auto commit mode which it doesn’t support (this is despite MySQL’s own driver defaulting to false), although passing in a parameter to the JDBC URL fixed that.

Throughout the whole process I’ve found Hibernate very user friendly – the error messages are informative and helpful, which is more than can be said for JBoss and Tomcat. Next up I’m going to create a sample object model with some one to many relationships with both Hibernate and as Entity beans, to get a rough idea of performance.

“Hibernate”:http://www.hibernate.org/, as you may or may not be aware is a persistence engine for Java. As such it is by no means unique – it joins the ranks of JDO, Java Serialisation and the persistence features of the EJB specification as another option in an already crowded market. Good overviews on Hibernate can be found at the “official site(Hibernate and Tomcat Quickstart)”:http://www.hibernate.org/hib_docs/examples/quickstart/html/quickstart.html and in an article over at “The ServerSide(The ServerSide – Introduction To Hibernate)”:http://www.theserverside.com/resources/article.jsp?l=Hibernate

I like several things about Hibernate. Firstly it seems a lot quicker to develop than EJBs are.The classes you want to persist don’t need stub classes, or special interfaces – they are just normal beans. So not only can you quickly create these classes using code generation tools already in most modern Ode’s, but the beans themselves are abstracted from the method of persistence being used. Hibernate even ships with a tool to generate these classes from your mapping files, which is handy when used as part of an Ant build. Next, the definition of relationships is fairly easy – as is implementing lazy loading. The fact that you have to keep a Hibernate session if you want to use lazy loading of relationships is a pain but understandable.

The mapping files (which define the relationships between beans to be persisted and the datasource) is very straightforward, and as another nice touch Hibernate can read your mapping files and automatically create and keep in sync the database tables – again a great feature as part of a development build and deployment.

The reduced development overhead compared to EJB development coupled with the apparent performance improvements over simple untuned EJB applications means that I may find myself using this more and more. This weekend I’ll try and get a Hibernate-JBoss install working to carry out some simple performance comparisons between an EJB-based persistence and Hibernate based persistence.

Finally, after much cursing, shouting, drinking and general annoyance, I have managed to deploy a sample “Hibernate”:http://www.hibernate.org/ application. I must point out here that the problems I had have nothing to do with Hibernate – the documentation is excellent, although their sample application could be packaged a little better (you know, to actually produce a WAR file). The problem I had was with “Tomcat”:http://jakarta.apache.org/tomcat/.

Up to this point I have been using “Jetty”:http://jakarta.apache.org/tomcat/, because it just WORKS. It doesn’t have fancy web-based admin consoles or more config files than you cna shake a stick at, you just put your WAR file in its webapps directory, and restart. At which point it deploys your webapp. Tomcat however doesn’t do that – oh no. Sometimes it ignores your WAR file. Othertimes it unpacks it, but if it does it won’t update this unpacked directory structure if the WAR file changes even if you restart Tomcat. So now my build process has to delete directory structures under Tomcat (I’m sorry, but I am not going to start dealing with unpacked WAR structures – that way madness lies). Oh, and guess what? When Tomcat has an error with a web.xml, it doesn’t tell you which webapp it found the file in! Isn’t that cool! “Now if Jetty was so good, why are you using Tomcat?” you may ask Well, this may be lazyness on my part but the example application had the tomcat config settings and I didn’t feel like working out the equivelent Jetty settings (why can’t these things be standardised?).

Up to then things had gone well – I got “MySQL”:http://www.mysql.com/ installed and running as a service quite quickly. After finally getting the Hibernate example working I had a quick look at getting Hibernate and JBoss working together, but hit the problem that I couldn’t find any documentation explaining how to get “JBoss”:http://www.jboss.org/ working with MySQL – that excersie can wait for another day. Anyway, expect a brief writeup on Hibernate tomorrow – I must say that from what I’ve seen already it looks excellent.

I’ve discovered a variety of solutions for providing rich UI controls for web-based applications of late:

* As mentioned before, the “Display Tag Library”:http://displaytag.sourceforge.net/ provides a JSP custom tag lib for producing CSS-styled tables with sorting, grouping, paging and various export formats. Its easy to use and works well, although could do with some updates to work better with the JSTL and JSP Expression Language.
* “Stuart Langridge’s”:http://www.kryogenix.org/ “DHTML sortable tables”:http://www.kryogenix.org/days/565.html provide a client-side solution to sortable tables – it doesn’t offer as many features as the Display Tag Library but is very easy to use and is great if you can’t rely on server side software, or even if you can but can’t use taglibs
* Via “Zeldman”:http://www.zeldman.com/daily/1003b.shtml#cssdropdowns, “Brainjar(Brainjar – Revenge of the Menu Bar Demo)”:http://www.brainjar.com/dhtml/menubar/demo2.html has some very good looking DHTML drop down menus
* Common Controls provide a set of commercial Tag libraries that integrate with struts to produce some very nice looking controls. The fact that they integrate with Struts is a big plus – one of the problems with the Display Tag Library is that it doesn’t integrate with struts too well. The big problem is that they want a per-server license AND developer licenses. Its not too expensive if you only have one developer and one server, but if you are deploying several servers (as some of my friends are) it can soon get quite expensive. If they offered a flat-fee rate I’d be more interested.
* The “struts menu”:http://struts-menu.sourceforge.net/ is an open source project that as you’d expect gives you nice menus, but isn’t tied to struts any more. Some of the menus it produced did cause problems under firebird however.

I’ve been looking into the topic of (business rule) engines for a while now. Frequently at work I find myself embedding business logic into my code – silly things such as ‘delivery takes and extra day if your in Cornwall’ for example due to time constraints and a lack of a decent framework get written directly into the code. I hate this approach – business rules can be far more fluid than the code itself. Business rule engines such as drools aim to separate these business rules from the code itself. There has been a lot of movement in this area recently – there are a number of commercial rule-based engines in addition to the opensource drools, including “ILOG JRules”:http://www.ilog.com/, “Haley Eclipse”:http://www.haley.com/, “Sandia Jess”:http://herzberg.ca.sandia.gov/jess and “CLIPS”:http://www.ghg.net/clips/CLIPS.html.
“JSR-94”:http://www.jcp.org/en/jsr/detail?id=94 details the API for a Java-based Rule Engine, and is currently at the Proposed Final Draft stage. Implementations of JSR-94 will be forthcoming – ILOG are in the expert group so its fair to assume they’ll have an implementation soon, and the people behind drools have also committed to provide an implementation in the near future.
Due to the other implementations being commercial, I’ve only really looked at drools, although its documentation is good (the “drools guide(Drools – Theory, Usage and Reference [PDF])”:http://drools.org/pdf/drools-guide.pdf gives a good overview not only of drools itself but of rule engines in general) but it seems fairly straightforward..

Whilst reading one of Hani’s “enjoyable rants”:http://www.jroller.com/page/fate/20031029#opensymphony_dirty_laundry over at “The Bile Blog”:http://www.jroller.com/page/fate, I can upon a reference to “Seraph(Atlassian – Seraph)”:http://opensource.atlassian.com/seraph/introduction.html, which at first glance seems to be one of those things that is destined to save me loads of time the next time I write a web app. For some reason, in nearly every web application I’ve been involved with (and there have been a few) I’ve ended up either writing the authentication and authorization code, or at least eneded up doing major work on it – for me its replaced the constant implementations of sortable tables for various companies Swing interfaces.
I’m going to be playing around with some changes to the “Display Tag Library”:http://displaytag.sourceforge.net/ at some point – I might combine it with a web application that uses Seraph to see how it hangs together. It should also be noted that Seraph and The Display Tag Library are the first projects I’ve seen that uses Maven and have gone to any effort to make the resulting sites look like something other than the default Maven L&F.