Package tool.model.jobs

Source Code of tool.model.jobs.LoadPlanCacheJob

package tool.model.jobs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import tool.ToolModelActivator;
import tool.ToolProjectSupport;
import tool.model.ToolPlan;

public class LoadPlanCacheJob extends Job{

  private IProject project;
  private Map<String, ToolPlan> cache;

  public LoadPlanCacheJob(IProject project, Map<String, ToolPlan> cache) {
    super("Load Plan Cache");
    setUser(true);
    this.project = project;
    this.cache = cache;
  }

  @Override
  protected IStatus run(IProgressMonitor monitor) {
    IStatus status = Status.OK_STATUS;
    try {
      // find each pex and parse it
      final List<IFile> pexFiles = new ArrayList<IFile>();
      project.accept(new IResourceProxyVisitor() {
        public boolean visit(IResourceProxy proxy) throws CoreException {
          if (proxy.getType() == IResource.FILE) {
            IFile file = (IFile) proxy.requestResource();
            if (file.getLocation().getFileExtension() != null
                && file.getLocation().getFileExtension().matches("pex") &&
                ToolProjectSupport.isSourceFolder(file.getParent())) {
              pexFiles.add(file);

            }
          }
          return true;
        }
      }, IResource.DEPTH_INFINITE);

      monitor.beginTask("Loading Plan Cache", pexFiles.size());
      for (IFile pexFile : pexFiles){
        if (monitor.isCanceled())
          break;
        monitor.subTask(pexFile.getName());
        ToolPlan plan = ToolPlan.createPlanFromFile(pexFile);
        if (plan != null){
          cache.put(plan.getToolName().toUpperCase(), plan);
          System.out.println("+++ Adding to cache: " + plan.getToolName());
        } else {
          System.out.println("--- Plan not parsed: " + pexFile.getName());
        }
        monitor.worked(1);
      }
    } catch (CoreException e) {
      ToolModelActivator.log(IStatus.ERROR, "Error loading Plan cache for "+ project.getName(), e);
      status = Status.CANCEL_STATUS;
    } finally {
      monitor.done();
    }
    System.out.println("#### Total plans in cache: " + cache.size());
    return status;
  }

  public Map<String, ToolPlan> getCache() {
    return cache;
  }

  public void setCache(Map<String, ToolPlan> cache) {
    this.cache = cache;
  }

}
TOP

Related Classes of tool.model.jobs.LoadPlanCacheJob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.