Tuesday, February 23, 2010

Open Source NoSQL Databases

The article lists open-source databases falls into the NoSQL category. NoSQL is another database paradigm where conventional relational approach and SQL query has no space, rather databases follows the semi-structured, graph based or key-value pair based storage approach for new age social and distributed application requirements.

Enjoy the article.

Saturday, February 20, 2010

Google Ambassadors: 45 Essential Google Links

Google has many well-known and some lesser-known products that everyone who uses Google should be familiar with. The following list has been prepared by Google Ambassadors, a group responsible to promote Google products to Internet users, an explanation about the group activities can be found here.

GWT client with REST

This article explains a small prototype i developed for a friend to show how REST services can be used can be used from the client code and avoid additional RPC call. The demo explains only two fundamental class structures to access the remote service.

RequestBuilder
GWT client API is having an HTTP request builder class which helps the application to make remote connection through HTTP request and supported methods (GET, POST).

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, urlAddress);
builder.sendRequest("", new RequestCallback() {
  
  public void onResponseReceived(Request request, Response response) {
     //use response.getText() to get the resulting text from the service
  }


  public void onError(Request request, Throwable exception) {
     // exception handling
  }
});

XmlParser
GWT client API also provides XML parsing capabilities but to use that XML.gwt.xml has to be included in the modules file.

The following code can parse the XML text and prepare complex node list which can be traversed to access the attributes and node values.

    Document doc = XMLParser.parse(response.getText());

Once Document instance is generated program can start traversal from the root node or can search for the particular nodes based on the name.

    NodeList nodeList = doc.getChildNodes(); // to start from root node
    NodeList nodeList = doc.getElementsByTagName(nodeName);  // to start from selected node

Full source code with example services is available here.

Friday, February 19, 2010

Social Databases

    Like many others i am not a big fan of SQL databases available today. But every application needs some or the other ways to persist the information. Being a Java programmer there are many options available to avoid raw SQL interaction during the development through Hibernate, JDO or JPA. But these all at the end lands at relational database only.

    Soon i started gaining familiarity with the Google Bigtable and Amazon Dynamo for their non-relational approach and why relational database will not help for their kind of applications. Well the approach was shocking and different but fascinated and proven at the same time. Meanwhile many developers realized that they might need different kind of database altogether to help their applications grow and respond faster.

    This realization resulted into many unorthodox, simple yet powerful databases which now-a-days falls into the Social Database category.

    Neo4j is a graph database. It is an embedded, disk-based, fully transactional Java persistence engine that stores data structures rather than in tables. The database is quite small in size, approx <500k jar. It provides objected-oriented API to interact with the database.

   Besides such a small database, Neo4j provides all the usual database features like ACID transaction, durable persistence, transaction recovery and concurrency control.
    
    For further information you can look out this presentation video and slides by Neo4j creator.

    Cassandra is a highly scallable second-generation distributed key-value based database. Initially developed at Facebook and now resides in Apache Incubator. Cassandra combines best features from Amazon Dynamo and Google Bigtable. Cassandra just like Neo4j is tested and proven for high-volume, high-transaction environments. Companies like Facebook, Twitter, Cisco, Mahalo and many more uses Cassandra in their applications.
    
    Cassandra follows the decentralized model where every node in the cluster is having identical information so there is no single point failure. Like many clustered databases, Cassandra follows the Eventually Consistent model and supports advanced features like Read Repair and Hinted Handoff to minimize inconsistency windows. 
    
     There are lot of presentations for the same can be found at SlideShare.net. 

    I have just started my working on this new kind of database. FluidDB is a hosted online cloud database. FluidDB follows the Wiki kind of approach where either user or application can created objects and modify the existing objects. Though it doesn't follow the Wiki model as it is and maintains certain restriction like permission and strict data type. It provides an efficient query mechanism to search through the database. The database provides simple HTTP request API for access.
    
    The database is in the private alpha as of now and you might get an access after sending the FluidDB team a mail API access request. As such i have not explored the database fully but there are plenty of ways this database can be used in today's applications.
     
    Tickery is a simple Twitter based application developed by team FluidDB team for demonstration purpose. 

  With the growing amount of social applications and their complexities we might see varying amount of databases for different requires over the period of time.

    Though these databases will not help you remove the relational databases altogether but will help you deliver the desired complex functionalities fast, easy and with less pain.

Thursday, February 18, 2010

Vaadin-Spring Integration

