ef871474e9cc694058d1623e62a257eeb4ffb387
[org.ibex.xt-crawshaw.git] / src / org / ibex / xt / Prevalence.java
1 package org.ibex.xt;
2 import org.ibex.js.*;
3 import org.ibex.util.*;
4 import org.ibex.io.*;
5 import java.io.*;
6 import java.net.*;
7 import java.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.*;
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                     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);
54                                 return xstream;
55                             }
56                         };
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();
63                 }
64             }
65             return prevayler;
66         } catch (Exception e) { throw new RuntimeException(e); } }
67
68     public static class JSTransaction implements Transaction {
69         public static final long serialVersionUid = 0xfb2aa281;
70         private JS js;
71         public JSTransaction(JS js) { this.js = js; }
72         public void executeOn(Object o, Date now) {
73             try {
74                 js.call(o, new JSDate(now.getTime()), null, null, 2);
75             } catch (Exception e) { throw new RuntimeException(e); }
76         }
77     }
78
79     public static class JSQuery implements Query {
80         public static final long serialVersionUid = 0xfb2aa282;
81         private JS js;
82         public JSQuery(JS js) { this.js = js; }
83         public Object query(Object o, Date now) {
84             try {
85                 return js.call(o, new JSDate(now.getTime()), null, null, 2);
86             } catch (Exception e) { throw new RuntimeException(e); }
87         }
88     }
89 }