//ConsoleWindow.java import java.io.*; import java.awt.*; import java.awt.event.*; /** * * * This class implements a simple console window, conceptually a replacement for * reading/writing to System.out and System.in. * * The intended use is as a shell for messages and simple command * processing. * */ public class ConsoleWindow extends java.awt.Frame implements WindowListener { private KeyboardBuffer area; private InputStreamReader input; private BufferedReader bufferedinput; private PrintWriter output; private boolean dead = false; String fname; int fstyle; int fsize; static int default_bufferchars = 10000; int bufferchars; public ConsoleWindow (String title) { this(title, default_bufferchars); } public ConsoleWindow (String title, int bufferchars) { // Initialize this.bufferchars = bufferchars; area = new KeyboardBuffer("",30,80,bufferchars); fname = "Courier"; fstyle = Font.PLAIN; fsize = 12; // Show window area.setFont(new Font(fname, fstyle, fsize)); setTitle(title); add("Center", area); pack(); show(); addWindowListener(this); } // Handle system event public void dispose () { dead=true;//System.out.println("declared dead"); area.die(); super.dispose(); } public PrintWriter PrintWriter () { if(output==null) { output = area.PrintWriter(); } return(output); } /** * get a InputStream corresponding to the typein area if this window. * Note that the input from the InputStream will be line buffered, so no * input is available from incomplete lines. * */ public InputStreamReader InputStreamReader () { if(input==null) { input = area.InputStreamReader();} return(input); } /** * get a BufferedReader corresponding to the typein area if this window. * Note that the input from the BufferedReader will be line buffered, so no * input is available from incomplete lines. * */ public BufferedReader BufferedReader () { if(bufferedinput==null) { bufferedinput = new BufferedReader(InputStreamReader()); } return(bufferedinput); } /** * returns true if the console window has been activated and is visible */ public boolean Active () { return(!dead); } /** * wait until the console window has been closed */ public void Wait_For_Finish () throws InterruptedException { while(Active()) { Thread.sleep(1000); } } /** * unconditionally wait until the console window has been closed */ public void Always_Wait_For_Finish () { try {Wait_For_Finish(); } catch (InterruptedException err) {} } /* handle window events */ public void windowActivated(WindowEvent ev) {} public void windowDeactivated(WindowEvent ev) {} public void windowClosed(WindowEvent ev) { System.exit(0); } public void windowClosing(WindowEvent ev) { System.exit(0); } public void windowDeiconified(WindowEvent ev) {} public void windowIconified(WindowEvent ev) {} public void windowOpened(WindowEvent ev) {} public static void main (String args[]) { String name = args.length >= 1 ? args[0] : "OSVM Console"; ConsoleWindow console = new ConsoleWindow(name); //---- returns KeyboardBuffer.PrintWriter() ---- PrintWriter out = console.PrintWriter(); BufferedReader in = console.BufferedReader(); String cmdargs = new String(); // Arguments Passed to Workers PipedInputStream inpipe = null; PipedOutputStream outpipe = null; WorkerThread myclass = null; FileClassLoader fileclass = null; fileclass = new FileClassLoader(); fileclass.bindir = "C:\\windows\\desktop\\OS\\bin\\"; Class newclass = null; out.println("Welcome to OSVM."); out.println("Type to exit."); out.println("ready"); while (console.Active()) { out.print("> "); out.flush(); try { String str = in.readLine(); Parser line = new Parser(); if (str.compareTo("quit") == 0) { console.dispose(); System.exit(0); } line.prepareWorker(inpipe,outpipe,cmdargs); line.start(); try{ line.Parse(str); } catch (Exception e){ out.println( e.getMessage()); out.flush(); } if(str!=null) { //out.println("Typed : " + str); try { newclass = fileclass.loadCmd(str); out.println("Loaded class " + newclass.getName() ); try { myclass = (WorkerThread) newclass.newInstance(); inpipe = new PipedInputStream(); outpipe = new PipedOutputStream(); ((WorkerThread)myclass).prepareWorker(inpipe,outpipe,cmdargs); ((WorkerThread)myclass).start(); // Must make new thread to read pipe data and display it to Console // Stalling this thread so other thread can output to pipe Thread.currentThread().sleep(500); // BUG Must use wait and notifyall to freeze this thread byte[] pipedata = new byte[inpipe.available()]; // Pipe Return Buffer of Specified Size out.println("Pipe Data Available: " + inpipe.available()); if(inpipe.read(pipedata,0,inpipe.available()) != -1) { //Closes Pipes used by previously created worker thread inpipe.close(); outpipe.close(); String strdata = new String(pipedata); //SOLVEDBUG Printing to console.out is locking some thread //Excess buffer length created string with many null characters //that froze bufferreader object out.println(strdata); } } catch(Exception ex) { out.println("Failed to instantiate worker"); ex.printStackTrace(); } } catch(Exception ex) { // out.println("Command not recognized or not found."); //ex.printStackTrace(); } }// End of If Not Null Str } catch (IOException err) { //if(console.Active()) //{ System.out.println("Exception " + err); //} //err.printStackTrace(); } }// End of Main Console Line Input Loop console.Always_Wait_For_Finish(); //System.exit(0); } }; class FileClassLoader extends ClassLoader { public String bindir; public Class loadCmd(String name) throws ClassNotFoundException { // loadClass() calls findClass() //This makes Case not apply, worker class names should be lowercase name = name.toLowerCase(); Class newclass = loadClass(name,true); return newclass; } protected Class findClass(String name) throws ClassNotFoundException { FileInputStream fi = null; try { //System.out.println("Finding class: " + name); fi = new FileInputStream(bindir + name + ".impl"); byte[] classBytes = new byte[fi.available()]; fi.read(classBytes); return defineClass(name, classBytes, 0, classBytes.length); } catch (Exception e) { throw new ClassNotFoundException(name); } finally { if ( null != fi ) { try { fi.close(); } catch (Exception e){} } } } };