CommandReader

From Pickwiki
Revision as of 02:50, 6 April 2004 by Stuboy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Nasty bit of code but it works.


/**
* A simple test program to execute a U2 command and parse the output.
*/

import asjava.uniobjects.*;
import asjava.uniclientlibs.*;
import java.io.*;

public class CommandReader extends BufferedReader {

    public CommandReader(StringReader st) {
        super(st);
    }

    public static UniSession doLogin(String cmd[]) {

        UniSession uSession;
        UniJava uJava = new UniJava();

        try {
            uSession = uJava.openSession();
            uSession.connect(cmd[0],cmd[1],cmd[2],cmd[3]);
            System.out.print("Connected:"   + uSession.isActive()
                + " to:" + uSession.getHostName() + "/" + uSession.getAccountPath()
                + " cmd:" + cmd[4]); 
            if (cmd.length > 5) System.out.println(" parsing:" + cmd[5]);
                else            System.out.println("");
        } catch (UniSessionException e) {
            System.out.println("Error: " + e);
            uSession = null;
        }
        return uSession;
    }

    public static boolean doLogout(UniSession uSession) {

        try {
            // lets disconnect from the database.
            if (uSession.isActive()) uSession.disconnect();
        }
            catch (UniSessionException e) {
            System.out.println("Error: " + e);
        }
        return !uSession.isActive();
    }

    public static String doUniCommand(UniSession uSession, String cmd) {
        try {
            UniCommand uCmd = uSession.command();
            uCmd.setCommand(cmd);
            uCmd.exec();
            return uCmd.response();
        } catch (Exception e) {
            return "Cmd Error: " + e;
        }
    }

    public static   void main(String args[]) {
        if (args.length < 5) {
            System.out.println("Usage: java -classpath <classpath> CommandReader <host> <user> <pass> <account> <cmd> <filter>]");
            System.out.println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
            System.out.println("<host> - uniVerse host");
            System.out.println("<user> - the user id to log in with");
            System.out.println("<password> - the password");
            System.out.println("<account> - uniVerse account");
            System.out.println("<cmd> - command to process");
            System.out.println("<filter> - filter string");
            System.out.println("\neg. java CommandReader ironclad fkruger fubar2 UV \"SORT UV.ACCOUNT\" /usr/lpp");
            System.out.println();
            System.exit(1);
        } else {
            String strLine; int n = 0;
            UniSession uSession = doLogin(args);
            if (args.length > 5) {
                try {
                    CommandReader cr = new CommandReader(new StringReader(doUniCommand(uSession, args[4])));
                    cr.mark(1);
                    while (cr.read() > 0) {
                        cr.reset();
                        n++;
                        strLine = cr.readLine();
                        if (strLine.indexOf(args[5])    > 0) {
                            System.out.println(strLine);
                        }
                        cr.mark(1);
                    }
                    cr.close();
                }   catch (Exception e) {
                    System.out.println("ex " + e);
                }
            } else {
                System.out.println(doUniCommand(uSession,args[4]));
            }
            System.out.println("Disconnected:" + doLogout(uSession) +   " parsed:"  + n + " lines");
        }
    }
}