1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 import org.ibex.util.*;
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14 import com.thoughtworks.xstream.*;
15 import org.prevayler.*;
16 import org.prevayler.implementation.snapshot.*;
18 public class Prevalence {
20 static final Hashtable prevaylers = new Hashtable();
22 public static void destroy(ServletContext cx, Prevayler prevayler) { try {
23 prevaylers.remove(cx);
24 prevayler.takeSnapshot();
26 } catch (Exception e) { e.printStackTrace(); } }
28 private static class SnapshotThread extends Thread {
30 public SnapshotThread(ServletContext cx) { this.cx = cx; }
34 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
35 if (privatePrevayler == null) return;
36 privatePrevayler.takeSnapshot();
37 } catch (Exception e) {
43 public static Prevayler getPrevayler(ServletContext cx) {
47 prevayler = (Prevayler)prevaylers.get(cx);
48 if (prevayler == null) {
49 PrevaylerFactory pf = new PrevaylerFactory();
50 String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
51 System.err.println("prevayling to " + base);
52 pf.configurePrevalenceBase(base);
54 XmlSnapshotManager manager = new XmlSnapshotManager(new JS(), base, null); {
55 protected XStream createXStream() {
56 XStream xstream = new XStream();
57 xstream.alias("js", JS.class);
58 xstream.alias("jsdate", JSDate.class);
62 System.err.println("configuring with " + manager);
63 pf.configureSnapshotManager(manager);
65 pf.configureSnapshotManager(new SnapshotManager(new JS.Obj(), base));
66 //pf.configureClassLoader(JSTransaction.class.getClassLoader());
67 prevayler = pf.create();
68 prevaylers.put(cx, prevayler);
69 new SnapshotThread(cx).start();
73 } catch (Exception e) { throw new RuntimeException(e); } }
75 public static class JSTransaction implements Transaction {
76 public static final long serialVersionUid = 0xfb2aa281;
78 Template.Scope newscope;
80 public JSTransaction(JS js) throws JSExn {
81 newscope = new Template.Scope(null);
82 this.js = JSU.cloneWithNewGlobalScope(js, newscope);
83 v = this.js.getFormalArgs();
84 for(int i=0; i<v.length; i++) {
85 if (JSU.S("prevalent").equals(v[i])) continue;
86 newscope.put(JSU.S(v[i]), js.get(JSU.S(v[i])));
89 public void executeOn(Object o, Date now) {
91 newscope.put(JSU.S("prevalent"), (JS)o);
92 newscope.put(JSU.S("now"), new JSDate(now.getTime()));
93 JS[] args = new JS[v.length];
94 for(int i=0; i<v.length; i++) args[i] = JSU.S(v[i]);
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 = JSU.cloneWithNewGlobalScope(js, null); this.a = a; }
105 public Object query(Object o, Date now) {
107 return js.call(null, new JS[] { (JS)o, (JS)a, new JSDate(now.getTime()) });
108 } catch (Exception e) { throw new RuntimeException(e); }