- 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/observers/Observer.java
r78 r84 3 3 import com.framsticks.core.Entity; 4 4 import com.framsticks.core.Instance; 5 import com.framsticks.core.Parameters;6 5 import com.framsticks.core.Program; 7 6 import com.framsticks.params.FramsClass; 8 import com.framsticks.util. Casting;7 import com.framsticks.util.lang.Casting; 9 8 import org.apache.commons.configuration.Configuration; 10 9 import org.apache.log4j.Logger; … … 17 16 public abstract class Observer extends Entity { 18 17 19 private final static Logger LOGGER= Logger.getLogger(Observer.class.getName());18 private final static Logger log = Logger.getLogger(Observer.class.getName()); 20 19 21 public Observer(Parameters parameters) { 22 super(parameters); 23 } 20 public Observer() { 21 } 24 22 25 23 protected final Map<String, Endpoint> endpoints = new HashMap<String, Endpoint>(); 26 24 27 28 29 25 protected Endpoint createEndpoint() { 26 return new Endpoint(); 27 } 30 28 31 @Override 32 protected void configure() throws Exception { 33 super.configure(); 34 for (String name : config.getStringArray("endpoints")) { 35 if (getEndpoints().containsKey(name)) { 36 LOGGER.fatal("observer already contains endpoint named: " + name); 37 continue; 38 } 39 Configuration c = config.subset("endpoints." + name); 40 Parameters p = new Parameters(c.subset("entity"), name, null, null); 41 Entity e = Program.configureEntity(p); 42 Instance i = Casting.tryCast(Instance.class, e); 43 if (i == null) { 44 LOGGER.error("failed to create instance: " + p); 45 continue; 46 } 47 i.configurePublic(); 48 Endpoint endpoint = createEndpoint(); 49 endpoint.configurePublic(this, i, name); 50 getEndpoints().put(name, endpoint); 51 LOGGER.info("instance " + i + " was added to " + Observer.this); 52 } 53 } 29 @Override 30 public void configure(Configuration config) { 31 super.configure(config); 32 for (String n : config.getStringArray("endpoints")) { 33 if (getEndpoints().containsKey(n)) { 34 log.fatal("observer already contains endpoint named: " + n); 35 continue; 36 } 37 Configuration instanceConfig = config.subset("endpoints." + n).subset("entity"); 38 Instance i = Casting.tryCast(Instance.class, Program.configureEntity(instanceConfig)); 39 if (i == null) { 40 log.error("failed to create instance: "); 41 continue; 42 } 43 i.setName(n); 44 i.configure(instanceConfig); 45 addEndpointForInstance(i); 46 } 47 } 54 48 55 @Override 56 protected void run() { 57 super.run(); 58 for (Endpoint e : getEndpoints().values()) { 59 e.getInstance().start(); 60 } 61 } 49 public Endpoint addEndpointForInstance(Instance instance) { 50 Endpoint endpoint = createEndpoint(); 51 endpoint.configurePublic(this, instance, instance.getName()); 52 getEndpoints().put(instance.getName(), endpoint); 53 log.info("instance " + instance + " was added to " + this + " as " + instance.getName()); 54 return endpoint; 55 }; 56 57 @Override 58 protected void run() { 59 super.run(); 60 for (Endpoint e : getEndpoints().values()) { 61 e.start(); 62 } 63 } 62 64 63 65 64 65 66 66 public Map<String, Endpoint> getEndpoints() { 67 return endpoints; 68 } 67 69 68 69 70 70 public static void constructFramsClass(FramsClass.Constructor constructor) { 71 constructor.method("getEndpoints"); 72 } 71 73 72 74
Note: See TracChangeset
for help on using the changeset viewer.