Monday, November 5, 2007

Distributed Service Delivery with Coadunation Part 3 (Speaking to a Java Demon)

This is the third part of "Distributed Service Delivery". It describes the implementation of a simple web application that communicates with the daemon implemented in "Part 2".

The example utilizes the following technologies:
1) Coadunation, available from Coadunation.
2) Netbeans ID, available from Netbeans.

Recap
In Part 2 we created a simple Coadunation Daemon named 1001-BasicDaemon. This daemon was implemented using the Netbeans Java library tool, and deployed in Coadunation using its automated deployment process. This daemon was an example of a distributed service that can be controlled through the use of Coadunation.

Creating a Web Application

Create a new project in Netbeans, using the following steps:
1) Click on new project button.
2) Select the Web Category.
3) Select the Web Application out of Projects.
4) Click the Next button.
5) Enter a Project Name, "BasicDaemonCommunication".
6) Enter a location to store the project.
7) Select the bundle Tomcat Server.
7) Click the Finish button.

Once this has been done a basic Web Application project will be present in Netbeans.

Library Reference

In order to communicate with the daemon, the Web Application must first have a reference to the 1001-BasicDaemon. This is done using the following steps:

1) Right click on libraries within the Web Application project to bring up the context menu.
2) Select "Add Project".
3) Using the file browser presented by Netbeans browse the file system until the the 1001-BasicDaemon is within the browser pane.
4) Select 1001-BasicDaemon.
5) Click "Add Project JAR Files"

The Web Application now has a link to the Daemon library and will include it within the war created by this project. (For best practice on Daemon creation look at the Coadunation getting started guide.)

Implementing a simple JSP page

This example implements a simple JSP page that communicates with the daemon, it is equally easy to make a JAVA Servlet or POJO communicate with the daemon.

Follow the steps below:

1) Select the index.jsp page created for the project. This is found in the "Web Pages" section of the project.
2) Add the necessary imports for this project at the top of the jsp page as follows.

<%@page import="javax.naming.Context"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="org.example.BasicDaemon"%gt;

3) Add the code to the page to narrow the basic daemon and call the helloWorld method on the daemon interface.

<%
// Setup the context
Context context = new InitialContext();
// narrow the daemon
BasicDaemon daemon = (BasicDaemon)PortableRemoteObject.narrow(context.lookup("BasicDaemon"),BasicDaemon.class);
// call the method
String result = daemon.helloWorld("test message");
%>

4) Print the message.

<br>message[<%=result%>]

Security Configuration

In order to communicate with the daemon the security on the Web Application must be setup. This example will use basic authentication though forum based authentication is also supported.

1) Select the web.xml under WEB-INF.
2) Select the Security tab.
3) Open up the Login Configuration section.
4) Select Basic authentication.
5) Click on the add button for the "Security Roles".
6) Enter "admin" as the role name.
7) Enter "admin" as the role description.
8) Click on the "OK" button.
9) Click on the "Add Security Constraint Button".
10) Enter a display name of "test".
11) Click on the add button for "Web Resource Collection:".
12) Enter a name of "test".
13) Enter a description of "test".
14) Enter a url patter of "/".
15) Select all http methods.
16) Click on the "OK" button.
17) Click on the "Enable Authentication Constraint" check box.
18) Enter a description of "test" for the "Enable Authentication Constraint"
19) Click on the edit button for role names under "Enable Authentication Constraints".
20) Click admin.
21) Click the "add" button.
22) Click the "OK" button.
23) Save the file changes.


Compile and Deploy

The deployment process of the Web Application project is the same as the deployment process for the daemon.

1) Build the project by left clicking on the project name and selecting build.
2) Using a file browser or command line tool copy the BasicDaemonCommunication.war into the deploy directory of the installed Coadunation instance. (If Coadunation is already running the Web Application will be automatically deployed by Coadunation, otherwise upon startup the Web Application will be loaded.).

Browse the Web Application

To see the web application in action follow these steps:

1) Open a web browser.
2) Go to the url of the web application running in Coadunation. If Coadunation is running on the local host the url should look like the following http://localhost:8080/BasicDaemonCommunication/
3) Enter the user name and password for the administrator of Coadunation. These are by default
username: admin
password: 112233
4) Look at the page. A message line should appears as follows:
message[Hello world]

