6 import javax.servlet.ServletContext; // FIXME: remove references
12 import org.prevayler.*;
13 import org.prevayler.implementation.snapshot.*;
14 //import com.thoughtworks.xstream.*; // FIXME find
16 public class Prevalence {
18 static final Hashtable prevaylers = new Hashtable();
20 public static void destroy(ServletContext cx, Prevayler prevayler) { try {
21 prevaylers.remove(cx);
22 prevayler.takeSnapshot();
24 } catch (Exception e) { e.printStackTrace(); } }
26 private static class SnapshotThread extends Thread {
28 public SnapshotThread(ServletContext cx) { this.cx = cx; }
32 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
33 if (privatePrevayler == null) return;
34 privatePrevayler.takeSnapshot();
35 } catch (Exception e) {
41 public static Prevayler getPrevayler(ServletContext cx) {
45 prevayler = (Prevayler)prevaylers.get(cx);
46 if (prevayler == null) {
47 PrevaylerFactory pf = new PrevaylerFactory();
48 String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
49 System.err.println("prevayling to " + base);
50 pf.configurePrevalenceBase(base);
52 XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
53 protected XStream createXStream() {
54 XStream xstream = new XStream();
55 xstream.alias("js", JS.class);
56 xstream.alias("jsdate", JSDate.class);
60 System.err.println("configuring with " + manager);
61 pf.configureSnapshotManager(manager);
63 pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
64 //pf.configureClassLoader(JSTransaction.class.getClassLoader());
65 prevayler = pf.create();
66 prevaylers.put(cx, prevayler);
67 new SnapshotThread(cx).start();
71 } catch (Exception e) { throw new RuntimeException(e); } }
73 public static class JSTransaction implements Transaction {
74 public static final long serialVersionUid = 0xfb2aa281;
78 public JSTransaction(JS js) throws JSExn {
79 newscope = new JSScope(null);
80 this.js = JS.cloneWithNewParentScope(js, newscope);
81 v = JS.getFormalArgs(this.js);
82 for(int i=0; i<v.size(); i++) {
83 if ("prevalent".equals(v.get(i))) continue;
84 newscope.put(v.get(i), JS.getParentScope(js).get(v.get(i)));
87 public void executeOn(Object o, Date now) {
89 newscope.put("prevalent", o);
90 newscope.put("now", new JSDate(now.getTime()));
91 Object a = v.size() <= 0 ? null : newscope.get(v.get(0));
92 Object b = v.size() <= 1 ? null : newscope.get(v.get(1));
93 Object c = v.size() <= 2 ? null : newscope.get(v.get(2));
94 Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
95 for(int i=3; i<v.size(); i++) rest[i-3] = v.get(i);
96 js.call(a, b, c, rest, v.size());
97 } catch (Exception e) { throw new RuntimeException(e); }
101 public static class JSQuery implements Query {
102 public static final long serialVersionUid = 0xfb2aa282;
105 public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
106 public Object query(Object o, Date now) {
108 return js.call(o, a, new JSDate(now.getTime()), null, 3);
109 } catch (Exception e) { throw new RuntimeException(e); }