View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.wsat.simple;
18  
19  import java.util.concurrent.atomic.AtomicInteger;
20  
21  /**
22   * This class represents the back-end resource for managing Restaurant bookings.
23   * 
24   * This is a mock implementation that just keeps a counter of how many bookings have been made.
25   * 
26   * @author paul.robinson@redhat.com, 2012-01-04
27   */
28  public class MockRestaurantManager {
29  
30      // The singleton instance of this class.
31      private static MockRestaurantManager singletonInstance;
32  
33      // A thread safe booking counter
34      private AtomicInteger bookings = new AtomicInteger(0);
35  
36      /**
37       * Accessor to obtain the singleton restaurant manager instance.
38       * 
39       * @return the singleton RestaurantManager instance.
40       */
41      public synchronized static MockRestaurantManager getSingletonInstance() {
42          if (singletonInstance == null) {
43              singletonInstance = new MockRestaurantManager();
44          }
45  
46          return singletonInstance;
47      }
48  
49      /**
50       * Make a booking with the restaurant.
51       * 
52       * @param txID The transaction identifier
53       */
54      public synchronized void makeBooking(Object txID) {
55          System.out.println("[SERVICE] makeBooking called on backend resource.");
56      }
57  
58      /**
59       * Prepare local state changes for the supplied transaction. This method should persist any required information to ensure
60       * that it can undo (rollback) or make permanent (commit) the work done inside this transaction, when told to do so.
61       * 
62       * @param txID The transaction identifier
63       * @return true on success, false otherwise
64       */
65      public boolean prepare(Object txID) {
66          System.out.println("[SERVICE] prepare called on backend resource.");
67          return true;
68      }
69  
70      /**
71       * commit local state changes for the supplied transaction
72       * 
73       * @param txID The transaction identifier
74       */
75      public void commit(Object txID) {
76          System.out.println("[SERVICE] commit called on backend resource.");
77          bookings.getAndIncrement();
78      }
79  
80      /**
81       * roll back local state changes for the supplied transaction
82       * 
83       * @param txID The transaction identifier
84       */
85      public void rollback(Object txID) {
86          System.out.println("[SERVICE] rollback called on backend resource.");
87      }
88  
89      /**
90       * Returns the number of bookings
91       * 
92       * @return the number of bookings.
93       */
94      public int getBookingCount() {
95          return bookings.get();
96      }
97  
98      /**
99       * Reset the booking counter to zero
100      */
101     public void reset() {
102         bookings.set(0);
103     }
104 }