001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.runner;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOApplicationConfig;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.Application;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.app.ErrorContext;
013: import com.tc.simulator.listener.ListenerProvider;
014:
015: import java.util.HashSet;
016: import java.util.Set;
017:
018: public abstract class AbstractTransparentApp implements Application {
019:
020: private TransparentAppCoordinator coordinator;
021: private int intensity;
022: private ListenerProvider listenerProvider;
023: private Set appIds = new HashSet();
024:
025: public AbstractTransparentApp() {
026: // niladic for instance Configuration only!
027: }
028:
029: public AbstractTransparentApp(String appId,
030: ApplicationConfig config, ListenerProvider listenerProvider) {
031: synchronized (appIds) {
032: if (!appIds.add(appId)) {
033: throw new AssertionError(
034: "You've created me with the same global ID as someone else: "
035: + appId);
036: }
037: }
038: this .listenerProvider = listenerProvider;
039: this .intensity = config.getIntensity();
040: this .coordinator = new TransparentAppCoordinator(appId, config
041: .getGlobalParticipantCount());
042: }
043:
044: protected int getIntensity() {
045: return this .intensity;
046: }
047:
048: protected int getParticipantCount() {
049: return coordinator.getParticipantCount();
050: }
051:
052: public String getApplicationId() {
053: return coordinator.getGlobalId();
054: }
055:
056: protected void moveToStage(int stage) {
057: coordinator.moveToStage(stage);
058: }
059:
060: protected void moveToStageAndWait(int stage) {
061: coordinator.moveToStageAndWait(stage);
062: }
063:
064: protected void notifyError(String msg) {
065: listenerProvider.getResultsListener().notifyError(
066: new ErrorContext(msg, new Error()));
067: }
068:
069: protected void notifyError(ErrorContext context) {
070: listenerProvider.getResultsListener().notifyError(context);
071: }
072:
073: protected void notifyError(Throwable t) {
074: listenerProvider.getResultsListener().notifyError(
075: new ErrorContext(t));
076: }
077:
078: public static void visitL1DSOConfig(ConfigVisitor visitor,
079: DSOClientConfigHelper config) {
080: config
081: .addIncludePattern(AbstractTransparentApp.class
082: .getName());
083: config.addRoot("AbstractTransparentAppAppIds",
084: AbstractTransparentApp.class.getName() + ".appIds");
085: config.addWriteAutolock("* "
086: + AbstractTransparentApp.class.getName() + ".*(..)");
087:
088: TransparencyClassSpec spec = config
089: .getOrCreateSpec(TransparentAppCoordinator.class
090: .getName());
091: spec.addRoot("participants", "participants");
092: config.addWriteAutolock("* "
093: + TransparentAppCoordinator.class.getName() + ".*(..)");
094: }
095:
096: public static void visitDSOApplicationConfig(ConfigVisitor visitor,
097: DSOApplicationConfig config) {
098: config
099: .addIncludePattern(AbstractTransparentApp.class
100: .getName());
101: config.addRoot("AbstractTransparentAppAppIds",
102: AbstractTransparentApp.class.getName() + ".appIds");
103: config.addWriteAutolock("* "
104: + AbstractTransparentApp.class.getName() + ".*(..)");
105:
106: config.addIncludePattern(TransparentAppCoordinator.class
107: .getName());
108: config.addRoot("participants", TransparentAppCoordinator.class
109: .getName()
110: + ".participants");
111: config.addWriteAutolock("* "
112: + TransparentAppCoordinator.class.getName() + ".*(..)");
113: }
114:
115: public void notifyResult(Boolean result) {
116: this .listenerProvider.getResultsListener().notifyResult(result);
117: }
118:
119: public boolean interpretResult(Object result) {
120: return result instanceof Boolean
121: && ((Boolean) result).booleanValue();
122: }
123: }
|