The Starter, the Architect, the Debugger and the Finisher — which one are you?

I liked this post so much I couldn’t help writing a post about it.

http://jacquesmattheij.com/The+Starter+the+Architect+the+Debugger+and+the+Finisher

–So, which one are you? I think I can perform all of them but not so sure about the “Finisher” ๐Ÿ™‚ . I tend to get bored and move to the next Starter/Debugger role. Writing code is much more fun than finishing up that last piece of user doc, although I know the user doc is much more important than the code itself. But I’m just a mere mortal.

My thoughts on upfront architecture and iterative development

Long ago, I was attending this training program on Software design and architecture. A very respected Architect was talking about upfront design. He was trying to illustrate the fact that upfront design was very critical and you should always design upfront keeping performance & extensibility in mind. He was giving analogy with the foundation of building which goes like this:

“If you build a five story building, you will never be able to extend it by another 5 story. So you should always prepare for worst”

… At that time, I agreed completely with him. After all, you can’t really extend a building that doesn’t have enough foundation for it, right !

About 5 years later and a couple of completely failed and successful project experience under my belt, I beg to differ. I have seen so many analogy with building houses to developing software! Is it because of Christopher Alexander and his influence in “Design Patterns”? Now a days, I find many of these analogy to be irrelevant, Because, “Software Development is not the same thing as Developing high rise Buildings! Period. “.

Have you ever been in a situation where you did foundation for a 10 story Building and after finishing the first two floor, you realize its not a House you want to develop, its an Airplane that is actually required ! Or even better, after developing a 10 story building, no one wants to live in that area?

Unless you are working on projects like “Facebook-Chat” where you know on the first day of release, you are going to get a million hit, It is always best to take the simple approach first. This is why I love Agile and Lean process. You do only what is necessary and when its necessary. In agile, we are always prepared to refactor, re-evaluate architecture, scrap the existing foundation if it doesn’t work.

Dan North on Agile & Architecture.

I know he has a natural Charisma and superb witty humor that makes him a nice presenter but thats not the reason I like him so much. I have never had so much common thoughts with another developer / manager about software development.

Here are my two most fav presentation by Dan North , if you are interested in Full Software Development Lifecycle, Agile , Architecture, you are going to love it (or hate it if you have been practicing waterfall for last 20 years):

1 . Simplicity, The Way of the Unusual Architect (Have you heard about ‘YAK Shaving’ ? if not, you must see this!!! I insist!)

2. Keeping Agile Agile

You can find many more presentation here:

http://www.infoq.com/author/Dan-North

Did I mention they are not just boring tech presentation, you will laugh out loud a few times…have fun ๐Ÿ™‚

Android for Java Developer

I have been developing small applications on android platform for last 4 months or so. What started as a hobby / plaything has become an important part of my day Job now! When I look back, I can see I have worked with almost all major components of android: Async work, Services, Wake locks, GPS, Alarm, REST etc.

And here is what I am trying to say “ITS REALLY EASY & FUN !!!”. Infact its so much fun that you wouldn’t even realize you have read 400 pages of books / 100s of articles without even knowing it! It is also one of my most favorite frameworks beside Spring where you get to see cool designs , great sample applications and lots of quality documentation.

Just grab the CommonsWare books and a 400$ android device(Optional, the emulator will do for the starters) and you are good to go!

Troubleshooting Tomcat deployment problem for jaxws-2.2 application : The Sherlock Holmes way

I just spent my last 3 hour trying to deploy my perfect JAXWS-2.2 application in tomcat 6.0.26. Again I felt so great and thanked God that I took programming as a profession. Sure you don’t get to solve mysterious murder cases like Sherlock Homes, but the things we do are not much different! Where else can you go to work, get a problem with a set of clues, judge / discard / follow each and every clue, follow the trails to come to identify the problem and fix it and get paid for it!

Background:

The application was running happily in Jetty ever since I wrote it three months ago but my client wants to run it in tomcat. It was a standard web application so I thought it will be a child’s play to deploy it in tomcat. Ah! As if I have learned nothing from history! Nothing is child’s play when it comes to deploy an application in a new container, no matter how standard it is! I have learned this lesson hard way from deploying application in tomcat, jetty, jboss, weblogic, glassfish (yeah..i’v been fortunate/unfortunate enough to play with them all) , yet I tend to forget.

Problem 1:
First time I tried to run the app, I ran into the ClassNotFoundException:


SEVERE: WSSERVLET11: failed to parse runtime descriptor: java.lang.NoClassDefFoundError: javax/xml/ws/soap/AddressingFeature$Responses
java.lang.NoClassDefFoundError: javax/xml/ws/soap/AddressingFeature$Responses
	at com.sun.xml.ws.policy.PolicyWSDLGeneratorExtension.loadConfigurators(PolicyWSDLGeneratorExtension.java:459)
	at com.sun.xml.ws.policy.PolicyWSDLGeneratorExtension.start(PolicyWSDLGeneratorExtension.java:111)

..........................

