| Consortium Solutions Middleware Forge MyObjectWeb | |||||||||
![]() |
|
Fractal
|
About this document
Table of Contents
About SpoonIn addition, Spoon provides templates in pure Java, which are Java class that contains template parameters, which are defined as fields annotated with @Parameter. Template parameters can represent primitive values (such as literal values, program element's names, types), or actual CtElements (ASTs). In the template code, all the references to template parameters can be substituted by their actual values using the substitution engine API provided by Spoon. The templates are useful to express patterns that can be used for generating code, as exampled by Generative Programming. To implement Fraclet with have developed a set of Spoon processors and templates to process our annotations. This tutorial does not detail the implementation of the processors and templates but focus on the use of Fraclet annotations. Available AnnotationsA small set of annotations have been defined for easy programming of Fractal components. The table below summarises these annotations.
In addition to this small annotations set, additional annotations are also available for handling advanced concepts of Fractal.
Core Annotations@Component
Details:
Parameters:
PS. The attribute Examples:
@Component
public class MyComponent implements Runnable {
(...)
}
Generate the following Fractal ADL definition:
<definition name="MyComponent">
(...)
<content class="MyComponent"/>
</definition>
@Component(name="MyComponentDefinition")
public class MyComponent implements Runnable {
(...)
}
Generate the following Fractal ADL definition:
<definition name="MyComponentDefinition">
(...)
<content class="MyComponent"/>
</definition>
@Component(name="MyComponentDefinition", provides=@Interface(name="r", signature=Runnable.class))
public class MyComponent implements Runnable {
(...)
}
Generate the following Fractal ADL definition:
<definition name="MyComponentDefinition">
<interface name="r" role="server" cardinality="singleton" contingency="mandatory" signature="java.lang.Runnable"/>
(...)
<content class="MyComponent"/>
</definition>
@Interface
Details:
Parameters:
Examples:
@Interface
public interface Service {
(...)
}
Generate the following Fractal ADL abstract definition:
<definition name="Service">
<interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/>
</definition>
@Interface(name="s")
public interface Service {
(...)
}
Generate the following Fractal ADL abstract definition:
<definition name="Service">
<interface name="s" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/>
</definition>
@Interface(name="s")
public interface Service {
(...)
}
@Component
public class Server implements Service {
(...)
}
Generate the following Fractal ADL definitions:
<definition name="Service">
<interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/>
</definition>
<definition name="Server" extends="Service">
(...)
<content class="Server"/>
</definition>
@Attribute
Details:
Parameters:
Example:
@Component
public class Server implements Service {
@Attribute(value=">>")
private String header ;
@Attribute(name="msg")
private String message ;
(...)
}
Generates the following Fractal ADL definitions:
<definition name="Server" arguments="msg,header=>>">
<interface name="service" signature="Service" role="server" cardinality="singleton" contingency="mandatory"/>
<content class="Server"/>
<attributes signature="ServerAttributes">
<attribute name="message" value="${msg}"/>
<attribute name="header" value="${header}"/>
</attributes>
</definition>
@Requires
Details:
Parameters:
Example:
@Component
public class Client implements Runnable {
@Requires(name = "default")
private Service defaut;
@Requires(cardinality = Cardinality.COLLECTION)
private Map<String,Service> service = new HashMap<String,Service>();
}
Generates the following Fractal ADL definitions:
<definition name="Client">
<interface name="default" signature="Service" role="client" cardinality="singleton" contingency="mandatory"/>
<interface name="service" signature="Service" role="client" cardinality="collection" contingency="mandatory"/>
<content class="Client"/>
</definition>
@Lifecycle
Details:
Parameters:
Example:
@Component
public class Client implements Runnable {
@LifeCycle(Step.START)
private void init() {
System.out.println("Starting the component Client...");
}
(...)
}
@ControllerDetails: This class annotation provides access to the controllers supported by the component. Parameters:
Examples:
@Component
public class Client implements Runnable {
@Controller
private org.objectweb.fractal.api.Component comp;
(...)
}
@Component
public class Client implements Runnable {
@Controller("name-controller")
private NameController nc;
(...)
}
Additional Annotations@Data
Details:
Example:
@Data
public class Message {
(...)
}
@Legacy
Details:
Parameters:
Example:
@Component
@Legacy("MyDefinition")
public class Client implements Runnable {
(...)
}
Generates the following Fractal ADL definition:
<definition name="Client" extends="MyDefinition">
(...)
</definition>
@Membrane
Details:
Parameters:
Example:
@Component
@Membrane(controller="myPrimitive")
public class Client implements Runnable {
(...)
}
Generates the following Fractal ADL definition:
<definition name="Client">
(...)
<controller desc="MyPrimitive"/>
</definition>
@Component
@Membrane(template="myTemplate")
public class Client implements Runnable {
(...)
}
Generates the following Fractal ADL definition:
<definition name="Client">
(...)
<template-controller desc="MyTemplate"/>
</definition>
@Node
Details:
Parameters:
Example:
@Component
@Node("clientNode")
public class Client implements Runnable {
(...)
}
Generates the following Fractal ADL definition:
<definition name="Client" extends="MyDefinition">
(...)
<virtual-node name="clientNode">
</definition>
ConfigurationSpoon ConfigurationYou should first describe the list of spoonlets that need to be
applied to your source code. Fraclet basically provides two
spoonlets for introducing the Fractal API callbacks and generating
the Fractal ADL definitions, respectively. To do so, you should
create a file named <?xml version="1.0" encoding="UTF-8"?> <spoon> <spoonlet groupId="org.objectweb.fractal.fraclet.java" artifactId="fractal-spoonlet" version="3.0.1" /> <spoonlet groupId="org.objectweb.fractal.fraclet.java" artifactId="fractaladl-spoonlet" version="3.0.1" /> </spoon> Maven2 ConfigurationThen, you should describe the list of spoonlets as Maven2 artefact dependencies.
<project>
(...)
<dependencies>
(...)
<dependency>
<groupId>org.objectweb.fractal.fraclet.java</groupId>
<artifactId>fractal-spoonlet</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.objectweb.fractal.fraclet.java</groupId>
<artifactId>fractaladl-spoonlet</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
(...)
<project>
Finally, you should extend the Maven2 build process by using the Spoon Maven2 plugin.
<project>
(...)
<build>
<plugins>
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>spoon-maven-plugin</artifactId>
<version>0.7</version>
<executions>
<execution>
<goals>
<goal>recompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
(...)
<project>
Revisiting HelloWorldThis section provides a quick overview of the benefits of Fraclet. In particular, it illustrates that, using annotations, component source code becomes more concise and easier to maintain. Moreover, using Fraclet, more than 60% of the source code (Java, FractalADL) can be saved. HelloWorld Architecture OverviewThe figure below introduces the well-known HelloWorld example implemented with Fractal components.
HelloWorld Components ImplementationThe source code below represents the Java code and the Fraclet
annotations written to implement the
@Component(provides=@Interface(name="r",signature=Runnable.class))
public class Client implements Runnable {
@Requires(name = "s")
private Service defaut;
@Attribute(value="Hello !")
protected String message;
public void run() {
this.defaut.print(this.message);
} }
The
@Interface(name="s")
public interface Service {
void print(String message);
}
The
@Component
public class Server implements Service {
@Attribute private String header;
@Attribute private int counter;
public void print(final String message) {
for (int i = 0; i < this.counter; i++)
System.out.println(this.header + message);
} }
The HelloWorld Architecture DefinitionThe assembly definition below represents the FractalADL assembly
defined to describe the
<definition name="HelloWorld" extends="ClientComposite(s=>server)">
<component name="server" definition="Server('>>',2)"/>
</definition>
This definition creates a composite component
Directory StructureThe directory structure below presents the list of compiled, generated and written files when using Fraclet to implement the HelloWorld example:
%example%
* src/main/java
- Client.java
- Server.java
- Service.java
* src/main/resources
- HelloWorld.fractal
* target/generated-sources/spoon
- Client.java
- Client.fractal
- ClientAttributes.java
- ClientComposite.fractal
- Server.java
- Server.fractal
- ServerAttributes.java
- Service.java
- Service.fractal
* target/classes
- Client.class
- Client.fractal
- ClientAttributes.class
- ClientComposite.fractal
- Server.class
- Server.fractal
- ServerAttributes.class
- Service.class
- Service.fractal
- HelloWorld.fractal
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright © 1999-2008, ObjectWeb Consortium | contact | webmaster | Last modified at 2008-10-14 10:36 AM |