June 28, 2007

Learning from the source

Filed under: Software — Chetan @ 8:27 pm

Learning is an important part in life of Software developer. With fast changing technology one has to keep oneself updated of current happenings. When I took this field as my carrier around two years ago I did not had any formal education in computer science. For me programming was a hobby since college days. Luckily the company I joined had a great belief in open source technology and I got a first hand experience with them. With that started my learning.

The open source project are a treasure mine for any body to learn how to do things and write code. How to follow best practices and avoid errors. I have gone through code of many open source project and each time I got to learn something from them. Here I would mention few of the projects that I learned from which may help new commers in Java to start with

  1. Spring – This is a fantastic piece of code. It was the second open source project (after Struts) that I used and explored (and still exploring !!). I would recommend any newbie to go through Spring code base. What it taught me was invaluable…
    • Writing comments – Its code is highly organized and documented. The javadocs are complete and you get to know how to document the facts. And really it improves readability. Remember that code is written once and read much more.
    • Organizing packages – How to structure your code. Here is excellent talk on this aspect by Juergen Hoeller.
    • Template Pattern – Spring uses Template Pattern heavily for all Transaction,JDBC handling. Wait what is a pattern !!. I saw the first reference to that in Spring doc and then I realized there usefulness and later I studied many more
    • Proxy pattern and AOP – Initially Txn management looked a magic to me. I always wondered how it gets done transparently. Again the code answered my query and I came across two great concepts AOP and Proxy pattern. It further taught me what a good framework is.
    • Dependency Injection – Frankly speaking this was strange for me for the first time. I had no experience of working with EJB’s till now so had no clue of what service locator pattern is. And why the code cannot be tested easily without it. It was a bit later I realized its importance when I worked with some older code and found how difficult it is to test your code without getting the whole application up
    • Test cases – Spring test cases taught me how to write true test cases. Those test which have real assert statements and not just to increase the coverage. How to take advantage of DI to its fullest.
    • Thread Locals – How the Spring know about transaction status when you call a method!! Where the state is maintained. Thread local can provide a very neat solution for such kind of problems where state has to be maintained across method calls
  2. Hibernate– This one was tough one but given the complexity of orm you cannot blame it also. Though code wise its not of that good quality but solution wise its cool. By clearity I mean comments and documentation.I always wondered how it generates those queries and performs on demand loading of collections. From it I learned about
    • CGLIB and ASM libraries – The magic behind the power of Hibernate lies in runtime code genration. Going through its code I came across this aspect of Java. And that forced me to understand about what a bytecode is. I always knew that Java is a bytecode based language but never understood what a bytecode means. Now these libraries can generate class bytecodes at runtime. So they are a powerful tool to solve few classes of problem
    • ANTLR – The HQL language that is used with Hibernate has a parser for it. Again I realized that parser can be used for such thing also. Till now I thought they were only meant to do something with the code only.
    • Hibernate in Action – If someone has to understand Hibernate this is a must. It would teach you much more beyond Hibernate and explains what ORM tries to solve. What are transactions and what are domain objects.
    • Cache – Hibernate can cache the domain objects transparently. And thus I understood the importance of cache and there use
  3. ACEGI– I came across this project while working on a security framework. It uses Spring configuration heavily and followed same practices a s Spring. But what i learned from it was how to design your code. Whats the use of Interfaces and how your design should be open to extension and not to modification. It reminds me of building blocks where you assemble your structure by assembling small pieces. Similarly it provides you all the ingredients and its upto you to assemble them. If you do not like a particular part replace it WITHOUT modifying the framework code.Initially it may seem overwhelming but once you get past the original learning curve you would get the big picture. Security is a complex domain and with a solution like this you can easily tackle any of your product requirement
  4. JDK – This is a real big mine and has lot more to offer.And With the code available out there you cannot ask for more. I have not seen the code for most but the one I got hooked into was the Concurrent package. For me threads and concurrency were like advanced concepts referred in last section of any Java book. And I never mustered enough courage to understand them. I read about the Concuurent package in some blog and then explored this alien area for the first time. Luckily I got to know of a book called Java Concurrency in Practice . It cleared many of my doubts in this area but still few remained. But the code for concurrent package cleared most of them Locks,Buffers ,Non blocking data structures etc. You might not use them but understanding how they work by seeing the code really helps. Further the documentation for this package is excellent and explains clearly what to expect and how to use.

