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.

Nov 26, 2010

Apache Camel

" Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration ".

So what is Enterprise Integration Patterns? In an enterprise they may use different software to do there task easily. But those software should interact with others and will not be isolated. Therefore, software must communicate with each other. But as an enterprise software, its communication is not so easy. To overcome this problem people use a common set of instructions or patterns when they implementing an enterprise solution. Most people use "Enterprise Integration Patterns" book and it describe about 65 categorise of integration patterns which was collected since 2002. Most of these patterns use asynchronous messaging architectures with the help of EAI, SOA, or ESB platforms such as  IBM WebSphere MQ, TIBCO, Vitria, SeeBeyond, WebMethods, BizTalk, Sonic, Fiorano, Mule ESB, ActiveMQ.

Beans Integration is, it provides a general purpose mechanism for processing messages using arbitrary Java objects. By using a bean reference into a route, you can call an arbitrary method on a Java object. From that you can access and modify the incoming exchange. So this bean binding mechanism is used in Apache Camel.

Then why we need camel? When you are designing an enterprise solution you need to use integration patterns and to implement that thing you may need to  use messaging modules like HTTP, ActiveMQ, JMS, JBI, SCA, MINA, etc. Camel create an easy environment to use these things, such as a pluggable data format option which can interact with other modules. It is a small library with less dependencies for easy embedding in any Java application. Camel can be used as a routing mechanism for Apache ServiceMix which is a powerful ESB and JBI container. So Camel can be used in complex enterprise integration solutions to do the job with less effort. 

Nov 25, 2010

Develop a BPEL project using Eclipse and Apache ODE.

To do this thing you need to install these software.
 Apache ODE on Apache Tomcat server and Eclipse IDE with BPEL designer plugin. 
First you should download and install tomcat web server.
http://tomcat.apache.org/download-connectors.cgi

Then you can download the ODE war file from this link;
http://www.apache.org/dyn/closer.cgi/ode/apache-ode-war-1.3.4.zip

Unzip this file and put that into the webapps folder in the tomcat.
Then you must restart the tomcat server.

After that you must install the Eclipse IDE (Eclipse IDE for Java EE Developers). You can download it from this website.
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr1

The run the Eclipse IDE and go to,
Help -> Install new software
Then in the window, add this URL to the space called "work with": http://download.eclipse.org/technology/bpel/update-site/

Then it will provide some plugins and you should install those things to your Eclipse IDE.

Now you need to setup the IED to communicate with the ODE. TO do that you must go to,
File -> New -> Other
and select "Server " in the server directory.
This will pop-up a new window,in that you must select,
apache -> Ode v1.x Server

This will ask some details according to your Apache-Tomcat and JDK setting of your machine.  Mainly you must give the path to the ODE field as;
path_to_tomcat/webapps/ode/


Then try to start the server from the IDE, first stop it from console if the server is running. 


Now you can try to design a project. First  go to, New -> Projects;
And there you should select BPEL -> BPEL Project
Then in the wizard you must select "Apache Ode v1.x Runtime" and in the configurations section select "BPEL2.0 Facets".


Then this will open a project folder, right click on that and select new -> other and in the wizard select BPEL -> New BPEL Process file.


Now you can see  some file like <something>.bpel, double click on it. It will open a graphical flow chart kind of a thing. In the right side  of that you can see some tools which can be use to design the process. Try these tools, but I will explain how to get a output from an input String. To do that thing;
You must insert "Assign" part in between the input and output. Then go to its properties by right clicking on it.
Then go to details and add a new. it  will show two fields to fill. In the first select input and click on "input:string", on the other select output and do the same. Then perss the save button. At this time it will ask some thing  like "Variable output doesn't have initializer. Should it be generated? ", In this, press "Yes" 
This step will create a file like <something>.wsdl, double click on it. It will view a network kind of a thing. You must edit the URL in the <project_name>Port to;
http://localhost:8080/ode/processes/<project_name>
And also you must select the protocol as "SOAP".


Save the project and right click on the main project folder. Go to  New -> Other and select "Apache ODE deployment Descriptor". This will generate a XML file called deploy.xml. You must double click and open it. In that select inbound Interface -> Associated Port and give the "<project_name>Port" in the drop down menu. If it does not excise, you  must restart the Eclipse IDE and all file names and project names must be equal. If there is no error you can try to run it.
To do that you must go to the Server, right click on it and select add/remove project. In the wizard you must add the project to the server and run it. If the server in not running, run it from the IDE by clicking the run button while the server is selected. 

Now  you can go to the project explore, select and right click on the  <project_name>.wsdl and go to;
web services -> Test with web service explore.
That action will view a web browser kind of an environment. In that you can see your project file, click on that and it will view a file call "process". Double click on that. In the next window you can insert a value and press go, you will see the same output from the below frame.


Our process was get a string and return it. So this done that thing. We can do more thing  by changing the process flow with the graphical aid. It is the amin advantage of this thing and this is a web based system, therefore it's more easy to manage and use.

Nov 23, 2010

Haskell for web development - web server for haskell

There are many web frameworks which use to develop web applications by using Haskell programming language. I will talk about "SNAP Framework".

Snap is a simple web development framework.It is very easy to install. To install this web framework you need to install GHC and Cabal-installer. Important thing is Cabal-installer must compatible with the GHC version. If both work correctly it will reduce lots of issues while installing the framework.

First you must update the cabal database by;
cabal update
Then try to install snap by;
cabal install snap-server

This will install all dependent packages which need by the snap-server. This may take some time to complete. If there is an error try to read and understand it. Most of the time those errors occur when the system is missing some dependent package and it should me manually installed. To get these packages try this website as possible as you can.
http://hackage.haskell.org/packages/archive/pkg-list.html

Then you need to install "expat library" which use in developments.
Use yum to install it.
yum install  expat-devel.i686

Now you should check if the path to the ~/.cabal/bin is in your path.
Then create a folder and do these steps.
mkdir test-snap
cd test-snap
snap init

