Welcome!

XML Authors: Elizabeth White, Sandi Mappic, Max Katz, Yakov Fain, Keith Mayer

Related Topics: Java, XML, SOA & WOA, Websphere, Weblogic, AJAX & REA

Java: Article

Part 3 | Component Models in Java

Service Component Architecture Model

Service Component Architecture (shortly referred as SCA) is a technology for creating services from components. SCA is a set of OASIS standards and part of it is developed with the collaboration of vendors from open source community, referred as "OSOA" Open SOA. SCA helps to build systems as a collection of interconnected components. The components created in SCA communicate through the services. The advantage of SCA is its neutral component model. It allows services to be built by any language component like Java, C++, BPEL, JavaScript, Ruby, Python etc. SCA addresses two important needs of component world:

  • Complexity
  • Reusability

SCA is best suited model for distributed systems. SCA simplifies building of systems by simple assembling of components. SCA like other component model discussed in the previous sections facilitates component assembly in the middle tier - business tier of N-tier architecture. SCA has some key elements like services, components, composites and domains.

In SCA, applications are created and assembled as components. Components contain the functionality to be exposed to the clients. The functionality provided by the components is exposed as services. Multiple components in the application interact and get bounded by a mechanism called "wire". Components are connected through wire and their services are exposed through a composite.

Several open source tools/IDEs helps to develop SCA application using designer tools. The key elements in SCA are represented through some standard symbols are represented in Figure 1.

Figure 1: SCA Key Elements

 

Services
Services are the functionality provided by the components. They are intended to perform the business function. Services can be implemented in any language like Java, C# and BPEL. Service has two primary attributes:

  • Service Contract
  • Service Address

Service contract defines the methods, input parameters for the methods and the return value. Service contract can be defined in several ways. In Java, service contract is a simple interface (POJI - Plain Old Java Interface) which exposes the functionality of the components. Service contract in Java is usually an interface which can be used locally as well as remotely. The remote service contract will have SCA annotation @Remotable. In SOA world, the service contract is defined as a WSDL file. IDL is also one of the ways to define service contract.

While service contract exposes the functionality provided by the component, Service address is used by the clients to uniquely identify the service. Service address is unique per service. Reusability is achieved through the service address, because clients connect to the service through the service address.

Component
Component provides one or more services. Components contain the business logic for the functionality exposed by service contracts - interfaces. Components can be implemented in any language supported by runtime like Java, C#, BPEL, Ruby, Python etc. Components are implemented as POJOs in Java. The Java class has to implement the Java interface defined as the service contract. Component will have an associated service, optionally a reference and a property. The service is used for exposing the functionality of the component. A reference is another interface contract provided by some other component. Component's dependency on another component is resolved through reference. A property of a component is a configurable parameter which is used to change the behavior of the component dynamically during runtime (with some of its parameters being changed). In the Java class which represents the component, property is specified as an annotation and the reference is also mentioned as an annotation.

Figure 2: Component with Services, References and Properties

Composite
A composite is a XML file which is used for configuring components. The association of the component with the service and the reference is all mentioned in the composite. A composite is the main element which is used for configuring components. The XML file is written using a language called SCDL - Service Component Definition Language which is based on XML. The composite can be written as set of XML elements or can be created using the composite designer tools provided by IDEs.

Figure 3: Composite graphical representation

First a composite is defined, then components are defined as part of it. The services provided are defined as part of the component definition. A composite is used to configure more than one component.

If a component is dependent on another component, then a reference is defined on the component which has dependency. The reference is service contract of the other component. Service and reference are connected using "Wire". Wire acts as a binding element between client component and the target component. The wire binding can be defined as a local or remote binding. The wire binding reference is also configured in the composite. In the implementation, the references to other components are referred using @Reference annotation. The reference of the target component is obtained using dependency injection. The reference dependency injection can be constructor based or setter methods based or field based injection. However the field based injection is concise which is preferred.

If the component's behavior has to be changed during runtime through change in the value of the attributes,  a property can be defined on the composite.

Figure 4: Component Wiring

Domain
Components are created and configured in composite. The composite is deployed in SCA runtime environment called ‘Domain'. A SCA domain is a collection of SCA runtime instances. SCA runtime instances host components in containers. SCA domain allows composites to be deployed as a whole and also individual component. It is possible to deploy components from a composite to be deployed in different runtimes which is purely vendor specific. The domain provides the required services like resource sharing, communication and management to the components deployed in it.

SCA Component Model Example
The SCA component model can be understood with the same shopping Cart example discussed in the models discussed earlier.

Here in this example, there is only a single component called ‘CartServiceComponent'  which exposes its functionality through the CartService interface which is the service contract for the CartServiceComponent. As a first step, the service contract is defined. The CartService interface is a service contract which contains the business methods defined by the component.

package com.online.shopping.intf;

import java.util.Collection;

public interface CartService {
public void addItem(Product product, int quantity);
public Collection<Product> listItems();    
public double getTotalPrice();
public void clearCart();
}

