5 import javax.servlet.ServletContext;
7 import org.ibex.util.*;
10 import org.prevayler.*;
11 import org.prevayler.implementation.snapshot.*;
12 //import com.thoughtworks.xstream.*; // FIXME find
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);
50 XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
51 protected XStream createXStream() {
52 XStream xstream = new XStream();
53 xstream.alias("js", JS.class);
54 xstream.alias("jsdate", JSDate.class);
58 System.err.println("configuring with " + manager);
59 pf.configureSnapshotManager(manager);
61 pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
62 //pf.configureClassLoader(JSTransaction.class.getClassLoader());
63 prevayler = pf.create();
64 prevaylers.put(cx, prevayler);
65 new SnapshotThread(cx).start();
69 } catch (Exception e) { throw new RuntimeException(e); } }
71 public static class JSTransaction implements Transaction {
72 public static final long serialVersionUid = 0xfb2aa281;
74 public JSTransaction(JS js) throws JSExn { this.js = js; }
75 public void executeOn(Object o, Date now) {
77 JSScope scope = JS.getParentScope(js);
78 scope.put("prevalent", o);
79 scope.put("now", new JSDate(now.getTime()));
80 js.call(null, null, null, null, 0);
81 } catch (Exception e) { throw new RuntimeException(e); }
85 public static class JSQuery implements Query {
86 public static final long serialVersionUid = 0xfb2aa282;
89 public JSQuery(JS js, Object a) { this.js = js; this.a = a; }
90 public Object query(Object o, Date now) {
92 return js.call(o, a, new JSDate(now.getTime()), null, 3);
93 } catch (Exception e) { throw new RuntimeException(e); }