1 | package com.framsticks.core; |
---|
2 | |
---|
3 | import com.framsticks.util.lang.IterableIterator; |
---|
4 | import com.framsticks.util.lang.Pair; |
---|
5 | import com.framsticks.util.lang.Strings; |
---|
6 | import org.apache.commons.configuration.*; |
---|
7 | import org.apache.log4j.Logger; |
---|
8 | import org.apache.log4j.PropertyConfigurator; |
---|
9 | |
---|
10 | import java.lang.reflect.Constructor; |
---|
11 | import java.util.*; |
---|
12 | import com.framsticks.util.dispatching.RunAt; |
---|
13 | |
---|
14 | /** |
---|
15 | * @author Piotr Sniegowski |
---|
16 | */ |
---|
17 | public class Program extends com.framsticks.util.dispatching.Thread<Program> { |
---|
18 | |
---|
19 | private final static Logger log = Logger.getLogger(Program.class.getName()); |
---|
20 | |
---|
21 | protected Entity entity; |
---|
22 | |
---|
23 | Configuration config; |
---|
24 | |
---|
25 | public Program(String name) { |
---|
26 | super(name, java.lang.Thread.currentThread()); |
---|
27 | } |
---|
28 | |
---|
29 | public static Entity configureEntity(Configuration config) { |
---|
30 | String typeName = config.getString("class"); |
---|
31 | log.info("configuring instance " + typeName); |
---|
32 | try { |
---|
33 | Class<?> type = Class.forName(typeName); |
---|
34 | Constructor<?> constructor = type.getConstructor(); |
---|
35 | Entity entity = (Entity) constructor.newInstance(); |
---|
36 | return entity; |
---|
37 | } catch (Exception e) { |
---|
38 | log.error("failed to instantiate: " + e); |
---|
39 | } |
---|
40 | return null; |
---|
41 | } |
---|
42 | |
---|
43 | protected static void resolve(Configuration config, String prefix) { |
---|
44 | for (String p : new IterableIterator<String>(prefix == null ? config.getKeys() : config.getKeys(prefix))) { |
---|
45 | if (p.endsWith(".mount")) { |
---|
46 | String source = config.getString(p); |
---|
47 | log.info("mounting " + source + " at " + p); |
---|
48 | config.clearProperty(p); |
---|
49 | Configuration mount = config.subset(source); |
---|
50 | for (String mk : new IterableIterator<String>(mount.getKeys())) { |
---|
51 | config.addProperty(p.substring(0, p.length() - 6) + "." + mk, mount.getProperty(mk)); |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | public void run(String[] args) { |
---|
58 | |
---|
59 | PropertyConfigurator.configure(getClass().getResource("/configs/log4j.properties")); |
---|
60 | log.debug("started in " + System.getProperty("user.dir")); |
---|
61 | try { |
---|
62 | config = new PropertiesConfiguration(getClass().getResource("/configs/framsticks.properties")); |
---|
63 | |
---|
64 | for (String a : args) { |
---|
65 | Pair<String, String> p = Strings.splitIntoPair(a, '=', "true"); |
---|
66 | config.setProperty(p.first, p.second); |
---|
67 | } |
---|
68 | |
---|
69 | resolve(config, null); |
---|
70 | |
---|
71 | Iterator<?> i = config.getKeys(); |
---|
72 | while (i.hasNext()) { |
---|
73 | String p = (String) i.next(); |
---|
74 | log.debug(p + " = " + config.getProperty(p)); |
---|
75 | } |
---|
76 | |
---|
77 | } catch (ConfigurationException e) { |
---|
78 | System.err.print("failed to parse configuration:" + e); |
---|
79 | return; |
---|
80 | } |
---|
81 | Configuration entityConfig = config.subset("com.framsticks.entity"); |
---|
82 | |
---|
83 | entity = configureEntity(entityConfig); |
---|
84 | entity.setOwner(new EntityOwner() { |
---|
85 | @Override |
---|
86 | public void onDone() { |
---|
87 | log.info("exiting"); |
---|
88 | Runtime.getRuntime().exit(0); |
---|
89 | } |
---|
90 | }); |
---|
91 | entity.setName("main"); |
---|
92 | try { |
---|
93 | entity.configure(entityConfig); |
---|
94 | } catch (Exception e) { |
---|
95 | log.fatal("exception caught during configuration: " + e); |
---|
96 | } |
---|
97 | log.info("all entities were configured"); |
---|
98 | entity.start(); |
---|
99 | } |
---|
100 | |
---|
101 | public static void main(final String[] args) { |
---|
102 | |
---|
103 | final Program program = new Program("program"); |
---|
104 | program.invokeLater(new RunAt<Program>() { |
---|
105 | @Override |
---|
106 | public void run() { |
---|
107 | program.run(args); |
---|
108 | } |
---|
109 | }); |
---|
110 | program.routine(); |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|