I remember the time when i was trying to integrate Spring with Struts2 and GWT applications. The problem was rather documenting the integration stuff i tried to remember them and as usual tend to forgot them. So this time have decided to document the Spring integration with Vaadin.

  • web.xml changes
So first we need to add the Spring Context listener to load the bean configuration file.

<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

Add the Spring Servlet to intercept Vaadin requests

<servlet>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <servlet-class>com.dhaval.web.vaadin.spring.servlet.SpringApplicationServlet</servlet-class>
  <init-param>
    <param-name>applicationBean</param-name>
    <param-value>testApplication</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <url-pattern>/vaadin/*</url-pattern>
</context-param>
  • Spring Servlet
This servlet connects the Spring with Vaadin. The servlet loads the Vaadin Application class and dispatches all the HTTP requests/responses. The Servlet is available in the Simple Application provided by Vaadin Team for Spring integration.
  • Spring bean configuration
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property>name="driverClassName" value="${db.driver}"/>
  <property>name="url" value="${db.url}"/>
  <property>name="username" value="${db.user}"/>
  <property>name="password" value="${db.pass}"/>    
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property>name="dataSource" ref="dataSource"/>
</bean>

<bean id="dao.user" class="com.dhaval.web.vaadin.spring.dao.UserDaoImpl">
  <constructor-arg>ref="jdbcTemplate"/>
</bean>

<bean id="testApplication" class="com.dhaval.web.vaadin.spring.MyApplication">
  <constructor-arg>ref="dao.user"/>
</bean>

testApplication bean id is same as we have given in the web.xml as an init-param to Spring Servlet.
  • Vaadin Main Application class
This is my Vaadin Application class

public class MyApplication extends Application{
    private UserDaoImpl userDao;

    public MyApplication(UserDaoImpl userDao){
        this.userDao = userDao;
    }

    public void init() {
        Window mainWindow = new Window("Simple Vaadin Spring Integration");

        final TextField txtUser = new TextField("Enter username");
        Button btnCheckUser = new Button("Check Username");
        final Label lblCheckMsg = new Label("");

        btnCheckUser.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                boolean result = userDao.hasUser(txtUser.getValue().toString());
                if(result){
                    lblCheckMsg.setValue("Available");
                }else{
                    lblCheckMsg.setValue("Not Available");
                }
            }
        });

        mainWindow.addComponent(txtUser);
        mainWindow.addComponent(btnCheckUser);
        mainWindow.addComponent(lblCheckMsg);

        setMainWindow(mainWindow);
   }
}

The full source code is available at vaadin-spring-integration in download section.

Wednesday, February 17, 2010

Groovy GPath for complex object traversal

GPath is a path expression language integrated into Groovy which allows parts of nested structured data to be identified and processed. GPath is named after popular XPath functionality to traverse XML nodes. GPath can be used to process XML nodes or composite objects.

class Person{
String firstName
String lastName
int age
}

def personList = [
new Person(firstName: "dhaval", lastName: "nagar", age: 25),
new Person(firstName: "nachiket", lastName: "patel", age: 24),
]

The easiest way to get all firstName from the above collection is to use collect{} closure:

println personList.collect{it.firstName}
// will print ["dhaval", "nachiket"]

But Groovy believes in shortening the most obvious stuff so the above statement can be rewritten like the following the still get the same result.

println personList.firstName

This feature becomes powerful when combined with Regular Expression. The following statement will print all the person whose first name starts with "d".

println personList.firstName.grep(~/d.*/)
// will print ["dhaval"]

However GPath is more popular to parse the XML documents effectively. Here we will take a small XML document and parse it with XmlSlurper and access it's nodes with GPath.

def mylinks = """
<links>
  <link>
    <id>1</id>
    <url>www.google.com</url>
  </link>
  <link>
    <id>2</id>
    <url>www.apple.co.in</url>
  </link>
</links>
"""

def links = new XmlSlurper().parseText(mylinks)

The following statement will print all the URL ending with "com"
println links.link.url.grep(~/.*com/)

The following statement will print all the URL ending with "co.in". One way to escape the . is to surround it with square brackets.
println links.link.url.grep(~/.*co[.]in/)

If my xml document changes radically, its affects my code very slightly.

def mylinks = """
<links>
  <link id="1" url="www.google.com"/>
  <link id="2" url="www.apple.co.in"/>
</links>    
"""

To find out urls ending with "com" and "co.in":

