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.wsba.participantcompletion.simple;
18  
19  import com.arjuna.wst.BusinessAgreementWithParticipantCompletionParticipant;
20  import com.arjuna.wst.FaultedException;
21  import com.arjuna.wst.SystemException;
22  import com.arjuna.wst.WrongStateException;
23  import com.arjuna.wst11.ConfirmCompletedParticipant;
24  
25  import java.io.Serializable;
26  
27  /**
28   * An adapter class that exposes the SetManager as a WS-BA participant using the 'Participant Completion' protocol.
29   * 
30   * The Set Service only allows a single item to be added to the set in any given transaction. So, this means it can complete at
31   * the end of the addValueToSet call, rather than having to wait for the coordinator to tell it to do so. Hence it uses a
32   * participant which implements the 'participant completion' protocol.
33   * 
34   * @author Paul Robinson (paul.robinson@redhat.com)
35   */
36  public class SetParticipantBA implements BusinessAgreementWithParticipantCompletionParticipant, ConfirmCompletedParticipant,
37          Serializable {
38  
39      private static final long serialVersionUID = 1L;
40  
41      private String value;
42  
43      /**
44       * Participant instances are related to business method calls in a one to one manner.
45       * 
46       * @param value the value to remove from the set during compensation
47       */
48      public SetParticipantBA(String value) {
49          this.value = value;
50      }
51  
52      /**
53       * The transaction has completed successfully. The participant previously informed the coordinator that it was ready to
54       * complete.
55       * 
56       * @throws WrongStateException never in this implementation.
57       * @throws SystemException never in this implementation.
58       */
59  
60      public void close() throws WrongStateException, SystemException {
61          // nothing to do here as the item has already been added to the set
62          System.out
63                  .println("[SERVICE] Participant.close (The participant knows that this BA is now finished and can throw away any temporary state)");
64      }
65  
66      /**
67       * The transaction has canceled, and the participant should undo any work. The participant cannot have informed the
68       * coordinator that it has completed.
69       * 
70       * @throws WrongStateException never in this implementation.
71       * @throws SystemException never in this implementation.
72       */
73  
74      public void cancel() throws WrongStateException, SystemException {
75          System.out.println("[SERVICE] Participant.cancel (The participant should compensate any work done within this BA)");
76  
77          // Compensate work
78          MockSetManager.rollback(value);
79      }
80  
81      /**
82       * The transaction has cancelled. The participant previously informed the coordinator that it had finished work but could
83       * compensate later if required, and it is now requested to do so.
84       * 
85       * @throws WrongStateException never in this implementation.
86       * @throws SystemException if unable to perform the compensating transaction.
87       */
88  
89      public void compensate() throws FaultedException, WrongStateException, SystemException {
90          System.out.println("[SERVICE] Participant.compensate");
91  
92          // Compensate work done by the service
93          MockSetManager.rollback(value);
94      }
95  
96      public String status() {
97          return null;
98      }
99  
100     public void unknown() throws SystemException {
101     }
102 
103     public void error() throws SystemException {
104         System.out.println("[SERVICE] Participant.error");
105 
106         // Compensate work done by the service
107         MockSetManager.rollback(value);
108     }
109 
110     /**
111      * method called to perform commit or rollback of prepared changes to the underlying manager state after the participant
112      * recovery record has been written
113      * 
114      * @param confirmed true if the log record has been written and changes should be rolled forward and false if it has not
115      *        been written and changes should be rolled back
116      */
117     public void confirmCompleted(boolean confirmed) {
118         if (confirmed) {
119             System.out
120                     .println("[SERVICE] Participant.confirmCompleted('"
121                             + confirmed
122                             + "') (This tells the participant that compensation information has been logged and that it is safe to commit any changes.)");
123             MockSetManager.commit();
124         } else {
125             MockSetManager.rollback(value);
126         }
127     }
128 }