Developing SOA Applications using POJOs

Exclusive offer: get 50% off this eBook here
GlassFish Administration

GlassFish Administration — Save 50%

Administer and configure the GlassFish v2 application server

$26.99    $13.50
by David Salter | April 2010 | Java

PoJos – Plain Old Java Objects, are, as the name describes, simple, ordinary Java objects. They do not have to be derived from a specific class, nor do they have to implement any specific interfaces. In the modern world of Java EE development where the latest frameworks have made enterprise development easier, wouldn't it be good if we could develop SOA based applications using PoJos? Fortunately, GlassFish ESB (OpenESB) allows us to do this using the PoJo Service Engine. In this article by David Salter, author of Building SOA-Based Composite Applications Using NetBeans IDE 6 and Seam 2.x Web Development, we will cover:

  • GlassFish ESB
  • Creating a PoJo and Deploying to GlassFish ESB
  • Creating A SOAP Binding for PoJos
  • Testing the Composite Application

GlassFish ESB

You may previously have used OpenESB, and if so you will be familiar with GlassFish ESB. GlassFish ESB is simply the OpenESB core runtime, bundled with the GlassFish application server, the NetBeans IDE and some of the more common JBI components already deployed to the ESB.

With release 2.2 of GlassFish ESB, the PoJo Service Engine has been included with the software download. Both GlassFish ESB and a number of additional JBI Components can be downloaded from the Open ESB website at: https://open-esb.dev.java.net/Downloads.html.

Creating a PoJo and Deploying to GlassFish ESB

Using GlassFish ESB, its easy to create PoJos and deploy them to the JBI runtime adding different bindings such as SOAP or REST to it or to deploy them to the BPEL engine and use them as part of a business process. By deploying to GlassFish ESB, we are allowing many different types of client to consume our PoJos.

For this article, I'm going to write a simple PoJo that reverses strings and then deploy this to GlasFish ESB with a SOAP binding. This will allow any web service client that supports SOAP to access the PoJo.

If you are familiar at all with NetBeans, then creating a PoJo for deployment to the ESB will be a simple matter. The first stage is creating a Java project in which the PoJo can be created. Note that PoJos are created in standard Java projects and not in a SOA project template.

Using the "New Project" option in NetBeans, create a new "Java Class Library" project and call it ReversePoJo.

Developing SOA Applications using POJOs

Upon creating a standard Java Project, PoJos can be created in the project by using the New PoJo Service menu option under the ESB category. Select this option and create a new PoJo Service as shown below.

Developing SOA Applications using POJOs

Developing SOA Applications using POJOs

Class Name: ReversePoJo
Package: com.davidsalter.soa.reversepojo
Method Name: reverseString
Input Argument Type: String
Return Type: String

When we press the Finish button, NetBeans will create an empty PoJo using the details supplied. The code is shown below.

@Provider
public class ReversePoJo {

public ReversePoJo() {
}

@Operation (outMessageTypeQN="{http://reversepojo.soa.
davidsalter.com/ReversePoJo/}ReversePoJoOperationResponse")
public String reverseString(String input) {
return input;
}
}

Looking at this code, we can see that this is indeed a simple PoJo. The class does not extend any special framework classes and does not need to implement any framework interfaces. We can see however that there are two annotations used on the class which are required to allow us to deploy the class to GlassFish ESB.

@Provider defines that the PoJo is a JBI provider, i.e. it provides business logic to other components. In this case, we are stating that the ReversePoJo class can provide business logic to other components, either by adding bindings to the service and exposing directly from the ESB, or being invoked within the ESB from the BPEL runtime.

@Operation defines methods in the class that can be consumed - that is methods that other components can invoke. In the simplest case, @Operation methods take a String as a parameter and return a String. More complex types such as org.w3c.dom.Node and javax.jbi.messaging.NormalizedMessage can however be used for both input and output parameters.

To implement the reverseString method in this class, add the following implementation.

@Operation (outMessageTypeQN="{http://reversepojo.soa.
davidsalter.com/ReversePoJo/}ReversePoJoOperationResponse")
public String reverseString(String input) {

StringBuffer reverse = new StringBuffer();
for (int i = input.length(); i > 0; i--) {
reverse.append(input.charAt(i-1));
}

return reverse.toString();
}

This code simply takes the input argument, reverses it and returns it to the caller.

One of the benefits of writing PoJos for deployment to the ESB runtime is that the PoJos can be easily tested. Unit tests can be added to a NetBeans project by selecting the Tools | Create Unit Tests menu option in the project explorer. A simple unit test for this class is shown below.

public class ReversePoJoTest {

public ReversePoJoTest() {
}

@Test
public void testReverseString() {

String input = "abcdefg";
ReversePoJo instance = new ReversePoJo();
String expResult = "gfedcba";
String result = instance.reverseString(input);
assertEquals(expResult, result);

}
}

GlassFish Administration Administer and configure the GlassFish v2 application server
Published: December 2009
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

Creating A SOAP Binding for PoJos

So far, we've looked at PoJos and seen how we can create a PoJo project that can be deployed to the ESB, and how we can create PoJos within the project. To deploy our ReversePojo to the ESB, we need to add a SOAP binding to it. Within GlassFish ESB, we do this as a Composite Application.

