Monday, February 23, 2009

Ajax Men with TOOLS

GWT is headed by Bruce Johnson.
JQuery by John Resig.
Prototype by Sam Stephenson.
Script.aculo.us by Thomas Fuchs
Ajax by James Garret of Adaptive Path.
Dojo by Alex Russel

GWT 101

What is GWT?
A set of tools to write AJAX applications in JAVA.
GWT is an open source, cross-browser framework that allows developers to write AJAX applications in Java and get the bytecode compiled into JavaScript.

What makes GWT interesting is Java to JavaScript compiler?
GWT is more than just a Java-JavaScript Compiler.
Based on the code that you write in Java, it performs dead code elimination, code optimization etc straightening polymorphic calls, method call in lining and string interning. basically the features of what a "compiler" does.

Well why Java ?
Because Java is a strongly typed language unlike python and that is when you get benefit of the compiler optimizations. But then again Java is the chosen one, because of the availability of IDE's, debugging tools etc and not that its the only best language in the world. Google didn't choose Java because of a particular attachment to the language, but because of the abundance of tools out there available for Java. According to Rasmussen the final code you end up with will be faster and smaller than what you would write yourself.

GWT enables software development capabilities with AJAX. These are some of its advantages:
>>Java IDEs -- you can use any Java IDE or Notepad if you prefer
>>Quick editing, testing, debugging and refactor cycle -- the browser lets you debug code at Java level. You start your hosted browser in debug mode, edit your Java code, refresh your browser and you will see the changes straight away.
>>Unit testing -- GWTTestCase class is provided so that it can be integrated with JUnit
>>Reuse through jars -- Modules or jar files of one application can be referenced from another
>>Make the most of Object Oriented design principles
>>Javadoc -- GWT API documentation
>>Compile time errors -- With Eclipse the errors are fixed as you are typing code.

How to build the UI in GWT?
GWT preserves the principles of Web usability by: providing regular UI elements; enabling keyboard-only use and font size preferences; allowing the user to be in control of the browser; facilitating a fast start-up; and, allowing the user to feel like they are using a standard Web app, but with an enhanced experience.

The User Interface is created using a GWT widget library. The widgets got to be added to the panels. And its very similar to Swings. It follows the observer pattern to handle events. Widgets publish events which can be subscribed to, when the event occurs all the subscribers will be notified.

How to GWT applications talk to the server?
GWT RPC makes it easy for the client and server to pass Java objects back and forth over HTTP. When used properly, RPCs give you the opportunity to move all of your UI logic to the client, resulting in greatly improved performance, reduced bandwidth, reduced web server load, and a pleasantly fluid user experience

"Don't Click the Back Button"- The History
GWT's history API provides a means for you to access the browser's history stack and control what happens when the back or reload button is clicked. This way, even if you click the back button you can still return to the same state of the app.

Some aspects of the API are: the historyToken parameter is the current history state so each item in the history stack is called a token; HistoryListener is triggered when the back or forward button is clicked, you would implement this to get notified when there is a change to the browser history; you can create new history items on the stack -- for instance, History.newItem(token); hyperlink class provides a link to a different state of the app where it creates a new history item without reloading the page.

Internationalization
GWT provides a way for you to internationalize your applications and libraries. With the I18N package you can translate your apps into other languages.

To create localised messages, you need to implement the Messages interface and then add the corresponding localised properties files. .properties files store the translated strings.

Controlling the look of widgets with CSS

With GWT you can control the look of your widgets using CSS and keep the code and presentation separate. Every widget has a style name which is associated with a CSS rule.

To change the font size of all your buttons, as demonstrated here, you would do the following:

.gwt-Button { font-size: 150%; }

Optimised Permutations

GWT compiler constructs multiple compilations based on all possible permutations and records them in a file -- suffix.cache.html

The end-user only downloads one optimized compilation for their particular circumstances. For instance, a compilation for Firefox 3 and UK English. The performance is optimised, because the user is downloading only what is relevant to them.

JavaScript Native Interface (JSNI)

JSNI allows you to incorporate JavaScript into your Java code with the use of the native keyword. The JavaScript code in JSNI methods is encapsulated inside a comment block. It starts with /*-{ and ends with }-*/ and is inserted between the parameter list and the ending semicolon.

What's new in GWT 1.5?

