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.configureClassLoader(JSTransaction.class.getClassLoader());
60 prevayler = pf.create();
61 prevaylers.put(cx, prevayler);
62 new SnapshotThread(cx).start();
66 } catch (Exception e) { throw new RuntimeException(e); } }
68 public static class JSTransaction implements Transaction {
69 public static final long serialVersionUid = 0xfb2aa281;
71 public JSTransaction(JS js) { this.js = js; }
72 public void executeOn(Object o, Date now) {
74 js.call(o, new JSDate(now.getTime()), null, null, 2);
75 } catch (Exception e) { throw new RuntimeException(e); }
79 public static class JSQuery implements Query {
80 public static final long serialVersionUid = 0xfb2aa282;
82 public JSQuery(JS js) { this.js = js; }
83 public Object query(Object o, Date now) {
85 return js.call(o, new JSDate(now.getTime()), null, null, 2);
86 } catch (Exception e) { throw new RuntimeException(e); }