Class StartupRequirementsChecker

java.lang.Object
edu.ucsb.nceas.metacat.startup.StartupRequirementsChecker

public class StartupRequirementsChecker extends Object

An implementation of ServletContextListener that is called automatically by the servlet container on startup, and used to verify that we have the essential components in place for Metacat to run successfully.

If any of the checks fail: 1. Startup is aborted (instead of allowing Metacat to limp along and malfunction), as was previously the case. 2. Clear and useful error messages and instructions are logged to (tomcat logs) 'catalina .out' and 'hostname.(date).log' files

If the environment variable named METACAT_IN_K8S is set to "true", it is assumed that the current metacat instance is running in a container (eg in a Kubernetes cluster). In these cases, checks may need to be tailored to the environment. For example, the site properties file is expected to be a read-only configMap in kubernetes, so the create/write checks should be skipped.

TODO: Add more test cases - this initial implementation is minimum viable solution to check metacat has the correct read-write permissions for config files and their locations (as appropriate for legacy or containerized deployments). This case should be supplemented with checks for other essential components - e.g: - SOLR is running, correct version, port is accessible (at least for k8s - see note below) - database is running and accessible (at least for k8s - see note below) - metacat's file location (typically /var/metacat, but configurable) exists and is writable by the web user - properties file exists or directory is writable NOTE: be careful what we add here! Sometimes, we want to allow Metacat to start with incomplete dependencies, since these are configured later through the admin interface - e.g. database connection & solr for non-k8s deployments

See Also:
  • ServletContextListener
  • Field Details

    • SOLR_BASE_URL_PROP_KEY

      protected static final String SOLR_BASE_URL_PROP_KEY
      See Also:
    • RUNNING_IN_CONTAINER

      protected boolean RUNNING_IN_CONTAINER
    • SOLR_CONFIGURED_PROP_KEY

      protected static final String SOLR_CONFIGURED_PROP_KEY
      See Also:
    • SOLR_CORE_NAME_PROP_KEY

      protected static final String SOLR_CORE_NAME_PROP_KEY
      See Also:
    • SOLR_SCHEMA_LOCATOR_PROP_KEY

      protected static final String SOLR_SCHEMA_LOCATOR_PROP_KEY
      See Also:
    • runtimeProperties

      protected Properties runtimeProperties
    • mockSolrTestUrl

      protected URL mockSolrTestUrl
  • Constructor Details

    • StartupRequirementsChecker

      public StartupRequirementsChecker()
  • Method Details

    • contextInitialized

      public void contextInitialized(javax.servlet.ServletContextEvent sce)
    • validateDefaultProperties

      protected Properties validateDefaultProperties(@NotNull @NotNull javax.servlet.ServletContextEvent sce) throws RuntimeException
      Check that the default properties file is readable and writeable, without throwing any exceptions. (protected to allow test access)
      Parameters:
      sce - the ServletContextEvent passed by the container on startup
      Returns:
      a Properties object initialized from the 'metacat.properties' file
      Throws:
      RuntimeException - if any unrecoverable problems are found that should cause startup to be aborted
    • validateSiteProperties

      protected void validateSiteProperties(@NotNull @NotNull Properties defaultProperties) throws RuntimeException
      Check if we can load properties from, and write properties to, 'metacat-site.properties', or can create a new one if it doesn't already exist (protected to allow test access)
      Parameters:
      defaultProperties - the Properties loaded from the 'metacat.properties' file
      Throws:
      RuntimeException - if any unrecoverable problems are found that should cause startup to be aborted
    • validateSolrAvailable

      protected void validateSolrAvailable() throws RuntimeException
      Ensure we get an HTTP 200 OK response and can retrieve the schema doc from the solr service that is configured in the properties file. NOTE: If this is a non-k8s deployment and metacat has not yet been properly configured, skip this validation, since the admin config pages require metacat to be able to run without solr being available (so the admin can enter the correct solr properties).
      Throws:
      RuntimeException - if any unrecoverable problems are found that should cause startup to be aborted
    • abort

      protected void abort(String message, Exception e) throws RuntimeException
      Compose a user-friendly and informative error message, log it to 'catalina.out', and include it when throwing a RuntimeException, so it also appears in 'hostname.(date).log'
      Parameters:
      message - a clear, concise error message indicating the root cause of the problem, along with useful instructions on how to fix the issue. Ideally, the String should include newline characters, so it is formatted nicely on multiple lines, to help readability. For example: "Can't get default metacat.properties from " + metacatPropsFilePath + "\n" + "Check that:\n" + " 1. this path is correct, and\n" + " 2. 'metacat.properties' is readable and writeable by the user running tomcat",
      e - the root cause Exception
      Throws:
      RuntimeException - to abort startup as requested