Dec 31, 2010

A simple vaadin application

This is a simple vaadin application which get 2 values and give it's sum.

To create this you need Eclipse IDE. (or you can use maven archetype:generate.).
You must install maven-eclipse and vaadin-eclipse plugins.

Then create a new project using maven. You can do it by creating a vaadin project also. But if you use maven, it will be very helpful.
This will create the project and there will be a sample project, but if you view the source code it will prompt some errors. Don't worry just run it once. When it is running, it will download all relevant library files including vaadin.jar.

Now you can add a new class file and insert this coding.

package org.thilina.vaadin;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.*;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window.Notification;


/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application {

    private Window window;
    private TextField tf,tf2;
    private Double a,b,sum;
    private Button ok,reset;
    private Notification notification;

  
    @Override
    public void init() {
        window = new Window("My Vaadin Application");
        setMainWindow(window);
        createForm();
    }

    private void createForm() {
        Form form = new Form();
        form.setCaption("Form Caption");
        tf = new TextField("Value 1");
        form.addField("value_1",tf);
      
        tf2 = new TextField("Value 2");
        form.addField("value_2",tf2);

        form.setFooter(new HorizontalLayout());
        Layout okbar = form.getFooter();
        ok=new Button("OK", this, "commit");
        okbar.addComponent(ok);
        reset=new Button("Reset", this, "reset");
        okbar.addComponent(reset);
        window.addComponent(form);
    }

    public void commit(Button.ClickEvent event){
        String temp;
        temp=tf.getValue().toString();
        a=Double.parseDouble(temp);
        temp=tf2.getValue().toString();
        b=Double.parseDouble(temp);
        sum=a+b;
        notification = new Notification("Answer is : "+sum,Notification.TYPE_WARNING_MESSAGE);
        window.showNotification(notification);
    }

    public void reset(Button.ClickEvent event){
    tf.setValue("");
    tf2.setValue("");
    }
}


You need to change the package details in the first row. Then check the class name and file name.

Then you can remove auto generated file.

There are sample source code in the vaadin website. http://demo.vaadin.com/sampler/
Select a part and in that window there is a description about it. Next to that there is a small link to view the source code of the selected item.

Dec 28, 2010

Creating a Clojure web project.

We can create a website using clojure programming language. It is very similar to java websites and it need a servelet container to host the site. If we can develop a .war file ti is very easy to distribute and use. So Lets try to do that. There is a simple open source war project, we can use as the base of our project. You can download it from this link;

git clone https://github.com/alienscience/leiningen-war.git

Do; lein install

git clone https://github.com/alienscience/compojure-war-example.git
(To get this you need to install GIT version control system.)


The source code will be like this.

├── project.clj
├── README.md
└── src
    ├── boot.clj
    ├── context.clj
    ├── deploy
    │   └── servlet.clj
    ├── html
    │   ├── index.html
    │   └── stylesheet.css
    ├── pages.clj
    ├── routes.clj
    └── web.xml


You can add dependencies in project.clj. Then you can check the "routes.clj" code. In that you can see some code segment like this;

"  (GET  "/app/dump" request
    (pages/dump-request request) "

When you run this in a web server and if you send a request to <host>/app/dump, it will links page which created by the class called pages.clj's "dump-reguest" function. If you view the "pages.clj" file you can see some thing like this.
(defn dump-request ...


You can add another function similar to this like;

(defn about
  (html
   [:html
    [:head
     [:title "About Me"]
     [:link {:rel "stylesheet"
             :href (context/link "/stylesheet.css")
             :type "text/css"}]]
    [:body
     [:h1 "About Me"]
     [:p "Hi ..."]]]))

And add this in the routes.clj file.
   (GET "/app/about" request
(pages/about request))

Then you can add your "about" page to the index page in html/index.html.

After that you need to compile the source code and make the war file. To do these things, first you must run;
lein compile  (you need leiningen environment)
This will download all jar files and first time it will take some time. Then, if there is no compilation error you can run

lein uberwar

Now you can see the war file. Now you need to put that war in a servlet container and run. Then visit <host>/app/about.

Dec 27, 2010

Create a project using Clojure.

First of all you need to install Clojure leiningen environment.
You can install it from the website - https://github.com/technomancy/leiningen

Then you need to run
lien new ui_project

This will create a project directory call ui_project. Inside that you will see some files like project.clj src/ui/core.clj.

Then you must edit the project.clj file and add main class. It will be like  " :main ui.core" and you can add dependencies as in maven. You can create a pom.xml but first you need to add all dependencies in the project.clj file. After that it will be like this.

If the pom dependency is like this;
    <dependency>
      <groupId>group.id</groupId>
      <artifactId>artifact</artifactId>
      <version>1.0.0</version>
    </dependency>


(defproject ui "1.0.0-SNAPSHOT"
  :description "FIXME: write"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [group.id/artifact "1.0.0"]]
  :main ui.core)