This  will create a src directory and a file called test-snap.cabal. You may see three files in the src directory.

Now you can try to view this in your web browser. To do that, you need to run these command within the project root (inside the test-snap directory where you can see the.cabal file).

cabal install
test-snap 8000

Now you can visit the url http://localhost:8000/ to view he web page.

If you need to stop the server just press Ctrl+c.

Note:- When you are  creating a folder to build the project try to use a unique name like  [somethilng]-snap  or likewise. Because after the installation of the web site in will create an executable file in the ~/.cabal/bin, and that place is also available in the path variable.

Nov 16, 2010

visualizing the way we communicate over a digital network.

These days we are using internet and other small digital networks as a common media to communicate. Sending a short message from your mobile phone to a VoIP conference call, it use a digital network. There are various methods each technology use to communicate each other. But most of these technologies use a common method to pass their message from one place to another through a digital network.
Data cannot travel through a network as a hole. It should know its destination, and how to get there. So to do these things service need to add some more information to the original data. So now this combined data set is called as a "packet" (also as a datagram, a frame, ect.). 
Many data packets contain more information according to the technology (protocol) that it use to communicate. There is a software to view these information of a packet called as "Wireshark".
First of all you must install wireshark on your machine. You may need root permission to do this. Download it from their website http://www.wireshark.org/ and install it.
Then try to run it as root. Running wireshark as a root or a supper user is the best way to capture packets. At least you should grant sudo  permission to run the wireshark.
Now you can run the wireshark. In that window you will see 4 main rows. First row contain all tools, second will display the live packets which a communicating with the interface, next will display details of a selected data packet and the last will display it's hex value. In the initial step you will see an empty rows but to view information you must select some thing.
As the  first time we are using wireshark we can try with our localhost  interface.
To select an interface you must click the button called "List available interfaces" and it will display a list of available interfaces. Select  "lo" or "localhost" and apply. Then open a terminal and type;
$ping localhost

what you can see on the wireshark, It should be like this;

5    2.001518    127.0.0.1    127.0.0.1    ICMP    Echo (ping) request
6    2.001601    127.0.0.1    127.0.0.1    ICMP    Echo (ping) reply

Now select a single packet and see the 3rd row of the wireshark.
In that field you can see the information that carry by a packet.

Now you can try this also;
$ssh localhost

Now you can see how many packets should involve to make an ssh connection. Try to use some other things also. To start another capture you should stop the ongoing thing first. You can save the captured information if you need. Then you can try some other interface like "usbmon2 usb bus number 2". In my computer the mouse is connected into this interface so i can see the packets exchange between the machine and the mouse.

So now you can try the ethernet interface which commonly use to connect to the internet. Select the interface  which you connect to the internet if you do not use a ethernet port.(if you use a usb modem or else). You can see many number of packets are travelling without any action from you. It happens due to DHCP, automatic updating software things and so on.
If you know about protocols you can get a filtered output from this software. Think if you need to see the details of sctp packets, you can use the filter in the first row. If you use the filter you will only get the live sctp packets.
Think if you did not  use a filter, but wireshark will display each packet type in a different background colour. Most of the known harmful packets will highlighted in dark colours.
Always wireshark gives a user interface, but there is a tool to do the same thing without the GUI. Its also shiped with wireshark and you can run it by typing "tshark" in the console. This command also required the superuser permission.
You can try this by typing;
tshark -i eth0

Nov 2, 2010

Important things when installing Haskell ghc and packages

Haskell is not like other programming languages. Even when it is installing the compilers installing person must manually install each and every module if he/she needs. I will tell some points that you may need when you are going to use Haskell.
These thing may change in future, but for now it may be the best way.

First thing, do not use "yum install ghc" . This will install " ghc 6-10. " or some old thing. You will be able to download the latest ghc from the website.
link : http://www.haskell.org/ghc/index.html 

If you had install an old version of ghc from yum, you can erase it and install new ghc manually.
$yum erase ghc

Installing ghc from the tar file ease easy. Only you need to do extract the tar file and reat the INSTALL file. You can use the default configurations which will install ghc in /usr/local. To do that you can; (You may need root permission)

$ ./configure
$ make install



Next thing is when you need to install any module, first search it from the http://hackage.haskell.org/  website. This will give all relevant details of the package.

Old version of ghc use a package installation program called Cabal-installer. But the new version is not working with that program. May be in future they will make it, but for now you can't use cabal.

Do not download the package as soon as you see the package downloading link. Go through the web page. You may many important details like;

*  Versions -
            Gives the version information. Very important when you are finding a dependent file. You must use the relevant version of the dependent package.

*  Dependencies -
           Gives information about packages which need to run the current selected package. You can not install, if a single package is missing. This thing also very important. There are words like "or" , "(>2.0)" which you must think before downloading or selecting a dependent file.

*  Built on / Built failure - This will tell if the package will install correctly or not, considering on the ghc version. Some files can only work properly on some ghc versions. Some packages give errors on some ghcs.

Selection the working file may be too hard, but this will help you a lot. Think when you installing a package and you got a error message as you need to install a dependent package, read it correctly and try to install only that dependent package, even if you got a new package if the old one is stable.

Connect to a MySQL database from a Java program

In a program, it may need to store and organise data. To do that in a secure way it must use a database service. MySQL is a good database which can this task by communicating with a Java program. MySQL supports other languages also but for now we only talk about the MySQL-Java combination.

If the Java program need to communicate with the database, the program need to convert instructions into a language which MySQL can understand. To do this Java need a special connector called Jconnector. You need to download and install it.
Link : http://www.mysql.com/downloads/connector/j/

After downloading you may get a  file like "mysql-connector-java-5.1.13.tar.gz"
Unzip this file. Now you can see a file call "mysql-connector-java-5.1.13-bin.jar". You should move this file to the jre/lib/ext, to do that;

$ mv mysql-connector-java-5.1.13-bin.jar   $JAVA_HOME/jre/lib/ext
( "$JAVA_HOME" is where the JDK is installed.)



