commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r149195 - in jakarta/commons/sandbox/javaflow/trunk: .classpath project.xml src/java/org/apache/commons/javaflow/Continuation.java src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java src/test/org/apache/commons/javaflow/testcode/Calculator.java
Date Mon, 31 Jan 2005 00:52:05 GMT
Author: tcurdt
Date: Sun Jan 30 16:52:04 2005
New Revision: 149195

URL: http://svn.apache.org/viewcvs?view=rev&rev=149195
Log:
added some fixes from phil@whirlycott.com


Removed:
    jakarta/commons/sandbox/javaflow/trunk/.classpath
Modified:
    jakarta/commons/sandbox/javaflow/trunk/project.xml
    jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
    jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
    jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java

Modified: jakarta/commons/sandbox/javaflow/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/project.xml?view=diff&r1=149194&r2=149195
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/project.xml (original)
+++ jakarta/commons/sandbox/javaflow/trunk/project.xml Sun Jan 30 16:52:04 2005
@@ -32,7 +32,7 @@
     <unitTestSourceDirectory>src/test</unitTestSourceDirectory>
     <unitTest>
       <includes>
-        <include>**/*Test.java</include>
+        <include>**/*TestCase.java</include>
       </includes>
     </unitTest>
   </build>
@@ -67,6 +67,12 @@
      <groupId>ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.5.3-1</version>
+   </dependency>
+
+   <dependency>
+     <groupId>junit</groupId>
+     <artifactId>junit</artifactId>
+     <version>3.8.1</version>
    </dependency>
 
   </dependencies>

Modified: jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java?view=diff&r1=149194&r2=149195
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
(original)
+++ jakarta/commons/sandbox/javaflow/trunk/src/java/org/apache/commons/javaflow/Continuation.java
Sun Jan 30 16:52:04 2005
@@ -30,7 +30,7 @@
  *
  * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
  * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
