01: package test.infra;
02:
03: import dalma.Conversation;
04: import dalma.Engine;
05: import dalma.helpers.ThreadPoolExecutor;
06: import dalma.impl.EngineImpl;
07: import dalma.impl.Util;
08: import org.apache.commons.javaflow.ContinuationClassLoader;
09: import org.apache.commons.javaflow.bytecode.transformation.bcel.BcelClassTransformer;
10: import test.ClickTest;
11:
12: import java.io.File;
13: import java.lang.reflect.Constructor;
14: import java.net.URLClassLoader;
15:
16: /**
17: * @author Kohsuke Kawaguchi
18: */
19: public abstract class Launcher {
20:
21: /**
22: * The {@link ClassLoader} that can load instrumented conversation classes.
23: */
24: protected final ClassLoader classLoader;
25:
26: /**
27: * Dalma engine.
28: */
29: protected final Engine engine;
30:
31: protected Launcher(String[] args) throws Exception {
32: BcelClassTransformer.debug = true;
33:
34: // creates a new class loader that loads test classes with javaflow enhancements
35: URLClassLoader baseLoader = (URLClassLoader) Launcher.class
36: .getClassLoader();
37: classLoader = new ContinuationClassLoader(
38: baseLoader.getURLs(),
39: new MaskingClassLoader(ClickTest.class.getClassLoader()));
40:
41: File root = new File("dalma-test");
42: root.mkdirs();
43: if (args.length > 0) {
44: // start fresh
45: System.out.println("Starting fresh");
46: Util.deleteContentsRecursive(root);
47: } else {
48: System.out.println("Picking up existing conversations");
49: }
50:
51: engine = new EngineImpl(root, classLoader,
52: new ThreadPoolExecutor(1));
53: setUpEndPoints();
54:
55: if (args.length > 0) {
56: init();
57: }
58:
59: System.out.println("We have "
60: + engine.getConversations().size() + " conversations");
61: }
62:
63: protected void setUpEndPoints() throws Exception {
64: }
65:
66: /**
67: * Creates initial set of conversations.
68: */
69: protected abstract void init() throws Exception;
70:
71: /**
72: * Creates a new conversation instance with given parameters.
73: */
74: protected Conversation createConversation(
75: Class<? extends Runnable> c, Object... args)
76: throws Exception {
77: Class clazz = classLoader.loadClass(c.getName());
78: Runnable r = (Runnable) findConstructor(clazz, args.length)
79: .newInstance(args);
80:
81: return engine.createConversation(r);
82: }
83:
84: private Constructor findConstructor(Class c, int length)
85: throws NoSuchMethodException {
86: for (Constructor init : c.getConstructors())
87: if (init.getParameterTypes().length == length)
88: return init;
89:
90: throw new NoSuchMethodException(
91: "Unable to find a matching constructor on "
92: + c.getName());
93: }
94: }
|