diff options
Diffstat (limited to 'sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache')
8 files changed, 544 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountNotFoundException.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountNotFoundException.java new file mode 100644 index 0000000000..d7d18181d9 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountNotFoundException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +/** + * @version $Rev$ $Date$ + */ +public class AccountNotFoundException extends Exception { + private static final long serialVersionUID = -4709084750220950706L; + + public AccountNotFoundException() { + super(); + } + + public AccountNotFoundException(String message) { + super(message); + } + +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountService.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountService.java new file mode 100644 index 0000000000..ac986dd417 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountService.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +/** + * @version $Rev$ $Date$ + */ +public interface AccountService { + float getBalance(String accountNumber) throws AccountNotFoundException; + + void deposit(String accountNumber, float amount) throws AccountNotFoundException; + + void withdraw(String accountNumber, float amount) throws OverDraftException, AccountNotFoundException; +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountServiceImpl.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountServiceImpl.java new file mode 100644 index 0000000000..a6ea036da7 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/AccountServiceImpl.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +import java.util.HashMap; +import java.util.Map; + +/** + * @version $Rev$ $Date$ + */ +public abstract class AccountServiceImpl implements AccountService { + protected Map<String, Float> accounts = new HashMap<String, Float>(); + + /** + * @see org.apache.tuscany.sca.itest.transaction.AccountService#deposit(String, float) + */ + public void deposit(String accountNumber, float amount) throws AccountNotFoundException { + float balance = getBalance(accountNumber); + balance += amount; + save(accountNumber, balance); + accounts.put(accountNumber, balance); + } + + /** + * @see org.apache.tuscany.sca.itest.transaction.AccountService#getBalance(String) + */ + public float getBalance(String accountNumber) throws AccountNotFoundException { + Float balance = accounts.get(accountNumber); + if (balance == null) { + balance = load(accountNumber); + accounts.put(accountNumber, balance); + } + return balance; + } + + /** + * @see org.apache.tuscany.sca.itest.transaction.AccountService#withdraw(String, float) + */ + public void withdraw(String accountNumber, float amount) throws OverDraftException, AccountNotFoundException { + float balance = getBalance(accountNumber); + if (amount > balance) { + throw new OverDraftException("Insufficient fund"); + } + balance -= amount; + save(accountNumber, balance); + accounts.put(accountNumber, balance); + } + + protected abstract float load(String accountNumber) throws AccountNotFoundException; + + protected abstract void save(String accountNumber, float balance) throws AccountNotFoundException; + +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/CheckingAccountServiceImpl.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/CheckingAccountServiceImpl.java new file mode 100644 index 0000000000..3e1cb29f33 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/CheckingAccountServiceImpl.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +import java.io.File; +import java.util.Enumeration; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MapMessage; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.QueueBrowser; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.ActiveMQSession; +import org.apache.activemq.broker.BrokerService; +import org.osoa.sca.ServiceRuntimeException; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; +import org.osoa.sca.annotations.Service; + +/** + * @version $Rev$ $Date$ + */ +@Service(AccountService.class) +@Scope("COMPOSITE") +public class CheckingAccountServiceImpl extends AccountServiceImpl { + private static final String url = "tcp://localhost:61616"; + private BrokerService broker; + private Queue queue; + + @Init + public void init() throws Exception { + broker = new BrokerService(); + broker.setBrokerName("localhost"); + broker.setPersistent(false); + broker.addConnector(url); + broker.start(); + + ActiveMQConnectionFactory connFac = new ActiveMQConnectionFactory(url); + Connection conn = connFac.createConnection(); + ActiveMQSession session = (ActiveMQSession)conn.createSession(true, Session.AUTO_ACKNOWLEDGE); + queue = session.createQueue("CheckAccounts"); + MessageProducer producer = session.createProducer(queue); + MapMessage map = session.createMapMessage(); + + for (int i = 0; i < 3; i++) { + String accountNumber = "C00" + (i + 1); + float balance = (float)(1000.0 + Math.random() * 200.0); + map.setStringProperty("accountNumber", accountNumber); + map.setFloatProperty("balance", balance); + + map.setString("accountNumber", accountNumber); + map.setFloat("balance", balance); + + producer.send(map); + } + session.commit(); + conn.close(); + } + + @Destroy + public void destroy() throws Exception { + if (broker != null) { + broker.stop(); + } + } + + @Override + protected float load(String accountNumber) throws AccountNotFoundException { + try { + ActiveMQConnectionFactory connFac = new ActiveMQConnectionFactory(url); + Connection conn = connFac.createConnection(); + conn.start(); + Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE); + QueueBrowser browser = session.createBrowser(queue, "accountNumber = '" + accountNumber + "'"); + Enumeration msgs = browser.getEnumeration(); + if (msgs.hasMoreElements()) { + MapMessage msg = (MapMessage)msgs.nextElement(); + float balance = msg.getFloat("balance"); + conn.close(); + return balance; + } else { + conn.close(); + throw new AccountNotFoundException(accountNumber); + } + } catch (JMSException e) { + throw new ServiceRuntimeException(e); + } + } + + @Override + protected void save(String accountNumber, float balance) throws AccountNotFoundException { + try { + ActiveMQConnectionFactory connFac = new ActiveMQConnectionFactory(url); + Connection conn = connFac.createConnection(); + conn.start(); + ActiveMQSession session = (ActiveMQSession)conn.createSession(true, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consumer = session.createConsumer(queue, "accountNumber = '" + accountNumber + "'"); + Message msg = consumer.receive(1000); + if (msg == null) { + conn.close(); + throw new AccountNotFoundException(accountNumber); + } + MapMessage map = session.createMapMessage(); + map.setStringProperty("accountNumber", accountNumber); + map.setFloatProperty("balance", balance); + + map.setString("accountNumber", accountNumber); + map.setFloat("balance", balance); + + MessageProducer producer = session.createProducer(queue); + producer.send(map); + conn.close(); + + } catch (JMSException e) { + throw new ServiceRuntimeException(e); + } + } + +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/OverDraftException.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/OverDraftException.java new file mode 100644 index 0000000000..4d504a6a36 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/OverDraftException.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +/** + * @version $Rev$ $Date$ + */ +public class OverDraftException extends Exception { + private static final long serialVersionUID = -4709084750220950706L; + + public OverDraftException() { + super(); + } + + public OverDraftException(String message) { + super(message); + } + +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/SavingsAccountServiceImpl.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/SavingsAccountServiceImpl.java new file mode 100644 index 0000000000..156b34227d --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/SavingsAccountServiceImpl.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.logging.Logger; + +import javax.sql.XAConnection; + +import org.apache.derby.jdbc.EmbeddedXADataSource; +import org.osoa.sca.ServiceRuntimeException; +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.Init; +import org.osoa.sca.annotations.Scope; +import org.osoa.sca.annotations.Service; + +/** + * @version $Rev$ $Date$ + */ +@Service(AccountService.class) +@Scope("COMPOSITE") +public class SavingsAccountServiceImpl extends AccountServiceImpl { + private final static Logger log = Logger.getLogger(SavingsAccountServiceImpl.class.getName()); + private EmbeddedXADataSource xads; + + @Init + public void init() throws SQLException { + // Create the database and a table + xads = new EmbeddedXADataSource(); + xads.setDatabaseName("target/test"); + xads.setCreateDatabase("create"); + + XAConnection xaconn = xads.getXAConnection(); + Connection conn = xaconn.getConnection(); + PreparedStatement ps = + conn.prepareStatement("create table SavingsAccounts(accountNumber char(100), balance float)"); + try { + ps.execute(); + } catch (SQLException ex) { + log.info(ex.getMessage()); + } + ps = conn.prepareStatement("delete from SavingsAccounts"); + ps.execute(); + + ps = conn.prepareStatement("insert into SavingsAccounts(accountNumber, balance) values(?, ?)"); + for (int i = 0; i < 2; i++) { + ps.setString(1, "S00" + (i+1)); + ps.setFloat(2, (float)(1000.0f + Math.random() * 500.0)); + ps.executeUpdate(); + } + conn.commit(); + conn.close(); + } + + @Override + protected float load(String accountNumber) throws AccountNotFoundException { + try { + XAConnection xaconn = xads.getXAConnection(); + + Connection conn = xaconn.getConnection(); + PreparedStatement ps = conn.prepareStatement("select balance from SavingsAccounts where accountNumber=?"); + ps.setString(1, accountNumber); + ResultSet rs1 = ps.executeQuery(); + boolean found = rs1.next(); + if (found) { + float balance = rs1.getFloat(1); + conn.commit(); + conn.close(); + return balance; + } else { + conn.commit(); + conn.close(); + throw new AccountNotFoundException(accountNumber); + } + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + @Override + protected void save(String accountNumber, float balance) throws AccountNotFoundException { + try { + XAConnection xaconn = xads.getXAConnection(); + + Connection conn = xaconn.getConnection(); + PreparedStatement ps = conn.prepareStatement("update SavingsAccounts set balance=? where accountNumber=?"); + ps.setFloat(1, balance); + ps.setString(2, accountNumber); + int rows = ps.executeUpdate(); + conn.commit(); + boolean found = (rows >= 1); + if (found) { + conn.close(); + } else { + conn.close(); + throw new AccountNotFoundException(accountNumber); + } + } catch (SQLException e) { + throw new ServiceRuntimeException(e); + } + } + + @Destroy + public void destroy() throws SQLException { + XAConnection xaconn = xads.getXAConnection(); + Connection conn = xaconn.getConnection(); + PreparedStatement ps = conn.prepareStatement("drop table SavingsAccounts"); + ps.execute(); + conn.commit(); + conn.close(); + } + +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferService.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferService.java new file mode 100644 index 0000000000..b7ea5f2663 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferService.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +/** + * @version $Rev$ $Date$ + */ +public interface TransferService { + String[] getAccounts(); + + float getBalance(String accountNumber) throws AccountNotFoundException; + + void transfer(String from, String to, float amount) throws OverDraftException, AccountNotFoundException; +} diff --git a/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferServiceImpl.java b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferServiceImpl.java new file mode 100644 index 0000000000..341a7e53e3 --- /dev/null +++ b/sca-java-1.x/tags/1.3.2/itest/transaction/src/main/java/org/apache/tuscany/sca/itest/transaction/TransferServiceImpl.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.itest.transaction; + +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Service; + +/** + * @version $Rev$ $Date$ + */ +@Service(TransferService.class) +public class TransferServiceImpl implements TransferService { + @Reference + protected AccountService savings; + + @Reference + protected AccountService checking; + + /** + * @see org.apache.tuscany.sca.itest.transaction.TransferService#transfer(java.lang.String, java.lang.String, float) + */ + public void transfer(String from, String to, float amount) throws OverDraftException, AccountNotFoundException { + if (from.startsWith("C")) { + checking.withdraw(from, amount); + } else { + savings.withdraw(from, amount); + } + if (to.startsWith("C")) { + checking.deposit(to, amount); + } else { + savings.deposit(to, amount); + } + } + + public float getBalance(String accountNumber) throws AccountNotFoundException { + if(accountNumber.startsWith("C")) { + return checking.getBalance(accountNumber); + } else { + return savings.getBalance(accountNumber); + } + } + + public String[] getAccounts() { + return new String[] {"S001", "S002", "C001"}; + } + +} |