01: package dalma.test;
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:
11: import java.io.File;
12: import java.lang.reflect.Constructor;
13: import java.net.URLClassLoader;
14:
15: /**
16: * @author Kohsuke Kawaguchi
17: */
18: public abstract class Launcher {
19:
20: /**
21: * The {@link ClassLoader} that can load instrumented conversation classes.
22: */
23: protected final ClassLoader classLoader;
24:
25: /**
26: * Dalma engine.
27: */
28: protected final Engine engine;
29:
30: protected Launcher(String[] args) throws Exception {
31: BcelClassTransformer.debug = true;
32:
33: // creates a new class loader that loads test classes with javaflow enhancements
34: URLClassLoader baseLoader = (URLClassLoader) Launcher.class
35: .getClassLoader();
36: classLoader = new ContinuationClassLoader(baseLoader.getURLs(),
37: new MaskingClassLoader(baseLoader));
38:
39: File root = new File("dalma-test");
40: root.mkdirs();
41: if (args.length > 0) {
42: // start fresh
43: System.out.println("Starting fresh");
44: Util.deleteContentsRecursive(root);
45: } else {
46: System.out.println("Picking up existing conversations");
47: }
48:
49: engine = new EngineImpl(root, classLoader,
50: new ThreadPoolExecutor(1));
51: setUpEndPoints();
52: engine.start();
53:
54: if (args.length > 0) {
55: init();
56: }
57:
58: System.out.println("We have "
59: + engine.getConversations().size() + " conversations");
60: }
61:
62: protected void setUpEndPoints() throws Exception {
63: }
64:
65: /**
66: * Creates initial set of conversations.
67: */
68: protected void init() throws Exception {
69: }
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: }
|