To create a Composite Application, select File | New Project and create a Composite Application from the SOA category on the New Project dialog screen.

Developing SOA Applications using POJOs

Create a new Composite Application called ReverseCompositeApplication as shown in the following screen shot.

Developing SOA Applications using POJOs

Project Name: ReverseCompositeApp

Upon creating a blank composite application, the design surface for the application is shown.

Developing SOA Applications using POJOs

This surface allows the developer to add different JBI consumers and providers (such as the PoJo we developed earlier) to the composite application, wire them together and add different bindings to the application.

For this sample application, we first need to drag and drop the ReversePoJo java application into the JBI modules section of the design surface. This then registers our PoJo as a JBI module to use in the application. Next, we need to add a SOAP binding onto the application. This is done by dragging and dropping a SOAP WSDL binding from the WSDL Bindings palette onto the WSDL ports of the design surface. At this point we need to build the Composite application so that NetBeans can correctly display the components and we can wire them together. After building the application, simply drag a connection from the SOAP binding consumer port to the PoJo provider port to complete the application.

Developing SOA Applications using POJOs

What we've done here is created a Composite Application that uses the ReversePojo JBI module as a service provider. We've then added a SOAP binding onto the Composite Application so that external clients can access the PoJo via SOAP calls.

Testing the Composite Application

Now that we've written our simple composite application, we need to test it to ensure that it is all working as expected. To create a new test case, right click on the Test node for the ReverseCompositeApp and select New Test Case.

Developing SOA Applications using POJOs

When creating the test case, NetBeans will display a list of all of the WSDL files found within the Composite Application so that you can specify which one to test. We want to select the WSDL for the composite application so that we can perform a full integration test on the application. On the Select WSDL Document dialog, select ReverseCompositeApp.wsdl

Developing SOA Applications using POJOs

After selecting the WSDL to test, we need to select the operation we wish to test. Select ReversePoJoOperation on the following screen.

Developing SOA Applications using POJOs

Finally, select Finish to create the test case. When the test case is created, NetBeans will automatically show the XML that is to be passed to the web service for testing. This is stored in a file called Input.xml.

<soapenv:Envelope xsi:schemaLocation=
"http://schemas.xmlsoap.org/soap/envelope/
http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:rev="ReverseCompositeApp">
<soapenv:Body>
<rev:ReversePoJoOperation>
<part1>abcdefg</part1>
</rev:ReversePoJoOperation>
</soapenv:Body>
</soapenv:Envelope>

Edit this file and change the value of the <part1/> element as shown above. This data will be passed into the reverseString method of the ReversePoJo class, so we would expect to get a return result the same as we did for the JUnit test earlier - gfedcba.

Right click on the unit test and select the Run option to start the unit test. NetBeans will package up the Composite Application and deploy it to the JBI runtime, starting GlassFish if necessary. Since we have not specified any expected test results, NetBeans will ask if the results from the test are to be stored for future test runs.

Developing SOA Applications using POJOs

If we select Yes, the results are stored in the Output.xml file so when future tests are run, NetBeans will compare the test results with this file to indicate whether the tests are successful or not.

Opening up the Output.xml will show the test results.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=
"http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/
http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<m:ReversePoJoOperationResponse xmlns:m="ReverseCompositeApp"
xmlns:msgns="http://reversepojo.soa.davidsalter.com/ReversePoJo/">
<part1>gfedcba</part1>
</m:ReversePoJoOperationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Hopefully, if everything has gone to plan, you will see the the <part1> element successfully contains the value gfedcba - the reverse of the input string.

Summary

In this article I've shown how GlassFish ESB can be used to create and deploy PoJo based JBI components and how composite applications can be created to consume the PoJo services with different WSDL bindings.

Although a simple example, hopefully this shows the power of the GlassFish ESB runtime which when combined with the design time tools of NetBeans can greatly aid development of integration and SOA based applications.

The source code for this article can be downloaded from: http://code.google.com/p/java-ee-samples.

Oracle SOA Suite 11g R1 Developer's Guide Develop Service-Oriented Architecture Solutions with Oracle SOA Suite with this Oracle book and eBook
Published: July 2010
eBook Price: $35.99
Book Price: $79.99
See more
Select your format and quantity:

About the Author :


David Salter

David Salter is an enterprise software developer who has been developing software professionally since 1991. His relationship with Java goes right back to the beginning, using Java 1.0 to write desktop applications and applets for interactive websites.

David has been developing Enterprise Java applications using both the Java EE standards and open source solutions for the last 10 years.

David is the co-author of Building SOA-based Composite Applications using NetBeans and the author of Seam 2.x Web Development.

Books From Packt


GlassFish Security
GlassFish Security

SOA Patterns with BizTalk Server 2009
SOA Patterns with BizTalk Server 2009

SOA Cookbook
SOA Cookbook

Business Process Driven SOA using BPMN and BPEL
Business Process Driven SOA using BPMN and BPEL

SOA Governance
SOA Governance

Building SOA-Based Composite Applications Using NetBeans IDE 6
Building SOA-Based Composite Applications Using NetBeans IDE 6

Service Oriented Architecture with Java
Service Oriented Architecture with Java

Service Oriented Java Business Integration
Service Oriented Java Business Integration


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software