01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.simulator.container;
05:
06: import com.tc.simulator.app.Application;
07: import com.tc.simulator.app.ErrorContext;
08: import com.tc.simulator.control.Control;
09: import com.tc.simulator.listener.ResultsListener;
10: import com.tc.simulator.listener.StatsListener;
11: import com.tc.simulator.listener.StatsListenerFactory;
12:
13: import java.util.Properties;
14:
15: class ApplicationRunner implements Runnable {
16: private final ApplicationRunnerConfig config;
17: private final Control control;
18: private final ResultsListener resultsListener;
19: private final Application application;
20: private final StatsListener statsListener;
21:
22: ApplicationRunner(ApplicationRunnerConfig config, Control control,
23: ResultsListener resultsListener, Application application,
24: StatsListenerFactory statsListenerFactory) {
25: this .config = config;
26: this .control = control;
27: this .resultsListener = resultsListener;
28: this .application = application;
29: Properties properties = new Properties();
30: properties.setProperty("sample_name",
31: "application_instance_execution_time");
32: properties.setProperty("application_class", application
33: .getClass().getName());
34: this .statsListener = statsListenerFactory
35: .newStatsListener(properties);
36: }
37:
38: public void run() {
39: try {
40: if (this .config.getStartTimeout() < 1) {
41: notifyError(new ApplicationRunnerConfigException(
42: "Start timeout must be greater than zero."));
43: return;
44: }
45: this .control.waitForStart();
46: long start = System.currentTimeMillis();
47: this .application.run();
48: long delta = System.currentTimeMillis() - start;
49: statsListener.sample(delta, "");
50: this .resultsListener.notifyResult(Boolean.TRUE);
51: } catch (Throwable t) {
52: this .resultsListener.notifyError(new ErrorContext(
53: "Caught Throwable from Application.run()", t));
54: this .resultsListener.notifyResult(Boolean.FALSE);
55: } finally {
56: control.notifyComplete();
57: }
58: }
59:
60: private void notifyError(Throwable t) {
61: this .resultsListener.notifyError(new ErrorContext(t));
62: this.resultsListener.notifyResult(Boolean.FALSE);
63: }
64: }
|