Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, April 20, 2007

From C to Java to Ruby

I was having this casual conversation with a colleague about Ruby who isn’t using Ruby and I was selling him the elegance of Ruby and all that.
I said – “Remember the feeling when you moved from C development to Java? It felt like you have shed several pounds of weight attached to your shoes and you can now walk light. It is the same Deja Vu when you start to use Ruby coming from Java world.”

Then later when I was at my desk I thought of a better analogy.

  • Working with C was like using an Axe, cutting away at a tree trunk. Powerful but tiring and you run the risk of missing the tree and getting your toe.

  • Working with Java was like throwing away the Axe but getting a nice hack saw. It was controlled and safe. A little slow but easy to work with.

  • But working with saw can be boring monotonous repetitive work. So come in Ruby. Ruby is like a Swiss army knife. Elegant, beautiful, small, doesn’t require you to wrestle with it, can be different things at different times but as the runtimes of today, much slower than Java. You may not be able to cut a tree but can carve beautiful motifs out of it.

Thursday, April 5, 2007

What is XML good for anyway?

XML has had a fairly successful run for almost 10 years now, shot into prominence by its “configuration” usage by Java platforms.
Sadly for XML that 10 year ride is showing signs of slowing down.

I have always disliked XML in most of its incarnations and I am extremely happy that people are now (finally) showing it the door from places where it should never even have found any foothold in the first place.

Let me recant some of what I said, XML does have some uses but for some very strange reasons it had greatness thrust upon it. Now that it is being relieved of its undeserved celebrity status, my prejudice is giving way to prudence.
So I thought of writing my honest thoughts about XML.

XML is great for hierarchical, repetitive-pattern data. It is both state and presentation clubbed into one document and that is what makes it unique. XML data is inherently de-normalized as against relational systems.


<employee>
<name>Nasir Khan</name>
<profession>programmer</profession>
<address>
<street> 235 Montgomery Street </street>
<city> San Francisco</city>
<country>USA</country>
</address>
<contact>
<email>myemail@mydomain.com</email>
<phone>
<home>408-111-1111</home>
<office>415-400-7000</office>
</phone>
</contact>
</employee>

<employee>
……


The above XML looks fairly standard but in a relational world this data would be constituted from several de-normalized relations (tables).

Well XMLs were never anyway designed for data storage so it’s a bad comparison. But unfortunately it was used like that in many situations.

