3 import org.ibex.util.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10 import com.thoughtworks.xstream.*;
11 import org.prevayler.*;
12 import org.prevayler.implementation.snapshot.*;
14 public class Prevalence {
16 static final Hashtable prevaylers = new Hashtable();
18 public static void destroy(ServletContext cx, Prevayler prevayler) { try {
19 prevaylers.remove(cx);
20 prevayler.takeSnapshot();
22 } catch (Exception e) { e.printStackTrace(); } }
24 private static class SnapshotThread extends Thread {
26 public SnapshotThread(ServletContext cx) { this.cx = cx; }
30 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
31 if (privatePrevayler == null) return;
32 privatePrevayler.takeSnapshot();
33 } catch (Exception e) {
39 public static Prevayler getPrevayler(ServletContext cx) {
43 prevayler = (Prevayler)prevaylers.get(cx);
44 if (prevayler == null) {
45 PrevaylerFactory pf = new PrevaylerFactory();
46 String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
47 System.err.println("prevayling to " + base);
48 pf.configurePrevalenceBase(base);
49 XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
50 protected XStream createXStream() {
51 XStream xstream = new XStream();
52 xstream.alias("js", JS.class);
53 xstream.alias("jsdate", JSDate.class);
57 System.err.println("configuring with " + manager);
58 pf.configureSnapshotManager(manager);
59 //pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
60 //pf.configureClassLoader(JSTransaction.class.getClassLoader());
61 prevayler = pf.create();
62 prevaylers.put(cx, prevayler);
63 new SnapshotThread(cx).start();
67 } catch (Exception e) { throw new RuntimeException(e); } }
69 public static class JSTransaction implements Transaction {
70 public static final long serialVersionUid = 0xfb2aa281;
74 public JSTransaction(JS js) throws JSExn {
75 newscope = new JSScope(null);
76 this.js = JS.cloneWithNewParentScope(js, newscope);
77 v = JS.getFormalArgs(this.js);
78 for(int i=0; i<v.size(); i++) {
79 if ("prevalent".equals(v.elementAt(i))) continue;
80 newscope.put(v.elementAt(i), JS.getParentScope(js).get(v.elementAt(i)));
83 public void executeOn(Object o, Date now) {
85 newscope.put("prevalent", o);
86 newscope.put("now", new JSDate(now.getTime()));
87 Object a = v.size() <= 0 ? null : newscope.get(v.elementAt(0));
88 Object b = v.size() <= 1 ? null : newscope.get(v.elementAt(1));
89 Object c = v.size() <= 2 ? null : newscope.get(v.elementAt(2));
90 Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
91 for(int i=3; i<v.size(); i++) rest[i-3] = v.elementAt(i);
92 js.call(a, b, c, rest, v.size());
93 } catch (Exception e) { throw new RuntimeException(e); }
97 public static class JSQuery implements Query {
98 public static final long serialVersionUid = 0xfb2aa282;
101 public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
102 public Object query(Object o, Date now) {
104 return js.call(o, a, new JSDate(now.getTime()), null, 3);
105 } catch (Exception e) { throw new RuntimeException(e); }