//*************************************************************** // // To Compile: // // From a PC command line (must have JDK 1.1.8 or higher) // // javac CommandServerJDQ.java -deprecation // //*************************************************************** // // This java class will execute PC commands via a Data Queue. // It will accept commands from an AS/400. When this class is // started, it will connect to the AS/400 and create a data // queue on the AS/400. AS/400 applications, such as those // written in RPG, can then send a PC command request, including // the fully qualified path name, to the data queue. Once it // arrives on the data queue it will be read and removed by this // class and executed by the java Runtime() class. // //*************************************************************** import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.lang.Process; import com.ibm.as400.access.*; public class CommandServerJDQ extends Frame { private TextArea commandWindow; private int ev = 0; private AS400 host = null; private String dqData = null; private String Command_To_Execute = ""; private byte [] IPKey; private boolean Continue = true; private int IPLen = 20; public CommandServerJDQ() { super ("CommandServerJDQ"); commandWindow = new TextArea( "", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY); add (commandWindow, BorderLayout.CENTER); setSize(400, 150); setVisible( true ); commandWindow.setEditable(false); commandWindow.setVisible(true); commandWindow.append("Connecting to AS/400..."); //********************************************************** // Get IP address of device CommandServerJDQ is running on // and then convert it from UNICODE to EBCDIC //********************************************************** try { InetAddress MyHost = InetAddress.getLocalHost(); String MyIP = MyHost.getHostAddress(); AS400Text IPValue = new AS400Text(20); IPKey = IPValue.toBytes(MyIP); } catch(UnknownHostException uh) { System.out.println("Unable to retrieve IP Address " + uh); System.exit(0); } } public void runDataQueueServer() { //**************************************** // Connect to the AS/400 //**************************************** host = new AS400(); commandWindow.append("\n...connected..."); //************************************* // Create the Keyed DataQueue object //************************************* KeyedDataQueue dq = new KeyedDataQueue(host, "/QSYS.LIB/YOURLIB.LIB/PCCMDDQ.DTAQ"); try { dq.create(20, 500); commandWindow.append("\nData Queue YOURLIB/PCCMDQ created\n"); } catch(Exception ce) { commandWindow.append("\nData Queue already exists."); } //***************************************************** // Loop to allow continuous command processing serving //***************************************************** while (Continue) { try { commandWindow.append("\nListening for commands..."); //***************************************************** // read data from the queue by the local IP address. // The "-1" parameter means to wait for an entry to // be placed on the Data Queue //***************************************************** KeyedDataQueueEntry dqData = dq.read(IPKey, -1, "EQ"); //**************************************************************** // Now extract the command from the Keyed Data Queue Entry object //***************************************************************** byte[] data = dqData.getData(); int Command_Length = 500; AS400Text textConverter = new AS400Text(Command_Length); String Cmd_Str = (String) textConverter.toObject(data); Command_To_Execute = Cmd_Str.trim(); commandWindow.append("\nCommand to be Executed = " + Command_To_Execute); //***************************** // Now execute the command //***************************** RunCommand(); } catch(AS400SecurityException se) { commandWindow.append("\nSecurity Exception " + se); Continue = false; } catch(ConnectionDroppedException cd) { commandWindow.append("\nConnection Dropped Exception " + cd); Continue = false; } catch(ErrorCompletingRequestException er) { commandWindow.append("\nError completing request " + er); Continue = false; } catch(IOException ioe) { commandWindow.append("\nIO Exception " +ioe); Continue = false; } catch(IllegalObjectTypeException io) { commandWindow.append("\nIlegal Object Type Exception " + io); Continue = false; } catch(InterruptedException ie) { commandWindow.append("\nInterrupted Exception " + ie); Continue = false; } catch(ObjectDoesNotExistException ne) { commandWindow.append("\nObject Does Not Exist Exception " + ne); Continue = false; } } // Disconnect using data queues host.disconnectService(AS400.DATAQUEUE); } //*************************************************** // Execute the PC command retrieved from Data Queue //*************************************************** public void RunCommand() { try { Runtime rt = Runtime.getRuntime(); Process ps = rt.exec(Command_To_Execute); try { ps.waitFor(); } catch (IllegalArgumentException ia) { commandWindow.append("Illegal Argument " + ia);} catch (InterruptedException i) { commandWindow.append("waitFor() method failed..." + i + "\n"); } ev = ps.exitValue(); if (ev == 1) {commandWindow.append("Command " + Command_To_Execute + "ended abnormally\n");} commandWindow.append("\n" + Command_To_Execute + " completed normally"); } catch (IOException e) { commandWindow.append("Command did not start :" + e + "\n"); } } //*************** // Main method //*************** public static void main (String args[]) { CommandServerJDQ s = new CommandServerJDQ(); s.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); s.runDataQueueServer(); } }