Some of the features in the release candidate for 1.5 are:
  • Java 5 support (enums, annotations, autoboxing etc)
  • An improved compiler to make apps faster
  • Additions to the UI library such as widget animations
  • DOM API -- to make DOM development easier
  • Improvements to internationalization
  • Accessibility Support 
  • Improvements to JRE Emulation Library

Exceptions - To check or uncheck

To decide whether to throw a checked exception or an unchecked runtime exception, you must look at the abnormal condition you are signalling. If you are throwing an exception to indicate an improper use of your class, you are signalling a software bug. The class of exception you throw probably should descend from RuntimeException, which will make it UNCHECKED.
Otherwise, if you are throwing an exception to indicate not a software bug but an abnormal condition that client programmers should deal with every time they use your method, your exception should be CHECKED.

The Moody Thread States

Understanding the moods of threads is critical to programming with threads.



1. New state – After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.

2. Runnable (Ready-to-run) state – A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.

3. Running state – A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.

4. Dead state – A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.

5. Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.



A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enter directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.

1.Sleeping – On this state, the thread is still alive but it is not runnable, it might be return to runnable state later, if a particular event occurs. On this state a thread sleeps for a specified amount of time. You can use the method sleep( ) to stop the running state of a thread.

static void sleep(long millisecond) throws InterruptedException

2.Waiting for Notification – A thread waits for notification from another thread. The thread sends back to runnable state after sending notification from another thread.

final void wait(long timeout) throws InterruptedException
final void wait(long timeout, int nanos) throws InterruptedException
final void wait() throws InterruptedException

3.Blocked on I/O – The thread waits for completion of blocking operation. A thread can enter on this state because of waiting I/O resource. In that case the thread sends back to runnable state after availability of resources.

4.Blocked for joint completion – The thread can come on this state because of waiting the completion of another thread.

5.Blocked for lock acquisition – The thread can come on this state because of waiting to acquire the lock of an object.

The Cache in Hibernate!

Hibernate has two kinds of cache, which are the first-level cache and the second-level cache. The first-level cache aka "session cache", and the second-level cache aka "process-level cache".

The first-level cache is mandatory and can’t be turned off; However, the second-level cache in Hibernate is optional. Because the process-level cache caches objects across sessions and has process or cluster scope, in some situation, turning on the process-level cache can improve the performance of the application. In fact, before accessing the database to load an object, Hibernate will first look in the Session cache and then in the process-level cache.

The process-level cache is best used to store objects that change relatively infrequently, and it is set up in two steps. First, you have to decide which concurrency strategy to use. After that, you configure cache expiration and physical cache attribtes using the cache provider.

Hibernate supports a variety of caching strategies including:

read-only : which is suitable for objects which never changes. Use if for reference data only.
read-write : which is suitable for objects that are modified by the application.

To cache these classes in the process-level cache, we must use the <cache> element in the O/R mapping.

iBATIS, Hibernate, and JPA - Which one ?

iBATIS, Hibernate, and JPA are three different mechanisms for persisting data in a relational database, each one with its own advantages and limitations.

Before we delve into which one - lets think back on our criteria of choosing. Is it that you require complete control over SQL for your application, do you need to auto-generate SQL, or just want an easy-to-program complete ORM solution.

iBATIS does not provide a complete ORM solution, and does not provide any direct mapping of objects and relational models. However, iBATIS provides you with complete control over queries.
Hibernate provides a complete ORM solution, but offers you no control over the queries. Hibernate is very popular and a large and active community provides support for new users.

JPA also provides a complete ORM solution, and provides support for object-oriented programming features like inheritance and polymorphism, but its performance depends on the persistence provider.

A much more detailed analysis is always most welcome.

Best Lyrics and Best Original Score

Truly, Not only A.R Rahman's moment to cherish - In a time when copy, paste has become the default tool, almost second habit, we have to remind ourselves that it is innovation which drives passion - to excel and always originality is lauded.

Remarkable Stories on Technologies I have experienced...

From the beginning of days I started delving into application development, I always felt there is always an alternative. An alternative to paths that ultimately leads to an end ie. "The End Product". What it means to be adopting Java or .NET, what it means to be adopting DOJO or YUI. I have a lot of experiences to share along the lines of "Why a certain technology has been adopted looking at the specifics for the project at hand"... So stay tuned...