FileSystem and Screen lib realization [try 2]
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
145
filesystem/src/main/java/FileSystemLib.java
Normal file
145
filesystem/src/main/java/FileSystemLib.java
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File System proxy lib
|
||||||
|
* <p>
|
||||||
|
* Created by daniil on 02.04.16.
|
||||||
|
*/
|
||||||
|
public class FileSystemLib {
|
||||||
|
/**
|
||||||
|
* Copy data from InputStream to OutputStream
|
||||||
|
*
|
||||||
|
* @param from Input Stream
|
||||||
|
* @param to Output Stream
|
||||||
|
* @return size of copied data
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
protected static long copy(InputStream from, OutputStream to) throws IOException {
|
||||||
|
assert from != null;
|
||||||
|
assert to != null;
|
||||||
|
byte[] buf = new byte[4096];
|
||||||
|
long total = 0L;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
int r = from.read(buf);
|
||||||
|
if (r == -1) {
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
to.write(buf, 0, r);
|
||||||
|
total += (long) r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy file or directory from one direction to another
|
||||||
|
*
|
||||||
|
* @param from File to copy
|
||||||
|
* @param to Destination
|
||||||
|
* @param replace replace the destination file if present?
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void copy(Path from, Path to, boolean replace) throws IOException {
|
||||||
|
assert from != null;
|
||||||
|
assert to != null;
|
||||||
|
|
||||||
|
if (!replace)
|
||||||
|
Files.copy(from, to);
|
||||||
|
else
|
||||||
|
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete file
|
||||||
|
*
|
||||||
|
* @param file file to delete
|
||||||
|
* @throws IOException, FileNotFoundException
|
||||||
|
*/
|
||||||
|
public void delete(File file) throws IOException {
|
||||||
|
assert file != null;
|
||||||
|
|
||||||
|
if (!file.exists())
|
||||||
|
throw new FileNotFoundException("Unable to delete nonexistent file");
|
||||||
|
if (!file.delete())
|
||||||
|
throw new IOException("Unable to delete this file");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a symlink
|
||||||
|
*
|
||||||
|
* @param linkFile Where symlink should be created
|
||||||
|
* @param actualFile Where should it point to
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void createSymlink(Path linkFile, Path actualFile) throws IOException {
|
||||||
|
assert linkFile != null;
|
||||||
|
assert actualFile != null;
|
||||||
|
|
||||||
|
if (!actualFile.toFile().exists())
|
||||||
|
throw new FileNotFoundException("Couldn't find symlink destination");
|
||||||
|
|
||||||
|
Files.createSymbolicLink(linkFile, actualFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void restoreFromResources(String resourceName, File destination, boolean replace) throws IOException {
|
||||||
|
assert resourceName != null;
|
||||||
|
assert destination != null;
|
||||||
|
|
||||||
|
if (destination.exists() && !replace)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
if (!destination.createNewFile())
|
||||||
|
throw new IOException("Unable to create new file");
|
||||||
|
|
||||||
|
try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(resourceName);
|
||||||
|
OutputStream os = new FileOutputStream(destination)) {
|
||||||
|
copy(is, os);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IOException("Unable to restore file from resource");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get FileReader for the file
|
||||||
|
* <p>
|
||||||
|
* We need this to make the code testable
|
||||||
|
*
|
||||||
|
* @param file file to read
|
||||||
|
* @return FileReader
|
||||||
|
* @throws FileNotFoundException
|
||||||
|
*/
|
||||||
|
public Reader getReader(File file) throws FileNotFoundException {
|
||||||
|
assert file != null;
|
||||||
|
|
||||||
|
return new FileReader(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get FileWriter for the file
|
||||||
|
* <p>
|
||||||
|
* We need this to make the code testable
|
||||||
|
*
|
||||||
|
* @param file file to write to
|
||||||
|
* @return FileWriter
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public Writer getWriter(File file) throws IOException {
|
||||||
|
return new FileWriter(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unzip archive to directory
|
||||||
|
*
|
||||||
|
* @param archive archive to extract
|
||||||
|
* @param directory destination
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void unzip(String archive, String directory) throws IOException {
|
||||||
|
UnzipUtility zip = new UnzipUtility();
|
||||||
|
zip.unzip(archive, directory);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
65
screen/src/main/java/ScreenLib.java
Normal file
65
screen/src/main/java/ScreenLib.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realization of class dedicated to the control
|
||||||
|
* of GNU Screen sessions
|
||||||
|
* <p>
|
||||||
|
* Created by daniil on 02.04.16.
|
||||||
|
*/
|
||||||
|
public class ScreenLib {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a screen with certain ID
|
||||||
|
* and execute a command inside it
|
||||||
|
*
|
||||||
|
* @param screenName Screen ID
|
||||||
|
* @param runPath Home path of started screen
|
||||||
|
* @param command Command [+args] to execute inside the screen
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void startScreen(String screenName, Path runPath, String... command) throws IOException {
|
||||||
|
assert screenName != null && screenName.length() != 0;
|
||||||
|
assert runPath != null;
|
||||||
|
|
||||||
|
// Merge two arrays
|
||||||
|
String[] commandBase = new String[command.length + 3];
|
||||||
|
commandBase[0] = "screen";
|
||||||
|
commandBase[1] = "-dmS";
|
||||||
|
commandBase[2] = screenName;
|
||||||
|
|
||||||
|
System.arraycopy(command, 0, commandBase, 3, command.length);
|
||||||
|
|
||||||
|
ProcessBuilder processBuilder = new ProcessBuilder(commandBase);
|
||||||
|
processBuilder.directory(runPath.toFile());
|
||||||
|
processBuilder.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send command to specific screen
|
||||||
|
*
|
||||||
|
* @param screenName Screen ID
|
||||||
|
* @param command Command to send
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void sendCommand(String screenName, String command) throws IOException {
|
||||||
|
assert screenName != null;
|
||||||
|
assert command != null;
|
||||||
|
|
||||||
|
ProcessBuilder processBuilder = new ProcessBuilder("screen", "-S", screenName, "-p", "0", "-X", "stuff", String.format(command + "%n"));
|
||||||
|
processBuilder.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Force terminate a screen session
|
||||||
|
*
|
||||||
|
* @param screenName Screen ID
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void killScreen(String screenName) throws IOException {
|
||||||
|
assert screenName != null;
|
||||||
|
|
||||||
|
ProcessBuilder processBuilder = new ProcessBuilder("screen", "-X", "-S", screenName, "quit");
|
||||||
|
processBuilder.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>arcadexsystem</artifactId>
|
<artifactId>arcadexsystem</artifactId>
|
||||||
|
|||||||
Reference in New Issue
Block a user