01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.springframework.webflow.engine;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20: import org.springframework.webflow.execution.Action;
21: import org.springframework.webflow.execution.Event;
22: import org.springframework.webflow.execution.RequestContext;
23:
24: /**
25: * A simple static helper that performs action execution that encapsulates
26: * common logging and exception handling logic. This is an internal helper class
27: * that is not normally used by application code.
28: *
29: * @author Keith Donald
30: * @author Erwin Vervaet
31: */
32: public class ActionExecutor {
33:
34: private static final Log logger = LogFactory
35: .getLog(ActionExecutor.class);
36:
37: /**
38: * Private constructor to avoid instantiation.
39: */
40: private ActionExecutor() {
41: }
42:
43: /**
44: * Execute the given action.
45: * @param action the action to execute
46: * @param context the flow execution request context
47: * @return result of action execution
48: * @throws ActionExecutionException if the action threw an exception while
49: * executing, the orginal exception is available as the cause if this exception
50: */
51: public static Event execute(Action action, RequestContext context)
52: throws ActionExecutionException {
53: try {
54: if (logger.isDebugEnabled()) {
55: if (context.getCurrentState() == null) {
56: logger.debug("Executing start " + action
57: + " for flow '"
58: + context.getActiveFlow().getId() + "'");
59: } else {
60: logger.debug("Executing " + action + " in state '"
61: + context.getCurrentState().getId()
62: + "' of flow '"
63: + context.getActiveFlow().getId() + "'");
64: }
65: }
66: return action.execute(context);
67: } catch (ActionExecutionException e) {
68: throw e;
69: } catch (Exception e) {
70: // wrap the exception as an ActionExecutionException
71: throw new ActionExecutionException(context.getActiveFlow()
72: .getId(),
73: context.getCurrentState() != null ? context
74: .getCurrentState().getId() : null, action,
75: context.getAttributes(), e);
76: }
77: }
78: }
|