Then you can use maven also.

Dec 22, 2010

Vaadin - Introduction

Vaadin is a Java library which is used to develop web-based user interfaces. Java developer can use SWING SWT or AWT to create the web page as he/she is designing a software with a UI. Knowledge of HTML or CSS is not necessary. Therefor, the developer need only to think a bout the logic of the web application and develop it in the way he/she develop a desktop software.
Vaadin use AJAX and Javascripts. Therefor it will only create a single window and all other things will viewed by using AJAX. This will minimize the loading time and increase the performance.

To use Vaadin we need to download Vaadin library from its website. Then we can create a Java web based project and import thar library file as a dependent to the project. For some IDEs there are some plugins too. There is a maven archetype also. Vaadin runs on any Applet Server like Apache-tomcat, Jetty etc. So in order to use Vaadin you must install some applet server. All these things are based on Java and therefor it needs Java Development Kit or JDK.

Now we can try a simple code. If you use Eclipse you will get a system generated sample code. It will be like this.


import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class MyVaadinApplication extends Application
{
    private Window mainWindow;

    @Override
    public void init()
    {
        mainWindow = new Window("Myproject Application");
        Label label = new Label("Hello Vaadin user");
        mainWindow.addComponent(label);
        setMainWindow(mainWindow);

    }

}

In here we haven't use any HTML or CSS thing. But if you view the source of that HTML, you can see HTML tags with some javascripts. We just need to use lables, buttons and related things like in software development, Vaadin will create a web site for us.

Vaadin - Introduction

Vaadin is a Java library which is used to develop web-based user interfaces. Java developer can use SWING SWT or AWT to create the web page as he/she is designing a software with a UI. Knowledge of HTML or CSS is not necessary. Therefor, the developer need only to think a bout the logic of the web application and develop it in the way he/she develop a desktop software.
Vaadin use AJAX and Javascripts. Therefor it will only create a single window and all other things will viewed by using AJAX. This will minimize the loading time and increase the performance.

To use Vaadin we need to download Vaadin library from its website. Then we can create a Java web based project and import thar library file as a dependent to the project. For some IDEs there are some plugins too. There is a maven archetype also. Vaadin runs on any Applet Server like Apache-tomcat, Jetty etc. So in order to use Vaadin you must install some applet server. All these things are based on Java and therefor it needs Java Development Kit or JDK.

Now we can try a simple code. If you use Eclipse you will get a system generated sample code. It will be like this.


import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class MyVaadinApplication extends Application
{
    private Window mainWindow;

    @Override
    public void init()
    {
        mainWindow = new Window("Myproject Application");
        Label label = new Label("Hello Vaadin user");
        mainWindow.addComponent(label);
        setMainWindow(mainWindow);

    }

}

In here we haven't use any HTML or CSS thing. But if you view the source of that HTML, you can see HTML tags with some javascripts. We just need to use lables, buttons and related things like in software development, Vaadin will create a web site for us.

Dec 17, 2010

Let's ride the Camel - The simplest program to understand Camel

Camel Is and integration framework. It can pass data from one system to another. Here I will create a simple program to copy a file another place. Maybe this is simple but I think this easiest way to understand camel. I will use camel core and Java to do this. Then I will use Maven to do the same thing.

Think you need to copy a file call "File_A" in "Inbox" directory and you need to copy that file into "Outbox" directory. We can write a simple java I/O program to do that. It will be like this;

import java.io.*;

public class FileCopier {

   public static void main(String args[]) throws Exception {

File inboxDir= new File("Inbox");
File outboxDir= new File("Outbox");

outboxDir.mkdir();
File[] files=inboxDir.listFiles();
for (File source : files)
   if (source.isFile()){
File dest=new File(outboxDir.getPath() + File.separator + source.getName());
copyFile(source, dest);
}
   }

   public static void copyFile(File source, File dest) throws IOException {

  OutputStream out = new FileOutputStream(dest);
byte[] buffer=new byte[(int) source.length()];
FileInputStream in= new FileInputStream(source);
in.read(buffer);
try {
out.write(buffer);
}
finally {
out.close();
in.close();
}
   }
}

Note : You may need to create the Inbox directory in the same directory where FileCopier.java ecesis. Otherwise you must give the correct path. Next thing is, before you run this program make sure that the file Inbox/File_A ecesis.


Now we can try the same thing with camel.
To use camel we need to download camel library files (http://camel.apache.org/), and place them in the classpath. Then try this;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class FileCopierWithCamel {

   public static void main(String args[]) throws Exception{

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder(){
  public void configure(){ from("file:Inbox?noop=true").to("file:Outbox");}
});
context.start();
Thread.sleep(10000);
context.stop();
   }

}