println links.link.@url.grep(~/.*com/)
println links.link.@url.grep(~/.*co[.]in/)

@ is the operator to access the property of a node. Parsing XML documents in such an easy way is a dream come true for a Java programmer.

Monday, February 15, 2010

Aardvark - A Valuable Business Tool

Couple of days before I got a news that Google acquired another popular Internet service called Aardvark in nearly $50 Millions. Aardvark is a social search engine service that connects users live with friends or friends-of-friends who are able to answer their questions. Based on my short interaction with the tool i realized that it can become a great boon for any kind of organizations.

I started using the Aardvark after this news and i found it really good tool to get the answers properly and pretty fast. I am using Yahoo Answers for all my non-technical questions and Stackoverflow for technical questions as I am a software engineer. Though both of these tools are doing pretty well, I was still finding that both the services are missing something.

Now to show Aardvark from an organization perspective, I work in a quite a big organization which employs huge and diversified manpower. Such a big enterprise contains an MIS system to keep employee profile information for future references. However its really hard to keep the latest profile information of each of the employee in the organization.

Every organization generates enormous amount of industry specific problem. When the problem arise they either seek an internal help based on the employee profile information or contact external agencies for support. In any case its time consuming at first and huge investment at the end, in the worst case an organization spends huge money on an external agency who fails to do the job and eventually either the problem is dropped or they find someone from inside.

To not cause such a heavy damage, now a days most of the companies (well software in particular) use Wiki/Forum/Blogs to tackle the situation and spread the knowledge. But this approach is limited to a set of companies and its not interactive. User seeking the information has to search through the set of services to find the answer to his problem.

People tend to learn new things every now and then so their profile keeps on updating compared to organization which prefers to update the profile information every six months or a year. So this approach creates mismatch between the actual employee profile and its organizational print. Because of this match many a times Organization end up hiring brand new resource or seek for some external agency to solve the problem.

Aardvark is a tool which can really bridge the gap between this mismatch. Employee getting new knowledge every now and then can update her profile immediately. Aardvark is intelligent enough in delegating the question to right persons. Such an intelligent and fast communication medium can be used in Enterprises to solve problems immediately without investing further valuable resources.

Well while writing this Aardvark is officially part of Google so in future we might see many new features combined with existing Google services.

Thursday, February 11, 2010

Google Buzz - sharing with GMail

Buzz a new service just announced by Google available under the GMail account. Buzz is designed to share day to day stuffs like text, images, videos, etc. Most of the features are similar to other popular social sharing services apart from the GMail front and other Google services integration.

Following are few buzzes of the Buzz.
  • Two mode of sharing is available. Public sharing similar to twitter and Private sharing where update is visible to the users of selected group(s)
  • Many external applications can be configured as a connected sites like YouTube, Google Reader, Picasa, Blogspot, Blogger and Flickr. As such you can paste link having images or videos, Buzz will scan the media files and show you the available contents to select if there are multiple images or videos available. I tried with flickr, vimeo, wikipedia and smashingmagazine
  • All Buzz entries are available in your Google Profile page under Buzz tab
  • Any post shared in the Google Reader will be available as a Buzz entry if the Reader is configured as a connected site
  • A Buzz can be directed to user(s) through '@' symbol before the username like @dhaval.b.nagar@gmail.com
  • Twitter updates are available as buzz entries if Twitter is configured as a connected site
  • To create RSS for your own Buzz entries, go to your profile page and select the Buzz tab. Copy the URL and add it in the Google Reader under Add Subscription
  • Buzz allows to post Link, Images and Videos to entries. Images can be seen with presentation feature on the same page. Videos can be seen live
  • Each conversation can be seen independently from the Time Link given on the top right of the dialog box
  • As such you can paste any link on the conversation dialog box and Buzz will find all the available images and videos on the page. You can select at max 4 media content to post.
Overall Buzz falls somewhere between the Google Wave and many other popular social applications. With the failure of Google Wave (or may be Buzz in near future) we will see many such applications trying to fit in the "Social Shoe" from Google.

Wednesday, February 10, 2010

Groovy Category and Dynamic Overriding

A Category class is an ordinary class which contains set of methods. The class can be used for a duration of code block and when used first parameter type of each method treats the declared method as its member method.

class TestCategory{
static def test(String str){
return "testing"
}
}

use(TestCategory){
// test() can be called as a member method inside the use block
assert "testing" == "a".test()
}

