From: Bill Erickson Date: Thu, 10 Nov 2016 16:43:45 +0000 (-0500) Subject: Native Messaging WIP -- more tests, set() expects objects X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=d70b372e4f843b9c8338112583840e4ccdd7c0d1;p=working%2FHatch.git Native Messaging WIP -- more tests, set() expects objects Signed-off-by: Bill Erickson --- diff --git a/run.sh b/run.sh index e6d64e122a..272bfe0731 100755 --- a/run.sh +++ b/run.sh @@ -1,23 +1,33 @@ +#!/bin/bash + JAVA_HOME=jdk1.8 JAVA=$JAVA_HOME/bin/java -CP=lib:lib/json-20160810.jar -#LOGS="-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'" -#LOGS=-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n' LOGS=-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n' -# compile -$JAVA_HOME/bin/javac -Xlint:unchecked -cp $CP -d lib src/org/evergreen_ils/hatch/*.java +COMMAND="$1" + +if [ "$COMMAND" == "compile" ]; then + + $JAVA_HOME/bin/javac -Xdiags:verbose -Xlint:unchecked \ + -cp lib:lib/\* -d lib src/org/evergreen_ils/hatch/*.java + +elif [ "$COMMAND" == "test" ]; then + + # 1. Run TestHatch in (default) send mode, which emits JSON requests + # 2. Run Hatch and process messages emitted from #1. + # 3. Run TestHatch in receive mode to log the responses. + + $JAVA "$LOGS" -cp lib:lib/\* org.evergreen_ils.hatch.TestHatch \ + | $JAVA "$LOGS" -cp lib:lib/\* org.evergreen_ils.hatch.Hatch \ + | $JAVA "$LOGS" -cp lib:lib/\* org.evergreen_ils.hatch.TestHatch receive -[ -z "$1" ] && exit; +elif [ "$COMMAND" == "run" ]; then -if [ "$1" == "1" ]; then + # run Hatch + $JAVA "$LOGS" -cp lib:lib/\* org.evergreen_ils.hatch.Hatch - # run - $JAVA "$LOGS" -cp $CP org.evergreen_ils.hatch.Hatch +else -else + echo "Usage: $0 [compile|test|run]"; - $JAVA "$LOGS" -cp $CP org.evergreen_ils.hatch.TestHatch \ - | $JAVA "$LOGS" -cp $CP org.evergreen_ils.hatch.Hatch \ - | $JAVA "$LOGS" -cp $CP org.evergreen_ils.hatch.TestHatch receive fi; diff --git a/src/org/evergreen_ils/hatch/PrintManager.java b/src/org/evergreen_ils/hatch/PrintManager.java index 98c8bdc993..ae9709bd86 100644 --- a/src/org/evergreen_ils/hatch/PrintManager.java +++ b/src/org/evergreen_ils/hatch/PrintManager.java @@ -48,36 +48,63 @@ public class PrintManager { static final Logger logger = Hatch.getLogger(); /** - * Shows the print dialog, allowing the user to modify settings, - * but performs no print. + * Returns all known Printer's. * - * @param params Print request parameters. This is the top-level - * request object, containing the action, etc. Within 'params' - * will be a sub-object under the "config" key, which contains - * the Printer configuration options (if any are already set). - * @return A Map of printer settings extracted from the print dialog. + * @return Array of all printers */ - public JSONObject configurePrinter( - JSONObject params) throws IllegalArgumentException { + protected Printer[] getPrinters() { + ObservableSet printerObserver = Printer.getAllPrinters(); - JSONObject settings = params.getJSONObject("config"); + if (printerObserver == null) return new Printer[0]; - PrinterJob job = buildPrinterJob(settings); - - boolean approved = job.showPrintDialog(null); + return (Printer[]) printerObserver.toArray(new Printer[0]); + } - // no printing needed - job.endJob(); + /** + * Returns a list of all known printers, with their attributes + * encoded as a simple key/value Map. + * + * @return Map of printer information. + */ + protected List> getPrintersAsMaps() { + Printer[] printers = getPrinters(); - if (approved) { - // extract modifications to the settings applied within the dialog - return extractSettingsFromJob(job); - } else { - // return the unmodified settings back to the caller - return settings; + List> printerMaps = + new LinkedList>(); + + Printer defaultPrinter = Printer.getDefaultPrinter(); + + for (Printer printer : printers) { + HashMap printerMap = new HashMap(); + printerMaps.add(printerMap); + printerMap.put("name", printer.getName()); + if (defaultPrinter != null && + printer.getName().equals(defaultPrinter.getName())) { + printerMap.put("is-default", new Boolean(true)); + } + } + + return printerMaps; + } + + + /** + * Returns the Printer with the specified name. + * + * @param name The printer name + * @return The printer whose name matches the provided name, or null + * if no such printer is found. + */ + protected Printer getPrinterByName(String name) { + Printer[] printers = getPrinters(); + for (Printer printer : printers) { + if (printer.getName().equals(name)) + return printer; } + return null; } + /** * Print the requested page using the provided settings * @@ -298,138 +325,5 @@ public class PrintManager { } } - /** - * Extracts and flattens the various configuration values from a - * PrinterJob and its associated printer and stores the values in a Map. - * - * @param job The PrinterJob whose attributes are to be extracted. - * @return The extracted printer settings map. - */ - protected JSONObject extractSettingsFromJob(PrinterJob job) { - JSONObject settings = new JSONObject(); - JobSettings jobSettings = job.getJobSettings(); - - //logger.info("Extracting print job settings from " + job); - - settings.put( - jobSettings.collationProperty().getName(), - jobSettings.collationProperty().getValue() - ); - settings.put( - jobSettings.copiesProperty().getName(), - jobSettings.copiesProperty().getValue() - ); - settings.put( - "paperSource", - jobSettings.getPaperSource().getName() - ); - settings.put( - jobSettings.printColorProperty().getName(), - jobSettings.printColorProperty().getValue() - ); - settings.put( - jobSettings.printQualityProperty().getName(), - jobSettings.printQualityProperty().getValue() - ); - settings.put( - jobSettings.printSidesProperty().getName(), - jobSettings.printSidesProperty().getValue() - ); - - // nested properties... - - // page layout -------------- - PageLayout layout = jobSettings.getPageLayout(); - JSONObject layoutMap = new JSONObject(); - layoutMap.put("bottomMargin", layout.getBottomMargin()); - layoutMap.put("leftMargin", layout.getLeftMargin()); - layoutMap.put("topMargin", layout.getTopMargin()); - layoutMap.put("rightMargin", layout.getRightMargin()); - layoutMap.put("pageOrientation", layout.getPageOrientation().toString()); - layoutMap.put("printableHeight", layout.getPrintableHeight()); - layoutMap.put("printableWidth", layout.getPrintableWidth()); - layoutMap.put("paper", layout.getPaper().getName()); - - settings.put("pageLayout", layoutMap); - - // page ranges -------------- - PageRange[] ranges = jobSettings.getPageRanges(); - if (ranges != null) { - JSONArray pageRanges = new JSONArray(); - - if (ranges.length == 1 && - ranges[0].getStartPage() == 1 && - ranges[0].getEndPage() == Integer.MAX_VALUE) { - // full range -- no need to store - - } else { - for (PageRange range : ranges) { - pageRanges.put(range.getStartPage()); - pageRanges.put(range.getEndPage()); - } - settings.put("pageRanges", pageRanges); - } - } - - return settings; - } - - /** - * Returns all known Printer's. - * - * @return Array of all printers - */ - protected Printer[] getPrinters() { - ObservableSet printerObserver = Printer.getAllPrinters(); - - if (printerObserver == null) return new Printer[0]; - - return (Printer[]) printerObserver.toArray(new Printer[0]); - } - - /** - * Returns a list of all known printers, with their attributes - * encoded as a simple key/value Map. - * - * @return Map of printer information. - */ - protected List> getPrintersAsMaps() { - Printer[] printers = getPrinters(); - - List> printerMaps = - new LinkedList>(); - - Printer defaultPrinter = Printer.getDefaultPrinter(); - - for (Printer printer : printers) { - HashMap printerMap = new HashMap(); - printerMaps.add(printerMap); - printerMap.put("name", printer.getName()); - if (defaultPrinter != null && - printer.getName().equals(defaultPrinter.getName())) { - printerMap.put("is-default", new Boolean(true)); - } - //logger.info("found printer " + printer.getName()); - } - - return printerMaps; - } - - - /** - * Returns the Printer with the specified name. - * - * @param name The printer name - * @return The printer whose name matches the provided name, or null - * if no such printer is found. - */ - protected Printer getPrinterByName(String name) { - Printer[] printers = getPrinters(); - for (Printer printer : printers) { - if (printer.getName().equals(name)) - return printer; - } - return null; - } } diff --git a/src/org/evergreen_ils/hatch/RequestHandler.java b/src/org/evergreen_ils/hatch/RequestHandler.java index 22064fa0a7..548ef827c9 100644 --- a/src/org/evergreen_ils/hatch/RequestHandler.java +++ b/src/org/evergreen_ils/hatch/RequestHandler.java @@ -21,7 +21,7 @@ public class RequestHandler extends Thread { /** Origin host/domain used for segregating files by browser host */ private static String origin = null; - void configure() { + private void configure() { // Find the profile directory. // The profile directory + origin string represent the base @@ -41,7 +41,7 @@ public class RequestHandler extends Thread { * @return True if the calling code should avoid calling reply() with * the response object. */ - boolean dispatchRequest( + private boolean dispatchRequest( JSONObject request, JSONObject response) throws JSONException { long msgid = request.getLong("msgid"); @@ -100,47 +100,32 @@ public class RequestHandler extends Thread { String val = fileIO.get(key); if (val != null) { - // set() stores bare JSON strings, but the caller to - // get() expects a unified JSON object in return, not - // a JSON string inside a JSON object. Parse the - // JSON and add it to the response object. + // Translate the JSON string stored by set() into a + // Java object that can be added to the response. Object jsonBlob = new JSONTokener(val).nextValue(); response.put("content", jsonBlob); } - - break; - - case "remove": - key = request.getString("key"); - - if (!fileIO.remove(key)) { - response.put("status", 500); - response.put("message", "Unable to remove key: " + key); - } - break; case "set" : key = request.getString("key"); - // content is a JSON string - content = request.getString("content"); - if (!fileIO.set(key, content)) { + // JSON-ify the thing stored under "content" + String json = JSONObject.valueToString(request.get("content")); + + if (!fileIO.set(key, json)) { response.put("status", 500); response.put("message", "Unable to set key: " + key); } - break; - case "append" : + case "remove": key = request.getString("key"); - content = request.getString("content"); - if (!fileIO.append(key, content)) { + if (!fileIO.remove(key)) { response.put("status", 500); - response.put("message", "Unable to append to key: " + key); + response.put("message", "Unable to remove key: " + key); } - break; default: diff --git a/src/org/evergreen_ils/hatch/TestHatch.java b/src/org/evergreen_ils/hatch/TestHatch.java index 7e75099d1d..240d1e579f 100644 --- a/src/org/evergreen_ils/hatch/TestHatch.java +++ b/src/org/evergreen_ils/hatch/TestHatch.java @@ -7,7 +7,7 @@ public class TestHatch { static MessageIO io; static final Logger logger = Hatch.getLogger(); - public static void rest() { + public static void pause() { try { Thread.sleep(1000); } catch (InterruptedException e) {} @@ -23,7 +23,7 @@ public class TestHatch { obj.put("origin", "https://test.hatch.evergreen-ils.org"); io.sendMessage(obj); - rest(); + pause(); // get a list of stored keys obj = new JSONObject(); @@ -31,18 +31,17 @@ public class TestHatch { obj.put("action", "keys"); io.sendMessage(obj); - rest(); + pause(); - // store a value + // store a string obj = new JSONObject(); obj.put("msgid", msgId++); obj.put("action", "set"); obj.put("key", "eg.hatch.test.key1"); - // "set" expects a pre-JSON-ified string. TODO: reconsider. - obj.put("content", "\"Rando content, now with cheese\""); + obj.put("content", "Rando content, now with cheese"); io.sendMessage(obj); - rest(); + pause(); // store a value obj = new JSONObject(); @@ -51,15 +50,33 @@ public class TestHatch { obj.put("key", "eg.hatch.test.key1"); io.sendMessage(obj); - rest(); + // store an array + obj = new JSONObject(); + obj.put("msgid", msgId++); + obj.put("action", "set"); + obj.put("key", "eg.hatch.test.key2"); + JSONArray arr = new JSONArray(); + arr.put(123); + arr.put("23 Skidoo"); + obj.put("content", arr); + io.sendMessage(obj); + + pause(); + + obj = new JSONObject(); + obj.put("msgid", msgId++); + obj.put("action", "get"); + obj.put("key", "eg.hatch.test.key2"); + io.sendMessage(obj); + pause(); obj = new JSONObject(); obj.put("msgid", msgId++); obj.put("action", "keys"); io.sendMessage(obj); - rest(); + pause(); // get a list of printers obj = new JSONObject(); @@ -67,7 +84,7 @@ public class TestHatch { obj.put("action", "printers"); io.sendMessage(obj); - rest(); + pause(); /* // Printing tests @@ -80,7 +97,7 @@ public class TestHatch { obj.put("showDialog", true); // avoid auto-print while testing io.sendMessage(obj); - rest(); + pause(); obj = new JSONObject(); obj.put("msgid", msgId++); @@ -96,7 +113,7 @@ public class TestHatch { obj.put("settings", settings); io.sendMessage(obj); - rest(); + pause(); */ }