ServiceResourcesImpl.java [src/csip] Revision:   Date:
/*
 * $Id: 2.7+55 ServiceResourcesImpl.java 54cb6e135a5e 2023-11-01 od $
 *
 * 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 static csip.Config.CSIP_DIR;
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.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import org.apache.commons.io.FileUtils;

/**
 * ServiceResources access.
 *
 * @author od
 */
class ServiceResourcesImpl extends ModelDataServiceAPI
    implements ServiceResources {

  ServiceResourcesImpl(ModelDataService mds) {
    super(mds);
  }

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

  @Override
  public String getResolved(String id) {
    Resource resource = get(id);
    return (resource == null) ? null : Utils.resolve(resource.file());
  }

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

  @Override
  public void copyIntoWorkspace(String id) throws ServiceException, IOException {
    Resource resource = Binaries.getResourceById(mds.getClass(), id);
    if (resource == null)
      throw new IllegalArgumentException("Resource not found :" + id);

    if (resource.type() == ResourceType.FILE) {
      FileUtils.copyFileToDirectory(getFile(id), mds.getWorkspaceDir0());
    } else if (resource.type() == ResourceType.ARCHIVE) {
      String rfile = getResolved(id);
      if (rfile.endsWith("/**")) {
        rfile = rfile.trim().substring(0, rfile.length() - 3);
        File src = new File(Config.getString(CSIP_DIR), rfile);
        FileUtils.copyDirectory(src, mds.getWorkspaceDir0(), true);
      } else {
        File src = new File(Config.getString(CSIP_DIR), rfile);
        FileUtils.copyToDirectory(src, mds.getWorkspaceDir0());
      }
    } else
      throw new IllegalArgumentException("Not a FILE or ARCHIVE Resource: " + id);
  }

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