The CartService is implemented by the class CartServiceImpl is a POJO which represents the CartServiceComponent.

package com.online.shopping.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.online.shopping.intf.CartService;
import com.online.shopping.intf.Product;

public class CartServiceImpl implements CartService {
Map<Product, Integer> items = new HashMap<Product, Integer>();      
@Override
public void addItem(Product product, int quantity) {
if(items.containsKey(product)) {
quantity +=items.get(product);
}
items.put(product, quantity);
}

@Override
public Collection<Product> listItems() {
return items.keySet();
}

@Override
public double getTotalPrice() {
double totalPrice = 0;
for(Product product: items.keySet()) {
totalPrice+=product.getPrice()* items.get(product);           
}
return totalPrice;
}

@Override
public void clearCart() {
items.clear();
}

}

The service contract and the component are connected using SCA composite designer tool provided by IDEs like Eclipse. Using the tool, a composite is created named ‘Cart.composite'. The composite is an XML file which will have an associated Cart.composite_diagram which represents the composite file as a graphical representation. In the Cart composite diagram, using the component creation tool, a component is created named ‘CartServiceComponent'. A service is added to the composite named ‘CartService'.  Using the designer tool, the Java interface file is dragged and dropped on to the Service defined and the Java implementation class to the CartServiceComponent. The service is promoted using the tool (that is exposing the service for clients to consume it). The graphical representation of composite diagram is as follows:

Figure 5: SCA Component Model - Cart Component Example

The composite file contains XML code that describes the component assembly in the composite diagram.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<sca:composite xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" name="Cart" targetNamespace="http://eclipse.org/SCACart/Cart">
<sca:component name="CartServiceComponent">
<sca:implementation.java
class="com.online.shopping.impl.CartServiceImpl"/>

<sca:service name="CartService">
<sca:interface.java
interface="com.online.shopping.intf.CartService"/>
</sca:service>

</sca:component>

<sca:service name="CartService"         
promote="CartServiceComponent/CartService"/>

</sca:composite>

The CartServiceComponent is created and assembled. The component is deployed in SCADomain as a single component with the id ‘CartService'. This is evident from the <sca:service> element having an attribute called ‘promote' in the composite XML file. The SCADomain is the runtime for the SCA components. In this example, Apache Tuscany is the runtime used for deploying composite. The client for the CartServiceComponent first looks up for the composite in the SCADomain where it is deployed. From the domain, client looks for the service with the service address and the service contract. Once it gets the service reference, the client consumes the methods exposed by the component.

package com.client.shopping;

import java.util.Collection;

import org.apache.tuscany.sca.host.embedded.SCADomain;

import com.online.shopping.intf.CartService;
import com.online.shopping.intf.Product;

public class CartClient {

public static void main(String arg[]) {
SCADomain scaDomain = SCADomain.newInstance("Cart.composite");
CartService cartService = scaDomain.getService(
CartService.class, "CartServiceComponent");

Product product = new Product();
product.setName("OSGi");
product.setPrice(550.00);
cartService.addItem(product, 20);

Product newProduct = new Product();
newProduct.setName("SCA");
newProduct.setPrice(300.00);
cartService.addItem(newProduct, 10);

Collection<Product> productItems = cartService.listItems();
for(Product items: productItems) {
System.out.println(items.getName()+"******"+ items.getPrice());             
}            

System.out.println("Total Price of Cart Items: "+cartService.getTotalPrice());

cartService.clearCart();
}
}

Thus SCA component model provides easy steps for creating and assembling components. Assembling components is easy by linking the reference to the service of the target components. SCA promotes reuse through service contracts and address and solves complexity through loose coupling. SCA as a component model promotes reusability and it is highly preferred for assembly if components. The advantage of SCA is its language neutral for several languages like Java, C#, Ruby etc.

Summary
Componentization provides several advantages. The component models play an important role in developing, deploying and assembling of components for an application. Component models provide a simpler way for component reference and look up and hence making the development easier.

In this article we discussed about four latest component models in Java - Enterprise JavaBeans, Spring, OSGi and SCA in brief. We understand that each component model has its own standards for development and assembly. The runtime environment provided by these component framework helps in execution of the components developed using the appropriate model. We also tried to understand the similarity and differences of these component models through a simple component ‘Cart' component which is part of online shopping application.

More Stories By Piram Manickam

Piram Manickam works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

More Stories By Subrahmanya SV

Subrahmanya SV works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

More Stories By S Sangeetha

S Sangeetha is a Senior Technical Architect at the E-Commerce Research Labs at Infosys Limited. She has over 15 years of experience in architecture, design and development of enterprise Java applications. She is also involved in enhancing the technical skills of Architects at Infosys. She has co-authored a book on ‘J2EE Architecture’ and also has written numerous articles on Java for various online Java forums like JavaWorld, java.net, DevX.com and internet.com. She can be reached at [email protected]

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.