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

org.snapscript.studio.agent.ProcessAgentService Maven / Gradle / Ivy

package org.snapscript.studio.agent;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.snapscript.core.ResourceManager;
import org.snapscript.core.scope.Model;
import org.snapscript.studio.agent.debug.BreakpointMatcher;
import org.snapscript.studio.agent.event.ProcessEventChannel;

public class ProcessAgentService {
   
   private final Map> breakpoints;
   private final ProcessEventChannel channel;
   private final ProcessResourceExecutor executor;
   private final ProcessContext context;
   
   public ProcessAgentService(ProcessContext context, ProcessEventChannel channel, ProcessResourceExecutor executor, ProcessMode mode, Model model) {
      this.breakpoints = new HashMap>();
      this.executor = executor;
      this.channel = channel;
      this.context = context;
   }

   public String loadScript(String project, String resource) {
      ResourceManager manager = context.getManager();
      String path = ProcessStore.getPath(project, resource);

      return manager.getString(path);
   }
   
   public void createBreakpoint(String resource, int line) {
      Map lines = breakpoints.get(resource);
      BreakpointMatcher matcher = context.getMatcher();
      
      if(lines == null) {
         lines = new HashMap();
         breakpoints.put(resource, lines);
      }
      lines.put(line, Boolean.TRUE);
      matcher.update(breakpoints);
   }
   
   public void removeBreakpoint(String resource, int line){
      Map lines = breakpoints.get(resource);
      BreakpointMatcher matcher = context.getMatcher();
      
      if(lines == null) {
         lines = new HashMap();
         breakpoints.put(resource, lines);
      }
      lines.put(line, Boolean.FALSE);
      matcher.update(breakpoints);
   }
   
   public void execute(String project, String resource, String dependencies, boolean debug) {
      BreakpointMatcher matcher = context.getMatcher();
      ProcessStore store = context.getStore();
      String actual = context.getProcess();

      matcher.update(breakpoints);
      store.update(project); 
      executor.execute(channel, actual, project, resource, dependencies, debug);
   }

   public boolean join(long time) {
      CountDownLatch latch = context.getLatch();

      try {
         latch.await();
      }catch(Exception e) {
         return false;
      }
      return true;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy