move transaction scope management into Template.java
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / Prevalence.java
1 package org.ibex.xt;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.ServletContext;
6
7 import org.ibex.util.*;
8 import org.ibex.js.*;
9
10 import org.prevayler.*;
11 import org.prevayler.implementation.snapshot.*;
12 //import com.thoughtworks.xstream.*; // FIXME find
13
14 public class Prevalence {
15
16     static final Hashtable prevaylers = new Hashtable();
17
18     public static void destroy(ServletContext cx, Prevayler prevayler) { try {
19         prevaylers.remove(cx);
20         prevayler.takeSnapshot();
21         prevayler.close();
22     } catch (Exception e) { e.printStackTrace(); } }
23
24     private static class SnapshotThread extends Thread {
25         ServletContext cx;
26         public SnapshotThread(ServletContext cx) { this.cx = cx; }
27         public void run() {
28             try {
29                 Thread.sleep(10000);
30                 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
31                 if (privatePrevayler == null) return;
32                 privatePrevayler.takeSnapshot();
33             } catch (Exception e) {
34                 e.printStackTrace();
35             }
36         }
37     }
38
39     public static Prevayler getPrevayler(ServletContext cx) {
40         try {
41             Prevayler prevayler;
42             synchronized(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                     /*
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);
55                                 return xstream;
56                             }
57                         };
58                     System.err.println("configuring with " + manager);
59                     pf.configureSnapshotManager(manager);
60                     */
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();
66                 }
67             }
68             return prevayler;
69         } catch (Exception e) { throw new RuntimeException(e); } }
70
71     public static class JSTransaction implements Transaction {
72         public static final long serialVersionUid = 0xfb2aa281;
73         private JS js;
74         public JSTransaction(JS js) throws JSExn { this.js = js; }
75         public void executeOn(Object o, Date now) {
76             try {
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); }
82         }
83     }
84
85     public static class JSQuery implements Query {
86         public static final long serialVersionUid = 0xfb2aa282;
87         private JS js;
88         private Object a;
89         public JSQuery(JS js, Object a) { this.js = js; this.a = a; }
90         public Object query(Object o, Date now) {
91             try {
92                 return js.call(o, a, new JSDate(now.getTime()), null, 3);
93             } catch (Exception e) { throw new RuntimeException(e); }
94         }
95     }
96 }