A category class can contain method without any parameters but it is not possible to use such method in the use block. A category class method should at least have one parameter to get called from the use block. The following code will throw an exception at the time of execution.

class EmptyMethodCategory{
static def emptyMethod(){
return "empty"
}
}

use(EmptyMethodCategory){
assert "empty" == emptyMethod()
}

Operator overriding is the most suitable use of the Category classes. In case of operator overriding, if the caller is not matching with any of the overridden methods, original method will be executed.

class StringSumCategory{
static def plus(String str1, String str2){
return str1.toInteger() + str2.toInteger()
}
}

use(StringSumCategory){
assert 3 == "1" + "2"
assert "12" == "1" + 2
}

If the category class contains a method with only one parameter of type Object, that method will be available to any possible type in Groovy.

class SerializeCategory{
static def serialize(Object obj){
// do serialization stuff
return serializedObj
}
}

use(SerializeCategory){
println 1.serialize()
println "a".serialize()
println ([1, 2, 3].serialize())
println this.serialize()
println serialize()
}

Groovy is full of surprises. It contains some of the best features from many static and dynamic languages.

Spread operator

Spread operator helps Groovy collections (list, range and map) to simple comma separated values. Spread operator is so flexible which can be used anywhere. For example:

def test(a, b, c){
return a + b + c
}

def numList = [1, 2, 3]

If you want to pass the above list of values to the test() method you have to write some boilerplate code to extract the elements and pass them in the method.

Whereas the spread operator is introduced to eliminate boilerplate code and follow clean coding. The above method can be called with the given list without a single extra line of code.

test(*numList)

An asterisk followed by list, range or map will spread the collection and convert all the containing values to comma separated values. The above example will work same for Range.

def numRange = 1..3
test(*numRange)

Map has different syntax compared to List and Range.

def aMap = [a:1, b:2]
def bMap = [c:3, *:aMap]

Likewise Groovy contains many such shortening facilities where development time can be reduces and code can remain clear and understandable.

Groovy spread-dot operator in GPath

GPath is an expression language integrated into Groovy to process nested objects. The dot and spread-dot operators helps to build the GPath expression. The dot operator is used to access the properties and spread-dot operator is used to access property, field or method.

class Person{
String firstName
String lastName
int age

int calc(){
return age * 2
}
}

def personList = [
new Person(firstName: "dhaval", lastName: "nagar", age: 25),
new Person(firstName: "nachiket", lastName: "patel", age: 24)
]

To access the firstName property one can use either of the operators:

println personList.firstName
// ["dhaval", "nachiket"]

println personList*.firstName
// ["dhaval", "nachiket"]

To access the calc() method spread-dot operator has to be used, whereas dot operator will raise an exception:

println personList*.total()
// [50, 48]

Apart from the access restrictions both the operators works differently when collection contains null values like:

personList << null
personList << new Person(firstName: "sandeep", lastName: "shah", age: 30)

println personList.firstName
// ["dhaval", "nachiket", "sandeep"]

println personList*.firstName
// ["dhaval", "nachiket", null, "sandeep"]

The spread-dot operator will add the null value for each null element in the resulting list.

Both the operators works same when the list element is not null but the requested property is null.

personList.clear()
personList << new Person(firstName: "dhaval", lastName: "nagar", age: 25)
personList << new Person(lastName: "nagar", age: 25)

println personList.firstName
// ["dhaval", null]

println personList*.firstName
// ["dhaval", null]

To avoid such null values in the resulting list, grep expression can be used.

println personList.grep{it?.firstName}.firstName
// ["dhaval"]

Here grep will try to evaluate the true boolean condition, as per the Groovy Truth if an object is NULL it will be considered as FALSE. So above expression will reject all the objects where firstName property value is null.

Tuesday, February 9, 2010

Calling Groovy closure from Java

After spending some time with Groovy alone i thought to mix Groovy code with Java. As closures seems the most weird in Groovy i tried to access a closure method from the Java class.

// Groovy code
class ClosureTest{
String firstName
String lastName

def proc(Closure closure){
closure.call(firstName, lastName)
}
}

// Groovy client
def ct = new ClosureTest()
ct.firstName = "dhaval"
ct.lastName = "nagar"
ct.proc{fn, ln -> println "$fn, $ln"}

// Java client

import groovy.lang.Closure;

class JavaClosureClient{
public static void main(String[] args){
ClosureTest ct = new ClosureTest();
ct.setFirstName("dhaval");
ct.setLastName("nagar");

ct.proc(new Closure(ct){
public Object call(Object[] argument){
System.out.println(argument[0] + ", " + argument[1]);
return null;
}
});
}
}

Well the difference shows the verbosity of the Java client compared to the Groovy one.

At my workplace we are not in a position to use Groovy extensively but with such silent integration between the two i am sure we can leverage some of the Groovy features.

Dynamic Typing in Groovy

Groovy like many other features works different from Java when it comes to method binding. Java uses the static binding at compile time to bind the method with the caller based on the parameter types. On the other hand Groovy does method binding at runtime based on the value type of the variable.

Java method binding

public class BindingTest{
public static void main(String[] args){
Object obj1 = 1;
Object obj2 = "hello";

proc(obj1);
proc(obj2);
}

public static void proc(Object obj){
System.out.println("object");
}

public static void proc(String str){
System.out.println("string");
}
}

At execution, the above Java program will print "object" for both the method calls. The reason is Java does "proc()" method binding based on the variable type defined in the call and ignores the actual values.

On the other hand the same Groovy code will print the output based on the variable values at the runtime.

Groovy method binding

def proc(Object obj){
println "object"
}

def proc(String str){
println "string"
}

proc 1
proc "hello"

The above Groovy program will print "object" followed by "string" output upon execution. This capability makes the code clear and concise. Developer can avoid unnecessary type casting in case of method overriding.

One such boilerplate code a Java programmer has to write while overriding the equals() method, which can be avoided with this approach.

// Java way
public boolean equals(Object obj){
if(!(obj instanceof MyType)){
return false;
}

MyType mt = (MyType) obj;
// do equality check
return result;
}

// Groovy way
public boolean equals(MyType mt){
// do equality check;
return result;
}

The Groovy code is quite simple as no type checking or type casting is involved. Besides if the caller will pass any other type of object; the super class version of equals() will be called and as the base equals() checks identity (==, reference equality), it will return false without any additional line of code.

So such small but effective techniques makes Groovy more popular in Java world.

Java Programmer's first look on Groovy

Like many Java programmers i started learning Groovy for some fascinated stuffs like Dynamic Typing, Closures, functional programming, etc. Overall the first experience with the Groovy was mind blowing. It has many new things to learn. To start with i am using Groovy in Action, its an excellent book for Groovy beginners.

My first encounter with Groovy started with the conventional "Hello World" program. The Hello World example finished and as such i didn't learn anything.

println "Hello, World"

But meanwhile i found that there are various ways to execute the above groovy code snippet:

1. From command prompt

>groovy -e "println 'Hello, World'"

2. From .groovy file

// HelloWorld.groovy

println "Hello, World"

// on prompt
// .groovy extension is optional
>groovy HelloWorld.groovy

3. From Groovy Interactive Console

// on prompt
>groovyConsole

A GUI application will open where in the upper half part developer can write the Groovy script and on lower half can see the script result.

4. From Groovy Shell

Though i haven't explored the Shell much but i could execute one line at a time and "Hello World" needs one line only. Following is the command to open the Groovy Shell:

// on prompt
>groovysh

Seemingly it would look quite simple to a new learner, but the real magic starts when the above code is either executed by Java interpreter or used by a Java program.

Convert Groovy files to .class files

// on prompt
// .groovy extension is NOT OPTIONAL this time
>groovyc HelloWorld.groovy

And the above command will create a .class file which can be executed with:

// on prompt
>java -classpath .;{groovy home}\embeddable\groovy-all-1.6.7.all.jar HelloWorld

And it works correctly. The same way this class file can be access by any Java program, it can create an instance or call the main() method directly.

So overall my first experience with Groovy was really good and i am quite sure to spend some time with this emerging language.

References

1. Groovy Home
3. Lots of links at Groovy DZone

Monday, February 8, 2010

Getting Started with Grails, Second Edition

The second edition of Getting started with Grails has just released. As the name says the book gives the step-by-step knowledge for the Grails framework and its fundamentals. The book is very well written and a must read for Grails beginners.

Scott Davis and Jason Rudolph both are very well known in Groovy and Grails world. The book will surely help you understand the power of Grails.

The book is available in the infoq.com bookshelf http://www.infoq.com/minibooks/grails-getting-started.

If you are a Groovy/Grails beginners like me, read Mastering Grails article series by Scott Davis at http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=mastering+grails