Welcome!

Java Authors: Greg Schulz, Nikita Ivanov, Jim Kaskade, Pat Romanski, Roberto Medrano

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.


@ThingsExpo Stories
There's Big Data, then there's really Big Data from the Internet of Things. IoT is evolving to include many data possibilities like new types of event, log and network data. The volumes are enormous, generating tens of billions of logs per day, which raise data challenges. Early IoT deployments are relying heavily on both the cloud and managed service providers to navigate these challenges. In her session at Big Data Expo®, Hannah Smalltree, Director at Treasure Data, discussed how IoT, Big Data and deployments are processing massive data volumes from wearables, utilities and other machines...
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
The Internet of Things promises to transform businesses (and lives), but navigating the business and technical path to success can be difficult to understand. In his session at @ThingsExpo, Sean Lorenz, Technical Product Manager for Xively at LogMeIn, demonstrated how to approach creating broadly successful connected customer solutions using real world business transformation studies including New England BioLabs and more.
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, discussed how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.
DevOps Summit 2015 New York, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential.
Explosive growth in connected devices. Enormous amounts of data for collection and analysis. Critical use of data for split-second decision making and actionable information. All three are factors in making the Internet of Things a reality. Yet, any one factor would have an IT organization pondering its infrastructure strategy. How should your organization enhance its IT framework to enable an Internet of Things implementation? In his session at Internet of @ThingsExpo, James Kirkland, Chief Architect for the Internet of Things and Intelligent Systems at Red Hat, described how to revolutioniz...
Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, examined three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective storage designed to handle the massive surge in back-end data in a world where timely analytics is e...
Scott Jenson leads a project called The Physical Web within the Chrome team at Google. Project members are working to take the scalability and openness of the web and use it to talk to the exponentially exploding range of smart devices. Nearly every company today working on the IoT comes up with the same basic solution: use my server and you'll be fine. But if we really believe there will be trillions of these devices, that just can't scale. We need a system that is open a scalable and by using the URL as a basic building block, we open this up and get the same resilience that the web enjoys.
The Internet of Things is tied together with a thin strand that is known as time. Coincidentally, at the core of nearly all data analytics is a timestamp. When working with time series data there are a few core principles that everyone should consider, especially across datasets where time is the common boundary. In his session at Internet of @ThingsExpo, Jim Scott, Director of Enterprise Strategy & Architecture at MapR Technologies, discussed single-value, geo-spatial, and log time series data. By focusing on enterprise applications and the data center, he will use OpenTSDB as an example t...
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happens, where data lives and where the interface lies. For instance, it's a mix of architectural styles ...
The definition of IoT is not new, in fact it’s been around for over a decade. What has changed is the public's awareness that the technology we use on a daily basis has caught up on the vision of an always on, always connected world. If you look into the details of what comprises the IoT, you’ll see that it includes everything from cloud computing, Big Data analytics, “Things,” Web communication, applications, network, storage, etc. It is essentially including everything connected online from hardware to software, or as we like to say, it’s an Internet of many different things. The difference ...
The security devil is always in the details of the attack: the ones you've endured, the ones you prepare yourself to fend off, and the ones that, you fear, will catch you completely unaware and defenseless. The Internet of Things (IoT) is nothing if not an endless proliferation of details. It's the vision of a world in which continuous Internet connectivity and addressability is embedded into a growing range of human artifacts, into the natural world, and even into our smartphones, appliances, and physical persons. In the IoT vision, every new "thing" - sensor, actuator, data source, data con...
P2P RTC will impact the landscape of communications, shifting from traditional telephony style communications models to OTT (Over-The-Top) cloud assisted & PaaS (Platform as a Service) communication services. The P2P shift will impact many areas of our lives, from mobile communication, human interactive web services, RTC and telephony infrastructure, user federation, security and privacy implications, business costs, and scalability. In his session at @ThingsExpo, Robin Raymond, Chief Architect at Hookflash, will walk through the shifting landscape of traditional telephone and voice services ...
"Matrix is an ambitious open standard and implementation that's set up to break down the fragmentation problems that exist in IP messaging and VoIP communication," explained John Woolf, Technical Evangelist at Matrix, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
An entirely new security model is needed for the Internet of Things, or is it? Can we save some old and tested controls for this new and different environment? In his session at @ThingsExpo, New York's at the Javits Center, Davi Ottenheimer, EMC Senior Director of Trust, reviewed hands-on lessons with IoT devices and reveal a new risk balance you might not expect. Davi Ottenheimer, EMC Senior Director of Trust, has more than nineteen years' experience managing global security operations and assessments, including a decade of leading incident response and digital forensics. He is co-author of t...
We are reaching the end of the beginning with WebRTC, and real systems using this technology have begun to appear. One challenge that faces every WebRTC deployment (in some form or another) is identity management. For example, if you have an existing service – possibly built on a variety of different PaaS/SaaS offerings – and you want to add real-time communications you are faced with a challenge relating to user management, authentication, authorization, and validation. Service providers will want to use their existing identities, but these will have credentials already that are (hopefully) i...
The 3rd International @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices - computers, smartphones, tablets, and sensors - connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades.
The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, discussed how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money!
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.