Now you can try to communicate with the database. You need to create a database in MySQL and if you need create a new user and a password. Then run the MySQL server. After the server is completely up run the Java program. A simple Java program to check the db connection;

/* File name :-  Connect.java



 *
 *
*/

import java.sql.*;

   public class Connect
   {
       public static void main (String[] args)
       {
           Connection conn = null;

           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost/test";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");   }
           catch (Exception e) {
               System.err.println ("Cannot connect to database server"); }
           finally {
               if (conn != null) {
                   try {   conn.close ();
                             System.out.println ("Database connection terminated");}
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
       }
   }

Installing JKD in fedora

Java development kit is the Java environment which supports to run and compile Java based programs. JDK is containing everything in  Java Runtime (JRE) as well as java compilers  debuggers. Most of the time fedora will contain openJDK but it's better to install Sun micro system JDK. You can download it from there website;

link : http://www.oracle.com/technetwork/java/javase/downloads/index.html
Note - download the JDK not the JRE.

For Fedora it is a file like jdk-6u22-linux-i586-rpm.bin. You need to convert this bin file to an executable file. To do that you can type;

chmod a+x jdk-6u18-linux-i586-rpm.bin

Now we can simply run this file. But this bin file will extract all the file in the current directory. So if you need to install JDK in "/usr/local/", first you must change your directory to that location which you need to install it.

(To install in this location you may need root permission. If you haven't got it you can install it in your home directory.)

$cd /usr/local
(or $cd ~/)
Then you can execute the bin file,

$ ./jdk-6u18-linux-i586-rpm.bin

This will extract files in to a file like "jdk1.6.0_22".

Note - JDK is a rapidly developing thing, there for you may need to install the latest JDK in order to get the maximum from Java. To do these updates you can use this kind of method.

Now you are in /urs/local/,

$ ln  -s    jdk1.6.0_22    jdk
This will create  a soft link in /usr/local/ called jdk which points to the "jdk1.6.0_22" directory. In this way you only need to update the link when ever you need to update/downgrade the JDK in the system.

Now you need to add the bin file in JDK to the PATH variable. To do that you must edit the .bash_profile (or .bashrc)  which contains the PATH variable.

Update the variable into;

       export JAVA_HOME=/usr/local/jdk
       PATH=$PATH:$JAVA_HOME/bin:$HOME/bin
       export PATH

then type;
$source .bash_profile

Now you can try
$java -version
This should give the correct version information. If this thing gives wrong information type;
$whereis java
This will give like
/usr/bin/java
Then type;
$ls -la /usr/bin/java
If it  gives like this;
  lrwxrwxrwx. 1 root root 23 2010-11-01 14:42 /usr/bin/java -> /usr/local/jdk/bin/java
If this is the matter you can unlink the java file  in /usr/bin by;
$unlink /usr/bin/java
($unlink /usr/bin/java*  this will unlink all java, javac, javawp)

Then again check the Java version. Make sure all paths and spelling are correct.

Oct 12, 2010

clojure looping

There are many ways of creating loops in clojure. In this  I will give some of them.

The first way is,

user=>(loop [i 1]
                 (when (< i 20)
                 (println i)
                 (recur (+ 2 i))
             ))

This will create a loop like 1, 3, 5 ... . There are some other ways  which  gives the  same output.

user=>(dorun
         (for [i (range 1 20 2)]
         (println i)))

user=>(doseq [i (range 1 20 2)]
           (println i))


Mutual Recursion
In clojure mutual recursion is little bit harder than the other languages.
This is a simple coding to check a given number is an even or an odd number.

(def even)
(defn odd [n]
  (if (zero? n)
      false
      (even (dec n))))
(defn even [n]
  (if (zero? n)
      true
      (odd (dec n))))

So try this by typing,
(even 13)
It will give tho output as false.

Libraries
(ns example.otherns
  (:require example.mylib))
(defn check-size
  [x]
  (if (too-small x)
    (example.my)flib/add1 x)
    x))
We can defined our own libraries and can use in many programs without retyping it again and again. As a test library lets try this. Write this coding in a directory called example with the name of mylib.clj. In that file you can write like this.
(ns example.mylib)
(defn add1
  [x]
  (add x 1))

And in your program you can try by this coding
(ns example.otherns
  (:require example.mylib))
(defn check-size
  [x]
  (if (too-small x)
    (example.mylib)flib/add1 x)
    x))

or else you can use like this also
(ns example.mylib
  (:load "mylib/add1"
         "mylib/otherfunc"
         "mylib/morefuncs"))
This will add more lib files to the mylib library file. Those shoulb be plased in a director called mylib in exsample directory.


HTML output from a haskell

Haskell is a programming language which is capabler of creating dynamic and static web pages. Now I will explain how to create a static web page by using haskell.
First of all you may install these modules to run your program. Install these things by using cabal module installer. Module is Html. If you get errors, read those error messages and try to install other needed modules.

In the program you should
import Text.Html

This will load all the relevant html things to the program.
So now we try to write a basic html codes.

page = [
   body<< [
     p<< "First paragraph",
     p<< "First Paragraph"
    ]
   ]
main = putStrLn  $ renderHtml page

Then you must run it like this,
runhaskell filename.hs
This will print some things with html tags.

So we can modify this as we need. We can insert a title, background colour like this.

htmlPage
      = header
        << thetitle
           << "My Haskell Home Page"
   +++ body ! [bgcolor "#aaff88"] << " "

main = putStrLn $ renderHtml htmlPage

Now we can add details to this program in order  to add content into the web page. For that only you shoula modify the line "+++ body ..." to some thing like this.
+++ bopdy ![bycolor "#aaaaaa"] << bodyCon
Now this will search for "body" key word. We must add the content to that like;

bodyCon = p ! [align "center"] << "This is the content of body tag"

You can add more thing to the body of your html page. So we can try,

bodyCon = p ! [align "center"] << "This is the body of the html page"
          +++ p << "second p tag"
          +++ table ! [border 2]<< tableCon

