// IBM_PROLOG_BEGIN_TAG // This is an automatically generated prolog. // // bos720 src/bos/usr/samples/ahafs/samplePrograms/evMon/mon_1event.java 1.1 // // Licensed Materials - Property of IBM // // Restricted Materials of IBM // // COPYRIGHT International Business Machines Corp. 2009 // All Rights Reserved // // US Government Users Restricted Rights - Use, duplication or // disclosure restricted by GSA ADP Schedule Contract with IBM Corp. // // IBM_PROLOG_END_TAG import java.io.*; public class mon_1event { static String prog="java mon_1event"; private static void syntax() { System.out.println("\nSYNTAX:"+ prog +" [=] "); System.out.println(" where: "); System.out.println(" : Pathname of an AHA file with suffix \".mon\"."); System.out.println(" The possible keys and their values are:"); System.out.println(" --------------------------------------------------------------- "); System.out.println(" Keys | values | comments "); System.out.println(" =============================================================== "); System.out.println(" CHANGED | YES (default) | Monitors state-change. "); System.out.println(" -----------|--------------------------|------------------------ "); System.out.println(" THRESH_HI | Positive Integer | Monitors high threshold."); System.out.println(" ----------------------------------------------------------------\n"); System.out.println("Examples: "); System.out.println(" 1: "+ prog +" /aha/fs/utilFs.monFactory/var.mon \"THRESH_HI=95\""); System.out.println(" 2: "+ prog +" /aha/fs/modFile.monFactory/etc/passwd.mon \"CHANGED=YES\" "); System.out.println(" 3: "+ prog +" /aha/mem/vmo.monFactory/npskill.mon "); } public static void main(String args[]) { if (args.length < 1) { syntax(); return ; } // Create the file object File f = new File(args[0]); if (! f.exists()) // File does not exist. { String parent=f.getParent(); // See if the parent directory exists. if (parent != null) { // Create the parent directory object. File d = new File(parent); if (!d.exists()) // If it does not exist. { // Create the parent directory. if (! d.mkdirs()) { System.out.println("Directory "+ parent +" is not created!"); return; } } // Now create the file. try { f.createNewFile(); } catch (Exception e) { e.printStackTrace(); } } } System.out.println("Monitor file name :"+ args[0]); // Get the write string (i.e. interest) to monitor the event. String wrStr = "WAIT_TYPE=WAIT_IN_READ"; if ((args.length >= 2) && (args[1] != null)) { wrStr = wrStr + ";" + args[1]; } else { wrStr = wrStr + ";" + "CHANGED=YES"; } System.out.println("Write String :"+wrStr); // NOTE: Do not close the writer stream before reading the file using the // read() or its variant which is required to start monitoring the event. // If the writer stream is closed, the specified interest to monitor // the event is deleted and hence read() will not start monitoring // the event. // Open a writer and specify the inetrest. BufferedWriter bw = null; try { // Open the writer bw = new BufferedWriter(new FileWriter(f)); // Write the interest bw.write(wrStr, 0, wrStr.length()); // Flush the stream to make sure the interest is written. bw.flush(); } catch (Exception e) { e.printStackTrace(); } // Open a reader and wait for the occurrence of event using read() or its variant. BufferedReader br = null; try { // Open the reader br = new BufferedReader(new FileReader(f)); System.out.println("Entering read() to wait till the event corresponding to the AHA node " +args[0]+" occurs."); System.out.println("Please issue a command from another window to trigger this event.\n"); String rdStr = null; // Wait for the event to occur by reading the input stream. while ( (rdStr = br.readLine()) != null) { // Print the content System.out.println(rdStr); } } catch (Exception e) { e.printStackTrace(); } // Now, close the reader and writer streams. if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); } catch (Exception e) { e.printStackTrace(); } } } // main }