V1_0.java [src/java/m/oms/dsl] Revision: 37a8b5cbc20db30b0d7f7e4238c495ac24efa014  Date: Wed Dec 29 18:58:50 MST 2021
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.oms.dsl;

import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import csip.utils.Binaries;
//import csip.utils.ZipFiles;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Path;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

/**
 * OMS service.
 *
 * @author od
 */
@Name("OMS Simulation execution")
@Description("OMS dsl file execution")
@VersionInfo("1.0")
@Path("m/dsl/1.0")
@Polling(first = 4000, next = 1000)
@Resource(file = "/bin/oms-all.jar", type = ResourceType.JAR, id = "oms_jar")
@Resource(file = "/bin/jcommon-1.0.15.jar", type = ResourceType.JAR, id = "jcommon_jar")
@Resource(file = "/bin/jfreechart-1.0.12.jar", type = ResourceType.JAR, id = "jfreechart_jar")
@Resource(file = "/bin/groovy-all-2.3.9.jar", type = ResourceType.JAR, id = "groovy_jar")
@Resource(file = "java-stdout.txt java-stderr.txt", type = ResourceType.OUTPUT)
public class V1_0 extends ModelDataService {

  // parameter keys
  static final String KEY_SCRIPT = "dsl";
  static final String KEY_LOGLEVEL = "loglevel";
  static final String KEY_OPTIONS = "java.options";


  @Override
  public void doProcess() throws Exception {
    String dsl = parameter().getString(KEY_SCRIPT);
    String loglevel = parameter().getString(KEY_LOGLEVEL, "INFO");
    String options = parameter().getString(KEY_OPTIONS, "");
    runSim(dsl, loglevel, options);
  }


  @Override
  protected void postProcess() throws Exception {
//    ZipFiles.zip(workspace().getFile("output"));
//    results().put(new File(workspace().getDir(), "output"));
    results().put(workspace().getFile("output"));
  }


  private void runSim(String dsl, String loglevel, String options) throws Exception {
    Map sysprops = new HashMap();
    sysprops.put("oms3.work", workspace().getDir().toString());
    sysprops.put("oms.csip.server", "true");

    workspace().getFile("output").mkdirs();

    // Create a Process.
    Executable p = createProcess(loglevel, dsl, sysprops, options);

    int result = p.exec();
    if (result != 0) {
      File err = workspace().getFile("java-stderr.txt");
      if (err.exists()) {
        throw new ServiceException(FileUtils.readFileToString(err));
      }
      throw new ServiceException("General Error." + result);
    }
  }


  private Executable createProcess(String loglevel, String dsl,
      Map<String, String> sysprops, String options) throws Exception {
    File ws = workspace().getDir();
    List<File> jars = new ArrayList<>();
    jars.add(resources().getFile("oms_jar"));
    jars.add(resources().getFile("jfreechart_jar"));
    jars.add(resources().getFile("groovy_jar"));
    jars.add(resources().getFile("jcommon_jar"));

    jars.addAll(Binaries.getJars(workspace().getFile("dist")));
    jars.addAll(Binaries.getJars(workspace().getFile("lib")));
    jars.addAll(Binaries.getJars(ws));

    String[] sp = Binaries.asSysProps(sysprops);
    if (options != null && !options.isEmpty()) {
      String[] opt = options.split("\\s+");
      sp = (String[]) ArrayUtils.addAll(sp, opt);
    }
    return Binaries.getResourceOMSDSL(workspace().getFile(dsl), sp, ws, jars, loglevel, LOG);
  }
}