Sunday, December 14, 2008

Closures are out of Java 7

Well it appears we will are going to have longer to wait for Closures in Java.

A status of Java 7 was given at Devoxx. Hamlet D’Arcy has a good summary here.

As mentioned, it seems that Closures are out. Personally I am a huge fan of closures and would love to be able to use them instead of the ugly, anonymous interface , pretend-its-almost-as-good workarounds one has to do in Java today. But even I have to admit that I understand the dangers and the downside in adding Closures to the language (see Generics)

JSR 294 (Modularization) is in. We will need to wait and see how close that ends up to OSGi or not.

The other interesting tidbit was the current time frame for Java 7 is Q1 2010.

Monday, December 1, 2008

Sharing a URI Context Between Wars in Tomcat

Today’s segment goes to some Tomcat configuration issues. Specifically, on how Tomcat sets the base of the URI which you can use to access your War based application,

In general, Tomcat takes the name of your war as the base of the URI. So if you have a war called app1.war (with a file called page1.jsp in the root) and you drop that war into the Tomcat webapps directory, you can then access your jsp at:

http://localhost:8080/app1/page1.jsp

If we have another war called app2.war (again with our favorite page1.jsp in the root), we can put that war in Tomcat and then access the page at

http://localhost:8080/app2/page1.jsp

But lets say we want the URI access to both wars to start with the same URI base. For example, we want app1.war to be accessible at the app1 url but we want the other war to be accessible at some URI under the app1 root (say app1/app2/page1.jsp)

This is actually something we actually needed to support as we refactored some of our application and for backward compatibility couldn’t change the access URIs.

In Tomcat this seemed hard to do. Our application ran in JBoss, so we were initially able to solve the problem by wrapping our app2.war into an ear and having our application.xml look like this:

<application>
    <display-name>App2 Stuff</display-name>
    <module>
        <web>
          <web-uri>app2.war</web-uri>
          <context-root>/app1/app2</context-root>
        </web>
    </module>
</application>

This is an ok solution if you are in a container like JBoss. But, if you aren’t, and while we are using JBoss, we want to move our application into a standalone Tomcat, this won’t work. So we needed to find a better solution.

It turns out that it’s not that hard though there seems to be very little documentation about it. 

The trick is the character #.

All you need to do is rename the war from app2.war to app1#app2.war

Tomcat then maps the war into the URI we want:

http://localhost:8080/app1/app2/page1.jsp

In some of the documentation and by fooling around with the Tomcat admin application, there seems to be other ways in Tomcat to do this with various xml files, but I couldn’t get them to work and was starting to give up hope.

Then we found the magic #. You may not need this often but when you do, it’s good to know about it.