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 com.arjuna.wst.*;
20  
21  import java.io.Serializable;
22  
23  /**
24   * An adapter class that exposes the RestaurantManager as a WS-T Atomic Transaction participant.
25   * 
26   * @author paul.robinson@redhat.com, 2012-01-04
27   */
28  public class RestaurantParticipant implements Durable2PCParticipant, Serializable {
29      private static final long serialVersionUID = 1L;
30  
31      // The back-end resource for managing bookings
32      private MockRestaurantManager mockRestaurantManager = MockRestaurantManager.getSingletonInstance();
33  
34      // The transaction ID for this transaction
35      private String txID;
36  
37      /**
38       * Creates a new participant for this transaction. Participants and transaction instances have a one-to-one mapping.
39       * 
40       * @param txID the ID of the transaction tht this participant will be enlisted within.
41       */
42      public RestaurantParticipant(String txID) {
43          this.txID = txID;
44      }
45  
46      /**
47       * Invokes the prepare step of the business logic, reporting activity and outcome.
48       * 
49       * @return Prepared where possible, Aborted where necessary.
50       * @throws WrongStateException
51       * @throws SystemException
52       */
53      public Vote prepare() throws WrongStateException, SystemException {
54          // Log the event and invoke the prepare operation
55          // on the back-end logic.
56          System.out.println("[SERVICE] Prepare called on participant, about to prepare the back-end resource");
57          boolean success = mockRestaurantManager.prepare(txID);
58  
59          // Map the return value from
60          // the business logic to the appropriate Vote type.
61          if (success) {
62              System.out.println("[SERVICE] back-end resource prepared, participant votes prepared");
63              return new Prepared();
64          } else {
65              System.out.println("[SERVICE] back-end resource failed to prepare, participant votes aborted");
66              return new Aborted();
67          }
68      }
69  
70      /**
71       * Invokes the commit step of the business logic.
72       * 
73       * @throws WrongStateException
74       * @throws SystemException
75       */
76      public void commit() throws WrongStateException, SystemException {
77          // Log the event and invoke the commit operation
78          // on the backend business logic.
79          System.out.println("[SERVICE] all participants voted 'prepared', so coordinator tells the participant to commit");
80          mockRestaurantManager.commit(txID);
81      }
82  
83      /**
84       * Invokes the rollback operation on the business logic.
85       * 
86       * @throws WrongStateException
87       * @throws SystemException
88       */
89      public void rollback() throws WrongStateException, SystemException {
90          // Log the event and invoke the rollback operation
91          // on the backend business logic.
92          System.out
93                  .println("[SERVICE] one or more participants voted 'aborted' or a failure occurred, so coordinator tells the participant to rollback");
94          mockRestaurantManager.rollback(txID);
95      }
96  
97      public void unknown() throws SystemException {
98          System.out.println("RestaurantParticipantAT.unknown");
99      }
100 
101     public void error() throws SystemException {
102         System.out.println("RestaurantParticipantAT.error");
103     }
104 
105 }