]> git.argeo.org Git - gpl/argeo-slc.git/blob - EfLauncher.java
3e93a7e1b7ac5db4e36160a204fb650e636b1301
[gpl/argeo-slc.git] / EfLauncher.java
1 package org.argeo.slc.execution.old;
2
3 import java.io.FileInputStream;
4 import java.util.Properties;
5
6 import org.apache.commons.io.IOUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.core.execution.ConsoleContextDescriber;
10 import org.argeo.slc.core.execution.ExecutionFinishedEvent;
11 import org.argeo.slc.core.execution.NewExecutionEvent;
12 import org.argeo.slc.execution.ExecutionContext;
13 import org.argeo.slc.logging.Log4jUtils;
14 import org.argeo.slc.process.SlcExecution;
15 import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
16 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
17 import org.springframework.context.ApplicationEvent;
18 import org.springframework.context.ApplicationListener;
19 import org.springframework.context.support.GenericApplicationContext;
20
21 public class EfLauncher implements ApplicationListener {
22 private final Log log;
23
24 private boolean running = false;
25
26 public EfLauncher() {
27 Properties userProperties = new Properties();
28 FileInputStream in = null;
29 try {
30 in = new FileInputStream("src/slc/conf/slc.properties");
31 userProperties.load(in);
32 } catch (Exception e) {
33 e.printStackTrace();
34 } finally {
35 IOUtils.closeQuietly(in);
36 }
37
38 // Set as System properties
39 for (Object obj : userProperties.keySet()) {
40 String key = obj.toString();
41 System.setProperty(key, userProperties.getProperty(key));
42 }
43
44 // Logging
45 System.setProperty("log4j.defaultInitOverride", "true");
46
47 Log4jUtils.initLog4j(null);
48 log = LogFactory.getLog(EfLauncher.class);
49 }
50
51 public void launch(String script) {
52 // describe(script);
53
54 GenericApplicationContext context = new GenericApplicationContext();
55 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
56 reader.loadBeanDefinitions(script);
57 // FileSystemXmlApplicationContext context = new
58 // FileSystemXmlApplicationContext(
59 // script);
60 context.addApplicationListener(this);
61 context.refresh();
62 context.start();
63 log.debug("Context initialized");
64
65 SlcExecution slcExecution = new SlcExecution();
66 slcExecution.getAttributes().put("slc.flows", "main");
67
68 running = true;
69 context.publishEvent(new NewExecutionEvent(this, slcExecution));
70
71 synchronized (this) {
72 while (running)
73 try {
74 wait();
75 } catch (InterruptedException e) {
76 // silent
77 }
78 }
79 }
80
81 public synchronized boolean isRunning() {
82 return running;
83 }
84
85 public synchronized void setRunning(boolean running) {
86 this.running = running;
87 }
88
89 public void onApplicationEvent(ApplicationEvent event) {
90 if (event instanceof ExecutionFinishedEvent) {
91 ExecutionContext executionContext = ((ExecutionFinishedEvent) event)
92 .getExecutionContext();
93 log.debug("Execution " + executionContext.getUuid()
94 + " finished, stopping launcher...");
95 synchronized (this) {
96 running = false;
97 notifyAll();
98 }
99 }
100
101 }
102
103 public static void main(String[] args) {
104 String script = "file:src/slc/conf/main.xml";
105 new EfLauncher().launch(script);
106 }
107
108 private static void describe(String script) {
109 SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
110 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
111 reader.loadBeanDefinitions(script);
112 new ConsoleContextDescriber().describeContext(registry);
113 }
114 }