CommandReader: Difference between revisions
From Pickwiki
Jump to navigationJump to search
mNo edit summary |
m link fix |
||
Line 11: | Line 11: | ||
import java.io.*; | import java.io.*; | ||
public class CommandReader extends BufferedReader { | public class [[CommandReader]] extends [[BufferedReader]] { | ||
public CommandReader(StringReader st) { | public [[CommandReader]]([[StringReader]] st) { | ||
super(st); | super(st); | ||
} | } | ||
public static UniSession doLogin(String cmd[]) { | public static [[UniSession]] doLogin(String cmd[]) { | ||
UniSession uSession; | [[UniSession]] uSession; | ||
UniJava uJava = new UniJava(); | [[UniJava]] uJava = new [[UniJava]](); | ||
try { | try { | ||
Line 26: | Line 26: | ||
uSession.connect(cmd[0],cmd[1],cmd[2],cmd[3]); | uSession.connect(cmd[0],cmd[1],cmd[2],cmd[3]); | ||
System.out.print("Connected:" + uSession.isActive() | System.out.print("Connected:" + uSession.isActive() | ||
+ " to:" + uSession. | + " to:" + uSession.get[[HostName]]() + "/" + uSession.get[[AccountPath]]() | ||
+ " cmd:" + cmd[4]); | + " cmd:" + cmd[4]); | ||
if (cmd.length > 5) System.out.println(" parsing:" + cmd[5]); | if (cmd.length > 5) System.out.println(" parsing:" + cmd[5]); | ||
else System.out.println(""); | else System.out.println(""); | ||
} catch (UniSessionException e) { | } catch ([[UniSessionException]] e) { | ||
System.out.println("Error: " + e); | System.out.println("Error: " + e); | ||
uSession = null; | uSession = null; | ||
Line 37: | Line 37: | ||
} | } | ||
public static boolean doLogout(UniSession uSession) { | public static boolean doLogout([[UniSession]] uSession) { | ||
try { | try { | ||
Line 43: | Line 43: | ||
if (uSession.isActive()) uSession.disconnect(); | if (uSession.isActive()) uSession.disconnect(); | ||
} | } | ||
catch (UniSessionException e) { | catch ([[UniSessionException]] e) { | ||
System.out.println("Error: " + e); | System.out.println("Error: " + e); | ||
} | } | ||
Line 49: | Line 49: | ||
} | } | ||
public static String | public static String do[[UniCommand]]([[UniSession]] uSession, String cmd) { | ||
try { | try { | ||
UniCommand uCmd = uSession.command(); | [[UniCommand]] uCmd = uSession.command(); | ||
uCmd.setCommand(cmd); | uCmd.setCommand(cmd); | ||
uCmd.exec(); | uCmd.exec(); | ||
Line 62: | Line 62: | ||
public static void main(String args[]) { | public static void main(String args[]) { | ||
if (args.length < 5) { | if (args.length < 5) { | ||
System.out.println("Usage: java -classpath <classpath> CommandReader <host> <user> <pass> <account> <cmd> <filter>]"); | 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("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar"); | ||
System.out.println("<host> - uniVerse host"); | System.out.println("<host> - uniVerse host"); | ||
Line 70: | Line 70: | ||
System.out.println("<cmd> - command to process"); | System.out.println("<cmd> - command to process"); | ||
System.out.println("<filter> - filter string"); | System.out.println("<filter> - filter string"); | ||
System.out.println("\neg. java CommandReader ironclad fkruger fubar2 UV \"SORT UV.ACCOUNT\" /usr/lpp"); | System.out.println("\neg. java [[CommandReader]] ironclad fkruger fubar2 UV \"SORT UV.ACCOUNT\" /usr/lpp"); | ||
System.out.println(); | System.out.println(); | ||
System.exit(1); | System.exit(1); | ||
} else { | } else { | ||
String strLine; int n = 0; | String strLine; int n = 0; | ||
UniSession uSession = doLogin(args); | [[UniSession]] uSession = doLogin(args); | ||
if (args.length > 5) { | if (args.length > 5) { | ||
try { | try { | ||
CommandReader cr = new CommandReader(new StringReader( | [[CommandReader]] cr = new [[CommandReader]](new [[StringReader]](do[[UniCommand]](uSession, args[4]))); | ||
cr.mark(1); | cr.mark(1); | ||
while (cr.read() > 0) { | while (cr.read() > 0) { | ||
Line 94: | Line 94: | ||
} | } | ||
} else { | } else { | ||
System.out.println( | System.out.println(do[[UniCommand]](uSession,args[4])); | ||
} | } | ||
System.out.println("Disconnected:" + doLogout(uSession) + " parsed:" + n + " lines"); | System.out.println("Disconnected:" + doLogout(uSession) + " parsed:" + n + " lines"); |
Latest revision as of 23:48, 26 February 2015
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.get[[HostName]]() + "/" + uSession.get[[AccountPath]]() + " 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 do[[UniCommand]]([[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]](do[[UniCommand]](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(do[[UniCommand]](uSession,args[4])); } System.out.println("Disconnected:" + doLogout(uSession) + " parsed:" + n + " lines"); } } }