ServiceResourcesImpl.java [src/csip] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, 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.api.server.ServiceException;
import csip.api.server.Executable;
import csip.annotations.Resource;
import csip.annotations.ResourceType;
import csip.api.server.ServiceResources;
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
 */
class ServiceResourcesImpl implements ServiceResources {

  private final ModelDataService mds;
  private SessionLogger LOG;


  ServiceResourcesImpl(ModelDataService mds, SessionLogger LOG) {
    this.mds = mds;
    this.LOG = LOG;
  }


  @Override
  public Resource get(String id) {
    return Binaries.getResourceById(mds.getClass(), id);
  }


  @Override
  public File getFile(String id) throws ServiceException {
    return Binaries.getResourceFile(mds.getClass(), id);
  }


  @Override
  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.getWorkspaceDir0(), LOG, mds.getClass());
      case PYTHON2:
      case PYTHON3:
        return Binaries.getResourcePython(resource, mds.getWorkspaceDir0(), LOG, mds.getClass());
      case RSCRIPT:
        return Binaries.getResourceRScript(resource, mds.getWorkspaceDir0(), LOG, mds.getClass());
      case CLASSNAME:
      case JAR:
        return Binaries.getResourceJava0(resource, mds.getWorkspaceDir0(), LOG);
      default:
        throw new ServiceException("Not an Executable Resource: " + id);
    }
  }


  @Override
  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.getWorkspaceDir0());
  }


  @Override
  public Connection getJDBC(String id) throws ServiceException {
    return Binaries.getResourceJDBC(mds.getClass(), id, LOG);
  }
}