Closing

As can be plainly seen in parts 1 - 3 it is very easy to setup a Daemon using Coadunation that can be managed through a Web Application. Using Coadunation means that a developer does not have to be concerned with implementing security, setting up inter process communication technologies such as CORBA, RMI or Web Services, configuring data sources, implementing a configuration technology, etc. They need only be concerned with solving the problem at hand.

For more information on Coadunation vist http://www.coadunation.net.

Monday, October 29, 2007

Distributed Service Delivery with Coadunation Part 2 (A Simple Java Demon)

This is a continuation of "Distributed Service Delivery". It takes the concepts discussed in the first document and implements a simple daemon example.

The example utilizes the following technologies:
1) Coadunation, available from Coadunation.
2) Netbeans ID, available from Netbeans.

To implement distributed service delivery, there must be a service in this example a Coadunation Daemon and a mechanism of administering the service in this case a simple web application.

To start with, I implemented the basic daemon. This consists of a Daemon Interface definition, the Daemon Implementation and a simple XML Coadunation configuration file. (There is more information on this in the Coadunation Documentation.)

Creating the Daemon Project

Create a new project in Netbeans, using the following steps:
1) Click on new project button.
2) Select General project.
3) Select Java Class Library.
4) Click the Next button.
5) Enter a Project Name, "1001-BasicDaemon" (The numbers at the front indicate start order to Coadunation. This is similar to the Unix rc start order process.)
6) Enter a location to store the project.
7) Click the Finish button.

Once this has been done a basic Java Class Library project will be present in Netbeans. It is now simply a matter of defining the components of the daemon, starting with the Java interface.

Creating the Daemon Interface

Coadunation supports both local and remote interfaces, but unlike EJB's it is possible to use the same interface for both local and remote, the process of narrow is the only difference. Like EJB's a local interface is only available within the same jar. As this example requires that the web application is able to make calls on the daemon it must be both and not just a local interface.

To implement a remote interface, simply extend java.rmi.Remote, and adhere to the RMI interface standard.

Create a new interface file in the project by following these steps:
1) Right click on the source package and select "New" than "File/Folder".
2) Select "Java Classes" out of the "Categories".
3) Select "Java Interface" out of the "File Types".
4) Click the Next button.
5) Enter a Interface Name eg, "BasicDaemon".
6) Enter a package location eg, "org.example".
7) Click the Finish button.

Once this is completed a new Java Interface will be present in the editor panel. It should look something like the following.

/*
* BasicDaemon.java
*
* Created on October 30, 2007, 6:35 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.example;

/**
*
* @author brett
*/
public interface BasicDaemon {

}


Now convert the interface to a remote interface by extending java.rmi.Remote. The interface defintion should now look like the following:


public interface BasicDaemon extends java.rmi.Remote {


In this example the only thing left to do on the interface is the method definition. This method must adhere to RMI standards, this means that it must through java.rmi.RemoteException. The implementation need not but the definition must.

A simple method as follows will suffice for this example:


/**
* A simple hello world example
*/
public String helloWorld(String msg) throws java.rmi.RemoteException;


Creating the Daemon Implementation

Now that the Interface is defined it must be implemented by a Java class. Create a class using the following steps:
1) Right click on the source package and select "New" than "File/Folder".
2) Select "Java Classes" out of the "Categories".
3) Select "Java Class" out of the "File Types".
4) Click the Next button.
5) Enter a Class Name eg, "BasicDaemonImpl".
6) Enter a package location eg, "org.example".
7) Click the Finish button.

As with the Java Interface a Java Class should now be present in the editor and look like the following:


/*
* BasicDaemonImpl.java
*
* Created on October 30, 2007, 7:00 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.example;

/**
*
* @author brett
*/
public class BasicDaemonImpl {

/** Creates a new instance of BasicDaemonImpl */
public BasicDaemonImpl() {
}

}



The class must now be modified to implement the interface, this is done by using the Java "implements" key word, appending the name of the interface and by implementing the interface defined method. The class definition should look like the following once the interface has been implemented:


/**
*
* @author brett
*/
public class BasicDaemonImpl implements BasicDaemon {

/** Creates a new instance of BasicDaemonImpl */
public BasicDaemonImpl() {
}

/**
* A simple hello world example
*/
public String helloWorld(String msg) {
System.out.println(msg);
return "Hello world";
}
}


