X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fxt%2FPrevalence.java;fp=src%2Forg%2Fibex%2Fxt%2FPrevalence.java;h=ef871474e9cc694058d1623e62a257eeb4ffb387;hb=5365f47787b1b4eeca31ad5da2373237371e264e;hp=0000000000000000000000000000000000000000;hpb=308cdcc279f012c0b69b871f81ec1c11c703bd13;p=org.ibex.xt.git diff --git a/src/org/ibex/xt/Prevalence.java b/src/org/ibex/xt/Prevalence.java new file mode 100644 index 0000000..ef87147 --- /dev/null +++ b/src/org/ibex/xt/Prevalence.java @@ -0,0 +1,89 @@ +package org.ibex.xt; +import org.ibex.js.*; +import org.ibex.util.*; +import org.ibex.io.*; +import java.io.*; +import java.net.*; +import java.util.*; +import javax.servlet.*; +import javax.servlet.http.*; +import com.thoughtworks.xstream.*; +import org.prevayler.*; +import org.prevayler.implementation.snapshot.*; + +public class Prevalence { + + static final Hashtable prevaylers = new Hashtable(); + + public static void destroy(ServletContext cx, Prevayler prevayler) { try { + prevaylers.remove(cx); + prevayler.takeSnapshot(); + prevayler.close(); + } catch (Exception e) { e.printStackTrace(); } } + + private static class SnapshotThread extends Thread { + ServletContext cx; + public SnapshotThread(ServletContext cx) { this.cx = cx; } + public void run() { + try { + Thread.sleep(10000); + Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx); + if (privatePrevayler == null) return; + privatePrevayler.takeSnapshot(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public static Prevayler getPrevayler(ServletContext cx) { + try { + Prevayler prevayler; + synchronized(cx) { + prevayler = (Prevayler)prevaylers.get(cx); + if (prevayler == null) { + PrevaylerFactory pf = new PrevaylerFactory(); + String base = cx.getRealPath("/") + "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; + } + }; + System.err.println("configuring with " + manager); + pf.configureSnapshotManager(manager); + //pf.configureClassLoader(JSTransaction.class.getClassLoader()); + prevayler = pf.create(); + prevaylers.put(cx, prevayler); + new SnapshotThread(cx).start(); + } + } + return prevayler; + } catch (Exception e) { throw new RuntimeException(e); } } + + public static class JSTransaction implements Transaction { + public static final long serialVersionUid = 0xfb2aa281; + private JS js; + public JSTransaction(JS js) { this.js = js; } + public void executeOn(Object o, Date now) { + try { + js.call(o, new JSDate(now.getTime()), null, null, 2); + } catch (Exception e) { throw new RuntimeException(e); } + } + } + + public static class JSQuery implements Query { + public static final long serialVersionUid = 0xfb2aa282; + private JS js; + public JSQuery(JS js) { this.js = js; } + public Object query(Object o, Date now) { + try { + return js.call(o, new JSDate(now.getTime()), null, null, 2); + } catch (Exception e) { throw new RuntimeException(e); } + } + } +}