Building Composite Applications
Components are the most basic structures in SCA applications. At their simplest level, components may be classes exposed as an interface. A group of components can be combined to perform a more complex functionality; such structures are called "composites". A set of composites will be encapsulated into a domain which will provide the SCA runtime.
Our distributed application consists of domains hosted on separate machines (or infrastructures in this case), consisting of one or more composites which encompass its own set of components. A simple distributed application, which fulfills all the aforementioned requirements, will be created in this section. This application will consist primarily of two separate domains hosted on a hybrid infrastructure. Domain 1, consists of a set of components interacting with each other within a single composite. A set of functionalities will be exposed as services from this domain. Domain 2, will consist of a reference component will use the services exposed by Domain 1 to perform certain tasks. Domain 1 will be hosted on an instance running on the Cloud. Domain 2 will be on the enterprise infrastructure.
Figure 2 illustrates the design layout of Domain 1. The composite helloworldws consists of three components -- ComponentOne, ComponentTwo, and HelloWorldServiceComponent. These components consists of a class (Compo1Impl or Compo2Impl) which implements the Compo1 and Compo2 interfaces, thereby exposing them as services. The methods present inside these classes (sayCompo1 and sayCompo2) take in a simple String object and returns a modified String.
public class Compo1Impl implements Compo1 {
public String sayCompo1(String name){
return "Hi "+name+". This message is from Component1.";
}
}
public class Compo2Impl implements Compo2 {
public String sayCompo2(String name){
return "Hi "+name+". This message is from Component2.";
}
}
The class HelloWorldImpl implements the interface HelloWorldService which exposes the methods getGreetings, getCompo1, and getCompo2 as services. The annotation @Service indicates that this is a service class. The interfaces Compo1 and Compo2 are used to create objects compo1 and compo2. The two methods setCompo1Service and setCompo2Service are mapped to the composite and hence will get the objects from the components Compo1Impl and Compo2Impl, the annotation (@Reference) indicates this. This will be detailed later in this article when the helloworldws.composite file is created.
@Service(HelloWorldService.class)
public class HelloWorldImpl implements HelloWorldService {
private Compo1 compo1;
private Compo2 compo2;
@Reference
public void setCompo1Service(Compo1 compo1) {
this.compo1 = compo1;
}
@Reference
public void setCompo2Service(Compo2 compo2) {
this.compo2 = compo2;
}
public String getGreetings(String name) {
return "Hello " + name+". Now you are accessing an SCA Application hosted on a Eucalyptus Instance through a through a secure VPN.";
}
public String getCompo2(String name){
return compo2.sayCompo2(name);
}
public String getCompo1(String name){
return compo1.sayCompo1(name);
}
}
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
targetNamespace="http://helloworld"
xmlns:hw="http://helloworld"
name="helloworldws">
<component name="HelloWorldServiceComponent">
<implementation.java class="helloworld.HelloWorldImpl" />
<reference name="compo1Service" target="ComponentOne" />
<reference name="compo2Service" target="ComponentTwo" />
<service name="HelloWorldService">
<interface.wsdl interface="http://helloworld#wsdl.interface(HelloWorld)" />
<binding.ws uri="http://localhost:8085/HelloWorldService"/>
</service>
</component>
<component name="ComponentOne">
<implementation.java class="helloworld.Compo1Impl"/>
</component>
<component name="ComponentTwo">
<implementation.java class="helloworld.Compo2Impl"/>
</component>
</composite>
The reference annotations (@Reference) specified before the setter methods in the HelloWorldService class are being referenced here :
<reference name="compo1Service" target="ComponentOne" />
The component which services this request is named as the target here -- ComponentOne. This component implements a Java class helloworld.Compo1Impl.
Figure 3 shows the design layout of Domain 1. The composite helloworldwsclient consists of one component, HelloWorldServiceComponent.
The component HelloWorldServiceComponent implements HelloWorldService, which provides in interface for referencing the methods getGreetings, getCompo2, and getCompo1. The composite helloworldwsclient works in the backend getting the web services required from the Service provider Domain ( i.e Domain 1).
/**
* The HelloWorld service implementation
*/
public class HelloWorldServiceComponent implements HelloWorldService {
HelloWorldService helloWorldService;
public String getGreetings(String name) {
System.out.println("Called getGreetings");
return helloWorldService.getGreetings(name);
}
public String getCompo2(String name){
return helloWorldService.getCompo2(name);
}
public String getCompo1(String name){
return helloWorldService.getCompo1(name);
}
public HelloWorldService getHelloWorldService() {
System.out.println("Got Injected helloWorldService");
return helloWorldService;
}
public void setHelloWorldService(HelloWorldService helloWorldService) {
System.out.println("Injected helloWorldService");
this.helloWorldService = helloWorldService;
}
}
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
targetNamespace="http://helloworld"
xmlns:hw="http://helloworld"
name="helloworldwsclient">
<!-- A component with a reference promoted as a composite reference connecting to an external webservice The wsdl interface for the reference is derived from the information specified by the 'wsdlElement'
-->
<component name="HelloWorldServiceComponent">
<implementation.java class="helloworld.HelloWorldServiceComponent"/>
</component>
<reference name="HelloWorldService" promote="HelloWorldServiceComponent/helloWorldService">
<interface.java interface="helloworld.HelloWorldService" />
<binding.ws wsdlElement="http://helloworld#wsdl.port(HelloWorldService/HelloWorldSoapPort)"/>
</reference>
</composite>
The component HelloWorldServiceComponent gets attributes from the reference HelloWorldService which derives its attributes from an external WebService (hosted in Domain1). Thus a web service binding will be established between Domain1 and Domain2.


