Commit 784c566f authored by Christophe Demarey's avatar Christophe Demarey
Browse files

tags reorganization.

parents b7f36377 e3c91427
Transaction Service 0.1
-----------------------
* first release
* integration of JOTM (http://jotm.ow2.org/) as the underlayed Transaction Manager
* Use Tinfi 0.3
============================================================================
OW2 FraSCAti Transaction Service
Copyright (C) 2008 INRIA
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: Nicolas Dolet
Contributor(s) :
============================================================================
This file explains how to run the example that uses the Frascati
transaction service.
The main SCA transfer composite contains 3 primitive components:
1) two account components, bound to a mysql database.
2) a transfer component that references the two accounts and promotes a service
to perform a transfer between them.
Requirements to run the example:
--------------------------------
- Update the Tinfi dependency in the assembly-factory module to reference the
0.3-SNAPSHOT version of Tinfi (which provides capabilities to manipulate
SCA intents)
- Install a mysql server and create the database, the table and insert data
(see src/database.sql)
- Compile/Install the transaction service:
$ mvn install (from the transaction-service module)
Run the example:
----------------
$ mvn -Pfactory:run (from the transaction-service/examples/transfer directory)
<?xml version="1.0"?>
<!--
* OW2 FraSCAti Transaction service.
*
* Copyright (C) 2008-2009 INRIA
*
* 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
-->
<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.factory.examples.transaction</groupId>
<artifactId>transfer</artifactId>
<version>0.2</version>
<name>Transactions Example: Transfer service</name>
<packaging>jar</packaging>
<properties>
<frascati.version>0.5</frascati.version>
<example.main.composite>transfer</example.main.composite>
<example.main.class>org.ow2.frascati.transaction.examples.transfer.Main</example.main.class>
</properties>
<dependencies>
<!-- The SCOrWare Assembly Factory Core Module. -->
<dependency>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-af-runtime</artifactId>
<version>${frascati.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.transaction</groupId>
<artifactId>frascati-transaction</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>javax.resource</groupId>
<artifactId>connector-api</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.experlog</groupId>
<artifactId>xapool</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<!-- Compile the ${example.main.composite} SCA composite with the FraSCAti Factory.-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<meminitial>64m</meminitial>
<maxmem>256m</maxmem>
<source>1.5</source>
<target>${java.specification.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.ow2.frascati.factory</groupId>
<artifactId>frascati-factory-plugin</artifactId>
<version>${frascati.version}</version>
<executions>
<execution>
<id>generate-factory</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<factory>GenerateFactory</factory>
<srcs>
<src>src/main/java</src>
<src>src/main/resources</src>
</srcs>
<composite>${example.main.composite}</composite>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<!-- Run the ${example.main.class} Java class. -->
<profile>
<id>factory:run</id>
<build>
<defaultGoal>compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<mainClass>${example.main.class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
-- * OW2 FraSCAti Transaction service
-- * Copyright (C) 2008 INRIA
-- *
-- * 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: Nicolas Dolet
CREATE USER scorware IDENTIFIED BY 'scorware';
create database Accounts;
GRANT ALL PRIVILEGES ON Accounts.* TO 'scorware'@'localhost' IDENTIFIED BY 'scorware' WITH GRANT OPTION;
use Accounts;
create table account (
account char(4) not null primary key,
balance decimal)type=InnoDB;
insert into account values("A001", 1000);
insert into account values("A002", 500);
insert into account values("A003", 5000);
/***
* OW2 FraSCAti Transaction service
* Copyright (C) 2008-2009 INRIA
*
* 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: Nicolas Dolet
*/
package org.ow2.frascati.transaction.examples.transfer;
/**
* Represents an account on which one can credit/debit money
*
* @author <a href="mailto:Nicolas.Dolet@inria.fr">Nicolas Dolet</a>
*/
public interface Account {
/**
* Credit the account
*
* @param amount
* the amount to credit
*/
public void credit(float amount);
/**
* Debit the account
*
* @param amount
* the amount to debit
* @throws InsufficientAmountException
* if there is not enough money to perform the debit
*/
public void debit(float amount) throws InsufficientAmountException;
}
/***
* OW2 FraSCAti Transaction service
* Copyright (C) 2008-2009 INRIA
*
* 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: Nicolas Dolet
*/
package org.ow2.frascati.transaction.examples.transfer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.XADataSource;
import javax.transaction.TransactionManager;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.osoa.sca.annotations.Property;
import org.ow2.frascati.transaction.TransactionManagerHelper;
/**
* Implementation of the {@link Account} component
*
* @author <a href="mailto:Nicolas.Dolet@inria.fr">Nicolas Dolet</a>
*/
public class AccountImpl implements Account {
/**
* The id of the account
*/
@Property
public String id;
// --------------------------------------------------------------------------
// Internal state
// --------------------------------------------------------------------------
/**
* The connection to the database
*/
private Connection conn = null;
// --------------------------------------------------------------------------
// Implementation of the Account interface
// --------------------------------------------------------------------------
/**
* @see Account#credit(float)
*/
public void credit(float amount) {
System.out.println("Credit " + amount + " on account number " + id);
try {
float oldBalance = getDBBalance();
float newBalance = oldBalance + amount;
setDBBalance(newBalance);
} catch (SQLException e) {
System.err.println("Can't retrieve balance of account " + id + ": "
+ "error during database access!");
}
}
/**
* @see Account#debit(float)
*/
public void debit(float amount) throws InsufficientAmountException {
System.out.println("Debit " + amount + " from account number " + id);
try {
float oldBalance = getDBBalance();
if (amount > oldBalance) {
throw new InsufficientAmountException("Can't debit " + amount
+ " from account " + id);
}
float newBalance = oldBalance - amount;
setDBBalance(newBalance);
} catch (SQLException e) {
System.err.println("Can't retrieve balance of account " + id + ": "
+ "error during database access!");
}
}
/**
* @see Account#toString()
*/
public String toString() {
try {
return "Account '" + this.id + "': " + getDBBalance();
} catch (SQLException e) {
return "Account '" + this.id + "': Unknown balance: "
+ "database unreachable.";
}
}
// --------------------------------------------------------------------------
// Getter / Setter for the balance property
// --------------------------------------------------------------------------
/**
* Get the balance of the account
* @return a String representing the balance
*/
@Property
public String getBalance() {
try {
return Float.toString(getDBBalance());
} catch (SQLException e) {
return null;
}
}
/**
* Set the balance of the account
* @param balance the String representing the balance to set
*/
@Property
public void setBalance(String balance) {
// this.internalBalance = Float.valueOf(balance);
setDBBalance(Float.valueOf(balance));
}
// --------------------------------------------------------------------------
// Private methods for database connection
// --------------------------------------------------------------------------
/**
* Make the connection to the database
*/
private void connectDB() {
try {
String login = "scorware";
String password = "scorware";
XADataSource xads = new StandardXADataSource();
((StandardXADataSource) xads)
.setDriverName("com.mysql.jdbc.Driver");
((StandardXADataSource) xads)
.setUrl("jdbc:mysql://localhost/Accounts");
TransactionManager tm = TransactionManagerHelper.getTransactionManager();
if(tm != null)
((StandardXADataSource) xads).setTransactionManager(tm);
conn = xads.getXAConnection(login, password).getConnection();
} catch (Exception e) {
System.err.println("Cannot connect to database server");
e.printStackTrace();
}
}
/**
* Close the connection to the database
*/
private void disconnectDB() {
if (conn != null) {
try {
conn.close();
// System.out.println("Database connection terminated");
} catch (Exception e) {
System.err.println("Cannot close the database connection");
e.printStackTrace();
}
}
}
/**
* Get the balance stored in the database
* @return the balance of the account
* @throws SQLException if an error occurs during database access
*/
private float getDBBalance() throws SQLException {
connectDB();
Statement s;
long result;
s = conn.createStatement();
s.executeQuery("SELECT balance FROM account WHERE account=\"" + id
+ "\"");
ResultSet rs = s.getResultSet();
if (rs.next()) {
result = rs.getLong("balance");
} else {
throw new SQLException("No account " + id
+ " found in the database!");
}
rs.close();
s.close();
disconnectDB();
return result;
}
/**
* Update the balance stored in the database
* @param balance the balance to set
*/
private void setDBBalance(float balance) {
connectDB();
Statement s;
try {
s = conn.createStatement();
s.executeUpdate("UPDATE account set balance = " + balance
+ " where account = \"" + id + "\"");
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
disconnectDB();
}
}
/***
* OW2 FraSCAti Transaction service
* Copyright (C) 2008-2009 INRIA
*
* 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: Nicolas Dolet
*/
package org.ow2.frascati.transaction.examples.transfer;
/**
* Exception to throw when trying to debit more money than the account's balance
*
* @author <a href="mailto:Nicolas.Dolet@inria.fr">Nicolas Dolet</a>
*/
public class InsufficientAmountException
extends Exception {
private static final long serialVersionUID = -4951925869024581896L;
/**
* The default constructor
* @param message the message
*/
public InsufficientAmountException(String message) {
super(message);
}
}