tableCon = (col1_r1 `above` col1_r2) `beside` (col2_r1 `above`col2_r2)
   where
        col1_r1 = td << "col1 r1"
        col1_r2 = td << "col1 r2"
        col2_r1 = td << "col2_r1"
        col2_r2 = td << "col2_r2"

You can see in this coding I had use two p tags and one table. Likewise we can create hole html page from this.

Up to this point we can get a html output to the screen. But we need to create a html page without printing this to the stdout.
So to do that we must modify the main in the coding to this.

main = writeFile "haskell.htm" (renderHtml htmlPage)

Now this will create a html file in the working directory. You can view  it by using a web browser.
You can make this page dynamic by adding more functions to create dynamic content.

Oct 11, 2010

Infinite data source - Clojure

As the other programming languages we can get number sequences in clojure too.
We can get a list of sequence numbers from a function like;
user=>(def num (iterate inc 0))
This will create an infinite list starts from 0. We can get a list of sequence numbers by;
user=>(take 10 num )
(0,1,2,3,4,5,6,7,8,9)
This will give a list from 0 to 9, 10 incriments.
If i need a list from 10 to some amount, it is like;
user=>(def num_2 (iterate inc 10 ))
user=>(take 5 num_2)
(10,11,12,13,14)

List comprehension

Think if I need to get a list of numbers which can be devide by 4 and I need to add 1 to that number.

user=>(def num_1(iterate inc 0))
user=>(def num_4p1 (for [x num_1 :when(zero? (rem x 4))](inc x)))
Then to get 5 of these things,
user=>(take 5 num_4p1)
This will give an output;
(1,5,9,13,17)
This is not the only way of creating sequences. We can use maps and filters to do this type of task and get the sane output.
user=>(def x (map inc (filter (fn [x] (zero? (rem x 4))) num_1)))

Lisp
Sequence functions

function - (first coll)

This gives the first element of a list like,
user=>(first (list 12 43 546 75))
12
If the function is empty it will return nil as the result,
user=> (first (list))
nil

Function - (rest coll)