I have tried to compile a list where XML is definitely NOT suited but unfortunately used pervasively, this is not a complete list but is based on my first hand experience.


  • Information Interchange: And yes this includes Web services and SOAP. They could have designed much simpler protocols for data exchange, markup just adds unnecessary clutter and wastes bandwidth. XML for data/information exchange is neither human readable nor machine friendly. “Extensible protocols” is an oxymoron, only when both client and server “understand” the protocol is when it is usable, so what is extensible about it? Go figure. The above data snippet (the record) could well be coded in a simple delimited string “Nasir Khan, programmer, 235 Montgomery street, San Francisco, USA, myemail@mydomain.com, 408-111-1111, 415-400-7000”.
    This is 115 characters as against 313 characters in XML form! If a machine is reading the string then believe me it does not care about angled brackets, it does what you tell it to do. If you tell it that the second record in the string is my profession it will just take my word for it.
    What about readability? Take a step back and ask someone who is not a programmer about which of these make sense to them. Even a school dropout will be able to make sense out of the delimited string, while most will cringe at the sight of that ungainly XML. The context (schema) is coded in human brains.
    I know you could argue – “how could you tell the difference between home and office phone”, well just prefix a “h:” or “w:” before the number and off you go.
    Bottomline - they could always have designed simpler text protocols which could map to object oriented systems (like CORBA IDLs or other schemes) for automatic generation of protocol handlers on automatons and save millions in bandwidth, time and eye sores (the pointy arrows really hurt).


  • Programming languages/DSLs: When I first saw constructs like


    <if>value</if>
    <then>
    …..
    <and>
    <or>
    ….
    </or>
    <or>
    …..
    </or>
    </and>

    I just asked “WHY?”
    I still do not understand why there are so many DSLs out there based on XML.


    <assign>
    <copy>
    <from variable="c1"/>
    <to variable="c2"/>
    </copy>
    </assign>

    This snippet above is an example of assignment in BPEL.This is equivalent to

    c2 = c1

    in any of the “normal” programming languages.
    Now if one were paid by the lines of code they write then it is a potential goldmine for developers but other than that it is a recipe for carpal tunnel syndrome.

    Even though “L” in XML stands for language, it is not a language in any sense of the word. It is a document format. People have tried to patch on language constructs like conditions, iteration constructs, recursion, assignment etc on it but it doesn’t work most of the time.
    Well theoretically you could write a full blown programming language in XML syntax. It will be utterly unreadable and make you see your chiropractor but what I think will be its nemesis is the fact that XML originally was designed to format data. There is no inherent concept of data types. Everything is a string, you could again argue that you will create tags for data types and use them to wrap your variables like <int>variable</int>, well sure but then how do you deal with collections, how would you create a tree data structure in that language?
    You will say that I can create a <tree> declaration and represent the actual elements in internal data structures. Good for you but you will then represent your program state in non-XML format while your program logic in XML!
    Remember XML is a data format structure; it will be like tacking wheels to your boat while trying to make your car float.

    I think the reason why people started writing DSLs in XML was that they wanted some dynamic evaluation to be done in static languages of the age like C++, Java etc. XML was popular at that time and seemed like a natural choice.

    In those days it went like –

    “Hey I am not on talking terms with my grandmother; she doesn’t talk to me at all!”

    “Have you tried XML? It might work. It’s hip”.

    That they could have done dynamic evaluation in hundreds of other ways got trampled under the XML gold rush.
    An embedded Java compiler, like the JSP processor is the best solution in most cases where you want such dynamic behavior. I am surprised why that pattern was just locked up in (immensely successful) web containers.


  • Build tools/Ant: I never liked Ant, that is not to say I did not use it. I did, as it was the only option to build Java projects for a long time. Anyone who has used Ant would agree that after your project has reached a critical mass the build files look clunky and un-navigable. In his article Martin Fowler describes the Rake build tool and describes the flaws that an Ant like XML based system would have, primarily because of the choice of language, namely XML. I do not want to repeat what he said in his article, so have a look at Martin Fowler's article on Rake


  • Configuration files: This is perhaps the most pervasive use of XML. I do not have too much problem with it except that the large configuration files are unreadable. The other problem is that since XML enforces a certain order, even for unrelated nodes, you have to be aware of the DTD or schema before you make any changes to the configuration. For most situations a simple Properties file or Java Preferences like access would have solved the problem nicely, particularly because the property names can very well be mapped to Java package scoped constants or variable for readability.

    e.g.

    com.mycompany.employee.name=”Nasir”
    com.mycompany.employee.phone=”415-111-1111”
    com.mycompany.threadmanager.numThreads=5
    com.mycompany.concurrency.policy=”Pessimistic”


    Is far more readable than

    <?xml version="1.0"?>
    <config-data>
    <employee>
    <name>Nasir</name>
    <phone>phone<phone>
    </employee>
    <thread-manager>

    <num-threads>5</num-threads>
    </thread-manager>

    <concurrency-policy>Pessimistic</concurrency-policy>
    </config-data>

    What is more is that in the XML case you need repetitive Java code to parse the XML through some expensive parser and maintain Java objects representing the configuration. On the other hand with Properties like access you have built in Java class java.util.Properties to manage the data with as simple an API as load() and save().

    It was for Java’s heavy reliance on fat XML configuration files that prompted the detractors to coin the term “XML sit ups”.

  • XSLT: The concept is pretty powerful. Have a base document and transform it according to the presentation needs. In fact this could be seen as a variation of the Builder Design pattern or Abstract Factory as the transformation could be based on the type of request or type of client etc.
    So what is wrong with it? It is the “X” in XSLT. Several years ago I wrote some XSLT scripts to convert XML to Java code, if you haven’t guessed what was I trying to do - I was converting a DSL in XML to plain (and simple) Java code for performance reasons. When I finished that project I had pulled a lot of my hair out, it was ridiculously difficult to do something simple. I added a todo note to rewrite that transformer in Java itself using a SAX parser rather than use XSLT.