- * @version CVS $Id: Continuation.java,v 1.1 2005/01/23 03:55:21 tcurdt Exp $
+ * @version CVS $Id:$
  */
 public class Continuation implements Serializable {
 
@@ -42,6 +42,7 @@
     private final static transient Map continuationsMap = new HashMap();
 
     private boolean restoring = false;
+
     private boolean capturing = false;
 
     /**
@@ -56,95 +57,87 @@
     }
 
     /**
-     * Return the stack, which is used to store the frame information.
-    private Stack getStack() {
-        return stack;
-    }
+     * Return the stack, which is used to store the frame information. private
+     * Stack getStack() { return stack; }
      */
 
-
     // REVISIT
     private Object context;
+
     public Object getContext() {
         return context;
     }
-    
-    public static Continuation continueWith(
-            final Continuation parent,
-            final ContinuationContext context
-            ) {
 
-        // setup context outside
-        
+    /**
+     * Creates a new continuation to capture the
+     * next state. Resume where the old one was
+     * suspended.
+     * 
+     * @param parent parent continuation or null
+     * @param context context of the continuation
+     * @return new child continuation
+     */
+    public static Continuation continueWith(final Continuation parent, final ContinuationContext
context) {
+
         final Continuation newContinuation = new Continuation(parent);
-        
+
         if (parent != null) {
             log.debug("resuming continuation " + parent);
             newContinuation.restoring = true;
-        }
-        else {
+        } else {
             log.debug("starting new flow");
             // create continuable instance
         }
 
         newContinuation.registerThread();
-
         newContinuation.context = context;
-        
+
         final Object instance = context.getInstance();
         final Method method = context.getMethod();
 
         try {
             method.invoke(instance, new Object[0]);
-        }
-        catch(Exception e) {
-            log.error("could not execute " + instance + " " + method, e);
-        }
-        finally {
-            
+
+        } catch (final Exception e) {
+            log.error("could not execute " + instance + " " + method, e);       
+        } finally {
             if (newContinuation.capturing) {
                 newContinuation.stack.popReference();
             }
 
             newContinuation.context = null;
-
             newContinuation.deregisterThread();
         }
-        
+
         return newContinuation;
     }
-    
+
     /**
      * Stop the running continuation.
      */
     public static void suspend() {
-    	
-    	log.debug("suspend()");
+        log.debug("suspend()");
 
-        Continuation continuation = Continuation.currentContinuation();
+        final Continuation continuation = Continuation.currentContinuation();
 
         if (continuation == null)
             throw new IllegalStateException("no continuation is running");
 
-        if (continuation.restoring) {
-            continuation.capturing = false;
-        } else {
-            continuation.capturing = true;
-        }
-        
+        continuation.capturing = !continuation.restoring;        
         continuation.restoring = false;
     }
 
     /**
-     * True, if the continuation restores the previous stack trace to the
-     * last invocation of suspend().
+     * True, if the continuation restores the previous stack trace to the last
+     * invocation of suspend().
      */
     public boolean isRestoring() {
         return restoring;
     }
 
     /**
-     * True, is the continuation freeze the strack trace, and stops the continuation.
+     * True, is the continuation freeze the strack trace, and stops the
+     * continuation.
      */
     public boolean isCapturing() {
         return capturing;
@@ -153,7 +146,7 @@
     public Stack getStack() {
         return stack;
     }
-    
+
     /**
      * Bind the continuation to running thread.
      */
@@ -173,13 +166,11 @@
     }
 
     /**
-     * Return the continuation, which is associated to the
-     * current thread.
+     * Return the continuation, which is associated to the current thread.
      */
     public static Continuation currentContinuation() {
         synchronized (continuationsMap) {
-            Thread t = Thread.currentThread();
-            return (Continuation) continuationsMap.get(t);
+            return (Continuation) continuationsMap.get(Thread.currentThread());
         }
     }
 }

Modified: jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java?view=diff&r1=149194&r2=149195
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
(original)
+++ jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/ContinuationClassLoaderTestCase.java
Sun Jan 30 16:52:04 2005
@@ -18,6 +18,8 @@
 import java.lang.reflect.Method;
 import java.util.Map;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.javaflow.utils.ReflectionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -27,40 +29,65 @@
  * @author tcurdt
  *
  */
-public final class ContinuationClassLoaderTestCase {
+public final class ContinuationClassLoaderTestCase extends TestCase {
 
+    /**
+     * Logger.
+     */
     private final static Log log = LogFactory.getLog(ContinuationClassLoaderTestCase.class);
 
-    private void testCalculator() throws Exception {
+    public void testCalculator() throws Exception {
+        log.debug("Doing testCalculator()");
+        
+        final String calculatorTestClass = "org.apache.commons.javaflow.testcode.Calculator";
+
+        // creating object instance
         final ClassLoader cl = new ContinuationClassLoader(getClass().getClassLoader());
-        final Class clazz = cl.loadClass("org.apache.commons.javaflow.testcode.Calculator");
- 
-        final Map m = ReflectionUtils.discoverMethods(clazz);
+        final Class clazz = cl.loadClass(calculatorTestClass);
+        assertNotNull(clazz);
         final Object instance = clazz.newInstance();
+        assertNotNull(instance);
+ 
+        // get method called "main"
+        final Map methods = ReflectionUtils.discoverMethods(clazz);
+        assertNotNull(methods);
+        final Method method = (Method) methods.get("main");
+        assertNotNull(method);
         
-        Continuation c = null;
+        Continuation continuation = null;
         ContinuationContext context = null;
-
+        
         
         context = new ContinuationContext();
-        context.setMethod((Method)m.get("main"));
+        context.setMethod(method);
         context.setInstance(instance);
-
-        log.debug("continuation 1");        
-        c = Continuation.continueWith(c, context);
-
         
+        log.debug("Continuation 1");
+        continuation = Continuation.continueWith(continuation, context);
+        
+        
+        final Continuation parent = continuation;
+
         context = new ContinuationContext();
-        context.setMethod((Method)m.get("main"));
+        context.setMethod(method);
         context.setInstance(instance);
 
-        log.debug("continuation 2");
-        c = Continuation.continueWith(c, context);
+        log.debug("Continuation 1.1");
+        final Continuation continuation11 = Continuation.continueWith(parent, context);
 
+        log.debug("Continuation 1.2");
+        final Continuation continuation12 = Continuation.continueWith(parent, context);
+        
     }
     
-    public static void main(String[] args) throws Exception {
-        ContinuationClassLoaderTestCase t = new ContinuationClassLoaderTestCase();
+    
+    /**
+     * The junit tests are preferred over running this main() method.
+     * @param args
+     * @throws Exception
+     */
+    public static void main(final String[] args) throws Exception {
+        final ContinuationClassLoaderTestCase t = new ContinuationClassLoaderTestCase();
         t.testCalculator();
     }
 }

Modified: jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java?view=diff&r1=149194&r2=149195
==============================================================================
--- jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
(original)
+++ jakarta/commons/sandbox/javaflow/trunk/src/test/org/apache/commons/javaflow/testcode/Calculator.java
Sun Jan 30 16:52:04 2005
@@ -19,6 +19,8 @@
 
 import org.apache.commons.javaflow.Continuable;
 import org.apache.commons.javaflow.Continuation;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * @author tcurdt
@@ -26,13 +28,18 @@
  */
 public final class Calculator implements Continuable, Serializable {
     
+    /**
+     * Logger.
+     */
+    private static final Log log = LogFactory.getLog(Calculator.class);
+    
     public void main() {
 
-        System.out.println("Calculator1");
+        log.debug("Calculator1");
 
         Continuation.suspend();
         
-        System.out.println("Calculator2");
+        log.debug("Calculator2");
         
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message