hatch: docs, cleanup, extra temporary logging
authorBill Erickson <berick@esilibrary.com>
Wed, 23 Apr 2014 22:00:07 +0000 (18:00 -0400)
committerJeff Godin <jgodin@tadl.org>
Fri, 3 Jun 2016 20:38:49 +0000 (16:38 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
src/org/evergreen_ils/hatch/FileIO.java
src/org/evergreen_ils/hatch/Hatch.java
src/org/evergreen_ils/hatch/HatchWebSocketHandler.java
src/org/evergreen_ils/hatch/PrintManager.java

index a5556b3..6b3166c 100644 (file)
@@ -195,11 +195,12 @@ public class FileIO {
      * Returns all keys begining with the specified prefix.
      *
      * @param prefix The initial substring used to limit the return set 
-     * of keys.
+     * of keys.  If the prefix is null, all keys are returned.
      * @return Array of keys
      */
     public String[] keys(String prefix) {
         logger.info("keys => " + prefix);
+
         File dir = new File(basePath);
         if (!dir.exists()) return new String[0];
 
index 898ea53..d81f911 100644 (file)
@@ -109,6 +109,7 @@ public class Hatch extends Application {
             return new Task<Map<String,Object>>() {
                 protected Map<String,Object> call() {
                     while (true) {
+                        logger.info("MsgListenService waiting for a message...");
                         try {
                             // take() blocks until a message is available
                             return requestQueue.take();
@@ -151,13 +152,15 @@ public class Hatch extends Application {
 
         browser = new BrowserView();
         Scene scene = new Scene(browser, 640, 480); // TODO: printer dimensions
+        //Scene scene = new Scene(browser);
         primaryStage.setScene(scene);
 
         browser.webEngine.getLoadWorker()
             .stateProperty()
             .addListener( (ChangeListener) (obsValue, oldState, newState) -> {
+                logger.info("browser load state " + newState);
                 if (newState == State.SUCCEEDED) {
-                    logger.debug("browser page loaded");
+                    logger.info("Print browser page load completed");
                     new PrintManager().print(browser.webEngine, params);
                 }
             });
@@ -175,18 +178,21 @@ public class Hatch extends Application {
 
         MsgListenService service = new MsgListenService();
 
+        logger.info("starting MsgTask");
+
         service.setOnSucceeded(
             new EventHandler<WorkerStateEvent>() {
 
             @Override
             public void handle(WorkerStateEvent t) {
-                Map<String,Object> message =
+                logger.info("MsgTask handling message.. ");
+                Map<String,Object> message = 
                     (Map<String,Object>) t.getSource().getValue();
 
-                if (message != null) handlePrint(message);
+                // send the message off to be printed
+                handlePrint(message);
 
-                // once this task is complete, kick off the next
-                // message task handler.
+                // go back to listening for more requests
                 startMsgTask();
             }
         });
@@ -208,10 +214,14 @@ public class Hatch extends Application {
 
         Server server = (Server) configuration.configure();
 
+        logger.info("Starting Jetty server");
+
         // start our server, but do not join(), since we want to server
         // to continue running in its own thread
         server.start();
 
+        logger.info("Launching FX Application");
+
         // launch the FX Application thread
         launch(args);
     }
index ee7f54c..c16a983 100644 (file)
@@ -55,7 +55,10 @@ public class HatchWebSocketHandler {
     /**
      * Apply trusted domains.
      *
-     * @param domains Array of domains which we should trust
+     * If the first domain in the list equals "*", that signifies that
+     * all domains should be trusted.
+     *
+     * @param domains Array of domains to trust.
      */
     public static void setTrustedDomains(String[] domains) {
         trustedDomains = domains;
@@ -73,7 +76,7 @@ public class HatchWebSocketHandler {
                 }
             }
         } else {
-            logger.warn("No domains are trusted");
+            logger.warn("No domains are trusted.  All requests will be denied");
         }
     }
 
@@ -180,16 +183,17 @@ public class HatchWebSocketHandler {
 
         Map<String, Object> response = new HashMap<String, Object>();
         response.put("msgid", msgid);
+
         if (success) {
             response.put("content", json);
         } else {
             response.put("error", json);
         }
 
-        logger.info("replying with : " + JSON.toString(response));
+        String jsonString = JSON.toString(response);
+        logger.info("replying with : " + jsonString);
 
         try {
-            String jsonString = JSON.toString(response);
             if (!success) logger.warn(jsonString);
             session.getRemote().sendString(jsonString);
         } catch (IOException e) {
@@ -218,7 +222,6 @@ public class HatchWebSocketHandler {
                 new Long(-1), false);
         }
 
-        FileIO io;
         Long msgid = (Long) params.get("msgid");
         String action = (String) params.get("action");
         String key = (String) params.get("key");
@@ -239,100 +242,64 @@ public class HatchWebSocketHandler {
             return;
         }
 
-        if (action.equals("keys")) {
-            io = new FileIO(profileDirectory);
-            String[] keys = io.keys(key); // OK for key to be null
-            if (keys != null) {
-                reply(keys, msgid);
-            } else {
-                reply("key lookup error", msgid, false);
-            }
-            return;
-        }
-
-        if (action.equals("printers")) {
-            List printers = new PrintManager().getPrintersAsMaps();
-            reply(printers, msgid);
-            return;
-        }
-
-        if (action.equals("print")) {
-            // pass ourselves off to the print handler so it can reply
-            // for us after printing has completed.
-            params.put("socket", this);
-            Hatch.enqueueMessage(params);
-            return;
-        }
-
-        if (action.equals("print-config")) {
-            try {
-                reply(
-                    new PrintManager().configurePrinter(params),
-                    msgid
-                );
-            } catch(IllegalArgumentException e) {
-                reply(e.toString(), msgid, false);
-            }
-            return;
-        }
+        Object response = null;
+        boolean error = false;
 
-        // all remaining requests require a key
-        if (key == null || key.equals("")) {
-            reply("No key specified in request", msgid, false);
-            return;
-        }
+        switch (action) {
+            case "keys":
+                response = new FileIO(profileDirectory).keys(key);
+                break;
 
-        if (action.equals("get")) {
-            String val = new FileIO(profileDirectory).get(key);
-            // set() calls store bare JSON. We must pass an 
-            // Object to reply so that it may be embedded into
-            // a larger JSON response object, hence the JSON.parse().
-            if (val == null) {
-                reply(null, msgid);
-            } else {
-                reply(JSON.parse(val), msgid);
-            }
-            return;
-        }
+            case "printers":
+                response = new PrintManager().getPrintersAsMaps();
+                break;
 
-        if (action.equals("remove")) {
-            io = new FileIO(profileDirectory);
-            if (io.remove(key)) {
-                reply("Removal of " + key + " successful", msgid);
-            } else {
-                reply("Removal of " + key + " failed", msgid, false);
-            }
-            return;
-        }
+            case "print":
+                // pass ourselves off to the print handler so it can reply
+                // for us after printing has completed.
+                params.put("socket", this);
+                Hatch.enqueueMessage(params);
+
+                // we don't want to return a response below, since the 
+                // FX thread will handle that for us.
+                return;
+
+            case "print-config":
+                try {
+                    response = new PrintManager().configurePrinter(params);
+                } catch(IllegalArgumentException e) {
+                    response = e.toString();
+                    error = true;
+                }
+                break;
 
-        // all remaining actions require value
-        if (value == null) {
-            reply("No value specified in request", msgid, false);
-            return;
-        }
+            case "get":
+                String val = new FileIO(profileDirectory).get(key);
+                if (val != null) {
+                    // set() stores bare JSON. We must pass an 
+                    // Object to reply so that it may be embedded into
+                    // a larger JSON response object, hence the JSON.parse().
+                    response = JSON.parse(val);
+                }
+                break;
 
-        switch (action) {
+            case "remove":
+                response = new FileIO(profileDirectory).remove(key);
+                break;
 
             case "set" :
-                io = new FileIO(profileDirectory);
-                if (io.set(key, value)) {
-                    reply("setting value for " + key + " succeeded", msgid);
-                } else {
-                    reply("setting value for " + key + " succeeded", msgid, false);
-                }
+                response = new FileIO(profileDirectory).set(key, value);
                 break;
 
             case "append" :
-                io = new FileIO(profileDirectory);
-                if (io.append(key, value)) {
-                    reply("appending value for " + key + " succeeded", msgid);
-                } else {
-                    reply("appending value for " + key + " succeeded", msgid, false);
-                }
+                response = new FileIO(profileDirectory).append(key, value);
                 break;
 
             default:
-                reply("No such action: " + action, msgid, false);
+                response = "No such action: " + action;
+                error = true;
         }
+
+        reply(response, msgid, !error);
     }
 }
index 13c8f24..b38a263 100644 (file)
@@ -101,12 +101,17 @@ public class PrintManager {
         }
 
         if (showDialog != null && showDialog.booleanValue()) {
+            logger.info("Print dialog requested");
             if (!job.showPrintDialog(null)) {
                 // job canceled by user
                 socket.reply("Print job canceled", msgid);
+                return;
             }
+        } else {
+            logger.info("No print dialog requested");
         }
 
+        logger.info("printing...");
         engine.print(job);
         job.endJob();