Commit 23736dfa authored by Lionel Seinturier's avatar Lionel Seinturier

New example mixing SCA and OSGi components.

parent 5fd79f17
To compile and run the example:
mvn install
<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>
<parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi</artifactId>
<version>0.4.5-SNAPSHOT</version>
</parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-assembly</artifactId>
<packaging>jar</packaging>
<name>Tinfi HelloWorld Example With OSGi - Assembling</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-interfaces</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.objectweb.fractal.juliac</groupId>
<artifactId>juliac-julia-membranes-oo</artifactId>
<version>${juliac.version}</version>
</dependency>
<dependency>
<groupId>org.objectweb.fractal.juliac.osgi</groupId>
<artifactId>juliac-osgi-membranes-oo</artifactId>
<version>${juliac.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.tinfi</groupId>
<artifactId>frascati-tinfi-membranes-oo</artifactId>
<version>${project.version}</version>
</dependency>
<!-- ============================= -->
<!-- Dependency for generated code -->
<!-- ============================= -->
<dependency>
<groupId>org.ow2.frascati.tinfi</groupId>
<artifactId>frascati-tinfi-runtime</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- ==================================== -->
<!-- Generate and compile the application -->
<!-- ==================================== -->
<plugin>
<groupId>org.objectweb.fractal.juliac</groupId>
<artifactId>maven-juliac-plugin</artifactId>
<version>${juliac.version}</version>
<executions>
<execution>
<id>juliac-compile</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<opt>org.ow2.frascati.tinfi.juliac.FCOOCtrlSourceCodeGenerator:org.objectweb.fractal.juliac.felix.FCOOCtrlSourceCodeGenerator:OO</opt>
<adls><adl>example.hw.HelloWorld</adl></adls>
<modules>
<module>org.ow2.frascati.tinfi:frascati-tinfi-compiler:${project.version}</module>
<module>org.objectweb.fractal.juliac.osgi:juliac-osgi-compiler:${juliac.version}</module>
<module>org.objectweb.fractal.juliac:juliac-fractaladl:${juliac.version}</module>
<module>org.objectweb.fractal.juliac:juliac-jdt:${juliac.version}</module>
</modules>
<compileInput>false</compileInput>
<!-- verbose>true</verbose -->
</configuration>
</plugin>
<!-- ==================================== -->
<!-- Copy bundles in target/classes -->
<!-- ==================================== -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>copy-bundles</id>
<phase>generate-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<stripVersion>true</stripVersion> <!-- Remove version number -->
<artifactItems>
<artifactItem> <!-- Fractal API -->
<groupId>org.ow2.fractal</groupId>
<artifactId>fractal-api-bundle</artifactId>
<version>2.0.2.0-0001</version>
</artifactItem>
<artifactItem> <!-- Julia Runtime -->
<groupId>org.ow2.fractal</groupId>
<artifactId>julia-runtime-bundle</artifactId>
<version>2.5.2.0-0001</version>
</artifactItem>
<artifactItem> <!-- Juliac Runtime OO -->
<groupId>org.ow2.fractal</groupId>
<artifactId>juliac-runtime-oo-bundle</artifactId>
<version>2.1.5.0-0001</version>
</artifactItem>
<artifactItem>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-interfaces</artifactId>
<version>${project.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-client</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<?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/standard.dtd">
<definition name="example.hw.HelloWorld" >
<interface name="r" signature="java.lang.Runnable" role="server" />
<component name="client">
<interface name="r" signature="java.lang.Runnable" role="server" />
<interface name="s" signature="example.hw.itf.Service" role="client" />
<content class="frascati-tinfi-helloworld-osgi-interfaces.jar frascati-tinfi-helloworld-osgi-client.jar"/>
<controller desc="osgiPrimitive" />
</component>
<component name="server">
<interface name="s" signature="example.hw.itf.Service" role="server" cardinality="singleton" contingency="mandatory"/>
<content class="example.hw.server.ServerImpl"/>
<controller desc="scaPrimitive"/>
</component>
<binding client="this.r" server="client.r" />
<binding client="client.s" server="server.s" />
</definition>
/***
* OW2 FraSCAti Tinfi
* Copyright (C) 2009 INRIA, USTL
*
* 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: Lionel Seinturier
*/
package example.hw;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.factory.InstantiationException;
import org.objectweb.fractal.juliac.felix.FelixHelper;
import org.osgi.framework.BundleException;
import org.ow2.frascati.tinfi.TinfiDomain;
import example.hw.itf.Console;
/**
* Automate the launching of the HelloWorld example and check that the example
* runs as expected.
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
* @since 0.4.5
*/
public class HelloWorldTestCase {
private Component root;
@Before
public void setUp()
throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
IllegalLifeCycleException, NoSuchInterfaceException,
java.lang.InstantiationException {
// Instanciate the HelloWorld composite
root = TinfiDomain.getComponent("example.hw.HelloWorld");
}
@After
public void tearDown() throws BundleException, IOException {
FelixHelper.getFelix().stop();
}
@Test
public void serverHelloWorld() throws NoSuchInterfaceException {
/*
* Use the Console class to direct the output of the example to a
* temporary file.
*/
Console.init(
System.err,
new String[]{
"Server: begin printing...",
"->hello world",
"Server: print done.",
});
/*
* Run the test.
*/
Runnable r = (Runnable) root.getFcInterface("r");
r.run();
}
}
<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>
<parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi</artifactId>
<version>0.4.5-SNAPSHOT</version>
</parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-client</artifactId>
<packaging>jar</packaging>
<name>Tinfi HelloWorld Example With OSGi - Client Code</name>
<dependencies>
<dependency>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-interfaces</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>bundle-bundle</id>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Name>OSGi Felix HelloWorld client</Bundle-Name>
<Bundle-Vendor>USTL - INRIA</Bundle-Vendor>
<Bundle-Description>${pom.name}</Bundle-Description>
<Bundle-Activator>example.hw.client.Activator</Bundle-Activator>
<Export-Package>example.hw.client</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
package example.hw.client;
import java.util.Dictionary;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import example.hw.itf.Console;
import example.hw.itf.Service;
public class Activator implements BundleActivator {
public void start( BundleContext context ) throws InvalidSyntaxException {
ServiceReference[] refs =
context.getServiceReferences( Service.class.getName(), null );
Service service = (Service) context.getService(refs[0]);
ClientImpl client = new ClientImpl();
client.setService(service);
context.registerService( Runnable.class.getName(), client, null );
Bundle bundle = context.getBundle();
Dictionary dict = bundle.getHeaders();
String name = (String) dict.get("Bundle-Name");
System.err.println("Bundle <"+name+"> started");
}
public void stop( BundleContext context ) {
Bundle bundle = context.getBundle();
Dictionary dict = bundle.getHeaders();
String name = (String) dict.get("Bundle-Name");
System.err.println("Bundle <"+name+"> stopped");
}
}
package example.hw.client;
import example.hw.itf.Service;
public class ClientImpl implements Runnable {
public void run() {
s.print("hello world");
}
public void setService( Service s ) {
this.s = s;
}
private Service 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>
<parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi</artifactId>
<version>0.4.5-SNAPSHOT</version>
</parent>
<groupId>org.ow2.frascati.tinfi.examples</groupId>
<artifactId>frascati-tinfi-helloworld-osgi-interfaces</artifactId>
<packaging>jar</packaging>
<name>Tinfi HelloWorld Example With OSGi - Interfaces</name>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>bundle-bundle</id>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Name>OSGi Felix HelloWorld interfaces</Bundle-Name>
<Bundle-Vendor>USTL - INRIA</Bundle-Vendor>
<Bundle-Description>${pom.name}</Bundle-Description>
<Bundle-Activator>example.hw.itf.Activator</Bundle-Activator>
<Export-Package>example.hw.itf</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
package example.hw.itf;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
public void start( BundleContext context ) {
}
public void stop( BundleContext context ) {
}
}
/***
* OW2 FraSCAti Tinfi
* Copyright (C) 2008-2009 INRIA, USTL
*
* 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: Lionel Seinturier
*/
package example.hw.itf;
import java.io.PrintStream;
/**
* This class provides a way for printing characters to a {@link PrintStream}
* while comparing with some expected characters.
*
* @author Lionel Seinturier <Lionel.Seinturier@univ-lille1.fr>
*/
public class Console {
// ---------------------------------------------------------------------
// Public static interface for initializing and retrieving the singleton
// instance of this class
// ---------------------------------------------------------------------
/** Singleton instance of this class. */
private static Console console;
/**
* Initialize the console.
*
* @param ps the {@link PrintStream} where characters are outputted
* @param expected the expected characters
*/
public static void init( PrintStream ps, String[] expected ) {
console = new Console(ps,expected);
}
/**
* Return the singleton instance of this class.
*
* @throws IllegalStateException
* if the instance has not been initialized with
* {@link #init(PrintStream, String[])}
*/
public static Console get() throws IllegalStateException {
if( console == null ) {
String msg = "Console has not been initialized";
throw new IllegalStateException(msg);
}
return console;
}
// ---------------------------------------------------------------------
// Constructor and instance variables
// ---------------------------------------------------------------------
private PrintStream ps;
private String[] expected;
private Console( PrintStream ps, String[] expected ) {
this.ps = ps;
this.expected = expected;
}
// ---------------------------------------------------------------------
// Printing methods
// ---------------------------------------------------------------------
public void println( String s ) {
print(s);
print('\n');
}
public void print( String s ) {
for( int i=0 ; i < s.length() ; i++ ) {
print(s.charAt(i));
}
}
public void print( char c ) {
// Manage line and column indexes
if( c == '\n' ) {
line++;
col=1;
}
else {
// Check whether there are still some expected characters
if( ei >= expected.length ) {
fail();
}
// Get the expected character
char e = expected[ei].charAt(ej);
// Compare with the actual character
if( c != e ) {
fail();
}
// Move the indexes to the next expected character
ej++;
if( ej >= expected[ei].length() ) {
ei++;
ej = 0;
}
// Move to the next column
col++;
}
// Propagate the printing of the character
ps.print(c);
}
private int ei = 0;
private int ej = 0;
private int line = 1;
private int col = 1;
// ---------------------------------------------------------------------
// Notify a difference between expected and actual characters
// ---------------------------------------------------------------------
/**
* @throws IllegalArgumentException
* to notify a difference between the expected and the actual character
*/
private void fail() throws IllegalArgumentException {
ps.println();
ps.println();
ps.println("Expected output is: ");