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);
53 XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
54 protected XStream createXStream() {
55 XStream xstream = new XStream();
56 xstream.alias("js", JS.class);
57 xstream.alias("jsdate", JSDate.class);
61 System.err.println("configuring with " + manager);
62 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.elementAt(i))) continue;
84 newscope.put(v.elementAt(i), JS.getParentScope(js).get(v.elementAt(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.elementAt(0));
92 Object b = v.size() <= 1 ? null : newscope.get(v.elementAt(1));
93 Object c = v.size() <= 2 ? null : newscope.get(v.elementAt(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.elementAt(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); }