SessionWorkspaceImpl.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.SessionWorkspace;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
/**
* Session Workspace management.
*
* @author od
*/
class SessionWorkspaceImpl implements SessionWorkspace {
private final ModelDataService mds;
SessionWorkspaceImpl(ModelDataService mds) {
this.mds = mds;
}
@Override
public Stream<String> lines(String filename) throws IOException {
return Files.lines(getFile(filename).toPath());
}
@Override
public Stream<String> lines(File file) throws IOException {
return Files.lines(file.toPath());
}
@Override
public File getDir() {
return mds.getWorkspaceDir0();
}
@Override
public File getFile(String filename) {
return new File(getDir(), filename);
}
@Override
public File[] getFiles(String pattern) throws IOException {
return Utils.expandFiles(getDir(), pattern);
}
@Override
public File getExistingFile(String filename) throws FileNotFoundException {
File f = getFile(filename);
if (!f.exists())
throw new FileNotFoundException(f.toString());
return f;
}
@Override
public Path getPath() {
return getDir().toPath();
}
@Override
public String readString(String filename) throws IOException {
return FileUtils.readFileToString(getFile(filename), "UTF-8");
}
@Override
public File writeString(String filename, String data) throws IOException {
File f = getFile(filename);
FileUtils.writeStringToFile(f, data, "UTF-8");
return f;
}
@Override
public byte[] readBytes(String filename) throws IOException {
return FileUtils.readFileToByteArray(getFile(filename));
}
@Override
public void writeBytes(String filename, byte[] data) throws IOException {
FileUtils.writeByteArrayToFile(getFile(filename), data);
}
@Override
public SessionWorkspaceImpl exist(String... filenames) throws ServiceException {
for (String n : filenames) {
if (!getFile(n).exists())
throw new ServiceException("Required file does not exist: " + n);
}
return this;
}
}