ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.pvm.internal.model;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
 
 
import org.jbpm.api.JbpmException;
import org.jbpm.api.listener.EventListener;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.cmd.CommandService;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.model.op.AtomicOperation;
import org.jbpm.pvm.internal.model.op.MoveToChildActivity;
import org.jbpm.pvm.internal.tx.Transaction;
import org.jbpm.pvm.internal.wire.Descriptor;
 
/**
 * @author Tom Baeyens
 */
public class ExceptionHandlerImpl implements Serializable {
 
  private static final long serialVersionUID = 1L;
  static final Log log = Log.getLog(ExceptionHandlerImpl.class.getName());
 
  protected long dbid;
  protected int dbversion;
  protected ProcessDefinitionImpl processDefinition;
  protected String exceptionClassName;
  protected boolean isTransactional;
  protected boolean isRethrowMasked;
  protected List<EventListenerReference> eventListenerReferences;
  protected String transitionName; // mutually exclusive with activityName
  protected String activityName;       // mutually exclusive with transitionName
  
  // construction methods /////////////////////////////////////////////////////
  
  public EventListenerReference createEventListenerReference(EventListener eventListener) {
    EventListenerReference eventListenerReference = createEventListenerReference();
    eventListenerReference.setEventListener(eventListener);
    return eventListenerReference;
  }
 
  public EventListenerReference createEventListenerReference(Descriptor descriptor) {
    EventListenerReference eventListenerReference = createEventListenerReference();
    eventListenerReference.setEventListenerDescriptor(descriptor);
    return eventListenerReference;
  }
 
  public EventListenerReference createEventListenerReference() {
    if (eventListenerReferences==null) {
      eventListenerReferences = new ArrayList<EventListenerReference>();
    }
    EventListenerReference eventListenerReference = new EventListenerReference();
    eventListenerReference.setProcessDefinition(processDefinition);
    eventListenerReferences.add(eventListenerReference);
    return eventListenerReference;
  }
  
  // runtime behaviour methods ////////////////////////////////////////////////
  
  public boolean matches(Exception exception) {
    return matches(exception.getClass());
  }
 
  public boolean matches(Class<?> exceptionClass) {
    if (exceptionClass==null) {
      return false;
    }
    if ( (exceptionClassName==null)
         || (exceptionClass.getName().equals(exceptionClassName)) 
       ) {
      return true;
    }
    Class<?> superClass = exceptionClass.getSuperclass();
    if (superClass!=null) {
      return matches(superClass);
    }
    return false;
  }
  
  public void handle(ExecutionImpl execution, Exception exception) {
    if (isTransactional) {
      EnvironmentImpl environment = EnvironmentImpl.getCurrent();
      Transaction transaction = (environment!=null ? environment.get(Transaction.class) : null);
      if (transaction!=null) {
        log.trace("registering exception handler to "+transaction);
        CommandService commandService = environment.get(CommandService.class);
        if (commandService==null) {
          throw new JbpmException("environment doesn't have a command service for registering transactional exception handler", exception);
        }
        ExceptionHandlerSynchronization exceptionHandlerSynchronization = new ExceptionHandlerSynchronization(
                this, execution, 
                exception,
                commandService
        );
        // registration of the synchronization is delegated to the AfterTxCompletionListener
        // to avoid a dependency on class Synchronization
        exceptionHandlerSynchronization.register(transaction);
        log.trace("registering exception handler to "+transaction);
        throw new JbpmException("transaction exception handler registered handler after transaction completed.  make sure this transaction is rolled back", exception); 
      } else {
        throw new JbpmException("no transaction present in the environment for transactional exception handler", exception);
      }
    } else {
      executeHandler(execution, exception);
    }
  }
  
