KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jass > hls > ont > CompensatingAction


1 /**
2  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3  -
4  - JASS: Java Advanced tranSaction Support
5  -
6  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7  -
8  - This module was originally developed by
9  -
10  - LSD (Distributed Systems Lab, http://lsd.ls.fi.upm.es/lsd/lsd.htm)
11  - at Universidad Politecnica de Madrid (UPM) as an ObjectWeb Consortium
12  - (http://www.objectweb.org) project.
13  -
14  - This project has been partially funded by the European Commission under
15  - the IST programme of V FP grant IST-2001-37126 and by the Spanish
16  - Ministry of Science & Technology (MCyT) grants TIC2002-10376-E and
17  - TIC2001-1586-C03-02
18  -
19  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20  - The original code and portions created by LSD are
21  - Copyright (c) 2004 LSD (UPM)
22  - All rights reserved.
23  -
24  - Redistribution and use in source and binary forms, with or without
25  - modification, are permitted provided that the following conditions are met:
26  -
27  - -Redistributions of source code must retain the above copyright notice, this
28  - list of conditions and the following disclaimer.
29  -
30  - -Redistributions in binary form must reproduce the above copyright notice,
31  - this list of conditions and the following disclaimer in the documentation
32  - and/or other materials provided with the distribution.
33  -
34  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35  - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  - ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
38  - LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  - POSSIBILITY OF SUCH DAMAGE.
45  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46  -
47  - Author: Francisco Perez Sorrosal (frperezs)
48  -
49  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 */

51
52 package org.objectweb.jass.hls.ont;
53
54 import org.jboss.logging.Logger;
55
56 import javax.activity.coordination.Action;
57 import javax.activity.*;
58 import javax.activity.opennested.Compensator;
59
60 /**
61  * Implements the compensating action required by the ONT model. It may receive
62  * the activity_committed and the activity_rollback signals, performing the required
63  * tasks in each case.
64  * @author fran
65  * Date: Feb 12, 2004
66  * org.objectweb.jass.hls.ontCompensatingAction.java
67  */

68 public class CompensatingAction implements Action {
69
70     // Contants ---------------------------------------------------------------
71

72     // Attributes -------------------------------------------------------------
73

74     private static Logger log = Logger.getLogger(CompensatingAction.class);
75
76     private Compensator compensator;
77     private int priority;
78
79     // Constructors -----------------------------------------------------------
80

81     /**
82      * Constructor.
83      *
84      * @param compensator - the element that compensates a previous task.
85      * @param priority - compensator priority.
86      */

87     public CompensatingAction(Compensator compensator, int priority) {
88         this.compensator = compensator;
89         this.priority = priority;
90     }
91
92     // Public -----------------------------------------------------------------
93

94     // Action implementation --------------------------------------------------
95

96     /**
97      * Invoked on the target object, by the AS, during signal processing. When
98      * the activity_committed signal is processed by a ONT action, it means
99      * that the activity has completted with success. But the compensator
100      * mantained by this action may be triggered later if other activity fails.
101      * That is why the action must promote its compensator to the parent
102      * activity. If a parent coordinator reference is found, the Action wraps
103      * its compensator in a new action that is registered with the parent.
104      * Depending on the result of the registration, the Action returns the
105      * parent_add_successful or the parent_has_completed/parent_add_failed
106      * outcomes. Else if the parent coordinator is null, the action invokes a
107      * forget operation on the compensator to inform it about the final
108      * completion and the compensation_successful outcome is returned. If the
109      * action receives the activity_rollback signal, it invokes the compensate
110      * operation on the Compensator object. If the action fails to invoke the
111      * Compensator object it returns the outcome compensation_failed.
112      */

113     public Outcome processSignal(Signal signal) throws ActionErrorException {
114         Outcome result = null;
115
116         if (signal.getName().equals("activity_committed")) {
117             ActivityCoordinator parentAC =
118                 (ActivityCoordinator) signal.getExtendedValue();
119             if (parentAC != null) {
120                 try {
121                     int priority =
122                         parentAC.getNumberRegisteredActions(ONTCompletionSS.COMPLETION_SS_NAME);
123                     parentAC.addAction(
124                         new CompensatingAction(compensator, 0),
125                     ONTCompletionSS.COMPLETION_SS_NAME,
126                         priority);
127                     result =
128                         new Outcome(
129                             "parent_add_successful",
130                             (java.io.Serializable JavaDoc) null);
131                     log.debug("OUTCOME: parent_add_successful");
132                 } catch (IllegalStateException JavaDoc e) {
133                     result =
134                         new Outcome(
135                             "parent_has_completed",
136                             (java.io.Serializable JavaDoc) null);
137                     log.debug("OUTCOME: parent_has_completed");
138                 } catch (Exception JavaDoc e) {
139                     result =
140                         new Outcome("parent_add_failed", (java.io.Serializable JavaDoc) null);
141                     log.debug("OUTCOME: parent_add_failed");
142                 }
143             } else {
144                 compensator.forget();
145                 result =
146                     new Outcome(
147                         "compensate_successful",
148                         (java.io.Serializable JavaDoc) null);
149                 log.debug("OUTCOME: compensate_successful (forget)");
150             }
151         }
152
153         // We must to invoke the compensate operation
154
if (signal.getName().equals("activity_rolledback")) {
155             try {
156                 compensator.compensate();
157                 result =
158                     new Outcome(
159                         "compensate_successful",
160                         (java.io.Serializable JavaDoc) null);
161                 log.debug("OUTCOME: compensate_successful (compensation)");
162             } catch (Exception JavaDoc e) {
163                 e.printStackTrace();
164                 result =
165                     new Outcome("compensate_failed", (java.io.Serializable JavaDoc) null);
166                 log.debug("OUTCOME: compensate_failed");
167             }
168         }
169         return result;
170     }
171
172     /**
173      * Clears the main attributes.
174      */

175     public void destroy() {
176         compensator = null;
177     }
178 }
179
Popular Tags