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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 3.1 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.util.lang.IterableIterator;
4import com.framsticks.util.lang.Pair;
5import com.framsticks.util.lang.Strings;
6import org.apache.commons.configuration.*;
7import org.apache.log4j.Logger;
8import org.apache.log4j.PropertyConfigurator;
9
10import java.lang.reflect.Constructor;
11import java.util.*;
12import com.framsticks.util.dispatching.RunAt;
13
14/**
15 * @author Piotr Sniegowski
16 */
17public 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}
Note: See TracBrowser for help on using the repository browser.