• Documentation
 
Fractal Tutorial: HelloWorld with Fractal ADL

Fractal Tutorial: HelloWorld with Fractal ADL


Chapter 1. Introduction

In this tutorial, we will work on the same helloworld application than the previous tutorial but without the Fraclet tool. The main differences are that we have to write extra Java code and ADL definitions that are normally created by Fraclet.

In the previous tutorial we've had a bottom-up approach, we've started by the .java code of the primitive components to end with the ADL definition of the composite component. In this tutorial we will show how we can have a top-down approach. We will start from the composite ADL definition to the Java implementation code.

1.1. Steps

  • Create the description of the composite component with Fractal ADL
  • Create primitif components. To do this :
    • create the description of the primitive components with Fractal ADL.
    • create Java interfaces that will be associated with our Fractal interfaces.
    • create a Java class which contains the business code of the component and implements needed Fractal control interfaces.
  • call the FractalADL factory which will instanciate the description of our architecture from Java.

1.2. Composite component

The ADL of the client-server composite is the same that in the previous tutorial.


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE definition PUBLIC "-//objectweb.org//DTD Fractal ADL 2.0//EN" 
"classpath://org/objectweb/fractal/adl/xml/basic.dtd">

<definition name="helloworld.ClientServerImpl">
  <interface name="r" role="server" signature="helloworld.Main"/>
  
  <component name="client" definition="helloworld.ClientImpl" />
  <component name="server" definition="helloworld.ServerImpl" />
  
  <binding client="this.r" server="client.r" />
  <binding client="client.s" server="server.s" />
</definition>

Note: Be careful with the order of the Fractal ADL xml elements. For composite components: first list the interface elements, then the list of sub-components, finally the bindings between sub-components.

1.3. Primitive components

As we don't use Fraclet we have to describe the architecture of our primitive components. It is a little tedious, but easy.

Example 1.1. Fractal ADL definition of the client primitive component


package helloworld;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;


public class ClientImpl implements java.lang.Runnable, BindingController {

	private Service service;
	
	public ClientImpl () {
		System.err.println("CLIENT created");
	}
	
	public void run () {
		service.print("Hello world !");
	}

	public String[] listFc () {
		return new String[] { "s" };
	}

	public Object lookupFc (final String cItf) {
		if (cItf.equals("s")) {
			return service;
		}
		return null;
	}

	public void bindFc (final String cItf, final Object sItf) {
		if (cItf.equals("s")) {
			service = (Service)sItf;
		}
	}

	public void unbindFc (final String cItf) {
		if (cItf.equals("s")) {
			service = null;
		}
	}
}



Example 1.2. Fractal ADL definition of the server primitive component


<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE definition PUBLIC "-//objectweb.org//DTD Fractal ADL 2.0//EN" 
"classpath://org/objectweb/fractal/adl/xml/basic.dtd">

<definition name="helloworld.ServerImpl">
  <interface name="s" role="server" signature="helloworld.Service"/>
  <content class="helloworld.ServerImpl"/>
  <attributes signature="helloworld.ServiceAttributes">
    <attribute name="header" value="->"/>
  </attributes>
  <controller desc="primitive"/>
</definition>



To describe the Fractal attributes of a primitive component, we need to indicate the Java interface used by the attribute controller, and describe the initialization of the attributes.

We indicate the Java interface that will be used by the attribute controller.


<attributes signature="helloworld.ServiceAttributes">

We can initialize the value of the attribute:


<attribute name="header" value="->"/>

Note: Be careful with the order of Fractal ADL xml elements. For primitive component: first list interfaces, after content, then attributes, finally the controller. Note also, that you can't define binding in a primitive component definition. They are defined only in composite components, because bindings between two components can only occur in a common super-component.

1.4. Java content

1.4.1. Client component

Example 1.3. ClientImpl.java


package helloworld;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;


public class ClientImpl implements java.lang.Runnable, BindingController {

	private Service service;
	
	public ClientImpl () {
		System.err.println("CLIENT created");
	}
	
	public void run () {
		service.print("Hello world !");
	}

	public String[] listFc () {
		return new String[] { "s" };
	}

	public Object lookupFc (final String cItf) {
		if (cItf.equals("s")) {
			return service;
		}
		return null;
	}

	public void bindFc (final String cItf, final Object sItf) {
		if (cItf.equals("s")) {
			service = (Service)sItf;
		}
	}

	public void unbindFc (final String cItf) {
		if (cItf.equals("s")) {
			service = null;
		}
	}
}



Java fields with interface types are used to store Fractal required interfaces. We need to inform Fractal about the private field to use for each required Fractal interface. To do this we have to implement the BindingController control interface.

Note. With the Fraclet tool you don't need to implement this interface because Fraclet automatically injects this code from the the information that you've provided with the annotations.

1.4.2. Server component

The server component also needs extra manual code without Fraclet. Our server component has a 'header' attribute. So here too we have to explicit the mapping between the Java attributes and Fractal attributes. To do so we need to add a ServiceAttributes control interface.

The ServiceAttributes interface just contains accessors to manipulate the Java attributes that you want to map to Fractal attributes.

Example 1.4. ServiceAttributes.java


package helloworld;

import org.objectweb.fractal.api.control.AttributeController;

public interface ServiceAttributes extends AttributeController {
  String getHeader ();
  void setHeader (String header);
}



The name of the accessor must have the same name that your Fractal attribute, in our case our Java attribute is named header so the accessors are named getHeader and setHeader.

As our interface will be a control interface to manipulate attributes, we need to extend the standard Fractal AttributeController interface.

Let's note that you can map more that one attribute in the same interface. If you have a other attribute name 'foo' you can add a getFoo and a setFoo in the same interface than the one used for the header attribute.

Example 1.5. ServerImpl.java


package helloworld;

public class ServerImpl implements Service, ServiceAttributes {

  private String header;

  public ServerImpl () {
		System.err.println("SERVER created");
	}
  
  public void print (final String msg) {
    System.out.println(header + msg);
  }

  public String getHeader () {
    return header;
  }

  public void setHeader (final String header) {
    this.header = header;
  }
}



Chapter 2. Run the application

To launch the application we have to instanciate our helloworld.ClientServerImpl Fractal ADL file.

With ant, we first need to package the helloworld application in a jar :

ant dist
	

We can call the Fractal ADL factory on our Fractal ADL file

Linux :

bash $FRACTAL_HOME/bin/fractal.sh -cp dist/helloworld.jar -adl helloworld.ClientServerImpl
	

Windows :

%FRACTAL_HOME%\bin\fractal.bat -cp dist/helloworld.jar -adl helloworld.ClientServerImpl
	

With Maven, just type :

mvn -Prun
	
 
1999-2009 © OW2 Consortium  | Last Published: 2009-02-03 18:29  | Version: 1.1.2-SNAPSHOT