[CVS nanning] Added CurrentPrevayler.getClock() this method should be used to fetch time in your transactions. (the method Prevayler.clock() should NOT be used in transactions as stated in the javadoc

Lecando Shared <[email protected]> Wed, 8 Oct 2003 02:25:40 -0500
Newsgroups gmane.comp.java.nanning.devel
Message-ID <[email protected]>
<html>
<head>
<style><!--
  body {background-color:#ffffff;}
  .file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
  .pathname {font-family:monospace; float:right;}
  .fileheader {margin-bottom:.5em;}
  .diff {margin:0;}
  .tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
  .tasklist ul {margin-top:0;margin-bottom:0;}
  tr.alt {background-color:#eeeeee}
  #added {background-color:#ddffdd;}
  #addedchars {background-color:#99ff99;font-weight:bolder;}
  tr.alt #added {background-color:#ccf7cc;}
  #removed {background-color:#ffdddd;}
  #removedchars {background-color:#ff9999;font-weight:bolder;}
  tr.alt #removed {background-color:#f7cccc;}
  #info {color:#888888;}
  #context {background-color:#eeeeee;}
  td {padding-left:.3em;padding-right:.3em;}
  tr.head {border-bottom-width:1px;border-bottom-style:solid;}
  tr.head td {padding:0;padding-top:.2em;}
  .task {background-color:#ffff00;}
  .comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
  .error {color:red;}
  hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>nanning/src/frameworks/prevayler/src</tt></b> on <span id="info">MAIN</span></td></tr>
<tr><td><tt>main/org/codehaus/nanning/prevayler/<a href="#file1">CurrentPrevayler.java</a></tt></td><td align="right" id="added">+24</td><td></td><td nowrap="nowrap" align="center">1.3 -&gt; 1.4</td></tr>
<tr class="alt"><td><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/<a href="#file2">InvokeTransaction.java</a></tt></td><td align="right" id="added">+2</td><td></td><td nowrap="nowrap" align="center">1.4 -&gt; 1.5</td></tr>
<tr><td><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/<a href="#file3"><span id="removed">InvokeCommand.java</span></a></tt></td><td></td><td align="right" id="removed">-83</td><td nowrap="nowrap">1.4 removed</td></tr>
<tr class="alt"><td><tt>test/org/codehaus/nanning/prevayler/<a href="#file4">PrevaylerTest.java</a></tt></td><td align="right" id="added">+50</td><td></td><td nowrap="nowrap" align="center">1.10 -&gt; 1.11</td></tr>
<tr><td></td><td align="right" id="added">+76</td><td align="right" id="removed">-83</td><td></td></tr>
</table>
<small id="info">1 removed + 3 modified, total 4 files</small><br />
<pre class="comment">
Added CurrentPrevayler.getClock() this method should be used to fetch time in your transactions. (the method Prevayler.clock() should NOT be used in transactions as stated in the javadoc for the method)
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname">nanning/src/frameworks/prevayler/src/main/org/codehaus/nanning/prevayler<br /></span>
<div class="fileheader"><big><b>CurrentPrevayler.java</b></big> <small id="info">1.3 -&gt; 1.4</small></div>
<pre class="diff"><small id="info">diff -u -r1.3 -r1.4
--- CurrentPrevayler.java	5 Sep 2003 07:56:42 -0000	1.3
+++ CurrentPrevayler.java	8 Oct 2003 07:25:40 -0000	1.4
@@ -5,8 +5,10 @@
</small></pre><pre class="diff" id="context"> import org.codehaus.nanning.AssertionException;
 
 import java.util.Stack;
</pre><pre class="diff" id="added">+import java.util.Date;
</pre><pre class="diff" id="context"> 
 public class CurrentPrevayler {
</pre><pre class="diff" id="added">+    private static ThreadLocal clocks = new InheritableThreadLocal();
</pre><pre class="diff" id="context">     private static ThreadLocal currentPrevayler = new InheritableThreadLocal();
     private static ThreadLocal currentSystems = new ThreadLocal();
 
</pre><pre class="diff"><small id="info">@@ -104,4 +106,26 @@
</small></pre><pre class="diff" id="context">         return currentPrevayler.get() != null;
     }
 
</pre><pre class="diff" id="added">+    public static void setClock(Date clock) {
+        if (clock == null) {
+            throw new AssertionException("Cant set null clock, use clearClock() instead");
+        }
+        clocks.set(clock);
+    }
+
+    public static Date getClock() {
+        Date date = (Date) clocks.get();
+        if (date == null) {
+            throw new AssertionException("No clock is set");
+        }
+        return date;
+    }
+
+    public static void clearClock() {
+        clocks.set(null);
+    }
+
+    public static boolean hasClock() {
+        return clocks.get() != null;
+    }
</pre><pre class="diff" id="context"> }
</pre></div>
<hr /><a name="file2" /><div class="file">
<span class="pathname">nanning/src/frameworks/prevayler/src/main/org/codehaus/nanning/prevayler<br /></span>
<div class="fileheader"><big><b>InvokeTransaction.java</b></big> <small id="info">1.4 -&gt; 1.5</small></div>
<pre class="diff"><small id="info">diff -u -r1.4 -r1.5
--- InvokeTransaction.java	22 Sep 2003 14:36:39 -0000	1.4
+++ InvokeTransaction.java	8 Oct 2003 07:25:40 -0000	1.5
@@ -59,6 +59,7 @@
</small></pre><pre class="diff" id="context"> 
         registerObjectIDForSystem(system);
         try {
</pre><pre class="diff" id="added">+            CurrentPrevayler.setClock(executionTime);
</pre><pre class="diff" id="context"> 
             AuthenticatedCall call = unmarshalCall();
             logInvocation(call);
</pre><pre class="diff"><small id="info">@@ -70,6 +71,7 @@
</small></pre><pre class="diff" id="context">             handleException(e);
             throw e;
         } finally {
</pre><pre class="diff" id="added">+            CurrentPrevayler.clearClock();
</pre><pre class="diff" id="context">             CurrentPrevayler.exitTransaction();
         }
     }
</pre></div>
<hr /><a name="file3" /><div class="file">
<span class="pathname" id="removed">nanning/src/frameworks/prevayler/src/main/org/codehaus/nanning/prevayler<br /></span>
<div class="fileheader" id="removed"><big><b>InvokeCommand.java</b></big> <small id="info">removed after 1.4</small></div>
<pre class="diff"><small id="info">diff -N InvokeCommand.java
--- InvokeCommand.java	9 Sep 2003 11:10:27 -0000	1.4
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,83 +0,0 @@
</small></pre><pre class="diff" id="removed">-package org.codehaus.nanning.prevayler;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.codehaus.nanning.Invocation;
-import org.codehaus.nanning.AssertionException;
-import org.prevayler.TransactionWithQuery;
-
-import javax.security.auth.Subject;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-
-public class InvokeCommand implements TransactionWithQuery {
-    private static final Log logger = LogFactory.getLog(InvokeCommand.class);
-    static final long serialVersionUID = 320681517664792343L;
-
-    private AuthenticatedCall call;
-
-    public InvokeCommand(Invocation invocation) throws Exception {
-        call = new IdentifyingCall(invocation);
-    }
-
-    public Object executeAndQuery(Object system, Date executionTime) throws Exception {
-
-        CurrentPrevayler.enterTransaction(system);
-
-        IdentifyingSystem identifyingSystem = (IdentifyingSystem) system;
-        if (!((Identifiable) identifyingSystem).hasObjectID()) {
-            identifyingSystem.register(identifyingSystem);
-            if (((Identifiable) identifyingSystem).getObjectID() != 0) {
-                throw new AssertionException();
-            }
-        }
-
-        try {
-            logInvocation();
-            Object result = call.invoke();
-            logger.debug("success!");
-            return result;
-        } catch (Exception e) {
-
-            /** Unwrap the invocation target exceptions */
-            if (e instanceof InvocationTargetException) {
-                InvocationTargetException invocationTargetException = (InvocationTargetException) e;
-                if (invocationTargetException.getTargetException() instanceof Exception) {
-                    e = (Exception) invocationTargetException.getTargetException();
-                }
-            }
-            logger.error("Failed to execute command.", e);
-
-            throw e;
-        } finally {
-            CurrentPrevayler.exitTransaction();
-        }
-    }
-
-    private void logInvocation() {
-        if (logger.isDebugEnabled()) {
-            Object target = call.getTarget();
-            Object[] args = call.getArgs();
-            Method method = call.getMethod();
-            Subject subject = call.getSubject();
-
-            List argsList = Collections.EMPTY_LIST;
-            if (args != null) {
-                argsList = Arrays.asList(args);
-            }
-            logger.debug("invoking method " + method + " on " + target);
-            logger.debug("args " + argsList);
-            logger.debug("user " + subject);
-        }
-    }
-
-    public Call getCall() {
-        return call;
-    }
-
-}
</pre></div>
<hr /><a name="file4" /><div class="file">
<span class="pathname">nanning/src/frameworks/prevayler/src/test/org/codehaus/nanning/prevayler<br /></span>
<div class="fileheader"><big><b>PrevaylerTest.java</b></big> <small id="info">1.10 -&gt; 1.11</small></div>
<pre class="diff"><small id="info">diff -u -r1.10 -r1.11
--- PrevaylerTest.java	11 Sep 2003 11:25:37 -0000	1.10
+++ PrevaylerTest.java	8 Oct 2003 07:25:40 -0000	1.11
@@ -3,6 +3,7 @@
</small></pre><pre class="diff" id="context"> import java.io.*;
 import java.lang.reflect.Method;
 import java.util.HashMap;
</pre><pre class="diff" id="added">+import java.util.Date;
</pre><pre class="diff" id="context"> 
 import org.codehaus.nanning.AspectInstance;
 import org.codehaus.nanning.Aspects;
</pre><pre class="diff"><small id="info">@@ -42,6 +43,55 @@
</small></pre><pre class="diff" id="context"> 
     protected void tearDown() throws Exception {
         prevaylerDir.delete();
</pre><pre class="diff" id="added">+    }
+
+    /**
+     * @entity
+     */
+    public static interface TestDatesWithPrevayler {
+        /**
+         * @transaction
+         */
+        void touchTime();
+
+        Date getTime();
+    }
+
+    public static class TestDatesWithPrevaylerImpl implements TestDatesWithPrevayler, Serializable {
+        private Date time;
+
+        public void touchTime() {
+            this.time = CurrentPrevayler.getClock();
+        }
+
+        public Date getTime() {
+            return time;
+        }
+    }
+
+    public void testDatesWithPrevayler() throws Exception {
+        newPrevayler();
+
+        final TestDatesWithPrevayler testDatesWithPrevayler = (TestDatesWithPrevayler) newInstance(TestDatesWithPrevayler.class);
+        withCurrentPrevayler(new PrevaylerAction() {
+            public Object run() throws Exception {
+                currentSystem().add(testDatesWithPrevayler);
+
+                assertNull(testDatesWithPrevayler.getTime());
+                testDatesWithPrevayler.touchTime();
+                assertNotNull(testDatesWithPrevayler.getTime());
+                return null;
+            }
+        });
+
+        final long testDatesWithPrevaylerId = getObjectId(testDatesWithPrevayler);
+        newPrevayler();
+        withCurrentPrevayler(new PrevaylerAction() {
+            public Object run() throws Exception {
+                assertNotNull(((TestDatesWithPrevayler) currentSystem().getIdentifiable(testDatesWithPrevaylerId)).getTime());
+                return null;
+            }
+        });
</pre><pre class="diff" id="context">     }
 
     public void testMethodCallWithSimpleStringIsPersisted() throws Exception {
</pre></div>
</body></html>