KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > simulator > container > ApplicationRunner


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.simulator.container;
5
6 import com.tc.simulator.app.Application;
7 import com.tc.simulator.app.ErrorContext;
8 import com.tc.simulator.control.Control;
9 import com.tc.simulator.listener.ResultsListener;
10 import com.tc.simulator.listener.StatsListener;
11 import com.tc.simulator.listener.StatsListenerFactory;
12 import com.tc.util.TCTimeoutException;
13
14 import java.util.Properties JavaDoc;
15
16 class ApplicationRunner implements Runnable JavaDoc {
17   private final ApplicationRunnerConfig config;
18   private final Control control;
19   private final ResultsListener resultsListener;
20   private final Application application;
21   private final StatsListener statsListener;
22
23   ApplicationRunner(ApplicationRunnerConfig config, Control control, ResultsListener resultsListener,
24                     Application application, StatsListenerFactory statsListenerFactory) {
25     this.config = config;
26     this.control = control;
27     this.resultsListener = resultsListener;
28     this.application = application;
29     Properties JavaDoc properties = new Properties JavaDoc();
30     properties.setProperty("sample_name", "application_instance_execution_time");
31     properties.setProperty("application_class", application.getClass().getName());
32     this.statsListener = statsListenerFactory.newStatsListener(properties);
33   }
34
35   public void run() {
36     try {
37       if (this.config.getStartTimeout() < 1) {
38         notifyError(new ApplicationRunnerConfigException("Start timeout must be greater than zero."));
39         return;
40       }
41       this.control.waitForStart(config.getStartTimeout());
42       long start = System.currentTimeMillis();
43       this.application.run();
44       long delta = System.currentTimeMillis() - start;
45       statsListener.sample(delta, "");
46       this.resultsListener.notifyResult(Boolean.TRUE);
47     } catch (TCTimeoutException e) {
48       this.resultsListener.notifyStartTimeout();
49       this.resultsListener.notifyResult(Boolean.FALSE);
50     } catch (Throwable JavaDoc t) {
51       this.resultsListener.notifyError(new ErrorContext("Caught Throwable from Application.run()", t));
52       this.resultsListener.notifyResult(Boolean.FALSE);
53     } finally {
54       control.notifyComplete();
55     }
56   }
57
58   private void notifyError(Throwable JavaDoc t) {
59     this.resultsListener.notifyError(new ErrorContext(t));
60     this.resultsListener.notifyResult(Boolean.FALSE);
61   }
62 }
Popular Tags