Friday, November 26, 2010

Oracle SCA Spring Bean Basics

Apache Tuscany is an Open Source implementation of the Service Component Architecture (SCA) standard programmed in Java and C++. Tuscany was my first contact with the SCA technology. When I played around on Tuscany I remember a simple and straightforward POJO-based Calculator sample application. This application demonstrates the simplicity to implement a Java POJO service on the SCA Container and offer the service as SOAP/HTTP and REST Web Service.

Since my first steps with an early beta version of the Oracle SOA Suite 11g I was missing this simplicity to just implement a Java POJO service on the SCA Container from Oracle. Since Oracle SOA Suite 11g Patch Set 2 it is possible to have Spring Beans as part of the Spring Framework on the Oracle SCA Container as first-class citizen. Therefore Oracle adds the possibility to have Java POJO services in terms of Spring Beans.

Let’s follow the simple Calculator example from the Apache Tuscany “Getting Started” web page and see how to implement the Calculator application on the Oracle SCA Container. I have chosen this Calculator example because it’s separated on smaller function blocks (SCA Components) demonstrating the flexibility and modularization of SCA Services. Smaller components can be easily wired to more complex components.

In case of the Calculator example, the application can be divided into five functional blocks: Add Service Component, Subtract Service Component, Multiply Service Component, Divide Service Component and a main block that takes a request and routes it to the right operation. This main block is called the Calculator Service Component.

image

The Calculator application is implemented with SCA Components which are placed on a SCA Composite. All the Components are implemented with Spring Beans connected (“wired”) with other Spring Beans.

The following describes the Spring Bean creation in a step-by-step approach. I assume that you have installed the “Oracle SOA Composite Editor” and “Spring & Oracle WebLogic SCA” extension on Oracle JDeveloper.

1. Create a JDeveloper Application with a meaningful name (e.g. OracleSCASamples)
clip_image003

2. Name the project “Calculator” and add SOA as project technology
clip_image004

3. Take the “Empty Composite” as project template
clip_image005

Afterwards you have an JDeveloper SOA project with an empty SCA Composite. On JDeveloper we have a one to one relationship between projects and composites.

clip_image007

Each Calculator operation on the Tuscany example is implemented as own SCA Component. So let’s create SCA Components for the AddService, SubtractService, MultiplyService and DivideService. The next steps are showing the creation of the AddService. The AddService Component will provide a service that adds two numbers together. The CalculatorService Component uses the AddService Component whenever it is asked to perform additions.

Later we do the same implementation steps for the other needed Calculator services.

4. Create a Java Class starting from the JDeveloper Technology Gallery
clip_image008

5. Name the class “AddServiceImpl” on the “calculator” package
clip_image009

6. Choose the “Calculator\src” folder as destination
clip_image010

7. Develop the “add” operation of the AddServiceImpl class. As you see, we just implement the AddService as Plain Old Java Object (POJO).
clip_image011
8. “Refactor” the Add Service Interface
clip_image013

9. Name the interface “IAddService” on the “calculator” package
clip_image014

Result: IAddService interface
clip_image015

Note: We also could first implement the interface and afterwards the implementation of this interface, but this way is a shortcut on this example.

10. Create a Spring Bean Configuration starting from the JDeveloper Technology Gallery
clip_image016

11. Name the Spring Bean Definition file “AddServiceBeanDef.xml”
clip_image017

12. Add a “bean” element by selecting “bean (Spring 2.5 Core)” from the Component Palette (place the cursor behind the comment)
clip_image019

13. Add the name attribute “AddServiceBean” and class attribute “calculator.AddServiceImpl” on the bean element
clip_image020

14. Add a “Service” element by selecting “Service (WebLogic SCA)” from the Component Palette. On the upcoming pop-up window add “AddService” as name, “AddServiceBean” as target and “calculator.IAddService” as type. The target name has to match the bean name.
clip_image022

clip_image023

Note: You may have to compile the Java classes. Take care that the SCA Service target attribute is matching the Bean name attribute.

15. Drag and Drop the “Spring Context” from the Component Palette on the Composite Component section
clip_image025

16. Name the Component “AddServiceComponent” and select the “Use Existing Context” option and start the SOA Resource Brower with the magnifying glass icon
clip_image026

17. Select “AddServiceBeanDef.xml” on the SOA Resource Browser (starts with the magnifying glass icon on step 16)
clip_image027

18. Accept the “src/META-INF/AddServiceBeanDef.xml” Spring Bean Context path on the “Use Existing Context” field
clip_image028

Afterwards the AddServiceComponent is placed on the SCA Composite

clip_image029

Additionally a <Spring Bean Component Name>.componentType file is created automatically on the SOA Content folder. This file keeps the exposed interface information (and the references on other services when the component is wired to other SCA Components).

clip_image030
19. Repeat step 4 to 19 for the SubtractService, MultiplyService and DivideService!

20. Let’s take a look at the Calculator Service Component. This component will call the other arithmetic Service Components. Create a Java class “CalculatorServiceImpl” on the “calculator” Package
clip_image031

21. Add private service variables (use the interface classes for the declaration), create the setter methods and create service methods for the basic arithmetic operations by using the existing arithmetic services.
clip_image032

Note: Like the shown add method you have to implement the subtract, multify and divide methods by using the right service.

22. “Refactor” the Calculator Service Interface and name the interface “ICalculatorService”. Select the basic arithmetic operations as class members to extract.
clip_image033