  void executeHandler(ExecutionImpl execution, Exception exception) {
    if (eventListenerReferences!=null) {
      for (EventListenerReference eventListenerReference: eventListenerReferences) {
        
        EventListener eventListener = eventListenerReference.getEventListener();
        
        log.trace("executing "+eventListener+" for "+this);
        try {
          eventListener.notify(execution);
        } catch (RuntimeException e) {
          throw e;
        } catch (Exception e) {
          throw new JbpmException("couldn't execute "+eventListener, e);
        }
      }
    }
    
    if (transitionName!=null) {
      ActivityImpl activity = execution.getActivity();
      if (activity==null) {
        // If the execution is not positioned in a activity, it must be 
        // positioned in a transition.  In that case we check if the 
        // transition is present on the enclosing activity.
        
        // The weird way of getting checking and fetching the parent activity
        // is because hibernate doesn't support instanceof.  The transition-->parent
        // relation is mapped as a ProcessElementImpl.  So we can't cast it to 
        // a ActivityImpl.  Ouch.
        // The workaround is to check if the parent is equal to the 
        // process definition.  If that is not the case, we can be sure 
        // that the parent is a activity.  In that case we look up the activity 
        // from the process definition by name.  OuchOuchSquare :-)
        
        TransitionImpl transition = execution.getTransition();
        log.trace("no current activity.  searching for transition from parent of "+transition);
        if (transition!=null) {
          OpenProcessDefinition processDefinition = transition.getProcessDefinition();
          ObservableElementImpl transitionParent = transition.getParent();
          
          if ( (transitionParent!=null)
               && (! transitionParent.equals(processDefinition))
             ) {
            activity = (ActivityImpl) processDefinition.findActivity(transitionParent.getName());
          }
        }
      }
      
      if (activity!=null) {
        TransitionImpl transition = activity.findOutgoingTransition(transitionName);
        if (transition!=null) {
          log.trace(toString()+" takes transition "+transitionName);
          execution.setTransition(transition);
          execution.performAtomicOperationSync(AtomicOperation.TRANSITION_END_ACTIVITY);
        } else {
          log.info("WARNING: "+toString()+" couldn't find transition "+transitionName+" on "+activity);
        }
      } else {
        log.info("WARNING: "+toString()+" couldn't find current activity to take transition "+transitionName);
      }
 
    } else if (activityName!=null) {
      // execute child activity
      ActivityImpl activity = execution.getActivity();
      ActivityImpl childActivity = ( activity!=null ? activity.getActivity(activityName) : null );
      if (childActivity!=null) {
        log.trace(toString()+" takes transition "+transitionName);
        execution.performAtomicOperationSync(new MoveToChildActivity(childActivity));
      } else {
        log.info("WARNING: "+toString()+" couldn't find child activity "+activityName);
      }
    }
  }
 
  public static void rethrow(Exception exception, String prefixMessage) {
    log.trace("rethrowing "+exception);
    if (exception instanceof RuntimeException) {
      throw (RuntimeException) exception;
    } else {
      throw new JbpmException(prefixMessage+": "+exception.getMessage(), exception);
    }
  }
 
  public String toString() {
    return (exceptionClassName!=null ? "exception-handler("+exceptionClassName+")" : "exception-handler");
  }
 
  // getters and setters //////////////////////////////////////////////////////
 
  public long getDbid() {
    return dbid;
  }
  public String getExceptionClassName() {
    return exceptionClassName;
  }
  public void setExceptionClassName(String exceptionClassName) {
    this.exceptionClassName = exceptionClassName;
  }
  public boolean isTransactional() {
    return isTransactional;
  }
  public void setTransactional(boolean isTransactional) {
    this.isTransactional = isTransactional;
  }
  public String getTransitionName() {
    return transitionName;
  }
  public void setTransitionName(String transitionName) {
    this.transitionName = transitionName;
  }
  public String getActivityName() {
    return activityName;
  }
  public void setActivityName(String activityName) {
    this.activityName = activityName;
  }
  public boolean isRethrowMasked() {
    return isRethrowMasked;
  }
  public void setRethrowMasked(boolean isRethrowMasked) {
    this.isRethrowMasked = isRethrowMasked;
  }
  public List<EventListenerReference> getEventListenerReferences() {
    return eventListenerReferences;
  }
  public void setEventListenerReferences(List<EventListenerReference> eventListenerReferences) {
    this.eventListenerReferences = eventListenerReferences;
  }
  public ProcessDefinitionImpl getProcessDefinition() {
    return processDefinition;
  }
  public void setProcessDefinition(ProcessDefinitionImpl processDefinition) {
    this.processDefinition = processDefinition;
  }
}