1 16 package org.apache.cocoon.components.flow.java; 17 18 import java.util.HashMap ; 19 20 28 public class Continuation { 29 private ContinuationStack stack; 30 private Object context; 31 32 private static HashMap continuationsmap = new HashMap (); 33 34 public boolean restoring = false; 35 public boolean capturing = false; 36 37 40 public Continuation(Object context) { 41 stack = new ContinuationStack(); 42 this.context = context; 43 } 44 45 48 public Continuation(Continuation parent, Object context) { 49 if (parent == null) 50 throw new NullPointerException ("Parent continuation is null"); 51 52 stack = new ContinuationStack(parent.stack); 53 this.context = context; 54 restoring = true; 55 } 56 57 60 public ContinuationStack getStack() { 61 return stack; 62 } 63 64 67 public Object getContext() { 68 return context; 69 } 70 71 74 public static void suspend() { 75 76 Continuation continuation = Continuation.currentContinuation(); 77 78 if (continuation == null) 79 throw new IllegalStateException ("No continuation is running"); 80 81 if (continuation.restoring) { 82 continuation.capturing = false; 83 } else { 84 continuation.capturing = true; 85 } 86 continuation.restoring = false; 87 } 88 89 93 public boolean isRestoring() { 94 return restoring; 95 } 96 97 100 public boolean isCapturing() { 101 return capturing; 102 } 103 104 107 public void registerThread() { 108 synchronized (continuationsmap) { 109 continuationsmap.put(Thread.currentThread(), this); 110 } 111 } 112 113 116 public void deregisterThread() { 117 synchronized (continuationsmap) { 118 continuationsmap.remove(Thread.currentThread()); 119 } 120 } 121 122 126 public static Continuation currentContinuation() { 127 synchronized (continuationsmap) { 128 Thread t = Thread.currentThread(); 129 return (Continuation) continuationsmap.get(t); 130 } 131 } 132 } 133 | Popular Tags |