package portal.flow;
import java.util.Hashtable;
import org.apache.commons.javaflow.Continuation;
public class Portal {
private static Hashtable<String,Continuation> portals = new Hashtable<String,Continuation>();
public static String open(Runnable runnable) {
String pid = Long.toString(System.currentTimeMillis());
Continuation c = Continuation.startWith(runnable);
if (c!=null) {
portals.put(pid,c);
System.out.println("Portal.open("+pid+")");
}
else System.err.println("ERROR : Can't open portal "+pid);
return pid;
}
public static void enter(String pid) {
Continuation c = portals.get(pid);
System.out.println("Enter ("+pid+")");
Continuation.continueWith(c);
}
public static void next(String pid) {
System.out.println("Portal.next("+pid+")");
Continuation c = portals.get(pid);
Continuation c2 = Continuation.continueWith(c);
if (c2!=null) portals.put(pid, c2);
else portals.remove(pid);
}
public static void exit(String pid) {
if (pid==null) pid = "";
Continuation c = portals.get(pid);
System.out.println("Portal.exit("+pid+")");
if (c==null) Continuation.suspend();
else
Continuation.suspend(c);
}
public static boolean exists(String pid) {
System.out.println("Portal.exists("+pid+") : "+portals.containsKey(pid));
return portals.containsKey(pid);
}
public static void close(String pid) {
if (pid!=null && portals.containsKey(pid)) {
System.out.println("Portal.close("+pid+") ");
Continuation c = portals.remove(pid);
if (c!=null) c.exit();
}
}
}