Declaring Your Logger - No Problem

I used to like the java specialists newsletter, most news used to be a bit challenging. Nowadays however the quality is lower. In the latest news, the author proposes to use the StackTrace to get the class name, in order to declare a Logger independently of any explicit reference to the class name.

While this is a clever hack, it still requires some code to be duplicated in every class, compared to Aspect/IoC approach. But was there really a problem with declaring loggers the usual way in the first place?

With modern IDEs, most people just create a template for the logger declaration line. Also, renaming MyClass to MyClass2 will rename MyClass.class to MyClass2.class automatically. Here is my template for Eclipse to which I assigned the short name log4j:

private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

Very Quick Django Trial -> Bye Bye Django

I tried a bit django for one very simple thing, where django is not of much use, but it was a way to try it with something else than examples on the site.

Even if django templates features seem very good and very convenient when reading the docs (for example the extends keyword), I found out that the template system is very annoying. Firstly one has to learn another templating system. Secondly, and much more importantly, the debug facilities for templates are close to non existent. I have a better memory using ruby on rails templates (which are not the best either).

Java templates, based on Velocity, JSPs or JSF are much nicer to debug. PHP (which I consider an ugly language that can unfortunately often gets the job done) used to be crap at debugging, but with eclipse plugin phpeclipse it is much better these days.

I think templates play a very important role in web application development, and I am surprised that a framework with so many good ideas fails in reality (at least for me) because debugging templates is a pain. Hopefully it will become better one day, but until then bye bye Django.

A Proof That JavaScript Sucks.

Google developed GWT.

Why would GWT be so well acclaimed if JavaScript was a good language. When you talk about GWT to someone (a developer preferably), the first reaction is often

great I don’t need to do any JavaScript

I recently discovered another similar open source project, ZK. The first thing you can read on the website is

ZK is an open source Ajax Web framework that enables rich user interface for web applications with no JavaScript and little programming.

If you compare that with how JRuby is (or Jython used to be) trendy, you will agree there must be a problem with JavaScript.

JavaScript is not bad at everything, for very simple scripting, it is quite decent, the recent inclusion of Rhino in the JDK 1.6 is useful anywhere you need simple dynamic behavior. But most developers obviously don’t want to deal with it.

Building With Ant + Maven Antlib?

I discovered a new baby in the build world. Some open source projects use a combination of ant and maven. I thought you could vaguely use ant from maven. But I did not know of Maven Antlib that allows you to use maven2 from Ant. Why would you use Maven2 from Ant?

  • because Maven2 requires to adapt your project to its standard hierarchy, or to have excellent Maven2 skills to create your own archetype for each existing project.
  • because everybody knows Ant, but Ant does not manage dependencies well. Maven2 Antlib can be used just for that: managing dependencies.

The Hibernate Network Proxy, A Dumb Idea.

A friend of mine had a good idea for a Java project named “hibernate proxy”. I renamed it “hibernate network proxy” because hibernate proxies are already a well known concept among hibernate users (it allows lazy loading through a Java Proxy class).

The hibernate network proxy, is a way to use hibernate remotely, that is to proxy hibernate calls through TCP (or even HTTP) to another machine, that will execute the session.saveOrUpdate or the session.load, etc. calls and return the corresponding objects, serialize them and deserialize them for use on the client. As most projects in my company involves several distributed services, this idea could have been potentially very interesting, allowing hibernate use in several services that would interact with a common service in charge of the db. It made me think about other standard patterns:

  • This is not very different from the classical EJB session facade architecture, except we don’t expose application functionalities, only hibernate stuff. In fact we don’t want to expose application functionalities since they are not shared between services.
  • This is very similar to Entity EJBs used without facade, remotely, the first kind of design we heard about when EJBs came out. This would actually be a good fit for doing this kind of stuff, amazingly. I never thought remote entity EJBs were of any real use. Unfortunately for our projects, we already have a messaging infrastructure, and I am not sure how feasible it is to change of EJB transport protocol. Seeing the complexity of something like IIOP, it looks like a dead end.
  • This is a kind of Hibernate Second Level Cache, a cache with remote capabilities. The cache would proxy the request to the db service, and the db service would answer to that. This is a possible implementation that seems very flexible.
  • Use a regular TCP connection to the DB, and hibernate in a standard way. Ok, it does not use our internal message bus, but it is so simple and standard.