In the above example the helloWorld method prints out the "msg" string passed to it and returns the string value "Hello world".

Creating the Daemon Configuration

At this point in time the daemon is almost ready for deployment in Coadunation, all that is required is the configuration file. This file must be added to the META-INF directory within the library. To add the file follow these steps.

1) Right click on the source package and select "New" than "File/Folder".
2) Select "XML" out of the "Categories".
3) Select "XML Document" out of the "File Types".
4) Click the Next button.
5) Enter the "File Name" as "coadunation"
6) Enter the "Folder" as "src/META-INF".
7) Click the Next button.
8) Select "Well-Formed Document"
9) Click the Finish button.

Netbeans will now present an XML document that looks like the following:


<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : coadunation.xml.xml
Created on : October 30, 2007, 7:19 AM
Author : brett
Description:
Purpose of the document follows.
-->

<root>

</root>



This document will have to be modified to look like the following:



<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : coadunation.xml
Created on :
Author : Author
Description:
This is a basic daemon example
-->

<coadunation version="1" name="BasicDaemon">
<beans>
<bean>
<interface>org.example.BasicDaemon</interface>
<class>org.example.BasicDaemonImpl</class>
<bindname>BasicDaemon</bindname>
<role>daemon</role>
</bean>
</beans>
</coadunation>



The section in the xml are broken down as follows:

name: The name of the Daemon.
interface: This is the Java interface that will be exposed by the daemon.
class: This is the Java class that implements the interface.
bindname: This is the name that will be bound to the JNDI.
role: The security role required to access this daemon.

(For more information on the Coadunation configuration file consult the Coadunation Documentation.)

Deploying the Daemon in Coadunation

It is now simply a matter of deploying the daemon in Coadunation. This requires a running instance of Coadunation. Which can be downloaded from the coadunation web site. It is available in either an install jar or source code.

Once Coadunation has been installed it can be started by running the "run.sh" (On unix) or "run.bat" (On windows). This script is found in the bin directory of the Coadunation install.

It is now simply a matter of copying the compiled jar from the Netbeans project into the deploy directory of the Coadunation install. Once this is down Coadunation will auto deploy the daemon.

To compile the project in Netbeans right click on project name and select "Build Project". Once this is done a dist directory will be create in the project folder. Within this directory is the compiled jar for the project.

End of Part 2
This ends part 2. For more information visit the Coadunation web site.

I will explain the web application implementation in Part 3.

Monday, October 22, 2007

Distributed Service Delivery with Coadunation

Coadunation is a distributed daemon server environment. This is a similar concept to an application server but aimed at daemons. This means that it provides a simple container based framework in which daemons can be implemented. Because of this approach it is possible to implement both daemons and web application in the same environment.

This is made possible through the implementation of a Tomcat Application Server daemon. This daemon provides the ability to implement web applications that communicate seamlessly with the deployed daemons in a Single Sign on environment (SSO).

Any deployed web application is able to communicate with any of the deployed daemons using RMI. The RMI references are made available via simple JNDI lookup.

The JNDI implementation with in Coadunation supports the standard "java:com/env" lookup provided by most application servers but also a "java:network/env" lookup. This lookup enable applications to perform a search for a daemon in a distributed Coadunation environment.

This means that it is possible to setup several Coadunation instances, bind them together and perform a simple JNDI lookup from a web application for a daemon, and than make the necessary requests on that daemon (For more information on the JNDI refer to the Coadunation getting started guide.).

Along with the JNDI implementation, Coadunation supplies a Service Broker daemon. This broker provides the ability to associate a service with a daemon. This means that it is possible to search for a service by name such as "world" within a distributed environment and find the JNDI looup for a service implementing "world". (Note: the service broker requires that a service be register with it when the daemon is initialized via Coadunation. More information can be found in the Coadunation getting started guide.).

Using these concepts, an application can be developed quickly and easily that controls the delivery of any service from front web application to distributed back-end daemon. A step by step exampled will be supplied in part 2.

There are existing examples provided with the Coadunation distribution, available at http://www.coadunation.net. Simply download the latest Coadunation Installer, read the getting started guide and get programing.

Part 2