edu.upc.dama.dex.shell
Class CommandShell

java.lang.Object
  extended by edu.upc.dama.dex.shell.CommandShell
All Implemented Interfaces:
java.lang.Runnable

public class CommandShell
extends java.lang.Object
implements java.lang.Runnable

Implementantion of the Shell using a new thread. For non interactive usage. The communication method with the new shell is with pipes executing the commands one by one using the method runCommand. All the results are writen on the Writer parameter given when the CommandShell is constructed.

Check the method workToDo to know if the shell is still running any command.

Basic sample:


  DEX dex = new DEX();
  GraphPool gp = dex.open("pathToDexImage");
  Session s = gp.newSession();
  DbGraph g = s.getDbGraph();
  
  //Pipes used to get the result of the executions  
  PipedReader resultsPipeRead = new PipedReader();
  PipedWriter resultsPipeWrite = new PipedWriter(resultsPipeRead);
    
  CommandShell shell = new CommandShell(g, resultsPipeWrite);
  //New Thread
  Thread t = new Thread(shell);
  t.start();
  //Used to print the results
  char[] text = new char[50];
  int count;
  //Execute command
  shell.runCommand("print attributes\n");
  //Waiting while the shell is not finished and the result is not printed
  while (shell.workToDo() || resultsPipeRead.ready()){
    count = resultsPipeRead.read(text);
    for (int i = 0; i < count; i++)
        System.out.print(text[i]);
  }
  //To finish the thread of the shell is important end it closing the shell.
  shell.close();
  

Author:
Sparsity Technologies

Constructor Summary
CommandShell(DbGraph graph, java.io.Writer writer)
          Constructor of the new command shell implemented with a new thread.
 
Method Summary
 void close()
          Close the shell
 void run()
          Abstract method for the Runnable interface.
 void runCommand(java.lang.String command)
          Put a new command to execute on the shell.
 void stopCommand()
          Stop the execution of the current command.
 boolean workToDo()
          Getter to know if the shell is busy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandShell

public CommandShell(DbGraph graph,
                    java.io.Writer writer)
             throws java.io.IOException
Constructor of the new command shell implemented with a new thread.

Parameters:
graph - Graph used for the shell to execute the commands.
writer - Writer used for the shell to write the commands results.
Throws:
java.io.IOException - If bad things happen during the construction of the communication with the shell.
Method Detail

workToDo

public boolean workToDo()
Getter to know if the shell is busy.

Returns:
Return True if ther shell is busy executing any command.

runCommand

public void runCommand(java.lang.String command)
                throws java.io.IOException
Put a new command to execute on the shell.

Parameters:
command - Command to execute
Throws:
java.io.IOException - If bad things happen writing the command on the shell reader.

stopCommand

public void stopCommand()
Stop the execution of the current command.


run

public void run()
Abstract method for the Runnable interface. Start the new thread, running the command shell.

Specified by:
run in interface java.lang.Runnable

close

public void close()
           throws java.io.IOException
Close the shell

Throws:
java.io.IOException - If bad things happen during close