So what is XML good for in real world? I would say a number of things, definitely not as many as it is used for but enough.


  • Hierarchical data representation and XPath: One thing that I instantly liked in the XML world was XPath expressions. It is a powerful mechanism that actually uses the XML representation in the most intuitive form. As I said in the beginning, the most important feature of a XML document is its hierarchical representation. Xpath leverages that by using a familiar “path” syntax.
    Even to some one who doesn’t know Xpath, an expression like

    //place/coordinates/latitude/text()

    will instantly make sense.
    There is a protocol called XCAP (XML Configuration Access Protocol) Jonathan Rosenberg's page on XCAP used in the SIP/SIMPLE world that very cleverly uses Xpath in conjunction with HTTP to access and modify hierarchical data.


  • GUI description: I think more than anything this is where XML truly excels. The most ubiquitous usage of XML as GUI description tool is of course HTML. If you have worked with building GUIs then you will immediately realize that GUI development is so much more complicated than “faceless” non UI code, there is not just behavioral aspect as any other program but also presentation aspects.
    XML based UI design frameworks take the pain out of UI development. Again this is possible because XML is hierarchical and presentation oriented. Widgets can be placed on the screen depending upon their position in hierarchy.

    <button-group name=”mygroup”>
    <radio-button name=”b1”, onSelect=”func1()>Male </radio-button>
    <radio-button name=”b2”, onSelect=”func2()>Female </radio-button>
    </button-group>

    Is so much more intuitive than

    JRadioButton maleButton = new JRadioButton(“Male”);
    JRadioButton femaleButton = new JRadioButton(“Female”);

    //Group the radio buttons.
    ButtonGroup group = new ButtonGroup();
    group.add(maleButton);
    group.add(femaleButton);

    //Register a listener for the radio buttons.
    maleButton.addActionListener(this);
    femaleButton.addActionListener(this);

    This is not to in any way belittle the excellent work done by Java UI team. Java APIs are great for portable UI code and very exhaustive too. To make these APIs more accessible to visually oriented developers there are a number of XML to Java projects underway. I think they hold a great deal of promise for Java UI development.
    http://www.java-source.net/open-source/xml-user-interface-toolkits

    There are a number of other XML based UI design languages, notable amongst them are XUL from Mozilla, MXML from Adobe, LzXML from OpenLazlo and UIML which has an open source Java binding http://sourceforge.net/projects/juiml.


  • Annotations/Sematic tagging: A lesser known but widely used feature of XML is tagging. How many times while replying an email message you have written your reply “inline” of the original message? The way you do it plain text is to enclose your replies in tags. Like I use may name as tags

    <nasir>Yes I will attend the meeting. </nasir>

    Now the resulting email response may not be a well-formed XML document but the notion of start and end tag is pretty powerful.
    On another occasion while compiling the specification ( JSR 289 EDR document) from a large number of smaller sub-documents I used simple markup tags. I inserted tags in the source documents and then had destination tags in the main documents where the text was pulled from the sources. Besides this there were a few more things like automatic linking and aliasing but overall it greatly simplified the document creation for me.



To summarize XML can be a great help in some situations but can be an excruciating pain in others, perhaps this is true for any technology but in this article I have focused on XML. Based upon my experience I can tell you to keep the following in mind while using XML in your projects.


  1. Checkout the alternatives like YAML, which is a document format http://www.yaml.org/ that is a very simple text based format and has object bindings in several languages. Instead of truckloads of XML and heavy parsers on two sides of web services world, you could easily have YAML going between a Java and C++ process with straightforward object binding on both sides.

  2. Think in terms of simple text protocols, look at existing ones like HTTP if you are writing a client server system rather than inventing an XML based protocol. If your data is hierarchical have a look at XCAP.

  3. See if your need for Java configuration can be satisfied by Java Properties, Preferences or simple YAML.

  4. If you are building complex Java projects have a look at JRake in place of Ant, which takes the pain out of build management process. Particularly if you are starting afresh and do not have Ant investment done already.

  5. If your data is hierarchical then XML representation may make sense, but don’t get overboard, instead use builder/transformer pattern to generate XML when needed but maintain data in a format that is best suited for your requirements.

  6. And last but not the least exercise good judgment in designing with XML, question established beliefs. Think objectively if XML is the best tool for this problem. Answer the questions around performance, readability, maintenance as your XML document grows, and if you have tooling to help you with your XML needs.

Tuesday, March 27, 2007

Dynamic Java

I originally wrote this article for Developer.com - www.developer.com/tech/article.php/600591

A class, as we understand it, is basically a "capability set," an entity type that agrees to abide by a contract of what it is capable of. Now, the capability of any class must be known before it can be used in a program. The capability is "published" in the form of method signatures of the class and also the "type" of accessible "fields."

In Java, this knowledge of the capabilities of the class being used in a program is enforced at compile time, javac checks and verifies this capability set very thoroughly. How is this achieved? Obviously javac has a mechanism to read and understand a class and find out the implication of the messages between objects. For example, there could be a method in a class, say Callee:

class Callee {
.
.
public void doIt () throws CantDoException {
}
....
}

And in a class, say Caller, you have an object of Callee and the method doIt() is called:

class Caller {
Callee callee;
.....
callee.doIt();
.....
}

Assuming that CantDoException is not derived from RuntimeException, the compiler will obviously complain that CantDoException should either be caught or declared in the throws clause of the method from within which the doIt() call is made. Here, javac is making use of the capability set and enforcing certain semantics.

The next logical question is: Can we find out the capability at runtime of a class whose capabilities were not known at compile time? A program that analyzes the capabilities of classes is referred to as being reflective. Java has a package, java.lang.reflect, that has classes that support this reflection, along with some classes in java.lang, the most notable of which is the java.lang.Class class. These classes support creation of programs such as debuggers, interpreters, object analyzers, WYSIWYG GUI tools and so on.

How many times while programming for applications other than those mentioned above do you need to use reflection? I reckon not much, but sometimes when you need to work on a class that was not known at compile time, reflection provides the answer.

The JavaBeans framework makes use of this reflection mechanism to actually plug in a bean to a container of beans or a beanbox.

Let me liven up the proceedings with an example. Suppose there is a great program that is running around the clock on a server and is collecting data about possible extraterrestrial life, it is continuously collecting data and cannot be brought down, suppose there is a small piece of an algorithm that plays a role in that program. Scientists find out a way to optimize that algorithm and thus save time, but the program cannot be brought down. If the early designers of the program were thoughtful enough, then there is a way to dynamically plug in the brand new algorithm.

Now, if the algorithm implemented an interface Algorithm

interface Algorithm {
public void process();
}

and if we have the reference to the algorithm as this interface type, then we could easily swap the Algorithm implementations after reading it at runtime, like:

Algorithm algo;
.
.
String className = ConsoleReader.readLine(); // read from console
algo = (Algorithm) ((Class.forName(className)).newInstance());

Let's say "algo" is the name of a variable of type Algorithm (interface) and this reference is used to call the method process(), now we can swap the implementations and make the "algo" reference point toward a new implementation of the Algorithm interface. Here, the String 'className' now contains the name of the new implementation class (which, say, you enter from the console). The forName(className) method of class "Class" ( Class.forName(String) ) loads and links this new implementation into the VM and returns an object of type "Class". The method newInstance()creates a new instance (i.e., an Object) of that class. Since our new class also implements the interface Algorithm, we can cast it to type Algorithm and assign it to reference "algo". So now the reference "algo" points toward the new implementation.

You can actually code separately (from a separate terminal window, compile and provide the name to our Star Trek program in DynamicLoad.java), and bingo, it gets into operation immediately. This by the way is oft repeated mechanism whenever there is a container and there are applications to be loaded or upgraded, like a Servlet or an EJB container.

