@Override
  public void contextInitialized(ServletContextEvent servletContextEvent) {

    // Configure log4j from the Toolkit's logging config file (if set).
    // (Don't use the default here - we need to know whether the property was overridden.)
    String loggingConfigFilename =
        System.getProperty(CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_KEY);
    if (loggingConfigFilename != null) {

      System.out.println(
          "Setting logging config filename to "
              + loggingConfigFilename
              + " (from override of "
              + CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_KEY
              + " property).");

    } else {

      String realPath = servletContextEvent.getServletContext().getRealPath("/");
      if (realPath != null) {

        loggingConfigFilename = realPath + CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_DEFAULT;
        System.out.println(
            "Setting logging config filename to "
                + loggingConfigFilename
                + ", derived from real path.");

      } else {

        // realPath can be null if, for example, the webapp is run from an un-exploded war.
        System.out.println(
            "Real path could not be determined - looking for in-war logging filename property.");
        loggingConfigFilename =
            System.getProperty(CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_IN_WAR_KEY);
        if (loggingConfigFilename != null) {

          System.out.println(
              "Setting logging config filename to "
                  + loggingConfigFilename
                  + " (from override of "
                  + CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_IN_WAR_KEY
                  + " property).");

        } else {

          loggingConfigFilename = CoreConfiguration.CORE_LOGGING_CONFIG_FILE_NAME_IN_WAR_DEFAULT;
          System.out.println(
              "Setting logging config filename to "
                  + loggingConfigFilename
                  + ", the default (as real path could not be determined).");
        }
      }
    }

    String appName = ConfigurationHelper.getAppName(servletContextEvent.getServletContext());

    // Initialize logging
    Properties loggingProps = new Properties();
    ToolkitHelper.setPropertiesFromClasspathOrFilesystem(loggingProps, loggingConfigFilename);
    setAppName(loggingProps, appName);
    setLoggingDir(loggingProps, servletContextEvent.getServletContext());
    PropertyConfigurator.configure(loggingProps);

    // Initialize Toolkit components
    Properties configProperties = getContextParameters(servletContextEvent.getServletContext());
    setAppName(configProperties, appName);
    ConfigurationHelper.setServerContextProperties(appName, configProperties);
  }
  protected void performXMLDiff(InputStream msgStream, StringBuilder failuresList, File file)
      throws ServiceException {

    String fileName = file.getName();

    try {

      File prettyPrintedXMLFile = File.createTempFile("BaseTestJAXBTranslator", ".xml");
      prettyPrintedXMLFile.deleteOnExit();

      Writer outWriter = null;
      try {

        outWriter = new FileWriter(prettyPrintedXMLFile);

      } catch (IOException e) {

        throw new ServiceException(ServiceError.RUNTIME_ERROR, e);
      }

      ToolkitHelper.prettyPrintXML(msgStream, outWriter);

      // Compare the two XML files
      // Because the org.diffxml.diffxml uses a GNU License, not an MIT license,
      // it is not provided as part of the Toolkit, we use reflection to invoke
      // it here so we can avoid problems compiling this class in the absence of that
      // package. Note that, if the package is absent this "diff" will be silently
      // skipped.
      try {
        // The following 3 lines or code are equivalent to this:
        // Diff diffInstance = DiffFactory.createDiff();
        Class<?> diffFactoryClass = Class.forName(DIFFXML_CLASS_NAME);

        try {

          Method createDiffMethod = diffFactoryClass.getMethod("createDiff");
          Object diffInstance = createDiffMethod.invoke(null);

          // The following 2 lines of code are equivalent to this:
          // Document delta = diffInstance.diff(file, temp);
          Method diffMethod = diffInstance.getClass().getMethod("diff", File.class, File.class);
          Document deltaDocument =
              (Document) diffMethod.invoke(diffInstance, file, prettyPrintedXMLFile);

          if (deltaDocument.getDocumentElement().hasChildNodes()) {

            if (reportDiffAsXMLIndented) {

              OutputStream byteStream = new ByteArrayOutputStream();
              // DOMOps.outputXMLIndented(deltaDocument, byteStream);
              Class<?> DOMOpsClass = Class.forName("org.diffxml.diffxml.DOMOps");
              Method outputXMLIndentedMethod =
                  DOMOpsClass.getMethod("outputXMLIndented", Document.class, OutputStream.class);
              outputXMLIndentedMethod.invoke(null, deltaDocument, byteStream);

              failuresList
                  .append(
                      new StringBuilder()
                          .append("performXMLDiff detected differences for ")
                          .append(fileName)
                          .append(":\n")
                          .append('\t')
                          .append("Result XML file does not match input:\n")
                          .append(byteStream.toString()))
                  .append('\n');

            } else {

              try {
                String[] parms = {file.getAbsolutePath(), prettyPrintedXMLFile.getAbsolutePath()};
                String diffOutput = runCmd(diffCommand, parms);
                if (diffOutput != null && diffOutput.length() > 0) {

                  failuresList.append(
                      new StringBuilder()
                          .append("performXMLDiff detected differences for ")
                          .append(fileName)
                          .append(":\n")
                          .append('\t')
                          .append("Input XML file (on the left) is not matched ")
                          .append("by result XML file (on the right):\n")
                          .append(diffOutput));
                }

              } catch (ServiceException e) {

                failuresList.append(collectException("performXMLDiff", fileName, e));
              }
            }
          }

        } catch (ClassNotFoundException e) {

          failuresList.append(collectException("performXMLDiff", fileName, e));

        } catch (InvocationTargetException e) {

          failuresList.append(collectException("performXMLDiff", fileName, e));

        } catch (NoSuchMethodException e) {

          failuresList.append(collectException("performXMLDiff", fileName, e));

        } catch (IllegalAccessException e) {

          failuresList.append(collectException("performXMLDiff", fileName, e));

        } catch (Exception e) {

          failuresList.append(collectException("performXMLDiff", fileName, e));
        }
      } catch (ClassNotFoundException e) {

        LOG.warn(
            "ClassNotFoundException trying to load "
                + DIFFXML_CLASS_NAME
                + ". Skipping diffXML comparison. Exception was:",
            e);

      } finally {

        prettyPrintedXMLFile.delete();
      }

    } catch (IOException e) {

      failuresList.append(collectException("performXMLDiff", fileName, e));
    }
  }