Commit 7ac4169d authored by Christophe Demarey's avatar Christophe Demarey
Browse files

Add a currency converter component that will be used in the reconfiguration scenario.

parent 7c99930c
============================================================================
OW2 FraSCAti Examples: Fscript reconfiguration
Copyright (C) 2010 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: Christophe Demarey
Contributor:
============================================================================
Fscript reconfiguration example:
------------------------
This example shows how to reconfigure an SCA application iat runtime.
It uses FraSCAti Script to easily request / reconfigure our SCA architecture.
Compilation with Maven:
-----------------------
mvn install
Execution with Maven:
---------------------
mvn -Prun (standalone execution)
mvn -Pexplorer (with FraSCAti Explorer)
Compilation and execution with the FraSCAti script:
---------------------------------------------------
frascati compile src reconfig
frascati run reconfig -libpath reconfig.jar -s r -m run
<?xml version="1.0"?>
<!--
* OW2 FraSCAti Examples: Fscript reconfiguration
*
* Copyright (c) 2010 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: Christophe Demarey
*
* 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>reconfig</artifactId>
<version>1.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>OW2 FraSCAti Examples: Fscript reconfiguration</name>
<parent>
<groupId>org.ow2.frascati.examples</groupId>
<artifactId>parent</artifactId>
<version>1.2-SNAPSHOT</version>
</parent>
<properties>
<logging.conf.file>../../distribution/standalone/src/main/conf/logging.properties</logging.conf.file>
<composite.file>reconfig</composite.file>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.ow2.frascati.examples.test</groupId>
<artifactId>frascati-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swing-layout</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
</project>
/***
* OW2 FraSCAti Examples : Fscript reconfiguration
* Copyright (C) 2008-2010 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
*
* Authors: Christophe Demarey
*/
package org.ow2.frascati.examples.reconfig.converter;
/**
* A basic currency converter.
*/
public interface CurrencyConverter {
/**
* Converts Euros to Dollars.
*
* @param value the amount of euros to convert.
*/
double euroToDollar(double value);
/**
* Converts Dollars to Euros.
*
* @param value the amount of dollars to convert.
*/
double dollarToEuro(double value);
}
/***
* OW2 FraSCAti Examples : Fscript reconfiguration
* Copyright (C) 2008-2010 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
*
* Author: Christophe Demarey
*/
package org.ow2.frascati.examples.reconfig.converter;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Scope;
/**
* The Currency converter service implementation.
*/
//@Scope(value="COMPOSITE")
public class CurrencyConverterImpl implements CurrencyConverter {
private double exchangeRate;
/** Default constructor. */
public CurrencyConverterImpl() {
System.out.println("Currency converter created.");
}
/**
* Set the exchangeRate property.
*
* @param rate - the new value of the exchange rate.
*/
@Property
public void setExchangeRate(final double rate) {
System.out.println("Setting exchange rate to '" + rate + "'.");
this.exchangeRate = rate;
}
// Implementation of the CurrencyConverter service
public double dollarToEuro(double value) {
return value * exchangeRate;
}
public double euroToDollar(double value) {
return value / exchangeRate;
}
}
\ No newline at end of file
/***
* OW2 FraSCAti Examples: Fscript reconfiguration
* Copyright (C) 2010 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: Christophe Demarey
*
* Contributor(s):
*
*/
package org.ow2.frascati.examples.reconfig.converter.explorer;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.NumberFormat;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import org.ow2.frascati.examples.reconfig.converter.CurrencyConverter;
import org.ow2.frascati.explorer.gui.AbstractSelectionPanel;
/**
* Is the FraSCAti Explorer plugin to show {@link CurrencyConverter} instances.
*
* @author Christophe Demarey
*/
@SuppressWarnings("serial")
public class ConverterPanel extends AbstractSelectionPanel<CurrencyConverter> {
private NumberFormat nf; // used to format double display
private JLabel jLabelDollar;
private JLabel jLabelEuro;
private JTextField jTextFieldDollar;
private JTextField jTextFieldEuro;
/**
* Default constructor creates the panel.
*/
public ConverterPanel() {
super();
nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(4);
jLabelEuro = new JLabel();
jLabelDollar = new JLabel();
jTextFieldEuro = new JTextField();
jTextFieldDollar = new JTextField();
jLabelEuro.setText("Euro");
jLabelEuro.setName("jLabelEuro");
jLabelDollar.setText("Dollar");
jLabelDollar.setName("jLabelDollar");
jTextFieldEuro.setName("jTextFieldEuro");
jTextFieldDollar.setName("jTextFieldDollar");
// layout
GroupLayout mainPanelLayout = new GroupLayout(this);
this.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.add(mainPanelLayout.createParallelGroup(GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(mainPanelLayout.createParallelGroup(GroupLayout.CENTER)
.add(jLabelEuro)
.add(jTextFieldEuro, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(mainPanelLayout.createParallelGroup(GroupLayout.CENTER)
.add(jLabelDollar)
.add(jTextFieldDollar, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE))))
.addContainerGap(607, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(GroupLayout.LEADING)
.add(mainPanelLayout.createSequentialGroup()
.add(20, 20, 20)
.add(mainPanelLayout.createParallelGroup(GroupLayout.TRAILING)
.add(mainPanelLayout.createSequentialGroup()
.add(jLabelDollar)
.addPreferredGap(LayoutStyle.RELATED)
.add(jTextFieldDollar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.add(mainPanelLayout.createSequentialGroup()
.add(jLabelEuro)
.addPreferredGap(LayoutStyle.RELATED)
.add(jTextFieldEuro, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(LayoutStyle.RELATED)
.addContainerGap(545, Short.MAX_VALUE))
);
// actions
jTextFieldEuro.addKeyListener( new KeyListener() {
public void keyPressed(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) {
if ( jTextFieldEuro.getText().length() > 0 ) {
double value = Double.parseDouble( jTextFieldEuro.getText().replaceAll(",", ".") );
double result = selected.euroToDollar(value);
jTextFieldDollar.setText( nf.format(result) );
}
}
}
);
jTextFieldDollar.addKeyListener( new KeyListener() {
public void keyPressed(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) {
if ( jTextFieldDollar.getText().length() > 0 ) {
double value = Double.parseDouble( jTextFieldDollar.getText().replaceAll(",", ".") );
double result = selected.dollarToEuro(value);
jTextFieldEuro.setText( nf.format(result) );
}
}
}
);
jTextFieldEuro.addFocusListener( new FocusListener() {
public void focusLost(FocusEvent e) { }
public void focusGained(FocusEvent e) {
jTextFieldEuro.setText("");
}
}
);
jTextFieldDollar.addFocusListener( new FocusListener() {
public void focusLost(FocusEvent e) { }
public void focusGained(FocusEvent e) {
jTextFieldDollar.setText("");
}
}
);
}
}
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
* ====================================================================
*
* OW2 FraSCAti Examples: Soap calculator
* 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: Christophe Demarey
*
* Contributor(s):
* ====================================================================
-->
<explorer>
<!-- ================================================================= -->
<!-- FraSCAti Explorer plugins for this example. -->
<!-- ================================================================= -->
<node type-name="org.ow2.frascati.examples.reconfig.converter.CurrencyConverter">
<panel>
<code>org.ow2.frascati.examples.reconfig.converter.explorer.ConverterPanel</code>
</panel>
</node>
</explorer>
<?xml version="1.0" encoding="ISO-8859-15"?>
<!-- OW2 FraSCAti Examples : Fscript reconfiguration -->
<!-- Copyright (C) 2008-2010 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: Christophe Demarey -->
<!-- -->
<!-- Contributor(s): -->
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" name="reconfig">
<service name="r" promote="currency-converter/currencyConverter"/>
<component name="currency-converter">
<implementation.java class="org.ow2.frascati.examples.reconfig.converter.CurrencyConverterImpl"/>
<service name="currencyConverter">
<interface.java interface="org.ow2.frascati.examples.reconfig.converter.CurrencyConverter"/>
</service>
<property name="exchangeRate">0.75</property>
</component>
</composite>
/***
* OW2 FraSCAti Examples: Fscript reconfiguration
* Copyright (C) 2010 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: Christophe Demarey
*
* Contributor(s):
*/
package org.ow2.frascati.examples.reconfig;
import org.junit.Test;
import org.ow2.frascati.examples.test.FraSCAtiTestCase;
public class ReconfigTestCase extends FraSCAtiTestCase {
@Override
public String getComposite() {
return "reconfig";
}
@Test
public void testService() {
// getService(Runnable.class, "r").run();
}
}
Supports Markdown
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