Dec 7 2005

JSP Include Me

Sometime back I wrote Import Script, Import CSS, Import PHP where I showed how to import external files in PHP and JSP. At this time I want to elaborate a little more on importing external JSP files from another JSP. One way to import another JSP files to statically import it using the following construct:

<%@ include file="path/to/file.jsp" %>

For the best performance use the above method for importing JSP code from another JSP file. To dynamically include a JSP file into another one you can use the following:

<jsp:include page="path/to/file.jsp" />

Dec 5 2005

Jakarta BeanUtils PropertyUtils Nested Properties

I have mentioned the Jakarta BeanUtils project before (see Jakarta BeanUtils PropertyUtils). The PropertyUtils class of the BeanUtils project allows you to dynamically access a JavaBean property. PropertyUtils gives you access to mapped, indexed, and simple properties via get/setMappedProperty, get/setIndexedProperty, and get/setSimpleProperty.

PropertyUtils also provides a generic getProperty method which can be used to access any arbitrary combination of mapped, indexed, or simple properties. I’ve taken an example from the BeanUtils documentation. Instead of this:

String city = employee.getSubordinate(1)
    .getAddress("home")
    .getCity();

You can write:

String city = (String)Property.getProperty(
    employee, "subordinate[1].address(home).city");

Dec 4 2005

Beyond Kayaking

I just finished going through a class I run into the future of programming languages by reading Beyond Java by Bruce Tate. In Beyond Java, Bruce makes the point, repeatedly, that Java will not be the reigning programming language of choice for ever. Bruce ‘predict’ that developer will move to more consistent dynamic languages such as Ruby and simple frameworks such as Rails. The book is pretty much a treatise of why static strong-type languages like Java suck. I agree with Bruce that Java will eventually be replaced with something else, I mean that is just that nature of evolution but I also feel that this book was 100 pages to long and $25 overpriced. He just repeats what nay sayers have been saying for a long time. His main arguments against Java are that the language is not as productive as alternatives, that Java is not a complete OO language because of primitives, and that there is simply too much to learn to develop an interesting web app. Basically his argument is that Java is a strongly type static language and we ought to be looking into more dynamic alternatives. I agree with all his points. I started this blog because of all the things I need to know and remember in a typical day at work, everything from JUnit, Ant, XML, CruiseControl, Hibernate, Spring, libraries and on an on.

Bruce himself writes that “the whole premise of this book is arrogant beyond belief.” I agree with him there also but only because Beyond Java is priced $24.95, a book that repeats itself a little too much and provides no new ideas. There are way to many bandwagon technical writers these days and solid tutorials on Ruby on Rails that I feel that this book should have been at least 100 pages shorter and made freely available online. After reading Beyond Java, all I have to say is that I really hate popular programming writers. Let me save you $25 and tell you that Java will not last forever, that you should looking into dynamic languages such as Ruby and start developing web applications on Ruby on Rails or continuation servers such as Seaside. In a sense start programming using The Hype Framework.

I once heard a joke that a consultant will tell what books programmers are reading by looking at the code. Consultants will know you read this book, or any of Bruce’s other books for that matter, just by one comment.

// Programming is like Kayaking.

Dec 4 2005

XML Object Cereal

You think that Java’s serialization mechanism has problems? You want to store/serialize an object as XML? Well you can use Java’s XMLEncoder and XMLDecoder classes to serialize an object to XML and then read it back. Want to see some code? Well here it is:

// Encode/Serialize bean to XML
MyBean bean = new MyBean();
bean.setName("Bill Gates");
bean.setTitle("Chairman of the Board");
XMLEncoder encoder = new XMLEncoder(
	new FileOutputStream(filePathStr)
);
encoder.writeObject(bean);
encoder.close();

In the above code block you instantiated a JavaBean and set the name and title properties. To save the object instance you write out the object using the writeObject method of the XMLEncoder class. How simple is that?

To read in an object from an XML file, you use the XMLDecoder. Here is some sample code:

// Read in an object from an XML file
XMLDecoder decoder = new XMLDecoder(
	new FileInputStream(filePathStr)
);
MyBean bean = (MyBean)decoder.readObject();
decoder.close();

Dec 3 2005

The Hype Framework

In the words of Public Enemy, don’t believe the hype. Technology is full of jargon and full of hype. The current hyped up terms are full-stack, web framework, Model 2, Web2.0, and Ajax. Every language under the sun has a web Model 2 based fully-stack framework these days. I am coming out with what I call the Hype Framework. The Hype Framework has everything you can dream off in whatever language you want but you have to implement it yourself.

On a more serious note here is a list of web frameworks that have received some attention, in no particular order. In the Java space you have Struts, WebWork, Spring, Wicket, Tapestry, and tons more. In Python CherryPy, Quixote, Twisted, WebWare and Zope come to mind. And of course, Ruby has Rails.

As a software engineer, I feel that every developer should know at least two languages, for example Java and Ruby (Java and C# count as one language. C# is the bizarro version of Java), and at least one of the above frameworks.


Nov 29 2005

Class HighLite Throwable

The first thing a programmer learns about exceptions is how to ignore them. Then a programmer learns that it is always best to throw them up the stack to the caller. And if you are the caller you can always print the stack trace to get a glimpse of what line of code is possibly at fault. The Throwable class is the base class for all exceptions, and errors for that matter. If you ever had to do anything interesting with Java you must had caught or thrown an exception.

The two methods from the Throwable class that most developers are familiar with are the printStackTrace and getMessage. The information that is printed out when you call printStackTrace is also available to you with the getStrackTrace method. The getStrackTrace method returns an array of StackTraceElement objects. The StackTraceElement class has getter methods for the class name, method name, file name, and line number to access the information of the call stack trace.

With the information made available to you by the Throwable class you see the caller hierarchy. You can trace the callers all the way back to the main method of the class that launched an application. You have access to this information by just creating a Throwable object, no need to throw it.

Throwable t = new Throwable();
StackTraceElement[] stack = t.getStackTrace();
System.out.println(stack[0].getLineNumber());

With this information you can allow or disallow certain call hierarchy or certain classes from calling a method a specific method. You can print the line number and class name in a debugging statement, just what Log4j does. The Throwable class provides access to metadata of your class environment at runtime. A note of warning, creating a Throwable seems like a very expensive call so make sure you know what you are doing.