V1_0.java [src/java/m/oms/dsl] Revision: default  Date:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.oms.dsl;

import csip.Executable;
import csip.ModelDataService;
import csip.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.lang.ArrayUtils;

/**
 * OMS service.
 *
 * @author od
 */
@Name("OMS Simulation execution")
@Description("OMS dsl file execution")
@VersionInfo("1.0")
@Path("m/dsl/1.0")
@Options(timeout = "P3D")
//@Polling(first = 4000, next = 1000)
@Resource(file = "/bin/QpgTools_gradle-na.jar", type = ResourceType.JAR, id = "qpgtools_jar")
@Resource(file = "/bin/QPG_OMSprj.jar", type = ResourceType.JAR, id = "qpgOMS_jar")
@Resource(file = "/bin/oms/3.6.22/cpptasks-1.0b6-od.jar", type = ResourceType.JAR, id = "cpp_jar")
@Resource(file = "/bin/oms/3.6.22/groovy-all-2.3.9.jar", type = ResourceType.JAR, id = "groovy_jar")
@Resource(file = "/bin/oms/3.6.22/jcommon-1.0.15.jar", type = ResourceType.JAR, id = "jcommon_jar")
@Resource(file = "/bin/oms/3.6.22/jfreechart-1.0.12.jar", type = ResourceType.JAR, id = "jfreechart_jar")
@Resource(file = "/bin/oms/3.6.22/oms-all.jar", type = ResourceType.JAR, id = "oms_jar")
@Resource(file = "java-stdout.txt java-stderr.txt", type = ResourceType.OUTPUT)
@Resource(file = "/data/data.zip", type = ResourceType.ARCHIVE)
@Resource(file = "/simulation_rlz/sim.zip", type = ResourceType.ARCHIVE)

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 {
        File zip = ZipFiles.zip(new File(getWorkspaceDir(), "res"));
        putResult(zip);
    }


    private void runSim(String dsl, String loglevel, String options) throws Exception {
        Map sysprops = new HashMap();
        sysprops.put("oms3.work", getWorkspaceDir().toString());
        // the following option terminates the service
        // sysprops.put("oms.csip.server", "true");
        new File(getWorkspaceDir(), "res").mkdirs();

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

        int result = p.exec();
        if (result != 0) {
            File err = new File(getWorkspaceDir(), "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 = getWorkspaceDir();
        List<File> jars = new ArrayList<>();
        jars.add(resources().getFile("qpgtools_jar"));
        jars.add(resources().getFile("qpgOMS_jar"));

        jars.add(resources().getFile("cpp_jar"));
        jars.add(resources().getFile("groovy_jar"));
        jars.add(resources().getFile("jcommon_jar"));
        jars.add(resources().getFile("jfreechart_jar"));
        jars.add(resources().getFile("oms_jar"));
        
        jars.addAll(Binaries.getJars(new File(ws, "dist")));
        jars.addAll(Binaries.getJars(new File(ws, "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);
        }

        Executable e = Binaries.getResourceOMSDSL(new File(ws, dsl), sp, ws, jars, loglevel, LOG);
        return e;
    }
}