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