source: java/main/src/main/java/com/framsticks/core/Program.java @ 78

Last change on this file since 78 was 78, checked in by psniegowski, 11 years ago

Add f0 parsing and f0->Model transformation.

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