]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/Executor.java
Adapt for agents
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / Executor.java
1 package org.argeo.slc.core.execution;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.execution.ExecutionFlow;
6 import org.argeo.slc.process.SlcExecution;
7 import org.springframework.beans.BeansException;
8 import org.springframework.context.ApplicationContext;
9 import org.springframework.context.ApplicationContextAware;
10 import org.springframework.context.ApplicationEvent;
11 import org.springframework.context.ApplicationListener;
12
13 public class Executor implements ApplicationListener, ApplicationContextAware {
14 private final static Log log = LogFactory.getLog(Executor.class);
15
16 private ApplicationContext applicationContext;
17
18 public void onApplicationEvent(ApplicationEvent event) {
19 if (event instanceof NewExecutionEvent) {
20 SlcExecution slcExecution = ((NewExecutionEvent) event)
21 .getSlcExecution();
22 ExecutionContext executionContext = new ExecutionContext();
23 ExecutionThread thread = new ExecutionThread(executionContext,
24 slcExecution);
25 thread.start();
26 }
27
28 }
29
30 public void setApplicationContext(ApplicationContext applicationContext)
31 throws BeansException {
32 this.applicationContext = applicationContext;
33 }
34
35 private class ExecutionThread extends Thread {
36 private final SlcExecution slcExecution;
37 private final ExecutionContext executionContext;
38
39 public ExecutionThread(ExecutionContext executionContext,
40 SlcExecution slcExecution) {
41 super("SLC Execution #" + executionContext.getUuid());
42 this.slcExecution = slcExecution;
43 this.executionContext = executionContext;
44 }
45
46 public void run() {
47 // Initialize from SlcExecution
48 ExecutionContext.registerExecutionContext(executionContext);
49 ExecutionContext.getVariables()
50 .putAll(slcExecution.getAttributes());
51
52 try {
53 log.info("Start execution #"
54 + ExecutionContext.getExecutionUuid());
55 String executionBean = slcExecution.getAttributes().get(
56 "slc.flows");
57 ExecutionFlow main = (ExecutionFlow) applicationContext
58 .getBean(executionBean);
59 main.execute();
60 } catch (Exception e) {
61 log.error("Execution " + executionContext.getUuid()
62 + " failed.", e);
63 } finally {
64 applicationContext.publishEvent(new ExecutionFinishedEvent(
65 this, executionContext));
66 }
67
68 }
69 }
70
71 }