This gives the result without the first element. It is like;
user=> (rest (list 12 23 35)
(23 35)

Function - (map x  coll*)

Apply x laizy to each item in the sequence.
user=> (map (fn [x] true) (range 4))
(true true true true)

Not only the true but alsom we can do other calculations as well.
So it may like this;
(map (fn [x] (* 5 x))(range 10))
In this function it will multiply all the numbers fron 0 to 9 by 5, and give the output as,
(0 5 10 15 20 25 30 35 40 45)

We can use this to get a x*x kind of list, like;
user=>(map (fn [a b] (* a b)) (range 5) (range 5))
This gives the out put as,
(0 1 4 9 16)

function - (every? pred coll)
This will return true is the "pred" value is always true in a sequence.
Try this out,
user=>(every? pos? (range 10))
This will give the output as "false"
The "pos?" will return true if the value is greater than 0, so in this range the first element is 0. The output is like first one is false and others are true.
You can modify this like;
user=>(every? pos? (range 1 10))
Now the result will be true, becouse of, all values are more than 0.

In this it will get the values from 1 to 10, try to devide it from 2 and check wether there remaining parts are  equal to 0.
(every? (fn [x] (= 0 (rem x 2))) (range 1 10))
This will give the result as "False " because of some elements remaining parts are not equal to 0. So we can modify this function like this.
(every? (fn [x] (= 0 (rem x 2))) (range 2 10 2))
This function will get a list like (2 4 6...). So these all numbers will give its remainder as 0 when it devides by 2. Thats why this value is "true"

 function - (not-every? pred coll)
This is like same as the (every? ) function, the only difference is when "every?" is giving true "not-every" will give the output as "false". It gives the complement of the "every?" function.

Oct 6, 2010

Clojure Namespace

This is required to load clojure libraries, use to reffer curent namespaces and
to import java classes. We can use these pre defined source codes in our progrms. These are for commen need in most of programs.
Load a clojure library you can use,
(require 'clojure.contrib.math)
and to print all the loaded namespaces,
(all-ns)
If you need to load and reffer all at onec, you can use this command,
(use 'clojure.something.something)
And if you only need to reffer to a singl thing you can use
(use '[clojure.some.some :only (thingYouNeed)])

Importing Java libraries and classes

Think if you need to import java IO file in you clojure program you can do it by writing;
(import java.io.File)
If you need to import classes from a java package you can use;
(import [java.util Date Random])
This will import both Date and Random in util package.

Writing clojure programs

We can write a clojure program and store it in a seperate file and we can use it by calling that file. So to do that we can use java namespaces. If the namespace is like com.my-app.util, your clojure file should be placed in file structure
like com/my-app/util.clj. So we must begin the file with namespace as ;
(ns com.my-app.util)

So let's do a test program.
First create a folder called student in the directory where the clojure.jar placed. Then write this coding to a file called name.clj .
(ns student.name)
(defn my_name [sentence] (str "Welcome " sentence "...!"))

Now run the clojure.jar file and import this clojure file to the interpreter by;(use 'student.name)
Then try this;
(my_name "Thilina")
It will give the output as;
Welcome Thilina...!

Oct 5, 2010

Automating a svn project by using hudson

Hudson is a continues integration system. It can monitor executions of a job continuously.
Hudson can use to monitor SVN projects and CVS projects. It can build a project at a given time and in a given frequency. User can setup it to build a project when some one commit the project.
The build can be done by invoking Mavan, Ant, Windows batch command or even a shell script. And also user can get a feedback to his/her e-mail.
The best thing is a it got very user friendly web interface where the all configurations take part.

Now we are going to setup hudson, to execute svn project with the help of Mavan. As I sad before, this is a web based thing we need to install apache tomcat server. So download it from the apache website and install it. Also you may need to install JDK in order to work properly. Now you may need to install mavan too. Download mavan from the apache site and install it. Make sure that you  are installing the stable versions of all these software.
While installing, check that you are make path variables as in the software installation document.
If javac, mvn and apache tomcat things are working properly, you can download the hudson. Go to hudson website and download a .WAR file. Stop the tomcat server and move the .WAR file into webapps folder in the tomcat server. Then start the web server. Server will execute the .WAR file and it will put hudson live. You will be able to use hudson by visiting http://localhost:8080/hudson URL. So up to this point we had setup the hudson. Now we should configure hudson to build a project.
To do that, go to new job and select "Build a free-style software project". Then it will give a form like thing to fill. That is the place you can configure the build. Give a name and fill other relevant fields as you need. Then in the source code management section choose subversion and give the URL where the svn project is located. Then you can move to build triggers. If you only need to build if someone commit the svn, you should use "pool scm". We will use mavan to build our project so, we invoke Mavan. In the goals field write the mavan commands without "mvn" word. Finally in the post build action section you can set E-mail notifications too.
Now when ever you commit that project it will automatically build the project. And if there any build fail, it will send a e-mail notification.

Installing Windows XP in Linux installed disk

The best way to install linux and windows in a same disk is, first install windosws in a seperate partition and then install the Linux OS. This way is the most shortest  way to make a dual boot machine.
But in some cases we may need to install a windows OS where a linux OS is running. If there is no free space to install windows it makes this tark much harder. In a case of that we muts use a tool like GParted to edit an excisting partition and make free space to windows. After editing the partition we must update the grub, which is the boot loader of the linux OS.
Then we can install the windows partition. But keep in mind, the windows partition needs extra 8Mb space also. So when you are making the partitions make sure to keep some free space. This instillation will deactivate the Linux partition when it installing its boot loader.
Now you will load only the windows OS. YOu may not see the linux partitions in when you are in windows, and also do not change or delete any of these partition.
Then reboot the system and insert linux cd, the bootable cd which you use to install the Linux OS. Boot the system by recovery option and login to your local machine. In fedora it may cay to use chroot /mnt/sysimage. Then change the user to root by typing su. Then check the disk partitions by typing "fdisk -l" command. It will list down the partitions and "*" will show what is the bootable partition. Now you should activate the linux partition by the help of fdisk command. Now we must get a backup of the boot details by doing "dd" like this;
dd if=/dev/sda1 of=/fedora.bin bs=512 count=1
For "if", you must give the partition where the linux partitions are located in. Then mount the windows partition and mv the fedora.bin file to the root "c:\ drive" of the windows partition. Then edit the boot.ini file in the root of windows to;
c:\fedora.bin="Linux Fedora"
And make sure you change the time of the boot menu display. Then reboot the system. Now you can see there are two boot options in the windows bootloder. Select one of it and use it.
If the linux options gives errors you can use grub as the bootloder which is the default bootloder of the fedora. To do that you should again reboot and boot the system with the linux cd in recovery mode. Then update the grub by typing these commands;
root (hd0,0)
setup (hd0)
In this "hd0" means the hard disk which the linux is installed and the (partition number-1) is 0. Likewise you should setup the root (hd#,#) to Linux and reboot the machine. Then after reboot again it will not show the windows and directly boot the Linux. So login as root and add these to the menu.lst file like this.
title Windows
    rootnoverify (hd0,1)
    chainloader +1

As previous you should say where the windows boot files are located in.
Now after restarting the machine you may able to use both operating systems.

Sep 30, 2010

Exceptions and a simple program

Exception handling in clojure

Clojure supports java based exceptions. There are two types of exceptions in clojure. One is clojure exceptions and the other is user defined exceptions. If you type as;
user=>(/ 1 0)
This is devide 1 by 0. So this will give an exception.
So, this is the way to catch the exceptions.
user=>(try (/ 1 0 ) (catch Exception e (prn "Cach the exception" ))(finally (prn "In finally...")))

Now it will handle the exception. But if you use a wrong type of exception it will not catch that and in the end of the try statment it will give the error.

Writing a simple clojure program.
Now we will try to write a clojure program to add remove and change things.

(alias 'set 'clojure.set)
This wiil make an alias to clojure.set. Now we can use "set" word when ever we need to write clojure.set/smoething.

(defstruct employee :name :id :role)
This will create a structore named employee.
(def employee-records (ref #{}))
The "ref" is to allow mutation of a storage with transactions.

(defn- update-role [n r recs]
(let [rec (set/select #(= (:name %) n) recs)
others (set/select #(not (= (:name %) n)) recs)]
(set/union (map #(set [(assoc % :role r)]) rec) others)))
In this set of codes, "defn-" defineds a private function. As previously sed here we use set/select behalf of clojure.set/select.

(defn- delete-by-name [n recs]
(set/select #(not (= (:name %) n)) recs))
Both of these functions update and delete will do the operation and create a
new set of data, becouse these sequences are immutable.
Now you can see some public functions which will not cantaining a "-" sign when definding it.

(defn update-employee-role [n r]
"update the role for employee named n to the new role r"
(dosync
(ref-set employee-records (update-role n r @employee-records))))

(defn delete-employee-by-name [n]
"delete employee with name n"
(dosync
(ref-set employee-records
(delete-by-name n @employee-records))))

(defn add-employee [e]
"add new employee e to employee-records"
(dosync (commute employee-records conj e)))
Now the code is completed. Save this as test.clj and in the clojure prompt you
can import that coding by,

(load-file "test.clj")
Then you can do add delete and change employee details. Here is some
coding to add some data initially. Then after that you can view what you enered.(add-employee (struct employee "Jack" 0 :Engineer))
(add-employee (struct employee "Jill" 1 :Finance))
(add-employee (struct-map employee :name "Hill" :id 2 :role :Stand))
@employee-records

Clojure program Exception handling in clojure

Sep 29, 2010

More about monad haskell

Monad Whitespace

We can recognise one or more spaces by importing monads librerys. We can try by
this coding.
readExpr input = case parse (spaces >> symbol) "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value"

Return values
We can convert return valuer of some inputs. To do this we need to defined a data type which can store any data type. So for do that;

data LispVal = Atom String
| List [LispVal]
| DottedList [LispVal] LispVal
| Number Integer
| String String
| Bool Bool

This defineds a set of data types that the LispVal can hold.
Atom is a letter or a symbol which can be a charactor number or a symbol.

Generic monad
We can write our own monad thing by this way.

Sep 28, 2010

More on monad haskell

Monad transformers
Monads transformers are also in a formata of monads. So we must make MaybeT m an instance of the Monad class.
instance Monad m => Monad (MaybeT m) where
return = MaybeT . return . Just

This will get the monads return and send it ot the monadts constructore. This thing can be wriiten as,
x >>=f= MaybeT $ do maybe_value <- runMaybeT x case maybe_value of Nothing -> return Nothing
Just value -> runMaybeT $ f value

List transformer
newtype ListT m a = ListT { runListT :: m [a] }

We can transform list also and to do that we need to crerate a datatype with a constructoore which takes an argument.

LiftM
This is a library which can transfer the non-monadic functions to monadic ones. The function is like this.
liftM :: Monad m => (a1 -> r) -> m a1 -> m r

Converting non-monadic:- myFn $ NonMonadic
Converting monadic :- myFn `liftM` monadic

Pasing monad
This is more powerful parser with monads like Maybe, list and sate. This will use parcer librery functions also. So first of all we must import relevent librery functions to our program.
import System
import Text.ParserCombinators.Parsec hiding (spaces)
Now we are going to use a parser librery function call oneOf
symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=>?@^_~"

This will keep track of each charactor. Then we must make some arrangements to handle the errors too.
readExpr :: String -> String
readExpr input = case parse symbol "lisp" input of
Left err -> "No match: " ++ show err
Right val -> "Found value"

Now we can write the program.
main :: IO ()
main = do args <- getArgs
putStrLn (readExpr (args !! 0))

Now you can run this by doing
ghc -package parsec -o filename.hs

Clojure Basics

Run clojure.jar by,
java -jar clojure.jar
and it will prompt,
user=>
Clojure got three main numerical types. Integr, Float and Ratio.
To do 1 + 2 it is; (+ 1 2)
If you need to defind a function you can defid as,
The function you need to defind is a = 2 + 3
So you can write it as,
user=> (def a(+ 2 3))
Then it will give an output as
#'user/a
and prompt user=>
If you type "a" in that you will get the output of 2+3 as 5.

Number formats
user=> 10 decimal 10
010 octal 8
0xff hex 255
1.0e-2 double 0.01
1.0e2 double 100.0

And also clojure supports a radix based entry format, in a form of (radix)r(number)
(note : 1(Integer/toString 10)
"10"
user =>(Integer/toBinaryString 10)
"1010"

String to Integer
user=>(Integer/parseInt "B" 16)
11
user=>(Integer/parseInt "10" 16) ; 16r10
16
user=>(Integer/parseInt "101" 8) ; 8r101
65
User =>(Integer/parseInt "10101" 2)

Data Structures
Creating a stucture;
(defstruct employee :name :id)
Using the structures
(struct employee "Mr. X" 10)
We can make functions from these things and get values from them like;
(def e1(struct employee "Nimal" 324))
And if you type e1 it will give an output like
{:name "Nimal", :id 324}
Also you can get only a value you need by typing like;
(e1 :name)
This will print the name of e1.

We can use accessor function to get easy access of feilds. It is like this.
(def emp-name(accessor employee :name))
Now if you want to get the employee name of e1 you cann type as;
(emp-name e1)

Add or remove new fields to structures
We can only remove fields which are added by assoc, can't delete the fields which are defined by defstruct.
Adding
(def e1-new (assoc e1 :function "engineer"))
e1-new
It will give the output as;
{:name "Nimal", :id 324, :function "engineer"}
Removing
(def e1-rm (dissoc e1-new :function))
e1-rm

Sep 27, 2010

Starting clojure with Labrepl

Labrepl is an environment for exploring the Clojure language. It is a web base application.

Before downloading labrepl you must check if you got github, java and leiningen.
Then make a directory and download the labrepl repository into that directory. So for do that;


git clone git://github.com/relevance/labrepl.git

Change the directory into that and type;

lein deps to install all the dependent libraries. This will take some time.

Then run;
script/repl
This will launch the labrel.

Now you can goth http://localhost:8080 for the labs.

And press Ctrl+D to exit.

Haskell class declarations

Haskell class is not like classes in Java or c++.
Haskell got many numeric types, which are Int, Integer and Float. You can compare two numbers of the same type for equality but in not different types. Also you can compare two values of type Bool for equality, but you cannot add them together. A class is a template for types, and it gives the operations that the types must do.

class Eq a where
(==), (/=) :: a -> a -> Bool
It defines the == and /= functions.

Simple type decleration

plus :: Int -> Int -> Int
plus x y = x + y

This plus function will get 2 Int numbers, do the calculation and then it will return an Int too.

Monad
This is a very useful concept in haskell.
It,
Do an action from the type of which returns by the function.
Do an action which simply produce a value.
Do a chain "actions" together, while allowing the result of an action to be used for the second action

Maybe

farther :: Person -> maybe Person

This function will return data with type Person or if there is no value it will return nothing.

Note;
let x = foo in bar can writtern as (\x -> bar) foo
x<-foo; bar can writtern as foo>>=(\x -> bar)

Do notation
do putStr "Hello"
putStr " "
putStr "world!"
putStr "\n"

This will print in a sequence.

Then and do notations.

do action
other_action
yet_another_action

This is written by only using do naotation. but this can be wriiten by using bothe do and then notations like this;

action >>
do other_action
yet_another_action

msum

msum :: MonadPlus m => [m a] -> m a
msum = foldr mplus mzero

Sep 24, 2010

Sorting a list in haskell

We can write a hascell coding for sort a list in assending or desending order.

quickSort []=[]
quickSort [x]=[x]
quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more)
where less = filter (< x) xs equal = filter (== x) xs more = filter (> x) xs


write this in a hakell file and load it into the intepreter. Now type;
quickSort ["i", "have", "a", "thing", "for", "linux"]
It will give the output like this.
["a","for","have","i","linux","thing"]

But if you type as;
quickSort ["I", "have", "a", "thing", "for", "Linux"]
you will get the output as;
["I","Linux","a","for","have","thing"]
this. Sorting is done by looking at the ASCII values and capital letters got less value than the others. So to avoid that thing we must ignore the case of the characters.

Modules
Module is a group of related functions in a singe package. We can use functions for our codings without implementing them. The only thing we must do is import the correct module and call the correct function. There are many ways of importing modules.

import Data -- import everything from Data module
import Data.List -- import everything exported from Data.List
import Data.Char (toLower, toUpper) -- import only the functions toLower and toUpper from Data.Char

Indentation
Haskell is depend on indentation. So you must think about the indentation when you are writing a haskell coding. Basic things are like this.
let x = a
y = b

do foo
bar

where x = a
y = b

case x of
p -> foo
p' -> baz

myFunction firstArgument secondArgument =
do foo
bar

or
myFunction firstArg secondArg = do foo
bar

Foo and bar must inline
If else statment --

if foo
then do first thing
second thing
third thing
else do something else

Setting the clojure-lein mode in emacs

First you should get the emacs configuration file. It is in your home directory as .emacs or .emacs.d/init.el (you should use ls -la to view the hidden files)

In that file you must insert this.

(let ((buffer (url-retrieve-synchronously
"http://tromey.com/elpa/package-install.el")))
(save-excursion
(set-buffer buffer)
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(eval-region (point) (point-max))
(kill-buffer (current-buffer))))

This will download come congigeration packages into the .emacs or .emacs.d directory and update the init.el file. Then you should remove the above code part (only that thing) from the init.el file.
Then open an emacs window and type

M-x package-list-packages
Then type r to update the list. And then select package (clojer-mode) from i and install it by x.

Then type;
M-x describe-variable and in the prompt type;
inferior-lisp-program

In that you should customize the variable to;
lein repl
then save it.
Now open a .clj file from emacs.

MySQL access form a Haskell script

This script will connect to a mysql database via haskell script.
Firs of all you must need to download some modules which used to communicate with the database. For that you may you can use
hackage.haskell.org/package/
This link.

You should download and install,
HDBC module and
HDBC-mysql module.

If you encount some problen while you are installing those modules, you should check the dependenci packages as well. You should install those thing also inorder to run those packages without errors. Dependence details are in the package webpage.

Then you can create a test database to test the script. So for that do these thing in MySQL.

>CREATE DATABASE animal
>CREATE TABLE `animal` (`name` char(40) DEFAULT NULL,`category` char(40) DEFAULT NULL)
>INSERT INTO `animal` VALUES ('snake','reptile'), ('crockadile','amphibian')

Now you should create the database.

import Database.HDBC
import Database.HDBC.MySQL

run_quary = do
conn <- connectMySQL defaultMySQLConnectInfo { mysqlHost = "localhost", mysqlDatabase = "animal", mysqlUser = "username",mysqlPassword = "", mysqlUnixSocket="/var/lib/mysql/mysql.sock"}
quickQuery' conn "INSERT INTO animal VALUES ('tuna','fish'),('racoon','mammal')" []


In this script you must change the user name, password and the mysqlUnixSocket path. The user name and its passwd should be able to access the animal database. You can get the mysql.sock path from the file /etc/my.cnf

Clojure

Clojure is a dynamic programing language which use the java virtual machine. It directly compile the source code into java bytecode.
Doing projects with leingingen
Leiningen is a simple building tool for clojure. It will make "Build" files for your clojure project.

You can install leniingin by this script. Alsoit will install clojure for you.

http://github.com/technomancy/leiningen/raw/stable/bin/lein

First download the lein file from this. Then,
chmod +x lein
./lein self-install

It will install all the nessesary things.

Now we can create the project. I will create a simple project to display "Hello World". So for do that;

mkdir helloworld
mkdir helloworld/src
touch helloworld/src/helloworld.clj

Then you should edit the helloworld.clj file like this.

(ns helloworld
(:gen-class))

(defn -main [& args]
(println "Hello world"))

Then you must create a file call project.clj in the helloworld directory. That file should contain,

(defproject helloworld "0.1"
:dependencies [[org.clojure/clojure
"1.1.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib
"1.0-SNAPSHOT"]]
:main helloworld)

Now you had complete the source files and project details files. It is time to compile the project.
To compile the project you must type,

$ lein compile
This will compile the project and create lib file which contains all jar files of the project. But we can create a single .jar file for the project. To make it,

$ lein uberjar

This will create two jar files, you can distribute your project with the standalone.jar file.
to run the jar file you must type,

java -jar .jar

Sep 23, 2010

selenium with python

Selenium is a good testing tool which can be controled by many programing languages. So here is some sample coding in python.

from selenium import selenium
import unittest

class TestGoogle(unittest.TestCase):
def setUp(self):
self.selenium = selenium("localhost", \
4444, "*firefox", "http://www.google.com/webhp")
self.selenium.start()

def test_google(self):
sel = self.selenium
sel.open("http://www.google.com/webhp")
sel.type("q", "hello world")
sel.click("btnG")
sel.wait_for_page_to_load(5000)
self.assertEqual("hello world - Google Search", sel.get_title())

def tearDown(self):
self.selenium.stop()

if __name__ == "__main__":
unittest.main()

This will test the google.com website.

Sep 21, 2010

Installing Selenium

Selenium is a good web application testing tool which is a free and open source software. It can run in many browsers, inmany operating systems and can controled by many programing languages and testing frameworks.
You can get more datails as well as the software from visiting the official website http://seleniumhq.org

First of all download the selenium ide from the website.

Then extrat the zip file.

Now you can see some jar file as well as some library files.

So first run the selenium server. For that you may need java. If you do not have java, you must install JDK first.
java -jar selenium-server.jar

Wait for about 1 min. It will give the configurations of your system.

Then you should deside which language you are using to control the selenium. I will use python but you can do it in come other language.(see the website)

Now you should change the directory to python library file. There you can see selenium lib files for python.
Then try to run a default test. To do that;

python test_default_server.py

So now you can write your script in this folder and run it.

Sep 20, 2010

Database connect via python

fgTo connect to a database we must first install a special module to communicate with the database.If the database is MYSQL you must install MySQLdb module.

To do that you must type;

yum install MySQL-python

After the instalation you shoyld be able to; >>>import MySQLdb

If this thing won't give any error you can continue.

First we must import modules which we need to runour script. So;

import sys
import MySQLdb

Then we must connect to the databace and to do that;

try:
conn=MySQLdb.connect(host="",user="",passwd="",db="")

except MySQLdb.Error,e:
print "Can not connect to the database"
sys.exit(1)

Now we are connected to the database. Now we need to send querys to our database.

Try:
cursor=conn.cursor()
cursor.execute(""" your query """)
cursor.close()
except MySQLdb.Error,e:
priint "Some error...!!!"
sys.exit(1)

You had done all the things for now. So you shoul close the connection which you open.

conn.commit()
conn.close()
sys.exit()

Haskell - filter

Filter
If a list meet a certain condition it will
create another list according to that. Try this,
retainEven :: [Int]->[Int]
retainEven []=[]
retainEven (n:ns)=
if (mod n 2 == 0)
then n:(retainEven ns)
else retainEven ns

If you run this program like

retainEven [12,13,14,15,16]
You will get the output as;
[12,14,16]

If then else statment with strings

msg :: Int -> String
msg n =
if n< 10 then "Less than 10" else "Equal or greater than 10" Let and where addStr :: Float -> String -> Float
addStr xstr = x + read str
sumStr :: [String] -> Float
SumStr = foldl addstr 0.0

But You can write this by using let keyword easilly.
sumStr=
let addStr x str = x + read str
in foldl addStr 0.0

or else you can write it by using where keyword like this.
aumStr = foldl addStr 0.0
where addStr x str = x + read str

Sep 17, 2010

Haskell folds

Folds - a flod apply a function to a list, accumilate items and give a single output.
There are 4 main fold functions;
foldr - this will fold up a list from the last element to the first element and apply a given function for each element.

fac n = foldr (*) 2 [1..n]

if you run this code and type as;
fac 3
it will give the output as
12
What will happen in this is,
3*2 + 2*2 + 1*2 = 12

foldl - this will go from the first element to the last element.
foldl1 and foldr1 -
these things will work as the way the foldl and foldr work. But this will give an error message when it is an empty array.

scan
This do the both things done by map and fold. It will accumilate values like fold but the output will be given as a list as in map for each intermediate value.

scanl (+) 0 [1,2,3]
this will give the output as
[0,1,3,6]

Data types

data SpecialDay = Birthday Int Int Int
| Wedding String Int Int Int
SpecialDay is a data which can be ether a birthday or a wedding. So,

mySpeDay :: SpecialDay
mySpeDay = Birthday 1988 09 16

Installing PostgreSQL database.

PostgreSQL is a very powerfull open source database. You can download the binary package or the source code by visitiing the website.
http://www.postgresql.org

I will describe how to install this by using the source code.

First of all you must download the source code from the website. It may like;
postgresql-.tar.bz2
Decompress the tar file.
Then change the directory into the decompressed diredtory.
Now you can see source files of the software.

Now type;
./configure
Then after that;
gmake
Then you should become a super user to comtinue the instalation.
For that type;
su
then if it ask for the password. Give the password and type,
gmake install

This will copy all files which needed to the software. But the instalation is not over. This database need an user called postgres. So to add this user to your system you must type;
useradd postgres
Then do these instructions
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
The last instruction will change the user root to postgres.

Also you should type some instructions to make the configuration files. For that;
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

/usr/local/pgsql/bin/postgres -D /user/local/pgsql/data >logfile 2>&1 &

So now you had compleated the instalation part.
Then you must create a database. For that,

/usr/local/pgsql/bin/createdb

To use that database;
/usr/local/pgsql/bin/pqsl

Sep 16, 2010

Installing a proxy server

Proxy is used to control network trafic and access controling. It will increase the network speed by caching the websites which freequently access by the users. It can also restrict ip addresses as well as protocls.

squid-cache is a proxy server which we can freely download and use.

You can download this by visiting the website http://squid-cache.org .

Download a stable vertion of squid.
There may by some distribution which are still on testing.

If you download a tar file decompress it.
Then change the directory to the squid director.

Now you can read the INSTALL file. It will say to run configuratoion file.
like
./configure --prefix=/usr/local/

This instalation will be placed in /user/local/ directory.
then type;
make all
Now you must switch to super user mode.
For that;
su -
After that;
make install

Now the instalation will run.
After the instalation you must customised the squid.conf file. It will be located in;
/usr/local/squid/etc/squid.conf

In that folder you may see some examples and those things will help you to do your task.

Then run the squid by typping
/usr/local/squid/sbin/squid

In that time if some permition error occure you may goto the directory,
/usr/llocal/squuid/
Then add a user and a group call squid by typing

useradd squid
groupadd squid

then type
chown squid -R var
chgrp qsuid -R var

And also change line of the squid.conf which like
#cache_effective_user nobody to
cache_effective_user squid

Then try to restart the squid.

More about list

Dot Dot notation
You can get a sequence of integer numbers like;
[1..10] it will give the result as [1,2,3,4,5,6,7,8,9,10]
And also if you type it as
[1,3..10] it will be like
[1,3,5,7,9]
And also on the other way around too.
[5..0]
[5,4,3,2,1,0]

Never ending list
This is like a never ending loop;

loop n=n: loop(n+1)
runLoop = loop 1

If the user type;
runLoop
The haskell will give a never ending loop.

Head and tali
If you type;
let list = [1,2,3,4,5,6]
head list
it will give the output as
1
tail list
it will give the output as
[2,3,4,5,6]

This will devide a list to two parts like (x:sx)