All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.studio.agent.debug.SuspendController Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.studio.agent.debug;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.snapscript.studio.agent.debug.ResumeListener;
import org.snapscript.studio.agent.debug.ResumeType;
import org.snapscript.studio.agent.debug.ScopeBrowser;

public class SuspendController {
   
   private final Map listeners;
   private final Map browsers;
   private final Map types;
   private final Map locks;
   
   public SuspendController() {
      this.listeners = new ConcurrentHashMap();
      this.browsers = new ConcurrentHashMap();
      this.types = new ConcurrentHashMap();
      this.locks = new ConcurrentHashMap();
   }
   
   public void resume(ResumeType type) {
      Set threads = listeners.keySet();
      
      for(String thread : threads) {
         try {
            resume(type, thread);
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
   
   public void resume(ResumeType type, String thread) {
      Object lock = locks.get(thread);
      ResumeListener listener = listeners.remove(thread);
      
      synchronized(lock) {
         try {
            browsers.remove(thread); 
            
            if(listener != null) {
               types.put(thread, type);
               listener.resume(thread);
            }
            lock.notify();
         }catch(Exception e) {
            throw new IllegalStateException("Could not resume thread '" + thread + "'", e);
         }
      }
   }

   public ResumeType suspend(ResumeListener listener, ScopeBrowser browser) {
      String name = Thread.currentThread().getName();
      Object lock = locks.get(name);
      
      if(lock == null) {
         lock = new Object();
         locks.put(name, lock);
      }
      synchronized(lock) {
         try {
            browsers.put(name, browser);
            listeners.put(name, listener);
            lock.wait();
         }catch(Exception e) {
            throw new IllegalStateException("Could not suspend thread '" + name + "'", e);
         }
      }
      return types.remove(name); // resume in a specific way
   }
   
   public void browse(Set expand, String thread) {
      Object lock = locks.get(thread);
      ScopeBrowser browser = browsers.get(thread);
      
      synchronized(lock) {
         try {
            if(browser != null) {
               browser.browse(expand);
            }
         }catch(Exception e) {
            throw new IllegalStateException("Could not browse thread '" + thread + "'", e);
         }
      }
   }
   
   public void evaluate(Set expand, String thread, String expression, boolean refresh) {
      Object lock = locks.get(thread);
      ScopeBrowser browser = browsers.get(thread);
      
      synchronized(lock) {
         try {
            if(browser != null) {
               browser.evaluate(expand, expression, refresh);
            }
         }catch(Exception e) {
            throw new IllegalStateException("Could not evaluate '" + expression + "' for thread '" + thread + "'", e);
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy