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

csip.ServiceResources 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: ServiceResources.java 3ee7ec8a352f 2019-04-16 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.Resource;
import csip.annotations.ResourceType;
import csip.utils.Binaries;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import org.apache.commons.io.FileUtils;

/**
 * ServiceResources access.
 *
 * @author od
 */
public class ServiceResources {

  private final ModelDataService mds;


  ServiceResources(ModelDataService mds) {
    this.mds = mds;
  }


  /**
   * Get a service resource file. Resources are defined as service annotations.
   *
   * @param id the id of the resource.
   * @return the extracted file within the local file system.
   * @throws ServiceException if file cannot be found or unpacked
   * @see csip.annotations.Resource
   */
  public File getFile(String id) throws ServiceException {
    return Binaries.getResourceFile(mds.getClass(), id);
  }


  /**
   * Get an executable from a resource definition. Resources are defined as
   * service annotations.
   *
   * @param id the id of the resource
   * @return the ProcessExecution for that executable
   * @throws ServiceException if there is no EXE
   * @see csip.annotations.Resource
   */
  public Executable getExe(String id) throws ServiceException {
    Resource resource = Binaries.getResourceById(mds.getClass(), id);
    if (resource == null) {
      throw new ServiceException("Not found: " + id);
    }
    switch (resource.type()) {
      case EXECUTABLE:
      case REFERENCE:
        return Binaries.getResourceExe0(resource, mds.getWorkspaceDir(), mds.LOG, mds.getClass());
      case PYTHON2:
      case PYTHON3:
        return Binaries.getResourcePython(resource, mds.getWorkspaceDir(), mds.LOG, mds.getClass());
      default:
        throw new ServiceException("Not an Executable Resource: " + id);
    }
  }


  /**
   * Copy a FILE Resource to the Workspace.
   *
   * @param id The FILE resource ID.
   * @throws ServiceException if id does not exist
   * @throws IOException if copy fails
   */
  public void copyFileToWorkspace(String id) throws ServiceException, IOException {
    Resource resource = Binaries.getResourceById(mds.getClass(), id);
    if (id == null) {
      throw new IllegalArgumentException("Resource not found :" + id);
    }
    if (resource.type() != ResourceType.FILE) {
      throw new IllegalArgumentException("Not a FILE Resource :" + id);
    }
    FileUtils.copyFileToDirectory(getFile(id), mds.getWorkspaceDir());
  }


  /**
   * Get a JDBC connection from a resource definition.
   *
   * @param id the id of the resource
   * @return the JDBC connection.
   * @throws ServiceException if connection fails
   * @see csip.annotations.Resource
   */
  public Connection getJDBC(String id) throws ServiceException {
    return Binaries.getResourceJDBC(mds.getClass(), id, mds.LOG);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy