ca.unbc.cpsc101
Class JavaPipe

java.lang.Object
  extended by ca.unbc.cpsc101.JavaPipe

public class JavaPipe
extends Object

The JavaPipe provides a means to start a separate java program and communicate with its System.in and System.out using Readers and Writers. The JavaPipe class uses the Process and ProcessBuilder classes to create a process that runs on the other side of the JavaPipe.


Field Summary
static String DEFAULT_EXEC_PATH
          The default PATH used to search for the java executable.
static String version
          describes the version of the JavaPipe class.
 
Constructor Summary
JavaPipe(String className)
          Sets up, but does not start a JavaPipe.
 
Method Summary
 JavaPipe destroy()
          This method provides an explicit means by which to release the resources acquired by a JavaPipe.
 String getClassPath()
          returns the CLASSPATH that is inserted in the environment when starting java for this JavaPipe.
 String getExecPath()
          This is the path that is used to locate the java executable.
 Reader getInputReader()
          This Reader provides the text that the corresponding process writes on System.out.
 PrintWriter getOutputWriter()
          This Writer provides the text written to it to the corresponding processes’ System.in.
 JavaPipe setClassPath(String cp)
          set the CLASSPATH environment variable to be used when starting the java process for this JavaPipe.
 JavaPipe setExecPath(String ep)
          Set the path used to find the java executable.
 JavaPipe start()
          creates a java process and connects pipes for communication with it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

version

public static final String version
describes the version of the JavaPipe class.

Since:
2.0
See Also:
Constant Field Values

DEFAULT_EXEC_PATH

public static final String DEFAULT_EXEC_PATH
The default PATH used to search for the java executable. Unless setExecPath is called before start, this is where the java executable is searched for.

See Also:
setExecPath., Constant Field Values
Constructor Detail

JavaPipe

public JavaPipe(String className)
Sets up, but does not start a JavaPipe.

Between the creation of the JavaPipe and when it is started, .setExecPath() can be used to change the path where the java command is found, and .setClassPath(...) can be used to set the CLASSPATH variable used by java to find the requested class.

Note that the constructor does not begin execution of java, and does not check the legitimacy of the className argument.

Parameters:
className - specifies the name of the class whose main is to be run to create the pipe.
See Also:
start
Method Detail

getClassPath

public String getClassPath()
returns the CLASSPATH that is inserted in the environment when starting java for this JavaPipe. This influences where the java runtime looks for its .class argument.

Returns:
the CLASSPATH variable to be used by this Pipe.

setClassPath

public JavaPipe setClassPath(String cp)
set the CLASSPATH environment variable to be used when starting the java process for this JavaPipe.

Parameters:
cp - the value to which to set the CLASSPATH.
Returns:
the JavaPipe that called this object. (allows for “chained” calls like fred.setClassPath(".:..").start().

getExecPath

public String getExecPath()
This is the path that is used to locate the java executable. When the start method is executed, JavaPipe attempts to start another java session remotely over a pipe using the path specified by getExecPath.

Returns:
the path used to find the java command.

setExecPath

public JavaPipe setExecPath(String ep)
Set the path used to find the java executable. When the start method is executed, JavaPipe attempts to start another java session remotely over a pipe using the path last path specified by setExecPath. The default is DEFAULT_EXEC_PATH.

Parameters:
ep - the value to which to set the PATH on which java will be found.
Returns:
the JavaPipe that called this object. (allows for “chained” calls like fred.setExecPath(".:..").start().

start

public JavaPipe start()
               throws IOException
creates a java process and connects pipes for communication with it. The associated process reads from System.in what this JavaPipe writes on this.getOutputWriter(), and what it prints on System.out appears on this JavaPipe’s this.getInputReader().

Returns:
the JavaPipe that called this object. (allows for “chained” calls like fred..start().getInputReader().
Throws:
IOException - when the start method of the underlying ProcessBuilder does.
IllegalThreadStateException - when this method is able to detect early failure of the java program (perhaps due to being unable to find the specified class). This is not guaranteed to work. Some errors may not be detected until attempting perform i/o with the JavaPipe.

destroy

public JavaPipe destroy()
This method provides an explicit means by which to release the resources acquired by a JavaPipe. It first closes the pipe connections to the process, and then kills the process itself.

Returns:
the JavaPipe that called this object. (allows for “chained” calls like fred.destroy().start().

getInputReader

public Reader getInputReader()
This Reader provides the text that the corresponding process writes on System.out. valid between the time the JavaPipe is .start()ed and when it is .destroy()ed, provided that the corresponding process is well behaved and does not quit.

Returns:
a Reader that is connected to the output of the process on the other side of the pipe.

getOutputWriter

public PrintWriter getOutputWriter()
This Writer provides the text written to it to the corresponding processes’ System.in. valid between the time the JavaPipe is .start()ed and when it is .destroy()ed, provided that the corresponding process is well behaved and doesn't quit.

Returns:
a Writer that is connected to the input of the process on the other side of the pipe.