Package tool.repository

Source Code of tool.repository.ImportWex

package tool.repository;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import tool.ToolPlugin;
import tool.ToolProjectSupport;

public class ImportWex implements IRunnableWithProgress{

  private static final String CONSOLE_NAME = "Import WEX";
  private IProject project;
  private File wexFile;
  protected MessageConsole msgConsole;
  protected MessageConsoleStream msgStream;
  private Process fsriptProcess;
  private File repos;
  private String workspace;
  private String workspacePassword;

  public ImportWex(IProject project, File wexFile){
    this.project = project;
    this.wexFile = wexFile;
    try {
      this.repos = new File(project.getPersistentProperty(ToolProjectSupport.reposQualifiedName).substring(3));
      this.workspace = project.getPersistentProperty(ToolProjectSupport.workspaceQualifiedName);
      this.workspacePassword = project.getPersistentProperty(ToolProjectSupport.worspacePasswordQualifiedName);
     
    } catch (CoreException e) {
      reportError(e);
    }
    this.msgConsole = ToolPlugin.findConsole(CONSOLE_NAME);
    if (this.msgConsole != null)
      msgStream = msgConsole.newMessageStream();
  }
  protected ImportWex(File wexFile, File repos, String workspace){// Testing
    super();
    this.project = null;
    this.wexFile = wexFile;
    this.repos = repos;
    this.workspace = workspace;
  }
  @Override
  public void run(IProgressMonitor monitor) {
    SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 100);
    try {
      Assert.isNotNull(this.project);
      Assert.isNotNull(this.repos);
      Assert.isNotNull(this.wexFile);
      subMonitor.beginTask("Importing WEX " + this.wexFile, 100);
      consumeWex().waitFor();
      subMonitor.worked(100);

    } catch (InterruptedException e) {
      reportError(e);
    } finally {
      subMonitor.done();
    }
  }

  public Process consumeWex() {
    writeToConsole("-- Import WEX at: " + wexFile);
    ToolPlugin.log(IStatus.INFO, "-- Import WEX at: " + wexFile);
    String osName = System.getProperty("os.name");
    if (!osName.startsWith("Windows")){ // Only run it on windows
      writeToConsole("-- Can only Import WEX on Windows --");
      ToolPlugin.log(IStatus.INFO, "-- Can only Import WEX on Windows --");
      return null;
    }
    File forteRoot = new File(ToolProjectSupport.getForteRoot());
    ToolPlugin.log(IStatus.INFO, "-- Fscript FORTE_ROOT = " + forteRoot.getAbsolutePath());
    writeToConsole("-- Interface FORTE_ROOT = " + forteRoot.getAbsolutePath());
    ToolPlugin.log(IStatus.INFO, "-- Fscript importin = " + this.wexFile);
    writeToConsole("-- Fscript importing = " + this.wexFile);
    // Start readers to consume output. It looks very innocuous with the output from the process
    // being mapped to p.inputStream, but this is correct.
    //OutputContainer container = new OutputContainer(this.msgStream);
    try {
      String result = consumeWex(forteRoot);
      ToolPlugin.log(IStatus.INFO, "-- Import WEX process completed --");
      writeToConsole("-- Import WEX process completed with:");
      writeToConsole(result);
    } catch (IOException e) {
      reportError(e);
    } catch (InterruptedException e) {
      reportError(e);
    } catch (ToolSystemException e) {
      reportError(e);
    } catch (InvalidToolRepositoryException e) {
      reportError(e);
    }
    return this.fsriptProcess;
  }
  public String consumeWex(File forteRoot) throws IOException, InterruptedException, ToolSystemException, InvalidToolRepositoryException{

    //create and write the commands
    String reposString = "bt:" + repos.getAbsolutePath();
   
   
    FScript repos = new FScript(forteRoot.getAbsolutePath(),
        reposString,
        "", "", ToolProjectSupport.DEFAULT_LOG_FLAGS, true);
   
    String commands = repos.createCommand("FscriptUtils.stg", "importWexFscript",
        new TemplateAttribute("repos", reposString),
        new TemplateAttribute("workspace", this.workspace),
        new TemplateAttribute("wexFileName", this.wexFile.getAbsolutePath()));
   
    String result = repos.executeCMD(commands);
//    ProcessBuilder pb = new ProcessBuilder(forteRoot + "/install/bin/fscript.bat",
//        "-fs",
//        "-fl",
//        ToolProjectSupport.DEFAULT_LOG_FLAGS);
//    Map<String, String> env = pb.environment();
//    env.put("FORTE_ROOT", forteRoot.getAbsolutePath());
//    //pb.directory(wexPath.toFile());
//
//      this.fsriptProcess = pb.start();
//      BufferedWriter out = new BufferedWriter( new OutputStreamWriter(this.fsriptProcess.getOutputStream()) );
//      out.write(commands);
//      out.flush();
//      Thread.sleep(150);
////      System.out.println("started fscript with " + pb.command());
////      System.out.println("started fscript with " + pb.environment());
//     
//
//      new Thread(new InputStreamHandler(this.fsriptProcess.getInputStream(), container, false), "OutputFileHandler").start();
//      new Thread(new InputStreamHandler(this.fsriptProcess.getErrorStream(), container, true), "ErrorFileHandler").start();
     
      return result;
  }
  protected void writeToConsole(String message){
    if (this.msgStream != null)
      this.msgStream.println(message);
  }
 
  protected void reportError(Throwable e){
    if (ToolPlugin.getDefault() != null)
      ToolPlugin.showError("Error generating Import WEX script", e);
    else
      e.printStackTrace();
   
  }

}
TOP

Related Classes of tool.repository.ImportWex

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.