The last solution raises the most important question:

Why does it need to be proxied?

To go through some particular port? -> use a port forwarder. To go through HTTP? -> use HTTPTunnel. You want Java only? ->use JHttpTunnel,there is even JSch for ssh.

Workaround When Eclipse Fucks Off With CVS

Until recently, I always found Eclipse CVS support to be excellent. The synchronize view was really a big plus to do clean commits, without forgetting any file.

But I have encountered several problem with Eclipse 3.2+:

On one project, it keeps putting the build directory into CVS, and I can't find how to change that setting. Each time I open the project I have to change the CVS/Entries file.

On another, where I have lots of files in, Eclipse delete some of the files (checked in CVS) regularly, although not often. It won't retrieve them back again from CVS. It seems to be the same files each time. I have no clue why.

The diff sometimes tells me the whole file has changed while there is absolutely no change.

I could not make the merge work. It kept on complaining about some parameters.

And now the workaround for all those cases:
The good old CVS command line.

I never remember how do to a merge with the command line, so I each time go to google and read this merge example page. Here are the very simple steps if you are in a branch and want to merge:
  1. cvs update -A mypoem.txt (use head as local repository)
  2. cvs update -j release-1 (merge release-1 changes into local repository)
  3. cvs update mypoem.txt
  4. cvs commit mypoem.txt

Maven: Game Over, Try Again

I am writing only as a newbie on Maven2. I tried to use it several times on small projects, but each time I found out that the time involved to make it work with an existing project was too much a pain in the a**. When confronted to very simple hierarchies that are not the standard of the documentation, I was a bit lost on how to arrange things, except changing the whole hierarchy to adapt to Maven.

Also on small scale code, Maven requires you again to adapt your project to it and it will make your small code look like an important enterprise project. For example if you want to build a small test that can be executed as a server (with one particular class), as a client (with another class), or as a servlet, using one or two other common classes, Maven requires at least 3 source hierarchies (with src directory).

I think it is not only because I know ant (much) better that ant proved many times to be quite flexible, and I have never been on a project where switching from Makefile to ant was a problem. I am not an ant fan. I have a preference of code over XML that ends up here and there being like code, in a much less flexible way. However Ant, as I said, is flexible. One can always use the good old scripting languages (javascript, beanshell, etc.) to define new tasks, and therefore bypass any inflexibility of the original design.

Still Maven2 has plenty of very good stuff, especially for more normal projects. It provides versioning, central repository and dependency checking for you. It is probably a good idea to use it rather than bad ant scripts, or, even worse, old Makefiles. Also Maven2 hierarchy conventions are not stupid, it is just not fitted for quick and dirty stuff.

How JDepend Changed My Java Packaging

One important feature of Java language is the package keyword. It helps a lot in modularizing your code. But how exactly one should use it is not that clear.

I have been on projects where 100s of classes are in a same package and more often where you have packages for every 2 classes. Often, packages are chosen so as to split functionalities. But often as well, people are packages maniacs and create way too many of them, because they want to sort things out, not necessarily applying a consistent logic. For example, I am sure many of you have seen the “blahblah.exceptions” packages where you dump exceptions classes.

Without any precise intention for packages, you encounter quickly cyclic dependencies in your project, and your build system becomes overly complicated. Then you try to remove cyclic dependencies, not by using the package keyword better, but by building jars differently (=ugly jars) and “refactoring” a bit.

I fell myself in many of those traps. Fortunately I stumbled on my way on JDepend package analysis program. At first I wondered why JDepend calculated dependencies between Java packages and not between Jars, as in your project what matters is jar interdependencies. After using it regularly, first for the fun of trying not to have cyclic dependencies in my Java packages - the green light effect (I wanted my green light on my project), I understood much better its interest. I now make Java packages that are much more meaningful. They not only separate functionalities, but also create excellent modularity. Building a project is very easy, reusing parts of it is very clear. If one needs to split a jar in a project, it’s very clear how to split it (by Java packages). And I find that in the end my packages make much more sense.