The list can go on and on as there is no end to the learning. I tried to list few of the code bases I explored. For any person with few years of experience these things might look obvious but for a new person and especially from non cs background they are the initial steps. Java land is a big one and its churning out things at a fast rate. Some of the open source stuff would amaze you with what they can do. For any new person starting may seem a difficult thing at first. But slowly going through such projects he can understand how things work. With frameworks abstracting out most such things it becomes more important to know such things. As till they work fine you do not have a problem but when they went wrong it would be you who has to solve the issue.

There are many projects and technologies which are on my todo list and I am not getting time to invest. I would list few as a reminder for me

  • Terracotta – It can share a normal POJO across multiple jvm and that to without code change. Power of AOP and a innovative way to manage the delta in object changes. It remain on top of my list
  • Grizzly – Use NIO for non blocking connections. It can significantly increase the scalibilty of any multi threaded application involved in network IO. NIO is still a enigma to me. My concepts are still not clear on non blocking and blocking operations

So from where do people learn in Java land??


Maven2 Hibernate Plugin and Spring

Filed under: Java — Chetan @ 2:43 pm

Continuing my explorations on Maven I had a requirement to generate the database schema using Hibernate tool feature. And maven had a plugin readymade for this. The maven plugin expects the hibernate configuration through the config file but we were using Spring’s LocalSessionFactoryBean to maintain the Hibernate configuration.

Stuck with that I downloaded the source which is scattered over multiple sub modules. Browsing through that I saw few annotations like
@component role=”org.codehaus.mojo.hibernate3.configuration.ComponentConfiguration” over some attributes. Now that was something new and I had no idea what it was meant for. Searching about it I found ( should have known) that Maven internally uses Plexus which is an IOC container similar to Spring.

That gave me the only hope of using the plugin without modifying it and not wasting much time. The plexus container works on the concept of Role. Let me explain that a bit with maven plugin. The plugin uses an interface ComponentConfiguration for retrieval of the Hibernate configuration from various sources like config file,annotation etc. Thus in theory I can plugin my own implementation which gets the config from Spring container. Now the question was how to inject it.

The Plexus container works on the concept of Role. Have a look at this 5 min tutorial at there site. Here is the interface for the ComponentConfiguration

public interface ComponentConfiguration {
    String ROLE = ComponentConfiguration.class.getName();

    Configuration getConfiguration( ExporterMojo exporterMojo )
        throws MojoExecutionException;

    String getName();

The ROLE field acts as a key for the container.The mapping of implementation to the role is done through an xml located at META-INF/plexus/components.xml inside the jar. So here was my mapping


Next comes the questions how the plugin would pick a particular implementation. Here the role-hint comes. The HibernateExporterMojo which all the mojo’s extend has a field

     * @component role="org.codehaus.mojo.hibernate3.configuration.ComponentConfiguration"
     * @noinspection MismatchedQueryAndUpdateOfCollection
    private List componentConfigurations = new ArrayList();

So when Maven creates the plugin class then Plexus populates componentConfigurations with the implementations of ComponentConfiguration. And thus our implementation also gets injected.Now only task left is the final wiring of all these things through pom.xml


Here though implementation property we tell the plugin to use my implementation for getting Configuration. Then through dependencies I add my class’s jar to plugin classpath. The hibernate jar was required as plugin was picking some wrong jar and causing problems.Thats all that was required.

Execute the plugin through mvn hibernate3:hbm2ddl and get the ddl script generated for the schema.

Create a free website or blog at