licensing update to APSL 2.0
[org.ibex.xt.git] / src / org / ibex / xt / Prevalence.java
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.
4
5 package org.ibex.xt;
6 import org.ibex.js.*;
7 import org.ibex.util.*;
8 import org.ibex.io.*;
9 import java.io.*;
10 import java.net.*;
11 import java.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.*;
17
18 public class Prevalence {
19
20     static final Hashtable prevaylers = new Hashtable();
21
22     public static void destroy(ServletContext cx, Prevayler prevayler) { try {
23         prevaylers.remove(cx);
24         prevayler.takeSnapshot();
25         prevayler.close();
26     } catch (Exception e) { e.printStackTrace(); } }
27
28     private static class SnapshotThread extends Thread {
29         ServletContext cx;
30         public SnapshotThread(ServletContext cx) { this.cx = cx; }
31         public void run() {
32             try {
33                 Thread.sleep(10000);
34                 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
35                 if (privatePrevayler == null) return;
36                 privatePrevayler.takeSnapshot();
37             } catch (Exception e) {
38                 e.printStackTrace();
39             }
40         }
41     }
42
43     public static Prevayler getPrevayler(ServletContext cx) {
44         try {
45             Prevayler prevayler;
46             synchronized(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);
58                                 return xstream;
59                             }
60                         };
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();
68                 }
69             }
70             return prevayler;
71         } catch (Exception e) { throw new RuntimeException(e); } }
72
73     public static class JSTransaction implements Transaction {
74         public static final long serialVersionUid = 0xfb2aa281;
75         private JS js;
76         JSScope newscope;
77         Vec v;
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)));
85             }
86         }
87         public void executeOn(Object o, Date now) {
88             try {
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); }
98         }
99     }
100
101     public static class JSQuery implements Query {
102         public static final long serialVersionUid = 0xfb2aa282;
103         private JS js;
104         private Object a;
105         public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
106         public Object query(Object o, Date now) {
107             try {
108                 return js.call(o, a, new JSDate(now.getTime()), null, 3);
109             } catch (Exception e) { throw new RuntimeException(e); }
110         }
111     }
112 }