Placing a class in the right package is most of the time not difficult, especially for experienced programmers. But now and then, with JDepend, I find mistakes. Correcting those mistakes regularly, when it’s just about one or two classes is quick and easy. Correcting them on a project where dependency analysis was never done is a nightmare.

Now I don’t really care about JDepend metrics about abstractness and instability so much, I did not find any good use of them. But JDepend (or a package dependencies checker) really is an essential program for good Java development.

You Know IOException? Think Again!

I was amazed today to find out that there was no constructor IOException(String, Throwable) or IOException(Throwable) in JDK1.4 and JDK1.5. It is finally in JDK1.6, I can’t believe it took Sun that much time to change that.

So the workaround is:

`IOException ioe = new IOException("message");`
`ioe.initCause(e);`
`throw ioe;`

It can also be written as:

`throw (IOException) new IOException("message").initCause(e);`

It is not a major problem, but still. We can all thank the guy who reported that as a bug to Sun in 2004.

Good Software Books - 2006 version

Here is an update of the most interesting software books I found interesting. I already made such a list in 2005:

  • Object Oriented Software Construction, 2nd Ed, by Bertrand Meyer. This made me understand why OOP is important, what is important in OOP and why it is still relevant. It contains lots of important guidelines you can apply to better design programs. One famous quote is "Real systems have no top". Code Complete (Microsoft Press) covers some of the same ideas as Meyer's book, and some more pragmatic issues that arise in software projects ("measure twice, cut once"). I also liked the chapter on "table driven design".
  • Concurrent Programming in Java, Second Edition, by Doug Lea: there is all you need to know about programming in a multithreaded environment in it. The chapter on synchronization should be a must read for every Java developer.
  • Design Patterns by the GoF: simply the best presentation of most common design patterns. I have a glance on it once in a while.
  • Artificial Intelligence through Prolog, by Neil C. Rowe (Prentice-Hall): if you don't remember much about Prolog, it's a good book. It details how Prolog interpreters work.
  • File Systems Forensic Analysis, by Brian Carrier (Addison Wesley): everything you need to know about disks and file systems, every trick to recover lost data. To go more in depth into file systems theory, you can read the free Practical File System Design with the Be File System from D. Giampaolo, the creator of BeFS.
  • Mind Hacks (O'Reilly): you probably already bought that one. Not about software, but excellent.
  • Practical Issues in Database Management by Fabian Pascal (Addison Wesley): short but concise book on relational database theory. F Pascal is of CJ Date school of thoughts, "null are evil". You don't need to read it if you have read any other similar book (by CJ Date for example).
  • Inside The Java Virtual Machine, by Bill Venners (McGraw-Hill): there are other books on the same subject. While this book has some bad reviews, I found it an easy read, and it explain well enough for me all the inner details of Java. I found "Programming for the Java Virtual Machine" not better, and with some bad example of a Prolog language for the JVM (1 full chapter for this to present a way too simple thing to be of any use).
  • Programming Jabber, by O'Reilly: I have read it 2 years ago, I enjoyed how they made you go through building a Jabber server.
  • Lucene In Action: the only book about Lucene, some of the info is useful to understand how it is designed.
  • Hibernate In Action: if you have to use hibernate, this is the book to read. It presents different ways of using it.
  • some Javascript+DHTML(+CSS) book: useful if you have none. They often have good tricks to solve the usual problems. In the AJAX days, it becomes useful again. I find O'Reilly's "Javascript - The Definitive Guide" good to have an API reference (as ebook), and "Javascript and DHTML Cookbook" has the most useful recipes.
  • Lean Software Development by Poppendieck (Addison Wesley): easy to read, good presentation of software dev management problems and pragmatic solutions. My favorite on the subject. Another good one but very specific is "Requirements Management" (MS Press).
  • UML Distilled by M Fowler: always useful to have if you need to draw UML diagrams.

Previous

Next