TP 2 - Correction Fractal/Julia - Le système de composants

Download Report

Transcript TP 2 - Correction Fractal/Julia - Le système de composants

TP 2 - Correction
Fractal/Julia - Le système de composants Fractal
et son implémentation Java de référence Julia.
Loïc LAGANE
Correction de l'exercice
En plus des classes et interfaces fournies avec le TP (à savoir Service.java, Main.java, ServiceAttributes.java, ClientImpl.java, ServerImpl.java, HelloWorld.java ), vous trouverez ci-dessous le code
de la classe Client2Impl.java implémentant le second client ainsi que le code de la classe principale
HelloWorld.java mis à jour.
Client2Impl.java
/*
* Classe ClientImpl2
*/
import org.objectweb.fractal.api.control.BindingController;
public class ClientImpl2 implements Main, BindingController {
private Service service;
// Implementation de l'interface Main
public void main(final String[] args) {
service.print("bonjour");
}
// Implementation de l'interface BindingController
public String[] listFc() {
return new String[] { "s" };
}
// Implementation de l'interface BindingController
public Object lookupFc(final String cInterface) {
if (cInterface.equals("s")) {
return service;
}
return null;
}
1
// Implementation de l'interface BindingController
public void bindFc(final String cInterface, final Object sInterface) {
if (cInterface.equals("s")) {
service = (Service)sInterface;
}
}
// Implementation de l'interface BindingController
public void unbindFc(final String cInterface) {
if (cInterface.equals("s")) {
service = null;
}
}
}
HelloWorld.java
/*
* Classe HelloWorld
*/
import
import
import
import
import
import
org.objectweb.fractal.api.Component;
org.objectweb.fractal.api.factory.GenericFactory;
org.objectweb.fractal.api.type.ComponentType;
org.objectweb.fractal.api.type.InterfaceType;
org.objectweb.fractal.api.type.TypeFactory;
org.objectweb.fractal.util.Fractal;
public class HelloWorld {
public static void main(final String[] args) throws Exception {
// Composant racine
Component rComp;
// Recuperation du composant bootstrap
Component boot = Fractal.getBootstrapComponent();
// Recuperation de l'interface TypeFactory
// fournie par le composant bootstrap
TypeFactory tf = Fractal.getTypeFactory(boot);
System.out.println("\nHelloWorld : Creation des types de composants
rType, cType et sType.");
// Creation du type du composant racine
// qui fournit un serveur Main nomme "m"
2
ComponentType rType = tf.createFcType(new InterfaceType[] {
tf.createFcItfType("m", "Main", false, false, false),
/* Question */
tf.createFcItfType("m2", "Main", false, false, false)
/* ******** */
});
// Creation du type du composant client
ComponentType cType = tf.createFcType(new InterfaceType[] {
tf.createFcItfType("m", "Main", false, false, false),
tf.createFcItfType("s", "Service", true, false, false)
});
// Creation du type du composant serveur
ComponentType sType = tf.createFcType(new InterfaceType[] {
tf.createFcItfType("s", "Service", false, false, false),
tf.createFcItfType("attribute-controller","ServiceAttributes",
false, false, false)
});
// Recuperation de l'interface GenericFactory
// fournie par le composant bootstrap
GenericFactory cf = Fractal.getGenericFactory(boot);
// Creation des composants
// - racine : de type rType et composite
System.out.println("HelloWorld : Creation d'un composant
racine composite.");
rComp = cf.newFcInstance(rType, "composite", null);
// - client : de type cType et primitif
System.out.println("HelloWorld : Creation d'un composant
client primitif.");
Component cComp = cf.newFcInstance(cType, "primitive",
"ClientImpl");
/* Question */
Component c2Comp = cf.newFcInstance(cType, "primitive", "ClientImpl2");
/* ******** */
// - serveur : de type sType et primitif
System.out.println("HelloWorld : Creation d'un composant serveur primitif.");
3
System.out.println("(l'affichage s'effectuera deux fois puisqu'on affecte " +
"la valeur 2 a l'attribut 'count' du serveur courant)");
Component sComp = cf.newFcInstance(sType, "primitive", "ServerImpl");
((ServiceAttributes)Fractal.getAttributeController(sComp)).setHeader("-> ");
((ServiceAttributes)Fractal.getAttributeController(sComp)).setCount(2);
/* Question */
Component s2Comp = cf.newFcInstance(sType, "primitive", "ServerImpl");
((ServiceAttributes)Fractal.getAttributeController(s2Comp)).setHeader("* ");
((ServiceAttributes)Fractal.getAttributeController(s2Comp)).setCount(3);
/* ******** */
// Assemblage des composants
// Le composant racine contient les composants client et serveur.
System.out.println("HelloWorld : Assemblage des composants.");
Fractal.getContentController(rComp).addFcSubComponent(cComp);
Fractal.getContentController(rComp).addFcSubComponent(sComp);
/* Question */
Fractal.getContentController(rComp).addFcSubComponent(c2Comp);
Fractal.getContentController(rComp).addFcSubComponent(s2Comp);
/* ******** */
// Creation d'une liaison entre le composant racine et le client
// Une seconde liaison est creee entre le composant client et le serveur
System.out.println("HelloWorld : Creation des liaisons.");
System.out.println("\t * composant racine - composant client,");
System.out.println("\t * composant client - composant serveur.");
Fractal.getBindingController(rComp).bindFc("m", cComp.getFcInterface("m"));
Fractal.getBindingController(cComp).bindFc("s", sComp.getFcInterface("s"));
/* Question */
Fractal.getBindingController(rComp).bindFc("m2", c2Comp.getFcInterface("m"));
Fractal.getBindingController(c2Comp).bindFc("s", s2Comp.getFcInterface("s"));
/* ******** */
// Lancement du composant racine de l'application
// qui lancera automatiquement tous les composants de l'application
Fractal.getLifeCycleController(rComp).startFc();
// Lancement de l'application : appel de la methode main
// a partir de l'interface fonctionnelle m du composant racine
System.out.println("HelloWorld : Appel de la methode main sur le
composant racine.\n");
((Main)rComp.getFcInterface("m")).main(null);
/* Question */
((Main)rComp.getFcInterface("m2")).main(null);
/* ******** */
}
4
// Code specifique a Julia
// Sert a l'affichage du contenu des composants crees
public static class DebugController
implements org.objectweb.fractal.julia.Controller {
public void initFcController
(org.objectweb.fractal.julia.InitializationContext ic) {
System.err.println("COMPONENT CREATED");
System.err.println("INTERFACES:");
java.util.Iterator i = ic.interfaces.keySet().iterator();
while (i.hasNext()) {
String key = (String)i.next();
if (!key.startsWith("/")) {
System.err.println(" "+key+" "+ic.interfaces.get(key));
}
}
System.err.println("CONTROLLER OBJECTS:");
i = ic.controllers.iterator();
while (i.hasNext()) {
Object o = i.next();
if (o instanceof org.objectweb.fractal.julia.Interceptor) {
Object p =
((org.objectweb.fractal.julia.Interceptor)o).getFcItfDelegate();
System.err.println(" "+o+" (Interceptor,impl="+p+")");
} else {
System.err.println(" "+o);
}
}
System.err.println("CONTENT:\n "+ic.content);
}
}
}
5