6 import javax.servlet.ServletContext;
8 import org.ibex.util.*;
11 import org.prevayler.*;
12 import org.prevayler.implementation.snapshot.*;
13 //import com.thoughtworks.xstream.*; // FIXME find
15 public class Prevalence {
17 static final Hashtable prevaylers = new Hashtable();
19 public static void destroy(ServletContext cx, Prevayler prevayler) { try {
20 prevaylers.remove(cx);
21 prevayler.takeSnapshot();
23 } catch (Exception e) { e.printStackTrace(); } }
25 private static class SnapshotThread extends Thread {
27 public SnapshotThread(ServletContext cx) { this.cx = cx; }
31 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
32 if (privatePrevayler == null) return;
33 privatePrevayler.takeSnapshot();
34 } catch (Exception e) {
40 public static Prevayler getPrevayler(ServletContext cx) {
44 prevayler = (Prevayler)prevaylers.get(cx);
45 if (prevayler == null) {
46 PrevaylerFactory pf = new PrevaylerFactory();
47 String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
48 System.err.println("prevayling to " + base);
49 pf.configurePrevalenceBase(base);
51 XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
52 protected XStream createXStream() {
53 XStream xstream = new XStream();
54 xstream.alias("js", JS.class);
55 xstream.alias("jsdate", JSDate.class);
59 System.err.println("configuring with " + manager);
60 pf.configureSnapshotManager(manager);
62 pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
63 //pf.configureClassLoader(JSTransaction.class.getClassLoader());
64 prevayler = pf.create();
65 prevaylers.put(cx, prevayler);
66 new SnapshotThread(cx).start();
70 } catch (Exception e) { throw new RuntimeException(e); } }
72 public static class JSTransaction implements Transaction {
73 public static final long serialVersionUid = 0xfb2aa281;
77 public JSTransaction(JS js) throws JSExn {
78 newscope = new JSScope(null);
79 this.js = JS.cloneWithNewParentScope(js, newscope);
80 v = JS.getFormalArgs(this.js);
81 for(int i=0; i<v.size(); i++) {
82 if ("prevalent".equals(v.get(i))) continue;
83 newscope.put(v.get(i), JS.getParentScope(js).get(v.get(i)));
86 public void executeOn(Object o, Date now) {
88 newscope.put("prevalent", o);
89 newscope.put("now", new JSDate(now.getTime()));
90 Object a = v.size() <= 0 ? null : newscope.get(v.get(0));
91 Object b = v.size() <= 1 ? null : newscope.get(v.get(1));
92 Object c = v.size() <= 2 ? null : newscope.get(v.get(2));
93 Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
94 for(int i=3; i<v.size(); i++) rest[i-3] = v.get(i);
95 js.call(a, b, c, rest, v.size());
96 } catch (Exception e) { throw new RuntimeException(e); }
100 public static class JSQuery implements Query {
101 public static final long serialVersionUid = 0xfb2aa282;
104 public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
105 public Object query(Object o, Date now) {
107 return js.call(o, a, new JSDate(now.getTime()), null, 3);
108 } catch (Exception e) { throw new RuntimeException(e); }