Going back to our original discussion, is this loading really dynamic, in the sense that the new class really was not known at compile time? The answer is no. We imposed a restriction on the behavior by restricting our interaction through the Algorithm interface, which was known at compile time; in fact, we made use of polymorphism apart from dynamically loading the class. Even in the case of JavaBeans, while using introspection, we enforce a design pattern of getter and setter methods to find out the properties and their kind (read/write or read-only).

Using the class Class methods like getMethod(), getMethods(), getInterfaces(), getConstructors(), getField(), and so forth, we can peek inside a class and find this information at runtime. We can then "search" for the matching method by looking at the parameters and return the type each method has and, depending upon what we are interested in, invoke the method of our choice.

Suppose our process() method requires that the input should be of type InputParam and the return should be of type ReturnValue, both defined earlier, now a new programmer has coded a new algorithm that has the same signatures (it has to be, otherwise it will be of no use to us) but he or she has named the method myInnovation(), we can still search through his or her class and use the method:

Method m[];
Class c = Class.forName(classname);
m = c.getDeclaredMethods();
for(int i=0;i<m.length;i++) {
if(m[i].getReturnType().getName().equals("ReturnValue") && m[i].getParameterTypes().equals("InputParam") )
{
rtObj = (ReturnValue) (m[i].invoke(c.newInstance(), ipObj));


Now this is not compilable, because getParameterTypes()returns an array of Class[], and we have to look for index 0, but for simplicity's sake, this is okay, what we have done is that we can iterate over the declared methods of a class and look for the method that adheres to the signature of our choice, then we can instantiate an object of that class and invoke the method of our choice, all dynamically! This takes us a step further in runtime identification of the capabilities of a class.

As a matter of fact the new Dynamic Proxy API with JDK 1.3 makes use of a similar searching mechanism to arrive at the functionality of having a dynamic proxy. Let me explain this, the API allows us to create a Proxy object, which is told about certain interfaces that it has to handle; then in an application an instance of this Proxy object is created, and calls meant to be handled by implementations of these interfaces are made to this Proxy object. This Proxy object is also told about an InvocationHandler, which contains the actual delegate object to which the call is finally made. Sounds cryptic! Let me clarify this with an example. InvocationHandler is actually an interface that has a single method:

public Object invoke (Object proxy, Method m, Object[] args) throws Throwable

Here, the proxy is the reference to the Proxy object created. This invoke() method is called at runtime and is transparent to the implementor, as well as the client. Here's the example of the InvocationHandler implementation:

public class MyHandler implements java.lang.reflect.InvocationHandler {

// this is the delegate object, can be any user defined object
Object delegate;

public MyHandler (Object obj) {
this.delegate = obj ;
}

public Object invoke (Object proxy, Method m, Object[] args)
throws Throwable {
try {
// may actually invoke on the delegate here and also do something before or after

}
catch(InvocationTargetException e){
throw e.getTargetException();
}
catch(Exception e) {
throw e;
}
// return here
}

Now, assume that the delegate object implements three interfaces — A, B, and C — the dynamic proxy API enables you to create a Proxy object with a type of any of the interfaces. For example:


A a = (A) java.lang.reflect.Proxy.newInstance (ClassLoader cl, Class[] { A.class, B.class,C.class }, new MyHandler(delegate) );

Or the same can also be assigned to reference of type B or C. When the client says "
a.doSomethig()", assuming that doSomething()was a method of interface A, then the call is actually forwarded to the InvocationHandler instance, which in our case is MyHandler. Before that, a reference to the Method is obtained (method pointer!) by reflecting over all the interfaces provided to Proxy as an array in the newProxyInstance() method. This Method reference and the arguments to it are passed to the invoke() method of the handler, where you can actually call it on the delegate object or do whatever you please.
This, I presume, is the best we can do in getting to dynamic pluggability of classes at runtime; the underlying idea being that some form of contract or some design pattern has to be adhered to. Reflection APIs actually take us very close to this goal.