Some updates to Osal

This commit is contained in:
Ziver Koc 2013-12-19 19:57:06 +00:00
parent 19611e9fe7
commit c7674a50bb
3 changed files with 18 additions and 36 deletions

View file

@ -26,7 +26,7 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.LinkedList; import java.util.ArrayList;
/** /**
* User: Ziver * User: Ziver
@ -53,15 +53,15 @@ public abstract class OSAbstractionLayer {
} }
/** /**
* Executes a command and returns the result * Executes a command and returns the first line of the result
* *
* @param cmd the command to run * @param cmd the command to run
* @return the first line of the output of the command or null if there where no output * @return first line of the command
*/ */
protected String runCommand(String cmd) throws InterruptedException, IOException { protected String getFirstLineFromCommand(String cmd) throws InterruptedException, IOException {
LinkedList<String> output = runCommand(cmd, 0, 1); String[] tmp = runCommand(cmd);
if(!output.isEmpty()) if(tmp.length > 1)
return output.getFirst(); return tmp[0];
return null; return null;
} }
@ -69,39 +69,22 @@ public abstract class OSAbstractionLayer {
* Executes a command and returns the result * Executes a command and returns the result
* *
* @param cmd the command to run * @param cmd the command to run
* @param offsetLine the number of lines to skip at the beginning
* @return the first line after the offset of the output of the command or null if there where no output
*/
protected String runCommand(String cmd, int offsetLine) throws InterruptedException, IOException {
LinkedList<String> output = runCommand(cmd, offsetLine, 1);
if(!output.isEmpty())
return output.getFirst();
return null;
}
/**
* Executes a command and returns the result
*
* @param cmd the command to run
* @param offsetLine the number of lines to skip at the beginning
* @param lineCount the number of lines to read
* @return a String list of the output of the command * @return a String list of the output of the command
*/ */
protected LinkedList<String> runCommand(String cmd, int offsetLine, int lineCount) throws InterruptedException, IOException { public String[] runCommand(String cmd) throws InterruptedException, IOException {
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec( cmd ); Process proc = runtime.exec(cmd);
proc.waitFor(); proc.waitFor();
BufferedReader output = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader output = new BufferedReader(new InputStreamReader(proc.getInputStream()));
LinkedList<String> ret = new LinkedList<String>(); ArrayList<String> ret = new ArrayList<String>();
String line = ""; String line;
for(int i=0; (line = output.readLine()) != null && while((line = output.readLine()) != null){
(lineCount <= 0 || i < offsetLine+lineCount); i++) { ret.add(line);
if(i >= offsetLine)
ret.addLast(line);
} }
output.close(); output.close();
return ret;
return ret.toArray(new String[1]);
} }
/** /**

View file

@ -46,7 +46,7 @@ public class OsalLinuxImpl extends OSAbstractionLayer {
@Override @Override
public String getKernelVersion() { public String getKernelVersion() {
try{ try{
return super.runCommand("uname -r"); return super.getFirstLineFromCommand("uname -r");
} catch(Exception e){ } catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
@ -56,7 +56,7 @@ public class OsalLinuxImpl extends OSAbstractionLayer {
@Override @Override
public String getUsername() { public String getUsername() {
try{ try{
return super.runCommand("whoami"); return super.getFirstLineFromCommand("whoami");
} catch(Exception e){ } catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -23,7 +23,6 @@
package zutil.osal; package zutil.osal;
import java.io.File; import java.io.File;
import java.io.IOException;
/** /**
* User: ezivkoc * User: ezivkoc
@ -47,7 +46,7 @@ public class OsalWindowsImpl extends OSAbstractionLayer {
@Override @Override
public String getKernelVersion() { public String getKernelVersion() {
try { try {
return runCommand("ver"); return getFirstLineFromCommand("ver");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }