Add validators, rework machine stores

This commit is contained in:
Christopher Schnick
2022-07-03 20:41:42 +02:00
parent f0f1417980
commit 4eb0c80d60
23 changed files with 608 additions and 33 deletions

View File

@@ -16,6 +16,10 @@ import java.util.Optional;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface DataStore {
default boolean isComplete() {
return true;
}
default void validate() throws Exception {
}

View File

@@ -13,22 +13,29 @@ import java.nio.file.Path;
public class FileStore implements StreamDataStore, FilenameStore {
public static FileStore local(Path p) {
return new FileStore(MachineStore.local(), p.toString());
return new FileStore(MachineFileStore.local(), p.toString());
}
public static FileStore local(String p) {
return new FileStore(MachineStore.local(), p);
return new FileStore(MachineFileStore.local(), p);
}
MachineStore machine;
MachineFileStore machine;
String file;
@JsonCreator
public FileStore(MachineStore machine, String file) {
public FileStore(MachineFileStore machine, String file) {
this.machine = machine;
this.file = file;
}
@Override
public void validate() throws Exception {
if (!machine.exists(file)) {
throw new IllegalStateException("File " + file + " could not be found on machine " + machine.toDisplay());
}
}
@Override
public InputStream openInput() throws Exception {
return machine.openInput(file);
@@ -40,7 +47,7 @@ public class FileStore implements StreamDataStore, FilenameStore {
}
@Override
public boolean canOpen() {
public boolean canOpen() throws Exception {
return machine.exists(file);
}
@@ -56,6 +63,6 @@ public class FileStore implements StreamDataStore, FilenameStore {
@Override
public String getFileName() {
return Path.of(file).getFileName().toString();
return file;
}
}

View File

@@ -1,14 +1,18 @@
package io.xpipe.core.store;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Value;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
@JsonTypeName("local")
public class LocalMachineStore implements MachineStore {
@Value
public class LocalStore implements ShellStore {
@Override
public boolean exists(String file) {
@@ -31,4 +35,19 @@ public class LocalMachineStore implements MachineStore {
var p = Path.of(file);
return Files.newOutputStream(p);
}
@Override
public String executeAndRead(List<String> cmd) throws Exception {
var p = prepare(cmd).redirectErrorStream(true);
var proc = p.start();
var b = proc.getInputStream().readAllBytes();
proc.waitFor();
//TODO
return new String(b, StandardCharsets.UTF_16LE);
}
@Override
public List<String> createCommand(List<String> cmd) {
return cmd;
}
}

View File

@@ -3,15 +3,15 @@ package io.xpipe.core.store;
import java.io.InputStream;
import java.io.OutputStream;
public interface MachineStore extends DataStore {
public interface MachineFileStore extends DataStore {
static MachineStore local() {
return new LocalMachineStore();
static MachineFileStore local() {
return new LocalStore();
}
InputStream openInput(String file) throws Exception;
OutputStream openOutput(String file) throws Exception;
public boolean exists(String file);
public boolean exists(String file) throws Exception;
}

View File

@@ -0,0 +1,19 @@
package io.xpipe.core.store;
import java.util.List;
public interface ShellStore extends MachineFileStore {
static ShellStore local() {
return new LocalStore();
}
default ProcessBuilder prepare(List<String> cmd) throws Exception {
var toExec = createCommand(cmd);
return new ProcessBuilder(toExec);
}
String executeAndRead(List<String> cmd) throws Exception;
List<String> createCommand(List<String> cmd);
}

View File

@@ -0,0 +1,61 @@
package io.xpipe.core.store;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;
public interface StandardShellStore extends ShellStore {
static interface ShellType {
List<String> createFileReadCommand(String file);
List<String> createFileWriteCommand(String file);
List<String> createFileExistsCommand(String file);
Charset getCharset();
String getName();
}
default String executeAndRead(List<String> cmd) throws Exception {
var type = determineType();
var p = prepare(cmd).redirectErrorStream(true);
var proc = p.start();
var s = new String(proc.getInputStream().readAllBytes(), type.getCharset());
return s;
}
List<String> createCommand(List<String> cmd);
ShellType determineType();
@Override
default InputStream openInput(String file) throws Exception {
var type = determineType();
var cmd = type.createFileReadCommand(file);
var p = prepare(cmd).redirectErrorStream(true);
var proc = p.start();
return proc.getInputStream();
}
@Override
default OutputStream openOutput(String file) throws Exception {
var type = determineType();
var cmd = type.createFileWriteCommand(file);
var p = prepare(cmd).redirectErrorStream(true);
var proc = p.start();
return proc.getOutputStream();
}
@Override
default boolean exists(String file) throws Exception {
var type = determineType();
var cmd = type.createFileExistsCommand(file);
var p = prepare(cmd).redirectErrorStream(true);
var proc = p.start();
return proc.waitFor() == 0;
}
}

View File

@@ -37,7 +37,7 @@ public interface StreamDataStore extends DataStore {
throw new UnsupportedOperationException("Can't open store output");
}
default boolean canOpen() {
default boolean canOpen() throws Exception {
return true;
}

View File

@@ -1,7 +1,7 @@
package io.xpipe.core.store;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.AllArgsConstructor;
import lombok.Value;
import java.io.ByteArrayInputStream;
@@ -10,11 +10,15 @@ import java.nio.charset.StandardCharsets;
@Value
@JsonTypeName("string")
@AllArgsConstructor
public class StringStore implements StreamDataStore {
byte[] value;
@JsonCreator
public StringStore(byte[] value) {
this.value = value;
}
public StringStore(String s) {
value = s.getBytes(StandardCharsets.UTF_8);
}

View File

@@ -38,7 +38,7 @@ public class CoreJacksonModule extends SimpleModule {
new NamedType(LocalDirectoryDataStore.class),
new NamedType(CollectionEntryDataStore.class),
new NamedType(StringStore.class),
new NamedType(LocalMachineStore.class),
new NamedType(LocalStore.class),
new NamedType(NamedStore.class),
new NamedType(ValueType.class),