summaryrefslogtreecommitdiffstats
path: root/sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java')
-rw-r--r--sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java299
1 files changed, 299 insertions, 0 deletions
diff --git a/sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java b/sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java
new file mode 100644
index 0000000000..e82b665b28
--- /dev/null
+++ b/sandbox/dougsleite/travelagency/src/main/java/client/ClientImpl.java
@@ -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!");
+ }
+ }
+}