added Form, other minor changes
[org.ibex.xt.git] / src / org / ibex / xt / Prevalence.java
index 5fb807c..2bfbfb1 100644 (file)
@@ -13,10 +13,30 @@ import javax.servlet.*;
 import javax.servlet.http.*;
 import com.thoughtworks.xstream.*;
 import org.prevayler.*;
-import org.prevayler.implementation.snapshot.*;
+import org.prevayler.implementation.snapshot.XmlSnapshotManager;
 
 public class Prevalence {
 
+    public static abstract class PrevalentServlet extends HttpServlet {
+       protected Hashtable prevalent;
+       protected Prevayler prevayler;
+       protected ServletContext cx = null;
+       public void destroy() { try {
+           synchronized(this.getClass()) {
+               Prevayler privatePrevayler = prevayler;
+               if (prevayler == null) return;
+               prevayler = null;
+               Prevalence.destroy(cx, prevayler);
+           }
+       } catch (Exception e) { e.printStackTrace(); } }
+       
+       public void init(ServletConfig sc) throws ServletException {
+           cx = sc.getServletContext();
+           prevayler = Prevalence.getPrevayler(cx);
+           prevalent = (Hashtable)prevayler.prevalentSystem();
+       }
+    }
+
     static final Hashtable prevaylers = new Hashtable();
 
     public static void destroy(ServletContext cx, Prevayler prevayler) { try {
@@ -47,21 +67,12 @@ public class Prevalence {
                 prevayler = (Prevayler)prevaylers.get(cx);
                 if (prevayler == null) {
                     PrevaylerFactory pf = new PrevaylerFactory();
-                    String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
+                    String base = cx.getRealPath("/") + File.separatorChar + "WEB-INF" + File.separatorChar + "prevalent";
                     System.err.println("prevayling to " + base);
                     pf.configurePrevalenceBase(base);
-                    XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
-                            protected XStream createXStream() {
-                                XStream xstream = new XStream();
-                                xstream.alias("js", JS.class);
-                                xstream.alias("jsdate", JSDate.class);
-                                return xstream;
-                            }
-                        };
+                    XmlSnapshotManager manager = new XmlSnapshotManager(new Hashtable(), base);
                     System.err.println("configuring with " + manager);
                     pf.configureSnapshotManager(manager);
-                    //pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
-                    //pf.configureClassLoader(JSTransaction.class.getClassLoader());
                     prevayler = pf.create();
                     prevaylers.put(cx, prevayler);
                     new SnapshotThread(cx).start();
@@ -73,27 +84,24 @@ public class Prevalence {
     public static class JSTransaction implements Transaction {
         public static final long serialVersionUid = 0xfb2aa281;
         private JS js;
-        JSScope newscope;
-        Vec v;
+        Scope newscope;
+        String[] v;
         public JSTransaction(JS js) throws JSExn {
-            newscope = new JSScope(null);
-            this.js = JS.cloneWithNewParentScope(js, newscope);
-            v = JS.getFormalArgs(this.js);
-            for(int i=0; i<v.size(); i++) {
-                if ("prevalent".equals(v.elementAt(i))) continue;
-                newscope.put(v.elementAt(i), JS.getParentScope(js).get(v.elementAt(i)));
+            newscope = new Scope(null);
+            this.js = JSU.cloneWithNewGlobalScope(js, newscope);
+            v = this.js.getFormalArgs();
+            for(int i=0; i<v.length; i++) {
+                if (JSU.S("prevalent").equals(v[i])) continue;
+                newscope.put(JSU.S(v[i]), js.get(JSU.S(v[i])));
             }
         }
         public void executeOn(Object o, Date now) {
             try {
-                newscope.put("prevalent", o);
-                newscope.put("now", new JSDate(now.getTime()));
-                Object a = v.size() <= 0 ? null : newscope.get(v.elementAt(0));
-                Object b = v.size() <= 1 ? null : newscope.get(v.elementAt(1));
-                Object c = v.size() <= 2 ? null : newscope.get(v.elementAt(2));
-                Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
-                for(int i=3; i<v.size(); i++) rest[i-3] = v.elementAt(i);
-                js.call(a, b, c, rest, v.size());
+                newscope.put(JSU.S("prevalent"), (JS)o);
+                newscope.put(JSU.S("now"), new JSDate(now.getTime()));
+                JS[] args = new JS[v.length];
+                for(int i=0; i<v.length; i++) args[i] = JSU.S(v[i]);
+                js.call(null, args);
             } catch (Exception e) { throw new RuntimeException(e); }
         }
     }
@@ -102,10 +110,10 @@ public class Prevalence {
         public static final long serialVersionUid = 0xfb2aa282;
         private JS js;
         private Object a;
-        public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
+        public JSQuery(JS js, Object a) { this.js = JSU.cloneWithNewGlobalScope(js, null); this.a = a; }
         public Object query(Object o, Date now) {
             try {
-                return js.call(o, a, new JSDate(now.getTime()), null, 3);
+                return js.call(null, new JS[] { (JS)o, (JS)a, new JSDate(now.getTime()) });
             } catch (Exception e) { throw new RuntimeException(e); }
         }
     }