So, I think now you can see the difference. Thread.sleep() is to give some time to copy the file. In this program, it required to download camel. But If we use maven, maven will manage that part. No need to copying to class paths and so on.

To do the same thing with the help of maven you can try;
mvn archetype:generate

Then chose the default one - "maven-archetype-quickstart" and give relevant details. This will create a files and a directory. It will look like pom.xml, src. Source code will be placed in src directory and pom.xml will contain all dependencies and plugin details. We use camel as a dependency, so we need to edit the pom.xml. Add this code segment in between the <dependencies> </dependencies> tags.

   <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-core</artifactId>
     <version>2.5.0</version>
   </dependency>

For this program, it will require only the camel-core, but for some programs there are many dependencies. You can find these details from the camel website.

Now we need to replace the code part. You can simply replace the maven generated "App.java" from previously created FileCopierWithCamel.java file. You should insert the package details in the FileCopierWithCamel.java file and that can be grabbed from the "App.java" file.
As previous, we need to create "Inbox/File_A". It should create where the pom.xml ecesis. Now the directory structure will like pom.xml, src, Inbox.

Then you can run it by;
mvn compile java:exec -Dexec.mainClass=<package name>.FileCopierWithCamel

Now you can see, that the program had created the "Outbox" Directory and inside that, you can see the "File_A".

Dec 16, 2010

Database connection pooling

Databases are the most common storing technology in todays IT world. Most of IT related things such as softwares and websites store data in a database. Database integration in a system got many advantages but there are some disadvantages also.
The main disadvantage is it require some time to retrieve or to store data. The database is an external thing from the software or the website. So first software or the website must make a connection to the database. This connection establishment require 1 to 3 seconds of time. Then it must send the query to retrieve data or to store data in the database. And then it should close the connection properly. This time requirement is an over head to the system. If the system is used by a large number of people this will make a huge impact.
So to avoid this, we can use a pool of connections. The pool will keep some database connections alive without disconnecting it from the database. Then, if the system request a database connection it will give one of its live connection to the request. While it in use there may be another request also. Then again pool will release another live connection to that. Now the systems does not need to make connections to the database, its almost done by the database pool. So that time requirement goes down.
But there are some limitations with the database pools also. Pool contains a limited number of connections but that may reduce performance issues. The programmer or the system designer must do additional work to use a database pool and he/she should think how to manage it. Mainly how to handle the maximum number of connections, connection queues, priority based connections and so on. There are many library files which can be used like c3p0.
So now we can have a good integrated system with good performances.  

Dec 6, 2010

Implementing a solution by using Camel.

Here we try to implement an integration solution by using Camel. The problem is, to send an e-mail to a central administration. In an organization each client can send an incident to the central administration. Organization use a website to do this process. So the client can visit the website and fill an incident form and send.  Website should pass that details to the relevant place by converting that details in to an E-mail.

To build this system we need Maven, Apache-cxf, Apache-Camel and Spring web framework. So lets try this.
first you need to create a Maven project. To do that you can type,
mvn archetype:create -DgroupId=org.apache.camel -DartifactId=camel-example-reportincident -DarchetypeArtifactId=maven-archetype-webapp

Then you need to add Apach-cxf dependencies to the pom.xml file.

