git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@743527 13f79535-47bb-0310-9956-ffa450edef68

This commit is contained in:
dougsleite 2009-02-11 22:01:31 +00:00
parent ea3155b9b5
commit 79a80b2832
23 changed files with 1732 additions and 0 deletions

View file

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca</artifactId>
<version>1.4.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>sample-travel-agency</artifactId>
<name>Travel Agency Sample</name>
<repositories>
<repository>
<id>apache.incubator</id>
<url>http://people.apache.org/repo/m2-incubating-repository</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-node-impl</artifactId>
<version>1.4.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-embedded</artifactId>
<version>1.4.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-java-runtime</artifactId>
<version>1.4.1-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-ws-axis2</artifactId>
<version>1.4.1-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-tomcat</artifactId>
<version>1.4.1-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
</build>
</project>

View file

@ -0,0 +1,98 @@
/*
* 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 client;
import common.CreditCard;
public interface Client {
public void bookRequest(FlightData flightData, HotelData hotelData);
public class FlightData {
private int flightNumber;
private int ticketNumber;
private CreditCard creditCard;
public FlightData(int flightNumber, CreditCard creditCard) {
this.flightNumber = flightNumber;
this.creditCard = creditCard;
}
public int getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(int flightNumber) {
this.flightNumber = flightNumber;
}
public int getTicketNumber() {
return ticketNumber;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}
}
public class HotelData {
private int roomID;
private CreditCard creditCard;
private int confirmationID;
public HotelData(int roomID, CreditCard creditCard) {
this.roomID = roomID;
this.creditCard = creditCard;
}
public int getRoomID() {
return roomID;
}
public void setRoomID(int roomID) {
this.roomID = roomID;
}
public CreditCard getCreditCard() {
return creditCard;
}
public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}
public int getConfirmationID() {
return confirmationID;
}
public void setConfirmationID(int confirmationID) {
this.confirmationID = confirmationID;
}
}
}

View file

@ -0,0 +1,299 @@
/*
* 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 client;
import flight.FlightService;
import hotel.HotelService;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
@Service(Client.class)
@Scope("COMPOSITE")
public class ClientImpl implements Client, FlightServiceCallback, HotelServiceCallback {
private FlightService flightService;
private FlightThread flightThread;
private HotelService hotelService;
private HotelThread hotelThread;
private boolean flightThreadIsAlive;
private boolean hotelThreadIsAlive;
public ClientImpl() {
flightThreadIsAlive = false;
hotelThreadIsAlive = false;
}
@Reference(name = "flightService", required = true)
public void setFlightService(FlightService flightService) {
this.flightService = flightService;
}
@Reference(name = "hotelService", required = true)
public void setHotelService(HotelService hotelService) {
this.hotelService = hotelService;
}
public void bookRequest(FlightData flightData, HotelData hotelData) {
//Flight thread
this.flightThread = new FlightThread(flightData, flightService);
Thread fThread = new Thread(flightThread);
fThread.start();
//Hotel thread
this.hotelThread = new HotelThread(hotelData, hotelService);
Thread hThread = new Thread(hotelThread);
hThread.start();
//Wait until both threads has died
while (flightThreadIsAlive || hotelThreadIsAlive) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClientImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void confirmFlightBook(int ticketNumber) {
flightThread.confirmFlightBook(ticketNumber);
}
public void confirmFlightPayment(int ticketNumber) {
flightThread.confirmFlightPayment(ticketNumber);
}
public void throwsFlightException(Exception ex) {
flightThread.throwsFlightException(ex);
}
public void confirmHotelBook(int confirmationID) {
hotelThread.confirmHotelBook(confirmationID);
}
public void confirmHotelPayment(int confirmationID) {
hotelThread.confirmHotelPayment(confirmationID);
}
public void throwsHotelException(Exception ex) {
hotelThread.throwsHotelException(ex);
}
private class FlightThread implements Runnable {
//Flight variables
private FlightService flightService;
private boolean flightBookConfirmation;
private boolean flightPaymentConfirmation;
private boolean flightExOccurrence;
private Client.FlightData fData;
private Exception flightException;
public FlightThread(FlightData flightData, FlightService flightService) {
//Initializing flight variables
flightBookConfirmation = false;
flightPaymentConfirmation = false;
flightExOccurrence = false;
flightException = null;
this.flightService = flightService;
this.fData = flightData;
}
public void run() {
flightThreadIsAlive = true;
try {
//Book request
flightService.book(fData.getFlightNumber());
waitFlightBookConfirmation();
try {
//Payment request
flightService.pay(fData.getFlightNumber(), fData.getTicketNumber(), fData.getCreditCard());
waitFlightPaymentConfirmation();
} catch (Exception ex) {
System.out.println("Exception on flight payment confirmation: " + ex.getMessage());
}
} catch (Exception ex) {
System.out.println("Exception on flight book confirmation: " + ex.getMessage());
}
flightThreadIsAlive = false;
}
public void confirmFlightBook(int ticketNumber) {
fData.setTicketNumber(ticketNumber);
flightBookConfirmation = true;
}
public void confirmFlightPayment(int ticketNumber) {
flightPaymentConfirmation = true;
}
public void throwsFlightException(Exception ex) {
flightException = ex;
flightExOccurrence = true;
}
private void waitFlightBookConfirmation() throws Exception {
System.out.println("Waiting for flight book confirmation...");
while (!flightBookConfirmation && !flightExOccurrence) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClientImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (flightException != null) {
System.out.println("Flight exception occurrence");
throw flightException;
}
System.out.println("Flight book confirmation received!");
}
private void waitFlightPaymentConfirmation() throws Exception {
System.out.println("Waiting for flight payment confirmation...");
while (!flightPaymentConfirmation && !flightExOccurrence) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClientImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (flightException != null) {
System.out.println("Flight exception occurrence");
throw flightException;
}
System.out.println("Flight payment confirmation received!");
}
}
private class HotelThread implements Runnable {
//Hotel variables
private HotelService hotelService;
private boolean hotelBookConfirmation;
private boolean hotelPaymentConfirmation;
private boolean hotelExOccurrence;
private Client.HotelData hData;
private Exception hotelException;
public HotelThread(HotelData hData, HotelService hotelService) {
//Initializing hotel variables
hotelBookConfirmation = false;
hotelPaymentConfirmation = false;
hotelExOccurrence = false;
hotelException = null;
this.hotelService = hotelService;
this.hData = hData;
}
public void run() {
hotelThreadIsAlive = true;
try {
hotelService.book(hData.getRoomID());
waitHotelBookConfirmation();
try {
//Payment request
hotelService.pay(hData.getRoomID(), hData.getConfirmationID(), hData.getCreditCard());
waitHotelPaymentConfirmation();
} catch (Exception ex) {
System.out.println("Exception on hotel payment confirmation: " + ex.getMessage());
}
} catch (Exception ex) {
System.out.println("Exception on hotel book confirmation: " + ex.getMessage());
}
hotelThreadIsAlive = false;
}
public void confirmHotelBook(int confirmationID) {
hData.setConfirmationID(confirmationID);
hotelBookConfirmation = true;
}
public void confirmHotelPayment(int confirmationID) {
hotelPaymentConfirmation = true;
}
public void throwsHotelException(Exception ex) {
hotelException = ex;
hotelExOccurrence = true;
}
private void waitHotelBookConfirmation() throws Exception {
System.out.println("Waiting for hotel book confirmation...");
while (!hotelBookConfirmation && !hotelExOccurrence) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClientImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (hotelException != null) {
System.out.println("Hotel exception occurrence");
throw hotelException;
}
System.out.println("Hotel book confirmation received!");
}
private void waitHotelPaymentConfirmation() throws Exception {
System.out.println("Waiting for hotel payment confirmation...");
while (!hotelPaymentConfirmation && !hotelExOccurrence) {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(ClientImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (hotelException != null) {
System.out.println("Hotel exception occurrence");
throw hotelException;
}
System.out.println("Hotel payment confirmation received!");
}
}
}

View file

@ -0,0 +1,33 @@
/*
* 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 client;
import org.osoa.sca.annotations.OneWay;
public interface FlightServiceCallback {
@OneWay
public void confirmFlightBook(int ticketNumber);
@OneWay
public void confirmFlightPayment(int ticketNumber);
@OneWay
public void throwsFlightException(Exception ex);
}

View file

@ -0,0 +1,33 @@
/*
* 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 client;
import org.osoa.sca.annotations.OneWay;
public interface HotelServiceCallback {
@OneWay
public void confirmHotelBook(int confirmationID);
@OneWay
public void confirmHotelPayment(int confirmationID);
@OneWay
public void throwsHotelException(Exception ex);
}

View file

@ -0,0 +1,27 @@
/*
* 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 common;
public interface Constants {
public static String NORMAL = "NORMAL";
public static String EXCEPTIONAL_BOOK = "EXCEPTIONAL_BOOK";
public static String EXCEPTIONAL_PAYMENT = "EXCEPTIONAL_PAYMENT";
}

View file

@ -0,0 +1,72 @@
/*
* 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 common;
import java.util.GregorianCalendar;
public class CreditCard {
private String number;
private int verificationNumber;
private String name;
private GregorianCalendar goodThru;
public CreditCard(String number, int verificationNumber, String name, GregorianCalendar goodThru) {
this.number = number;
this.verificationNumber = verificationNumber;
this.name = name;
this.goodThru = goodThru;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getVerificationNumber() {
return verificationNumber;
}
public void setVerificationNumber(int verificationNumber) {
this.verificationNumber = verificationNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GregorianCalendar getGoodThru() {
return goodThru;
}
public void setGoodThru(GregorianCalendar goodThru) {
this.goodThru = goodThru;
}
}

View file

@ -0,0 +1,29 @@
/*
* 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 common;
public class InvalidCreditCardException extends RuntimeException {
public InvalidCreditCardException() {
super();
}
public InvalidCreditCardException(String message) {
super(message);
}
}

View file

@ -0,0 +1,120 @@
/*
* 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 flight;
import java.util.GregorianCalendar;
public class Flight {
private int id;
private String from;
private String to;
private GregorianCalendar date;
private int seatNumber;
private FlightTicket[] tickets;
public Flight(int flightID, String from, String to, GregorianCalendar date, int seatNumber) {
this.from = from;
this.to = to;
this.date = date;
this.id = flightID;
this.seatNumber = seatNumber;
tickets = new FlightTicket[seatNumber];
}
public int addTicket() {
for (int i = 0; i <= tickets.length; i++) {
if (tickets[i] == null) {
FlightTicket ticket = new FlightTicket(i + 1);
tickets[i] = ticket;
return ticket.getID();
}
}
return -1;
}
public boolean hasAvailableSeat() {
for (int i = 0; i <= tickets.length; i++) {
if (tickets[i] == null) {
return true;
}
}
return false;
}
public boolean removeTicket(int id) {
for (int i = 0; i <= tickets.length; i++) {
if (tickets[i].getID() == id) {
tickets[i] = null;
return true;
}
}
return false;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public GregorianCalendar getDate() {
return date;
}
public void setDate(GregorianCalendar date) {
this.date = date;
}
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
return obj instanceof FlightService &&
super.equals(obj) &&
this.from.toLowerCase().equals(((Flight) obj).getFrom().toLowerCase()) &&
this.to.toLowerCase().equals(((Flight) obj).getTo().toLowerCase()) &&
this.date.equals(((Flight) obj).getDate());
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + (this.from != null ? this.from.hashCode() : 0);
hash = 71 * hash + (this.to != null ? this.to.hashCode() : 0);
hash = 71 * hash + (this.date != null ? this.date.hashCode() : 0);
return hash;
}
}

View file

@ -0,0 +1,69 @@
/*
* 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 flight;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
public class FlightBD {
private Map<Integer, Flight> flightList;
private static FlightBD instance = null;
protected FlightBD() {
Flight flight1 = new Flight(0000,"Sao Paulo", "Campinas",
new GregorianCalendar(2009, 02, 12, 21, 15), 100);
Flight flight2 = new Flight(0001,"Campinas", "Rio de Janeiro",
new GregorianCalendar(2009, 03, 22, 19, 00), 150);
Flight flight3 = new Flight(0002,"Belo Horizonte", "Sao Paulo",
new GregorianCalendar(2009, 03, 25, 20, 00), 120);
Flight flight4 = new Flight(0003,"Curitiba", "Porto Alegre",
new GregorianCalendar(2009, 04, 2, 10, 00), 120);
Flight flight5 = new Flight(0004,"Rio de Janeiro", "Brasilia",
new GregorianCalendar(2009, 4, 17, 12, 00), 100);
flightList = new HashMap<Integer, Flight>();
flightList.put(flight1.getID(), flight1);
flightList.put(flight2.getID(), flight1);
flightList.put(flight3.getID(), flight3);
flightList.put(flight4.getID(), flight4);
flightList.put(flight5.getID(), flight5);
}
public static FlightBD getInstance() {
if (instance == null) {
instance = new FlightBD();
}
return instance;
}
public Flight get(int id) {
return flightList.get(id);
}
public Flight put(int id, Flight ticket) {
return flightList.put(id, ticket);
}
public Flight delete(int id) {
return flightList.remove(id);
}
}

View file

@ -0,0 +1,35 @@
/*
* 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 flight;
import org.osoa.sca.annotations.Callback;
import client.FlightServiceCallback;
import common.CreditCard;
@Callback(FlightServiceCallback.class)
public interface FlightService {
//@OneWay
//throws UnavailableFlightException
public void book(int flightNumber);
//@OneWay
//throws InvalidCreditCardException
public void pay(int flightNumber, int ticketNumber, CreditCard creditCard);
}

View file

@ -0,0 +1,166 @@
/*
* 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 flight;
import client.FlightServiceCallback;
import common.Constants;
import common.CreditCard;
import common.InvalidCreditCardException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osoa.sca.annotations.Callback;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
@Service(FlightService.class)
@Scope("COMPOSITE")
public class FlightServiceImpl implements FlightService {
@Callback
protected FlightServiceCallback callback;
private FlightBD bd;
private int sleepBookTime;
private int sleepPaymentTime;
private String behavior;
public FlightServiceImpl() {
bd = FlightBD.getInstance();
sleepBookTime = 0;
sleepPaymentTime = 0;
behavior = Constants.NORMAL;
}
public void book(int flightNumber) {
new Thread(new BookThread(flightNumber)).start();
}
public void pay(int flightNumber, int ticketNumber, CreditCard creditCard) {
new Thread(new PayThread(flightNumber, ticketNumber, creditCard)).start();
}
public String getBehavior() {
return behavior;
}
@Property(name = "behavior", required = false)
public void setBehavior(String behavior) {
this.behavior = behavior.toUpperCase();
}
public int getSleepBookTime() {
return sleepBookTime;
}
@Property(name = "sleepBookTime", required = false)
public void setSleepBookTime(int sleepBookTime) {
this.sleepBookTime = sleepBookTime;
}
public int getSleepPaymentTime() {
return sleepPaymentTime;
}
@Property(name = "sleepPaymentTime", required = false)
public void setSleepPaymentTime(int sleepPaymentTime) {
this.sleepPaymentTime = sleepPaymentTime;
}
private class BookThread implements Runnable {
private int flightNumber;
BookThread(int flightNumber) {
this.flightNumber = flightNumber;
}
public void run() {
//Extra time to book
try {
Thread.sleep(sleepBookTime);
} catch (InterruptedException ex) {
Logger.getLogger(FlightServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
//NORMAL behavior
if (behavior.equals(Constants.NORMAL) || behavior.equals(Constants.EXCEPTIONAL_PAYMENT)) {
Flight flight = bd.get(flightNumber);
int ticketNumber = flight.addTicket();
if (ticketNumber == -1) {
callback.throwsFlightException(new UnavailableFlightException());
return;
}
callback.confirmFlightBook(ticketNumber);
}
//EXCEPTIONAL_BOOK behavior
else if (behavior.equals(Constants.EXCEPTIONAL_BOOK)) {
callback.throwsFlightException(new UnavailableFlightException());
}
}
}
private class PayThread implements Runnable {
private int flightNumber;
private int ticketNumber;
private CreditCard creditCard;
public PayThread(int flightNumber, int ticketNumber, CreditCard creditCard) {
this.flightNumber = flightNumber;
this.ticketNumber = ticketNumber;
this.creditCard = creditCard;
}
public void run() {
//Extra time to pay
try {
Thread.sleep(sleepPaymentTime);
} catch (InterruptedException ex) {
Logger.getLogger(FlightServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
//NORMAL behavior
if (behavior.equals(Constants.NORMAL)) {
if (!verifyCreditCard(creditCard)) {
Flight flight = bd.get(flightNumber);
flight.removeTicket(ticketNumber);
callback.throwsFlightException(new InvalidCreditCardException());
return;
}
callback.confirmFlightPayment(ticketNumber);
}
//EXCEPTIONAL_PAYMENT behavior
else if(behavior.equals(Constants.EXCEPTIONAL_PAYMENT)) {
callback.throwsFlightException(new InvalidCreditCardException());
}
}
private boolean verifyCreditCard(CreditCard creditCard) {
// Random r = new Random(creditCard.getVerificationNumber());
// return r.nextBoolean();
return true;
}
}
}

View file

@ -0,0 +1,32 @@
/*
* 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 flight;
class FlightTicket {
private int id;
public FlightTicket(int id) {
this.id = id;
}
public int getID(){
return id;
}
}

View file

@ -0,0 +1,29 @@
/*
* 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 flight;
public class UnavailableFlightException extends RuntimeException {
public UnavailableFlightException() {
super();
}
public UnavailableFlightException(String message) {
super(message);
}
}

View file

@ -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 hotel;
import java.util.HashMap;
import java.util.Map;
public class HotelBD {
private Map<Integer, HotelRoom> hotelList;
private static HotelBD instance = null;
protected HotelBD() {
HotelRoom room1 = new HotelRoom(1, 2, true, true, true, HotelRoom.FLOOR_MARBLE);
HotelRoom room2 = new HotelRoom(2, 2, true, true, true, HotelRoom.FLOOR_MARBLE);
HotelRoom room3 = new HotelRoom(3, 1, false, false, true, HotelRoom.FLOOR_CARPET);
HotelRoom room4 = new HotelRoom(4, 1, false, true, true, HotelRoom.FLOOR_CARPET);
HotelRoom room5 = new HotelRoom(5, 2, false, true, true, HotelRoom.FLOOR_MARBLE);
hotelList = new HashMap<Integer, HotelRoom>();
hotelList.put(room1.getID(), room1);
hotelList.put(room2.getID(), room2);
hotelList.put(room3.getID(), room3);
hotelList.put(room4.getID(), room4);
hotelList.put(room5.getID(), room5);
}
public static HotelBD getInstance() {
if (instance == null) {
instance = new HotelBD();
}
return instance;
}
public HotelRoom get(int id) {
return hotelList.get(id);
}
public HotelRoom put(int id, HotelRoom ticket) {
return hotelList.put(id, ticket);
}
public HotelRoom delete(int id) {
return hotelList.remove(id);
}
}

View file

@ -0,0 +1,131 @@
/*
* 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 hotel;
public class HotelRoom {
public static int FLOOR_MARBLE = 0;
public static int FLOOR_CARPET = 1;
private int bedroomNumber;
private boolean hasBath;
private boolean hasAirConditioning;
private boolean hasCloset;
private int floorType;
private int id;
private boolean isBooked;
private int alredyBookedNumber;
public HotelRoom(int id, int bedroomNumber, boolean hasBath,
boolean hasAirConditioning, boolean hasCloset, int floorType) {
this.bedroomNumber = bedroomNumber;
this.hasBath = hasBath;
this.hasAirConditioning = hasAirConditioning;
this.hasCloset = hasCloset;
this.floorType = floorType;
this.alredyBookedNumber = 0;
this.id = id;
this.isBooked = false;
}
public int getAlreadyBookedNumber() {
return alredyBookedNumber;
}
public int book() {
if (isBooked == false) {
isBooked = true;
return ++alredyBookedNumber;
}
return -1;
}
public boolean unbook() {
if (isBooked == true) {
isBooked = false;
return true;
}
return false;
}
public boolean isBooked() {
return isBooked;
}
public int getBedroomNumber() {
return bedroomNumber;
}
public void setBedroomNumber(int bedroomNumber) {
this.bedroomNumber = bedroomNumber;
}
public boolean hasBath() {
return this.hasBath;
}
public void setHasBath(boolean hasBath) {
this.hasBath = hasBath;
}
public boolean hasAirConditioning() {
return this.hasAirConditioning;
}
public void setHasAirConditioning(boolean hasAirConditioning) {
this.hasAirConditioning = hasAirConditioning;
}
public boolean hasClose() {
return this.hasCloset;
}
public void setHasCloset(boolean hasCloset) {
this.hasCloset = hasCloset;
}
public int getFloorType() {
return floorType;
}
public void setFloorType(int floorType) {
this.floorType = floorType;
}
public int getID() {
return id;
}
@Override
public boolean equals(Object obj) {
return obj instanceof HotelRoom &&
super.equals(obj) &&
this.getID() == ((HotelRoom) obj).getID();
}
@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + this.bedroomNumber;
hash = 37 * hash + (this.hasBath ? 1 : 0);
hash = 37 * hash + (this.hasAirConditioning ? 1 : 0);
hash = 37 * hash + (this.hasCloset ? 1 : 0);
hash = 37 * hash + this.floorType;
return hash;
}
}

View file

@ -0,0 +1,35 @@
/*
* 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 hotel;
import client.HotelServiceCallback;
import common.CreditCard;
import org.osoa.sca.annotations.Callback;
@Callback(HotelServiceCallback.class)
public interface HotelService {
//@OneWay
//throws UnavailableHotelException
public void book(int roomID);
//@OneWay
//throws UnavailableHotelException
public void pay(int roomID, int confirmationID, CreditCard creditCard);
}

View file

@ -0,0 +1,175 @@
/*
* 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 hotel;
import client.HotelServiceCallback;
import common.Constants;
import common.CreditCard;
import common.InvalidCreditCardException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osoa.sca.annotations.Callback;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
@Service(HotelService.class)
@Scope("COMPOSITE")
public class HotelServiceImpl implements HotelService {
@Callback
protected HotelServiceCallback callback;
private HotelBD bd;
private int sleepBookTime;
private int sleepPaymentTime;
private String behavior;
public HotelServiceImpl() {
bd = HotelBD.getInstance();
sleepBookTime = 0;
sleepPaymentTime = 0;
behavior = Constants.NORMAL;
}
public void book(int roomID) {
new Thread(new BookThread(roomID)).start();
}
public void pay(int roomID, int confirmationID, CreditCard creditCard) {
new Thread(new PayThread(roomID, confirmationID, creditCard)).start();
}
public String getBehavior() {
return behavior;
}
@Property(name = "behavior", required = false)
public void setBehavior(String behavior) {
this.behavior = behavior.toUpperCase();
}
public int getSleepBookTime() {
return sleepBookTime;
}
@Property(name = "sleepBookTime", required = false)
public void setSleepBookTime(int sleepBookTime) {
this.sleepBookTime = sleepBookTime;
}
public int getSleepPaymentTime() {
return sleepPaymentTime;
}
@Property(name = "sleepPaymentTime", required = false)
public void setSleepPaymentTime(int sleepPaymentTime) {
this.sleepPaymentTime = sleepPaymentTime;
}
private class BookThread implements Runnable {
private int roomID;
public BookThread(int roomID) {
this.roomID = roomID;
}
public void run() {
//Extra time to book
try {
Thread.sleep(sleepBookTime);
} catch (InterruptedException ex) {
Logger.getLogger(HotelServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
//NORMAL behavior
if (behavior.equals(Constants.NORMAL) || behavior.equals(Constants.EXCEPTIONAL_PAYMENT)) {
HotelRoom room = bd.get(roomID);
int confirmationID = room.book();
if (confirmationID != -1) {
callback.confirmHotelBook(confirmationID);
return;
}
callback.throwsHotelException(new UnavailableHotelException(
"Room " + roomID + " is already booked!"));
} //EXCEPTIONAL_BOOK behavior
else if (behavior.equals(Constants.EXCEPTIONAL_BOOK)) {
callback.throwsHotelException(new UnavailableHotelException(
"Room " + roomID + " is already booked!"));
}
}
}
private class PayThread implements Runnable {
private int roomID;
private int confirmationID;
private CreditCard creditCard;
public PayThread(int roomID, int confirmationID, CreditCard creditCard) {
this.roomID = roomID;
this.confirmationID = confirmationID;
this.creditCard = creditCard;
}
public void run() {
//Extra time to pay
try {
Thread.sleep(sleepPaymentTime);
} catch (InterruptedException ex) {
Logger.getLogger(HotelServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
//NORMAL behavior
if (behavior.equals(Constants.NORMAL)) {
HotelRoom room = bd.get(roomID);
//Testing the confirmation ID
if (room.getAlreadyBookedNumber() != confirmationID) {
room.unbook();
callback.throwsHotelException(new InvalidConfirmationIDException());
return;
}
//Testing the credit card
if (!verifyCreditCard(creditCard)) {
room.unbook();
callback.throwsHotelException(new InvalidCreditCardException());
return;
}
callback.confirmHotelPayment(confirmationID);
} //EXCEPTIONAL_PAYMENT behavior
else if (behavior.equals(Constants.EXCEPTIONAL_PAYMENT)) {
callback.throwsHotelException(new InvalidCreditCardException());
}
}
private boolean verifyCreditCard(CreditCard creditCard) {
// Random r = new Random(creditCard.getVerificationNumber());
// return r.nextBoolean();
return true;
}
}
}

View file

@ -0,0 +1,30 @@
/*
* 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 hotel;
public class InvalidConfirmationIDException extends RuntimeException {
public InvalidConfirmationIDException() {
super();
}
public InvalidConfirmationIDException(String message) {
super(message);
}
}

View file

@ -0,0 +1,29 @@
/*
* 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 hotel;
public class UnavailableHotelException extends RuntimeException {
public UnavailableHotelException() {
super();
}
public UnavailableHotelException(String message) {
super(message);
}
}

View file

@ -0,0 +1,43 @@
/*
* 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 launch;
import client.Client;
import common.CreditCard;
import java.util.GregorianCalendar;
import org.apache.tuscany.sca.host.embedded.SCADomain;
public class Launch {
public static void main(String... args) {
SCADomain scaDomain = SCADomain.newInstance("travelagency.composite");
Client client = scaDomain.getService(Client.class, "TravelAgencyComponent");
CreditCard card1 = new CreditCard("2765437907051286", 187, "John Smith",
new GregorianCalendar(2010, 02, 01));
Client.FlightData fData = new Client.FlightData(0000, card1);
Client.HotelData hData = new Client.HotelData(1, card1);
client.bookRequest(fData, hData);
scaDomain.close();
}
}

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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 (thea
* "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.
-->
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0"
targetNamespace="http://travelagency"
name="travelagency">
<component name="TravelAgencyComponent">
<implementation.java class="client.ClientImpl"/>
<reference name="flightService" target="FlightServiceComponent"/>
<reference name="hotelService" target="HotelServiceComponent"/>
</component>
<component name="FlightServiceComponent">
<implementation.java class="flight.FlightServiceImpl"/>
<property name="sleepBookTime">0</property>
<property name="sleepPaymentTime">0</property>
<property name="behavior">normal</property>
</component>
<component name="HotelServiceComponent">
<implementation.java class="hotel.HotelServiceImpl"/>
<property name="sleepBookTime">0</property>
<property name="sleepPaymentTime">0</property>
<property name="behavior">normal</property>
</component>
</composite>

View file

@ -0,0 +1,53 @@
/*
* 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 test;
import client.Client;
import common.CreditCard;
import java.util.GregorianCalendar;
import junit.framework.TestCase;
import org.apache.tuscany.sca.host.embedded.SCADomain;
public class TravelAgencyTestCase extends TestCase {
private Client client;
private SCADomain domain;
protected void setUp() throws Exception {
domain = SCADomain.newInstance("travelagency.composite");
client = domain.getService(Client.class, "TravelAgencyComponent");
}
protected void tearDown() throws Exception {
domain.close();
}
public void test() throws Exception {
CreditCard card1 = new CreditCard("2765437907051286", 187, "John Smith",
new GregorianCalendar(2010, 02, 01));
Client.FlightData fData = new Client.FlightData(0000, card1);
Client.HotelData hData = new Client.HotelData(1, card1);
client.bookRequest(fData, hData);
}
}