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

csip.DynamicPyModelDataService Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: DynamicPyModelDataService.java 6ec9259c272f 2019-03-13 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import csip.annotations.Author;
import csip.annotations.Description;
import csip.annotations.Documentation;
import csip.annotations.License;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.annotations.ResourceType;
import csip.annotations.State;
import csip.annotations.VersionInfo;
import csip.utils.Binaries;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.util.logging.Level;
import javax.ws.rs.Path;

@Name("DynPy")
@Description("Dynamic, server-side Python execution.")
@Documentation("https://alm.engr.colostate.edu/csip")
@State(State.STABLE)
@License(License.MIT)
@VersionInfo("$Id: DynamicPyModelDataService.java 6ec9259c272f 2019-03-13 od $")
@Author(name = "od", email = "", org = "CSU")
@Path("p/dynpy/1.0")
public class DynamicPyModelDataService extends ModelDataService {

  static final String PY_SCRIPT = "py_script";


  @Override
  protected void doProcess() throws Exception {

    if (!metainfo().hasName(PY_SCRIPT)) {
      throw new ServiceException("Missing Python script in metainfo.");
    }

    String pyScript = getMetainfo().getString(PY_SCRIPT);
    if (!attachments().hasFile(pyScript)) {
      throw new ServiceException("Missing Python attachment: " + pyScript);
    }

    Resource res = new Resource() {
      @Override
      public String file() {
        try {
          return attachments().getFile(pyScript).toString();
        } catch (ServiceException ex) {
          LOG.log(Level.SEVERE, null, ex);
        }
        return null;
      }


      @Override
      public ResourceType type() {
        return ResourceType.PYTHON3;
      }


      @Override
      public String id() {
        return "";
      }


      @Override
      public boolean wine() {
        return false;
      }


      @Override
      public String args() {
        return "";
      }


      @Override
      public String[] env() {
        return new String[]{};
      }


      @Override
      public Class from() {
        return Object.class;
      }


      @Override
      public Class annotationType() {
        return Resource.class;
      }
    };

    Executable e = Binaries.getResourcePython(res, getWorkspaceDir(), LOG, this.getClass());
    StringWriter err = new StringWriter();
    e.redirectError(err);
    LOG.info("running : " + e.getName());
    int ret = e.exec();
    LOG.info("done with exit value: " + ret);

    // Not Successful.
    if (ret != 0) {
      throw new ServiceException(err.toString());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy