Commit 58983047 authored by Philippe Merle's avatar Philippe Merle
Browse files

Added a new hello world example illustrating how using the new scaPrimitiveWithState membrane.

parent 399856b8
============================================================================
OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
Copyright (C) 2012 Inria, University of Lille 1
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
Contact: frascati@ow2.org
Author: Philippe Merle
Contributor(s):
============================================================================
HelloWorld with membrane scaPrimitiveWithState:
-----------------------------------------------
This example illustrates how using the scaPrimitiveWithState membrane.
The scaPrimitiveWithState membrane adds a new controller to SCA components.
This new controller manages the state of the component.
Have a look to Client and Server classes where:
- @Membrane(desc="scaPrimitiveWithState") defines which membrane is used.
- @StateField defines which class fields are part of the state of the component.
Have a look to the HelloWorldTestCase class to see how to manage component states
via StateController and State interfaces.
The scaPrimitiveWithState membrane is available into the following module:
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-membrane-scaPrimitiveWithState</artifactId>
<version>${frascati.version}</version>
</dependency>
Compilation with Maven:
-----------------------
mvn install
Execution with Maven:
---------------------
mvn -Prun (standalone execution)
mvn -Pexplorer (with FraSCAti Explorer)
mvn -Pexplorer-fscript (with FraSCAti Explorer and FScript plugin)
mvn -Pfscript-console (with FraSCAti FScript Console)
mvn -Pfscript-console-explorer (with FraSCAti Explorer and FScript Console)
mvn -Pexplorer-jdk6 (with FraSCAti Explorer and JDK6)
Compilation and execution with the FraSCAti script:
---------------------------------------------------
TBD
<?xml version="1.0"?>
<!--
* OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
*
* Copyright (c) 2012 Inria, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ow2.frascati.examples</groupId>
<artifactId>helloworld-scaPrimitiveWithState</artifactId>
<packaging>jar</packaging>
<name>OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState</name>
<parent>
<groupId>org.ow2.frascati.examples</groupId>
<artifactId>parent</artifactId>
<version>1.5-SNAPSHOT</version>
</parent>
<properties>
<composite.file>helloworld-scaPrimitiveWithState</composite.file>
<service.name>Runnable</service.name>
<method.name>run</method.name>
</properties>
<build>
<plugins>
<!-- Compile the SCA composite ${composite.file}. -->
<plugin>
<groupId>org.ow2.frascati.mojo</groupId>
<artifactId>frascati-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- OW2 FraSCAti Membrane scaPrimitiveWithState. -->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-membrane-scaPrimitiveWithState</artifactId>
<version>${project.version}</version>
</dependency>
<!-- OW2 FraSCAti Testing. -->
<dependency>
<groupId>org.ow2.frascati.examples.test</groupId>
<artifactId>frascati-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/**
* OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
* Copyright (C) 2012 Inria, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
*
*/
package org.ow2.frascati.examples.helloworld;
import org.oasisopen.sca.annotation.Init;
import org.oasisopen.sca.annotation.Scope;
import org.oasisopen.sca.annotation.Service;
import org.oasisopen.sca.annotation.Reference;
import org.objectweb.fractal.fraclet.extensions.Membrane;
import org.ow2.frascati.membrane.state.annotation.StateField;
/**
* Client component implementation.
*
* @author Philippe Merle at Inria
*/
@Scope("COMPOSITE")
@Service(value=Runnable.class)
@Membrane(desc="scaPrimitiveWithState") // Client components have a state controller.
public class Client
implements Runnable
{
// --------------------------------------------------------------------------
// SCA Reference.
// --------------------------------------------------------------------------
private HelloService helloService;
@Reference(name="HelloService")
public final void setHelloService(HelloService helloService)
{
this.helloService = helloService;
System.out.println("Client setHelloService(" + this.helloService + ").");
}
// --------------------------------------------------------------------------
// Internal state accessible via the StateController interface.
// --------------------------------------------------------------------------
@StateField
private int foo;
@StateField
private int counter;
//--------------------------------------------------------------------------
// Default constructor.
// --------------------------------------------------------------------------
public Client()
{
System.out.println("Client created.");
}
@Init
public final void init()
{
System.out.println("Client initialized.");
}
// --------------------------------------------------------------------------
// Implementation of the Runnable interface.
// --------------------------------------------------------------------------
public final void run()
{
System.out.println("Call the HelloService...");
// Increments the controlled state.
counter++;
// Call the hello service.
helloService.print("hello world");
}
}
/**
* OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
* Copyright (C) 2012 Inria, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
*
*/
package org.ow2.frascati.examples.helloworld;
public interface HelloService
{
void print(String msg);
}
/**
* OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
* Copyright (C) 2012 INRIA, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
*
*/
package org.ow2.frascati.examples.helloworld;
import org.oasisopen.sca.annotation.Init;
import org.osoa.sca.annotations.Property;
//import org.oasisopen.sca.annotation.Property; // BUG: property 'count' is mandatory.
import org.oasisopen.sca.annotation.Scope;
import org.oasisopen.sca.annotation.Service;
import org.objectweb.fractal.fraclet.extensions.Membrane;
import org.ow2.frascati.membrane.state.annotation.StateField;
/**
* Server component implementation.
*
* @author Philippe Merle at Inria
*/
@Scope("COMPOSITE")
@Service(HelloService.class)
@Membrane(desc="scaPrimitiveWithState") // Server components have a state controller.
public class Server
implements HelloService
{
// --------------------------------------------------------------------------
// SCA Properties.
// --------------------------------------------------------------------------
private String header = "->";
private int count = 1;
public final String getHeader()
{
return header;
}
@Property
public final void setHeader(final String header)
{
this.header = header;
}
public final int getCount()
{
return count;
}
@Property
public final void setCount(final int count)
{
this.count = count;
}
// --------------------------------------------------------------------------
// Internal state accessible via the StateController interface.
// --------------------------------------------------------------------------
@StateField
private int foo;
@StateField
private int counter;
// --------------------------------------------------------------------------
// Default constructor.
// --------------------------------------------------------------------------
public Server()
{
System.out.println("Server created.");
}
@Init
public final void init()
{
System.out.println("Server initialized.");
}
// --------------------------------------------------------------------------
// Implementation of the HelloService interface.
// --------------------------------------------------------------------------
public final void print(final String msg)
{
System.out.println("Server: begin printing...");
for (int i = 0; i < (count); ++i) {
System.out.println(((header) + msg));
}
// Increments the controlled state.
counter++;
System.out.println("Server: print done.");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState -->
<!-- Copyright (C) 2012 Inria, University of Lille 1 -->
<!-- -->
<!-- This library is free software; you can redistribute it and/or -->
<!-- modify it under the terms of the GNU Lesser General Public -->
<!-- License as published by the Free Software Foundation; either -->
<!-- version 2 of the License, or (at your option) any later version. -->
<!-- -->
<!-- This library is distributed in the hope that it will be useful, -->
<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of -->
<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -->
<!-- Lesser General Public License for more details. -->
<!-- -->
<!-- You should have received a copy of the GNU Lesser General Public -->
<!-- License along with this library; if not, write to the Free Software -->
<!-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -->
<!-- USA -->
<!-- -->
<!-- Contact: frascati@ow2.org -->
<!-- -->
<!-- Author: Philippe Merle -->
<!-- -->
<!-- Contributor(s): -->
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
targetNamespace="http://frascati.ow2.org/examples/helloworld-scaPrimitiveWithState"
name="helloworld-scaPrimitiveWithState">
<service name="Runnable" promote="Client/Runnable" />
<component name="Client">
<service name="Runnable">
<interface.java interface="java.lang.Runnable" />
</service>
<implementation.java class="org.ow2.frascati.examples.helloworld.Client" />
<reference name="HelloService" target="Server/HelloService">
<interface.java interface="org.ow2.frascati.examples.helloworld.HelloService"/>
</reference>
</component>
<component name="Server">
<service name="HelloService">
<interface.java interface="org.ow2.frascati.examples.helloworld.HelloService" />
</service>
<implementation.java class="org.ow2.frascati.examples.helloworld.Server" />
</component>
</composite>
/**
* OW2 FraSCAti Examples: HelloWorld with membrane scaPrimitiveWithState
* Copyright (C) 2012 Inria, University of Lille 1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* Contact: frascati@ow2.org
*
* Author: Philippe Merle
*
* Contributor(s):
*
*/
package org.ow2.frascati.examples.helloworld.test;
import org.junit.Assert;
import org.junit.Test;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.control.ContentController;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.ow2.frascati.examples.test.FraSCAtiTestCase;
import org.ow2.frascati.membrane.state.api.StateController;
import org.ow2.frascati.membrane.state.api.State;
/**
* Test HelloWorld with membrane scaPrimitiveWithState.
*/
public class HelloWorldTestCase
extends FraSCAtiTestCase
{
@Override
public final String getComposite()
{
return "helloworld-scaPrimitiveWithState";
}
@Test
public void test() throws Exception
{
// Get the service 'Runnable' of the SCA composite.
Runnable runnable = getService(Runnable.class, "Runnable");
// Call 5 times the service then the 'counter' state field of each subcomponent is equals to 5.
for(int i=0; i<5; i++) {
runnable.run();
}
// Iterates over all sub components of the SCA composite.
for(Component component : ((ContentController)scaComposite.getFcInterface("content-controller")).getFcSubComponents()) {
// Get the state controller of each sub component.
StateController stateController = (StateController)component.getFcInterface(StateController.NAME);
State state = null;
// Try to get the state of each sub component without stopping it before.
try {
state = stateController.getState();
Assert.fail("Not excepted as the component is not stopped!");
} catch(IllegalLifeCycleException ilce) {
// This is excepted as the component is not stopped.
}
// Try to set the state of each sub component without stopping it before.
try {
stateController.setState(state);
Assert.fail("Not excepted as the component is not stopped!");
} catch(IllegalLifeCycleException ilce) {
// This is excepted as the component is not stopped.
}
// Stop the component.
((LifeCycleController)component.getFcInterface("lifecycle-controller")).stopFc();
// Get the state of each sub component.
try {
state = stateController.getState();
// Ok as the component is stopped.
} catch(IllegalLifeCycleException ilce) {
Assert.fail("Not excepted as the component is stopped!");
}
// Read the foo state field.
int foo = state.getField("foo", int.class);
// Check that the counter state field is equals to 5.
Assert.assertEquals((long)5, (long)state.getField("counter", int.class));
// Reset the counter state field.
state.setField("counter", 0);
// Set the state of each sub component.
try {
stateController.setState(state);
// Ok as the component is stopped.
} catch(IllegalLifeCycleException ilce) {
Assert.fail("Not excepted as the component is stopped!");
}
// Get the state of each sub component.
try {
state = stateController.getState();
// Ok as the component is stopped.
} catch(IllegalLifeCycleException ilce) {
Assert.fail("Not excepted as the component is stopped!");
}
// Check that the counter state field was really updated.
Assert.assertEquals((long)0, (long)state.getField("counter", int.class));
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment