Catches and reports missing web.xml init parameter
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / pz2utils4jsf / pazpar2 / Pz2Session.java
index 74980bc..1bac38b 100644 (file)
@@ -12,10 +12,16 @@ import javax.inject.Named;
 \r
 import org.apache.log4j.Logger;\r
 \r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2Client;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration;\r
+import com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric;\r
 import com.indexdata.masterkey.pazpar2.client.exceptions.ProxyErrorException;\r
 import com.indexdata.pz2utils4jsf.config.Pz2Configurator;\r
 import com.indexdata.pz2utils4jsf.controls.ResultsPager;\r
-import com.indexdata.pz2utils4jsf.pazpar2.data.ApplicationError;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorInterface;\r
+import com.indexdata.pz2utils4jsf.errors.ErrorHelper;\r
+import com.indexdata.pz2utils4jsf.errors.ConfigurationError;\r
+import com.indexdata.pz2utils4jsf.pazpar2.data.CommandError;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.ByTarget;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseData;\r
 import com.indexdata.pz2utils4jsf.pazpar2.data.Pazpar2ResponseParser;\r
@@ -37,11 +43,12 @@ public class Pz2Session implements Pz2Interface {
   private QueryStates queryStates = new QueryStates();\r
   \r
   private static final long serialVersionUID = 3947514708343320514L;  \r
-  private com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration cfg = null;\r
-  private com.indexdata.masterkey.pazpar2.client.Pazpar2Client client = null;   \r
+  private Pazpar2ClientConfiguration cfg = null;\r
+  private Pazpar2Client client = null;   \r
   private TargetFilter targetFilter = null;  \r
   private ResultsPager pager = null; \r
-  private ApplicationTroubleshooter errorHelper = null;\r
+  private ErrorHelper errorHelper = null;\r
+  private List<ErrorInterface> configurationErrors = null;\r
   \r
   public Pz2Session () {\r
     logger.info("Instantiating pz2 session object [" + Utils.objectId(this) + "]");      \r
@@ -49,17 +56,28 @@ public class Pz2Session implements Pz2Interface {
     \r
   public void init(Pz2Configurator pz2conf) {\r
     if (client==null) {\r
+      configurationErrors = new ArrayList<ErrorInterface>();\r
+      errorHelper = new ErrorHelper(pz2conf);\r
       logger.info(Utils.objectId(this) + " is configuring itself using the provided " + Utils.objectId(pz2conf));\r
       try {\r
-        cfg = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientConfiguration(pz2conf.getConfig());\r
-        client = new com.indexdata.masterkey.pazpar2.client.Pazpar2ClientGeneric(cfg);\r
-        errorHelper = new ApplicationTroubleshooter(pz2conf);        \r
-        resetDataObjects();\r
-      } catch (ProxyErrorException e) {\r
-        e.printStackTrace();\r
-      } catch (IOException ioe) {\r
-        ioe.printStackTrace();\r
+        cfg = new Pazpar2ClientConfiguration(pz2conf.getConfig());\r
+      } catch (ProxyErrorException pe) {\r
+        logger.error("Could not configure Pazpar2 client: " + pe.getMessage());\r
+        configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + pe.getMessage(),errorHelper));\r
+      } catch (IOException io) {\r
+        logger.error("Could not configure Pazpar2 client: " + io.getMessage());\r
+        configurationErrors.add(new ConfigurationError("Pz2Client Config","ProxyError","Could not configure Pazpar2 client: " + io.getMessage(),errorHelper));\r
+      }\r
+      if (cfg != null) {\r
+        try {\r
+          client = new Pazpar2ClientGeneric(cfg);     \r
+        } catch (ProxyErrorException pe) {\r
+          logger.error("Could not instantiate Pazpar2 client: " + pe.getMessage());\r
+          configurationErrors.add(new ConfigurationError("Pz2Client error","ProxyError","Could not create Pazpar2 client: " +pe.getMessage(),errorHelper));                \r
+        } \r
+        logger.info("Found " + configurationErrors.size() + " configuration errors");        \r
       }\r
+      resetDataObjects();\r
     } else {\r
       logger.warn("Attempt to configure session but it already has a configured client");\r
     }\r
@@ -95,33 +113,38 @@ public class Pz2Session implements Pz2Interface {
    * @return Number of activeclients at the time of the 'show' command\r
    */\r
   public String update (String commands) {\r
-    if (hasQuery()) {\r
-      handleQueryStateChanges(commands);\r
-      logger.debug("Processing request for " + commands); \r
-      List<CommandThread> threadList = new ArrayList<CommandThread>();\r
-      StringTokenizer tokens = new StringTokenizer(commands,",");\r
-      while (tokens.hasMoreElements()) {\r
-        threadList.add(new CommandThread(getCommand(tokens.nextToken()),client));            \r
-      }\r
-      for (CommandThread thread : threadList) {\r
-        thread.start();\r
-      }\r
-      for (CommandThread thread : threadList) {\r
-        try {\r
-          thread.join();\r
-        } catch (InterruptedException e) {\r
-          e.printStackTrace();\r
+    if (! hasConfigurationErrors()) {\r
+      if (hasQuery()) {\r
+        handleQueryStateChanges(commands);\r
+        logger.debug("Processing request for " + commands); \r
+        List<CommandThread> threadList = new ArrayList<CommandThread>();\r
+        StringTokenizer tokens = new StringTokenizer(commands,",");\r
+        while (tokens.hasMoreElements()) {\r
+          threadList.add(new CommandThread(getCommand(tokens.nextToken()),client));            \r
         }\r
+        for (CommandThread thread : threadList) {\r
+          thread.start();\r
+        }\r
+        for (CommandThread thread : threadList) {\r
+          try {\r
+            thread.join();\r
+          } catch (InterruptedException e) {\r
+            e.printStackTrace();\r
+          }\r
+        }\r
+        for (CommandThread thread : threadList) {\r
+           String commandName = thread.getCommand().getName();\r
+           Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
+           dataObjects.put(commandName, responseObject);        \r
+        }\r
+        return getActiveClients();\r
+      } else {\r
+        logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
+        resetDataObjects();\r
+        return "0";\r
       }\r
-      for (CommandThread thread : threadList) {\r
-         String commandName = thread.getCommand().getName();\r
-         Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(thread.getResponse());\r
-         dataObjects.put(commandName, responseObject);        \r
-      }\r
-      return getActiveClients();\r
     } else {\r
-      logger.debug("Skipped requests for " + commands + " as there's not yet a query."); \r
-      resetDataObjects();\r
+      logger.error("Did not attempt to execute query since there are configuration errors.");\r
       return "0";\r
     }\r
     \r
@@ -268,15 +291,15 @@ public class Pz2Session implements Pz2Interface {
     return queryStates.getCurrentStateKey();\r
   }\r
       \r
-  public void setCurrentStateKey(String key) {\r
-    logger.debug("************** request to set state key to: [" + key + "]");    \r
+  public void setCurrentStateKey(String key) {       \r
     queryStates.setCurrentStateKey(key);\r
   }\r
   \r
-  /**\r
-   * Returns true if application error found in any response data objects \r
-   */\r
-  public boolean hasErrors () {\r
+  public boolean hasConfigurationErrors () {\r
+      return (configurationErrors.size()>0);      \r
+  }\r
+  \r
+  public boolean hasCommandErrors () {\r
     if (dataObjects.get("search").hasApplicationError()) {\r
       logger.info("Error detected in search");\r
       return true;\r
@@ -287,17 +310,27 @@ public class Pz2Session implements Pz2Interface {
         return true;\r
       }\r
     }    \r
-    return false;\r
+    return false;    \r
+  }\r
+  \r
+  /**\r
+   * Returns true if application error found in any response data objects \r
+   */\r
+  public boolean hasErrors () {\r
+    return hasConfigurationErrors() || hasCommandErrors();\r
   }\r
 \r
+  public List<ErrorInterface> getConfigurationErrors() {    \r
+    return configurationErrors;\r
+  }\r
   \r
   /**\r
    * Returns a search command error, if any, otherwise the first\r
    * error found for an arbitrary command, if any, otherwise\r
    * an empty dummy error. \r
    */    \r
-  public ApplicationError getOneError() {\r
-    ApplicationError error = new ApplicationError();    \r
+  public ErrorInterface getCommandError() {\r
+    CommandError error = new CommandError();    \r
     if (dataObjects.get("search").hasApplicationError()) {\r
       error = dataObjects.get("search").getApplicationError();                        \r
     } else {\r
@@ -308,7 +341,7 @@ public class Pz2Session implements Pz2Interface {
         } \r
       }\r
     }\r
-    error.setTroubleshooter(errorHelper);\r
+    error.setErrorHelper(errorHelper);\r
     return error;         \r
   }\r
 \r
@@ -339,7 +372,7 @@ public class Pz2Session implements Pz2Interface {
     return pager;\r
   }\r
   \r
-  protected ApplicationTroubleshooter getTroubleshooter() {\r
+  protected ErrorHelper getTroubleshooter() {\r
     return errorHelper;\r
   }\r
   \r
@@ -421,10 +454,10 @@ public class Pz2Session implements Pz2Interface {
     return defaultValue;    \r
   }\r
 \r
-  private String doCommand(String commandName) {\r
+  private String doCommand(String commandName) {      \r
     Pazpar2Command command = getCommand(commandName);    \r
     logger.debug(command.getEncodedQueryString() + ": Results for "+ getCommand("search").getEncodedQueryString());\r
-    return update(commandName);\r
+    return update(commandName);      \r
   }\r
   \r
   private void resetDataObjects() {\r
@@ -438,5 +471,4 @@ public class Pz2Session implements Pz2Interface {
     dataObjects.put("search", new SearchResponse());\r
   }\r
   \r
-\r
 }\r