23. Create a Spring Bean Definition “CalculatorServiceBeanDef”
clip_image034
24. Set the bean name “CalculatorServiceBean” attribute and class “calculator.CalculatorServiceImpl” attribute
clip_image035

25. Add the Calculator Spring Context on the Composite Component section
clip_image036

26. Next step is to “wire” the existing Spring Bean Components. Drag and Drop a wire from the CalculatorServiceComponent to the other Spring Bean Components.
clip_image038

Afterwards all the arithmetic SCA Components are wired to the CalculatorServiceComponent. This graphical wiring will add appropriate <sca:reference> elements on the Calculator Bean context definition file.

clip_image039

The new services references have to be injected on the Calculator Bean (“setter DI”). Therefore you have to add standard Spring property elements on the bean declaration within the Spring Bean context definition. These property elements causing a call on the setter operations on the CalculatorServiceImpl class. The bean property ref names have to match the right reference name.

clip_image040

Note: When you missed the <sca:service> tags before you create the Spring Bean Components and you try to wire the beans you will see this error …

clip_image041

This error occurs because the automatic created <Spring Bean Component Name>.componentType files are missing the interface service information. So you have to add the missing service elements manually or you have to delete and re-create the SCA Components (don’t delete the Bean Definition File, the wizard is asking for it!).

clip_image042

Another common error is the missing property declaration on the bean which should use other services (“missing setter DI”). This result in a nice NullPointerException during the first test (java.lang.Exception: oracle.sysman.emSDK.webservices.wsdlapi.SoapTestException: oracle.fabric.common.FabricException: Invocation failed: java.lang.NullPointerException).

27. Next step is to give the Calculator Service Component an exposed service interface. Therefore you have to give the Calculator service a callable interface declaration. You only have to declare an SCA service interface on the Calculator Service Bean definition.

clip_image044

Afterwards you have to add the new service interface also on the CalculatorServiceComponent.componentType file. This declaration makes the Calculator Service Component connectable as public (callable from the SCA outside world) or private (callable from other SCA Components) service.

clip_image045

Note: The service interface configuration gives the components the “green handle”. Only with this handle you are able to connect this component.

28. Now the Calculator service can be exposed as public service. Drag and Drop a wire from the Calculator Service Component to the Exposed Service section.

clip_image047

A pop-up window is asking you what kind of interface you want to create for the service. Because we want to create a Web Service interface and not an EJB interface we press the Web Service button. Btw. you could also have both interface in parallel – Web Service and EJB.

clip_image048

The wizard is creating the necessary mapping between the Java classes and the XML representations. An ICalculatorService.wsdl file is created which offers the basic arithmetic operations of the Calculator Service Component.

clip_image049

The Calculator Service is ready for deployment. Each component has a well-defined interface, using Java interfaces in the case of the calculator example. Notice that these are just plain Java services configured as Spring Beans with business logic and nothing else inside the Java classes (no extend on pre-specified classes, no implement on pre-specified interfaces, no pre-specified annotations, etc.).

clip_image050

This screenshot looks very similar to the diagram on the Tuscany guidelines.

image

29. A quick test could happen with the Test Web Service Client of the Oracle Enterprise Manager. But first you have to create the deployment unit (one JAR file) and deploy the SCA Composite on the SOA Server. My personal favorite Web Service Test Client is the Open Source soapUI tool. Therefore you have to copy the WSDL URL from the Test Web Service client.

clip_image053

Start soapUI and create a new soapUI project. Paste the WSDL on the new project.

clip_image054

Start your tests …

The source code is available as 7-zip here.

That's it.

Friday, October 22, 2010

SCA Spring Bean Components

Since Oracle SOA Suite 11g Patch Set 2, the Oracle SOA Suite uses the Spring Framework functionality provided by the WebLogic SCA (Service Component Architecture) of the Oracle WebLogic Server. The WebLogic Spring Container, based on the SCA Spring Component Implementation Specification, can be used for deploying Spring Components wired with SCA semantics. Actually the Oracle SOA Suite supports the Spring Framework version 2.5.6 which was released in November, 2008.

Oracle marries SCA technology with the benefits of the Spring Framework. Objects created by the Spring Container are called Spring Beans. The Spring Container is configured by loading XML configuration files containing bean definitions which provide the information required to create the beans. Objects can be obtained by means of Dependency Lookup and Dependency Injection. Dependency Lookup (DL) is a pattern where a caller asks the container for an object with a specific name or of a specific type. Dependency Injection (DI) is a pattern where the container passes objects by name to other objects, via constructors, properties, or factory methods.

Spring Bean development for the usage within an Oracle SCA Composite is easy and straightforward. Spring Beans will be created from new or existing Java classes which should become POJO services within the SCA Composite. The Spring Bean Component Editor is used to create a Spring Bean context definition. Based on a Spring Context, a SCA Spring Bean Component is created that transparently bridges the gap between Java and XML. Internally the Open Source EclipseLink MOXy JAXB implementation is used to map between Java classes and XML representations. Existing Spring Beans could be quickly reused on the Oracle SCA Container.

The Spring Bean context definition configuration XML file keeps the Spring context definition and shields the service implementation. The context definition configuration XML file will allow for Dependency Lookup and Dependency Injection.



Spring Beans complete the Oracle SOA platform. Enabling of Java POJOs as SCA Components was definitely a missing feature. The Spring Framework adds the power of Dependency Injection (DI), Aspect Oriented Programming (AOP) and a lot of simplified API abstraction to the Oracle SOA developers.