You may need "cxf-rt-core" , "cxf-rt-frontend-jaxws", "cxf-rt-transports-http". After that we need to create a .wsdl file. We should put it in src/main/webapp/WEB-INF/wsdl/report_incident.wsdl .
 (refer this link to get the coding :- http://camel.apache.org/tutorial-example-reportincident-part1.html)

Now we need to add some plugins to generate java classes from the wsdl. To do that we need to integrate the cxf wsdl2java converter plugins and maven-compiler-plugin. After inserting these plugins you should edit the web.xml file.

In that file we must add a listener. Now we will use spring to do that.
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Then we should specify the cxf servlet, URI and cxf-config,xml path details in the web.xml file.

So the path for cxf-config.xml is src/main/resources and you should place that xml file in that place.

The basic configurations are done for now. Now you need to create a java file and it should like this.

package org.apache.camel.example.reportincident;

/**
 * The webservice we have implemented.
 */
public class ReportIncidentEndpointImpl implements ReportIncidentEndpoint {

    public OutputReportIncident reportIncident(InputReportIncident parameters) {
        System.out.println("Hello ReportIncidentEndpointImpl is called from " + parameters.getGivenName());

        OutputReportIncident out = new OutputReportIncident();
        out.setCode("OK");
        return out;
    }

}

Before that you may need to remove the .jsp page which created by maven initially. Then you can try to compile the project.
Try;
mvn clean compile

If the project compiles without errors you can try to run it on a web container. So you need to edit the pom file again and insert maven-jetty-plugin to the main pom file. using jetty is easy than using any other web container, but camel supports for many finds of web containers too.
Then try;
mvn jetty:run

If you try to visit http://localhost:8080 you will see some details to your web application. Still it is not complete, si you wil not see any UI. You can use SOAPUI to do that thing.

Cont...

Dec 2, 2010

Important things when integrating an enterprice systems.

There are some important and critical things developers must think before starting the integration. In an enterprise, it will use different kinds of software on different platforms. This is the biggest issue which all the developers will face.

For an enterprise a developer or a team of developers cannot create a whole system, because it is a time consuming task. So what the developers do is, they try to integrate the existing software products. But there are some software products which offers its source code, but not almost all the software products do that thing. This will make the process more difficult.

An integrated system, applications must communicate with each other. But if an application totally depended on another application it will be a mess. Think if "A" application request some thing from "B" but "B" fails to respond to that request. If this kind of a thing happens, what "A" application must do? So the developer must integrate the system with low coupling to avoid such a situation. As in this case, "A" an use a time-out and send a error message to continue its work.

As I mentioned, there are some software products which offer its source code. But if a developer try to change the coding as he/she wish, it will create a performance issue on that software. So the developer must try to integrate the system with a minimum amount of changes.

Other thing is the technology which use to integrate the system. There are various kinds of techniques and most of them need some special knowledge hardware and some other things. That thing also a problem we should think.

As an integrated system applications must understand what the other application send to it. So the communication media (maybe a message or a signal) should be in a common data format. Developer must convert the message into a common format before it leave that sub system.

Even if these systems can communicate with each other, still there are some issues. If a sub system is sharing its data among other sub systems, every sub system must use it within a short time frame. Because, if a single sub system is using a shared resource it will lock that resource and other sub systems will wait until that resource is available. so the developer must think about the time limitations.

If the enterprise system got a common functionality, it will create another issue. Sharing a functionality between sub systems is not a simple task. And also if the system need to communicate with a remote system like a banking system or a shipping system that thing also add more weight on developers. These areas will me more complex.

The most important thing is the reliability. The information which send from a sub system should be reliable. Developers most make those communication in a secure manner. This will be a huge issue when it comes to remote communication.

These are the most important thing a development team  must think before developing an enterprise system. If a single information goes wrong, it will be a huge damage to the company. So the developer must use the suitable solutions as it requires in an integration. 

Dec 1, 2010

Do we need "Integration Patterns" ?

Have you ever think how this Blog works? No, isn't it. In the top you may see something like "Thilina's Blog". Then the whole area is divided in to two separate columns. One containes this article and the other may contain some additional links. If you try to view the same article after few days, you may see some new details on the right side column (maybe new links). So how it happen?

Most modern web pages display a main content and with some related links and informations. Even if you request a single thing, it must offer that thing as well as other related stuff. Likewise  there are some systems which need other systems informations in order to display or to do its task. When a system depends on another system, these things need to communicate each other.

Think you need to change your address details in your bank account. What you will do is, just simply login to the bank website and change the address. But if you try to view your address through a ATM machine or from a bank statement, that thing also updated. But have you update those thing? The thing is, when you update the details through the website it automatically update all other things also. So how to do this kind of a thing? One system should send data to the aother system and the that system should be able to read data from it.

In a bank there are several different sections. Likewise in a bank network also there are different services provided by different systems. As an example, ATM will provide a different service than a bank website. But both things work in a single bank system. In here we can see some systems must responds according to the service it can offer. How to implement this kind of a service oriented thing. System must respond to according to the request and chose the correct system which is capabler of doing the requested task.

Think you had login to your  banking website. Now you can view your bank details. If you go to an ATM machine and try to login with your PIN number, there also you will see your details. In both case you will see only your details but not others. Both systems must identify the user and view the correct content. The identification of the user is a common process for these two system. If this is more common thing, coding it again and again in different sub systems is not needed. But there is a need to communicate with that process in order to  complete a process in a different sub system.

If you need to buy something from a website, what you will do? We need only to insert our bank details. But, the website must verify wither we got enough credit to buy that thing or not. So  to do that it need to communicate with our bank. Now a system in a business need to communicate with another system which is also in another business. So how to communicate between two enterprise systems?

These are the main areas that an enterprise system developers must think. Most of the time enterprise system developers use per-build sub systems, because developing a hole system is very time consuming thing. That's why we need "Integration Patterns". Here I have describe main types of integration;

* Information Portal
* Data Replication
* Shared Business Function
* Service Oriented Architecture
* Distributed Business Process
* Business to Business integration.

When a developer need to develop an enterprise system he/she can use these methods in order to do it in less effort and without wasting time.