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