Skip to content

Commit

Permalink
Scripts class and shutdown operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Pozhidaev committed Mar 14, 2016
1 parent 7b7a071 commit c03b312
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion scripts/lwr-reboot
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash -e

/sbin/reboot
/sbin/reboot --force
2 changes: 1 addition & 1 deletion scripts/lwr-shutdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash -e

/sbin/poweroff
/sbin/poweroff --force
15 changes: 5 additions & 10 deletions src/main/java/org/luwrain/linux/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@ public class Linux implements org.luwrain.os.OperatingSystem
private static final String LUWRAIN_LINUX_LIBRARY_NAME = "luwrainlinux";

private Path scriptsDir;
private Scripts scripts = null;
private Hardware hardware;

/*
interface ChannelBasicData
{
String getType();
};
*/

@Override public boolean init(String dataDir)
{
NullCheck.notNull(dataDir, "dataDir");
System.loadLibrary(LUWRAIN_LINUX_LIBRARY_NAME);
scriptsDir = Paths.get(dataDir).resolve("scripts");
scripts = new Scripts(scriptsDir);
return true;
}

Expand All @@ -55,17 +50,17 @@ interface ChannelBasicData

@Override public boolean shutdown()
{
return false;
return scripts.runSync("lwr-shutdown", true);
}

@Override public boolean reboot()
{
return false;
return scripts.runSync("lwr-reboot", true);
}

@Override public boolean suspend(boolean hibernate)
{
return false;
return scripts.runSync("lwr-suspend", true);
}

@Override public void openFileInDesktop(Path path)
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/luwrain/linux/Scripts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

package org.luwrain.linux;

import java.nio.file.*;
import java.io.*;
import org.luwrain.core.*;

class Scripts
{
private Path scriptsDir;

Scripts(Path scriptsDir)
{
NullCheck.notNull(scriptsDir, "scriptsDir");
this.scriptsDir = scriptsDir;
}

boolean runSync(String scriptName, boolean sudo)
{
try {
final Process p = sudo?new ProcessBuilder("sudo", scriptsDir.resolve(scriptName).toString()).start():
new ProcessBuilder(scriptsDir.resolve(scriptName).toString()).start();
p.getOutputStream().close();
p.waitFor();
return p.exitValue() == 0;
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
return false;
}
catch(IOException e)
{
e.printStackTrace();
return false;
}
}
}

0 comments on commit c03b312

Please sign in to comment.