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.

No comments: