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