Commit 9ce60606 authored by Christophe Demarey's avatar Christophe Demarey

Adapt the reconfig example to the new architecture of FScript. Now use an SCA...

Adapt the reconfig example to the new architecture of FScript. Now use an SCA component implemented by an fscript file.
parent afcdd357
......@@ -44,7 +44,6 @@
<composite.file>reconfig</composite.file>
<service.name>r</service.name>
<method.name>run</method.name>
<org.ow2.frascati.class>org.ow2.frascati.fscript.FrascatiScriptFactory</org.ow2.frascati.class>
<org.ow2.frascati.bootstrap>org.ow2.frascati.bootstrap.FraSCAtiFractal</org.ow2.frascati.bootstrap>
</properties>
......@@ -64,10 +63,18 @@
<!-- FraSCAti FScript -->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-fscript</artifactId>
<artifactId>frascati-fscript-core</artifactId>
<version>${frascati.version}</version>
</dependency>
<!-- OW2 FraSCAti Script Implementation. -->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-implementation-script</artifactId>
<version>${frascati.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.examples.test</groupId>
<artifactId>frascati-test</artifactId>
......@@ -77,49 +84,4 @@
</dependencies>
<!-- ========= -->
<!-- Profiles -->
<!-- ========= -->
<profiles>
<!-- To execute an SCA composite with FraSCAti Explorer type 'mvn -Pexplorer'. -->
<profile>
<id>explorer</id>
<dependencies>
<!-- Add OW2 FraSCAti Explorer FScript plugin -->
<dependency>
<groupId>org.ow2.frascati</groupId>
<artifactId>frascati-explorer-fscript-plugin</artifactId>
<version>${frascati.version}</version>
</dependency>
</dependencies>
</profile>
<!-- To execute an SCA composite with FraSCAti Explorer type 'mvn -Pdemo-adt-galaxy'. -->
<profile>
<id>demo-adt-galaxy</id>
<build>
<defaultGoal>exec:exec</defaultGoal>
<plugins>
<!-- Run the Explorer -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Dfractal.provider=org.objectweb.fractal.julia.Julia</argument>
<argument>-cp</argument>
<classpath />
<argument>org.ow2.frascati.explorer.fscript.FrascatiScriptExplorerLauncher</argument>
<argument>just-converter</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
......@@ -20,7 +20,8 @@
*/
package org.ow2.frascati.examples.reconfig;
import org.objectweb.fractal.fscript.FScriptException;
import javax.script.ScriptException;
import org.osoa.sca.annotations.Reference;
import org.ow2.frascati.examples.reconfig.converter.CurrencyConverter;
import org.ow2.frascati.examples.reconfig.converter.fscript.ExchangeRateUpdater;
......@@ -36,6 +37,8 @@ public class Client implements Runnable {
protected CurrencyConverter converter;
@Reference
protected ExchangeRateUpdater updater;
@Reference
protected ExchangeRateUpdater updaterScript;
private final static double newRate = 0.823;
private final static double newRateV2 = 0.698177756;
......@@ -49,24 +52,24 @@ public class Client implements Runnable {
result = converter.dollarToEuro(100);
System.out.println("100$ are worth about "+result+"€.");
System.out.print("Updating exchange rate to "+newRate+" ...");
System.out.println("Updating exchange rate to "+newRate+" ...");
try {
updater.updateDollarExchangeRate(newRate);
System.out.println(" Done!");
result = converter.dollarToEuro(100);
System.out.println("100$ are now worth about "+result+"€.");
} catch (FScriptException e) {
} catch (ScriptException e) {
System.out.println("Reconfiguration failed!");
e.printStackTrace();
}
System.out.println("Updating exchange rate to "+newRateV2+" ...");
try {
updater.updateDollarExchangeRateV2(newRateV2);
updaterScript.updateDollarExchangeRate(newRateV2);
System.out.println("Done!");
result = converter.dollarToEuro(100);
System.out.println("100$ are now worth about "+result+"€.");
} catch (FScriptException e) {
} catch (ScriptException e) {
System.out.println("Reconfiguration failed!");
e.printStackTrace();
}
......
......@@ -30,10 +30,10 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JTextField;
import org.objectweb.fractal.fscript.FScriptException;
import org.ow2.frascati.examples.reconfig.converter.fscript.ExchangeRateUpdater;
import org.ow2.frascati.explorer.gui.AbstractSelectionPanel;
......@@ -72,7 +72,7 @@ public class UpdaterPanel extends AbstractSelectionPanel<ExchangeRateUpdater> {
double newValue = Double.parseDouble( jTextFieldValue.getText() );
try {
selected.updateDollarExchangeRate(newValue);
} catch (FScriptException e1) {
} catch (ScriptException e1) {
System.out.println("Reconfiguration failed!");
e1.printStackTrace();
}
......
......@@ -20,7 +20,7 @@
*/
package org.ow2.frascati.examples.reconfig.converter.fscript;
import org.objectweb.fractal.fscript.FScriptException;
import javax.script.ScriptException;
/**
* This service allows to dynamically update the exchange rate of the currency
......@@ -34,14 +34,5 @@ public interface ExchangeRateUpdater {
*
* @param newValue The new exchange rate value.
*/
void updateDollarExchangeRate(double newValue) throws FScriptException;
/**
* Update the exchange rate of the converter with the help of fscript.
* This method does the same thing as {@link #updateExchangeRate(double)} but
* in a different way: it first loads an fscript file and then calls the defined
* action to reconfigure the converter.
*
* @param newValue The new exchange rate value.
*/
void updateDollarExchangeRateV2(double newValue) throws FScriptException;
void updateDollarExchangeRate(double newValue) throws ScriptException;
}
......@@ -20,12 +20,14 @@
*/
package org.ow2.frascati.examples.reconfig.converter.fscript;
import java.util.Set;
import java.util.TreeSet;
import javax.script.Bindings;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.objectweb.fractal.fscript.FScriptException;
import org.objectweb.fractal.fscript.jsr223.FScriptEngineFactoryProxy;
import org.objectweb.fractal.fscript.jsr223.InvocableScriptEngine;
import org.osoa.sca.annotations.Property;
import org.ow2.frascati.fscript.FraSCAtiScript;
import org.ow2.frascati.fscript.jsr223.FraSCAtiScriptEngineFactory;
/**
* A SCA component used to dynamically reconfigure another SCA component: the
......@@ -39,23 +41,15 @@ public class ExchangeRateUpdaterImpl implements ExchangeRateUpdater {
@Property
protected String reconfigurationScript;
public void updateDollarExchangeRate(double newValue) throws FScriptException {
FraSCAtiScript fscript = FraSCAtiScript.getSingleton();
fscript.execute("converter = $domain/scachild::reconfig/scachild::converter/scachild::currency-converter;");
fscript.execute("rate = $converter/scaproperty::dollarExchangeRate;");
fscript.execute("set-value($rate,"+newValue+")");
}
public void updateDollarExchangeRate(double newValue) throws ScriptException {
System.setProperty(FScriptEngineFactoryProxy.SCRIPT_ENGINE_FACTORY_PROPERTY_NAME,"org.ow2.frascati.fscript.jsr223.FraSCAtiScriptEngineFactory");
ScriptEngineManager manager = new ScriptEngineManager();
new FraSCAtiScriptEngineFactory().addDomainToContext( manager.getBindings() ); // Add a global var for domain
InvocableScriptEngine engine = (InvocableScriptEngine) manager.getEngineByExtension("fscript");
Bindings ctx = engine.createBindings(); // our execution context
public void updateDollarExchangeRateV2(double newValue) throws FScriptException {
Set<String> loadedProc = new TreeSet<String>();
FraSCAtiScript fscript = FraSCAtiScript.getSingleton();
loadedProc = fscript.load(reconfigurationScript);
for (String proc : loadedProc) {
System.out.println("+-> Adding procedure '"+ proc +"' to FraSCAtiScript engine");
}
fscript.execute("reconfigure-converter("+newValue+")");
engine.eval("converter = $domain/scachild::reconfig/scachild::converter/scachild::currency-converter;", ctx);
engine.eval("rate = $converter/scaproperty::dollarExchangeRate;", ctx);
engine.eval("set-value($rate,"+newValue+")", ctx);
}
}
......@@ -30,7 +30,8 @@
<binding.ws uri="http://localhost:9000/converter-ws"/>
</service>
<service name="exchangeRateUpdater" promote="exchange-rate-updater/exchangeRateUpdater"/>
<service name="exchangeRateUpdater" promote="exchange-rate-updater/exchangeRateUpdater"/>
<service name="exchangeRateUpdaterScript" promote="exchange-rate-updater-script/exchangeRateUpdater"/>
<component name="currency-converter">
<implementation.java class="org.ow2.frascati.examples.reconfig.converter.CurrencyConverterImpl"/>
......@@ -53,4 +54,12 @@
<property name="reconfigurationScript">reconfig.fscript</property>
</component>
<component name="exchange-rate-updater-script">
<frascati:implementation.script script="reconfig.fscript"/>
<service name="exchangeRateUpdater">
<interface.java interface="org.ow2.frascati.examples.reconfig.converter.fscript.ExchangeRateUpdater"/>
</service>
</component>
</composite>
......@@ -37,7 +37,8 @@
</service>
</component>
<wire source="client/converter" target="converter/currencyConverter"/>
<wire source="client/updater" target="converter/exchangeRateUpdater"/>
<wire source="client/converter" target="converter/currencyConverter"/>
<wire source="client/updater" target="converter/exchangeRateUpdater"/>
<wire source="client/updaterScript" target="converter/exchangeRateUpdaterScript"/>
</composite>
-- Fscript file defining some fscript actions
-- Implementation of the exchange-rate-updater-script SCA component
action reconfigure-converter(newValue){
action updateDollarExchangeRate(newValue){
echo("[FScript] Running updateDollarExchangeRate ...");
converter = $domain/scachild::reconfig/scachild::converter/scachild::currency-converter;
rate = $converter/scaproperty::dollarExchangeRate;
set-value($rate, $newValue);
......
......@@ -25,10 +25,16 @@
package org.ow2.frascati.examples.reconfig;
import org.junit.Test;
import org.objectweb.fractal.fscript.jsr223.FScriptEngineFactoryProxy;
import org.ow2.frascati.examples.test.FraSCAtiTestCase;
public class ReconfigTestCase extends FraSCAtiTestCase {
public ReconfigTestCase() {
System.setProperty( FScriptEngineFactoryProxy.SCRIPT_ENGINE_FACTORY_PROPERTY_NAME,
"org.ow2.frascati.fscript.jsr223.FraSCAtiScriptEngineFactory" );
}
@Override
public String getComposite() {
return "reconfig";
......
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