I remember this! This was due to the jaxws-2.2 libraries conflicting with jaxws-2.1 libraries shipped with jdk-1.6! But I had it fixed by placing the latest jaxb-api.jar & jaxws-api.jar into the JDK_HOME/jre/lib/endorsed folder!

Hmm…here is a clue…the library is setup properly in JDK , app is running fine with Jetty with the same JDK, but wouldn’t load in tomcat!! Who is the ‘Usual Suspect’ ? Of course the tomcat classloading framework! Its ignoring the ‘endorsed’ jars configured in JDK ! Ah, at this point I begin to miss my old complex Tomcat 5.5, there was this ‘endorsed’ directory in the ‘common/endorsed’ folder. Where is it in tomcat 6? Luckily I had some old trick under my belt…Quickly Added a ServletContextListener like this:

public class WebappLoadListener implements ServletContextListener {
	private static final Log log = LogFactory.getLog(WebappLoadListener.class);
	public void contextDestroyed(ServletContextEvent arg0) {
	}

	public void contextInitialized(ServletContextEvent arg0) {
		log.info("\n\n\n ENDORSED DIR: " +System.getProperty("java.endorsed.dirs"));		
	}
}

And the output was:

ENDORSED DIR:  /usr/local/development/servers/apache-tomcat-6.0.26/endorsed

How the hack am I supposed to know that, the directory is not even there! Come on tomcat guys, I understand you want to make things simple for the new tomcat adopters, but why screw us – the old 5.5 users!!! Created folder “/usr/local/development/servers/apache-tomcat-6.0.26/endorsed” and copied the latest jars (jaxb-api.jar jaxws-api.jar) in there, got rid of the ClassNotFoundException.

Problem 2:
As soon as I got rid of problem 1, I ran into this:

SEVERE: WSSERVLET11: failed to parse runtime descriptor: java.lang.NoSuchMethodError: com.sun.istack.logging.Logger.getLogger(Ljava/lang/Class;)Lcom/sun/istack/logging/Logger;
java.lang.NoSuchMethodError: com.sun.istack.logging.Logger.getLogger(Ljava/lang/Class;)Lcom/sun/istack/logging/Logger;
	at com.sun.xml.ws.api.config.management.policy.ManagementAssertion.<clinit>(ManagementAssertion.java:87)
	at com.sun.xml.ws.server.MonitorBase.createManagedObjectManager(MonitorBase.java:121)
	at com.sun.xml.ws.server.WSEndpointImpl.<init>(WSEndpointImpl.java:143)
	at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:232)
	at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:505)
	at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:253)
	at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:147)

Now this got my head spinning! All the clue I got from Google was pointing to ‘Problem 1’…I was also a little confused and headed that way, placed the ‘jaxb-api.jar jaxws-api.jar’ in various locations, with no luck ! Ok, time to give up that trail and think clear. So, it was not a ClassNotFoundException, the class is there, but the Method is not. Why do this sounds familier? Because I have seen it countless times! Its because of two version of same class staying in the classpath and the faulty one getting preference by Tomcat ClassLoader! If you know about tomcat classloading, you know that it creates a ClassLoader for each webapp and the jars in “WEB-INF/lib” are loaded first (by the child classloader). So I must have some jar in my dependency which should not be there…Hmm..how do I know which jar? Wait, I know a way, infact I even blogged about it HERE.

So quickly added this method in my WebappLoadListener:

public static String getClassLocation(Class clazz) {
		if(clazz == null) {
			return null;
		}

		try {
			ProtectionDomain protectionDomain = clazz.getProtectionDomain();
			CodeSource codeSource = protectionDomain.getCodeSource();
			URL location = codeSource.getLocation();
			return location.toString();
		} catch (Throwable e) {
			e.printStackTrace();
			return "Not found";
		}
	}

	public void contextInitialized(ServletContextEvent arg0) {		
		log.error("jar file: " + getClassLocation(com.sun.istack.logging.Logger.class));		
	}

OUTPUT:

ERROR WebappLoadListener:21 - jar file: file:/home/sajid/workspace_test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/jaxws-sample/WEB-INF/lib/istack-commons-runtime-1.1-SNAPSHOT.jar

I got you “Professor Moriarty” (in case you are not familiar with Sherlock Holmes, Professor Moriarty is the common villain)! How did you got in my LIB folder ! The very name “istack-commons-runtime-1.1-SNAPSHOT.jar” suggests that I should not bundle it in my app! I deleted the jar manually and ran the code. Ah! Sweet. At last my code deployed in tomcat and all my SOAP-UI tests(only Three to be honest) are passed! And where is the class loaded from now?

ERROR WebappLoadListener:21 - jar file: file:/home/sajid/workspace_test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/jaxws-sample/WEB-INF/lib/jaxb-impl-2.2.jar

See! The right class is supplied with the jaxb-impl Jar.

Now, back to the old question, how did istack-commons-runtime-1.1-SNAPSHOT.jar ended up in my library? Did a quick ‘Dependency Graph’ in eclipse

So I added an exclusion rule in the dependency of jaxws-rt:


		<dependency>
			<groupId>com.sun.xml.ws</groupId>
			<artifactId>jaxws-rt</artifactId>
			<version>2.2</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.istack</groupId>
					<artifactId>istack-commons-runtime</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

Ran all the tests and everything seemed to be OK. I feel so lucky that I spent time writing those SOAP-UI test cases (at the time it felt like complete waste of time)! Otherwise, I’d have to test the application manually to see if anything was broken by this.

Conclusion:
I see now why newbies like to adopt those exotic Ruby framework instead of Java. Imagine someone trying to build a webservice using JAX-WS. As if the api specs are not confusing enough, he has to go through all these pain to run the app! I feel sorry for the poor guy if he starts with tomcat, he would have no clue that its not his code’s fault that the app is not running. Luckily I started with Jetty, not because I was wise but because the client wanted it in Jetty.

But did I have fun going through all these pain? I sure did ๐Ÿ™‚ . Good to know that I still get a kick out of fixing these weird problems, no matter how many times I did it before.

My first take on Roo framework

I recently started playing with Spring Roo Framework. I had two main objective.

  • See if its a suitable replacement for “Appfuse” when I start a new project
  • Learn best practices for building spring application from the spring guys

At first I was ‘WOW’ ed by the dynamic code generation and when I saw the app running, but as soon as I “perform eclipse” and imported the code in my workspace, I was disheartened. It is a great effort from the spring team but I am not gonna like it.

Why you may ask? I have couple of reasons, but the most important one is , Spring tried to inject their cutting edge features, aspect-j notations into the generated source. I am not against aspects but when I have more “Aspect-J” codes than my regular codes, I become worried. Worried about how I am going to maintain it, is there good enough IDE for it, are the regular java developers who will be working on the codes going to like/learn the new AspectJ concepts? Again, I am against overuse of annotation. I am ok with Hibernate annotations or validation annotations or transactional annotations, but its too much for me when I write annotations on the controller and map my http requests against it.

I am a big fan of spring , not because it gives me magical transaction management or supercool Controller annotations but because it lets me dependency inject my POJO beans into one another. This is an awesome tool for any designer. But with the latest Roo effort from Spring, I am worried about the future of spring framework. It seems like they are focusing on Rapid Application Development too much.

Anyone who has worked on a product for a decent amount of time will soon realize that the initial development time of the code is only 30-40% of the total lifecycle of the code (or even less). You have to update the code, add twisted domain logic which is never predictable by any annotation driven framework, perform bug fix on the code and the list goes on. By adding all these “Magic” into the code, you are going to leave the new developer (who will eventually take over the code from you) in the wonderland.

I liked the Spring Roo’s shell though. And the JPA POJO generation is also cool..but thats about it. Why don’t the spring guys make a better PetClinic application instead of overdoing annotations?

My version of erlang ‘Ring’ problem

I have been reading ‘Erlang Programming‘ , like a good student, I decided to solve the ring exercise:

Here is the problem in my words:

Create a linked list of N Ring where each Ring is an erlang process. When you pass a message to the head Ring , it should propagate the message to theย  next Ring and that propagates to the next ring and so on. I added additional clauseย  — “Each ring should report its successful message delivery to the previous ring and the previous ring should print successful message delivery from its child rings.

Here is the code:

-module(ring).
-compile(export_all).


createRing(N) ->
    Self = self(),
    Head = spawn(fun() -> ring(N, Self) end),   
    Head.

ring(N, PreviousRing) when N > 0 ->
    Self = self(),
    io:format('~w #ring created PID: [~w] ListenerPid: [~w] ~n',[N, Self, PreviousRing]),
    NextRing = spawn(fun() ->
			     ring(N-1 , Self) end),
    loop(N ,NextRing, PreviousRing);

ring(N, PreviousRing) ->
    io:format('last ring reached! ~w ~n',[N]),
    NullRing = spawn(fun() -> 
			     receive
				 Any ->
				     io:format('dummy ring received ~w ~n', Any)
			     end
		     end),
    loop(N, NullRing, PreviousRing).



loop(N, NextRing, PreviousRing) ->
    receive
	{send_message, Message} ->
	    io:format('ring # ~w with pid: #~w received ~w ~n',[N, self(), Message]),
	    NextRing ! {send_message, Message},
	    PreviousRing ! {ok, self()},
	    loop(N, NextRing, PreviousRing);
	{ok, NextRing} ->
	    io:format('~w with pid# ~w successfully sent message ~n',[N-1, NextRing]),
	    loop(N, NextRing, PreviousRing);
	{quit} ->
	    io:format('quiting ring: ~w with Pid: ~w ~n',[N, self()])
    end.


sendMessage(Head, M) when M > 0 ->
    Head ! {send_message, M},
    sendMessage(Head, M - 1);
sendMessage(Head, M) ->
    io:format('sending last message ~n'),
    Head ! {send_message, M}.

I have had my share of multi-threaded programming in Java, thanks to the telephony applications I’v worked on. Just imagine the pain you would have to go through to implement this in Java / C++ !