- public DefaultExecutionFlow(ExecutionSpec executionSpec,
- Map<String, Object> parameters) {
- // be sure to have an execution spec
- this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec()
- : executionSpec;
-
- // only parameters contained in the executionSpec can be set
- for (String parameter : parameters.keySet()) {
- if (!executionSpec.getAttributes().containsKey(parameter)) {
- throw new SlcException("Parameter " + parameter
- + " is not defined in the ExecutionSpec");
- }
- }
-
- // set the parameters
- this.parameters.putAll(parameters);
-
- // check that all the required parameters are defined
- MapBindingResult errors = new MapBindingResult(parameters, "execution#"
- + getName());
- for (String key : executionSpec.getAttributes().keySet()) {
- ExecutionSpecAttribute attr = executionSpec.getAttributes()
- .get(key);
-
- if (attr.getIsImmutable() && !isSetAsParameter(key)) {
- errors.rejectValue(key, "Immutable but not set");
- break;
- }
-
- if (attr.getIsConstant() && !isSetAsParameter(key)) {
- errors.rejectValue(key, "Constant but not set as parameter");
- break;
- }
-
- if (attr.getIsHidden() && !isSetAsParameter(key)) {
- errors.rejectValue(key, "Hidden but not set as parameter");
- break;
- }
- }
-
- if (errors.hasErrors())
- throw new SlcException("Could not prepare execution flow: "
- + errors.toString());
-
- }
-
- public void run() {
- try {
- for (Runnable executable : executables) {
- if (Thread.interrupted()) {
- log.error("Flow '" + getName() + "' killed before '"
- + executable + "'");
- Thread.currentThread().interrupt();
- return;
- // throw new ThreadDeath();
- }
- this.doExecuteRunnable(executable);
- }
- } catch (RuntimeException e) {
- if (Thread.interrupted()) {
- log.error("Flow '" + getName()
- + "' killed while receiving an unrelated exception", e);
- Thread.currentThread().interrupt();
- return;
- // throw new ThreadDeath();
- }
- if (failOnError)
- throw e;
- else {
- log.error("Execution flow failed,"
- + " but process did not fail"
- + " because failOnError property"
- + " is set to false: " + e);
- if (log.isTraceEnabled())
- e.printStackTrace();
- }
- }
- }
-
- /**
- * List sub-runnables that would be executed if run() method would be
- * called.
- */
- public Iterator<Runnable> runnables() {
- return executables.iterator();
- }
-
- /**
- * If there is one and only one runnable wrapped return it, throw an
- * exeception otherwise.
- */
- public Runnable getRunnable() {
- if (executables.size() == 1)
- return executables.get(0);
- else
- throw new SlcException("There are " + executables.size()
- + " runnables in flow " + getName());
- }
-
- public void doExecuteRunnable(Runnable runnable) {
- try {
- if (executionContext != null)
- if (runnable instanceof ExecutionFlow)
- executionContext.beforeFlow((ExecutionFlow) runnable);
- runnable.run();
- } finally {
- if (executionContext != null)
- if (runnable instanceof ExecutionFlow)
- executionContext.afterFlow((ExecutionFlow) runnable);
- }