Class MigrationContext


  • public class MigrationContext
    extends Object
    This class is instantiated for each migration process, it contains useful methods to handle the current migration lifecycle. This class allow for keeping track of the migration steps by persisting the steps and there state on the FileSystem, allowing for a migration to be able to restart from a failure in case it happens. This class allow also for logging the migration informations depending on the current context: - if executed in karaf shell using a karaf shell session, then the logging will be done in the shell console - if executed outside karaf shell using OSGI service direct, then the logging will be done using classical logger systems This class allow also to do a best effort on missing configuration information, by prompting questions in the karaf shell (not supported in case direct OSGI service usage)
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  MigrationContext.MigrationStep
      A simple migration step to be performed
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected MigrationContext​(org.apache.karaf.shell.api.console.Session session, MigrationConfig migrationConfig)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      String askUserWithAuthorizedAnswer​(String msg, List<String> authorizedAnswer)
      This method allow you to ask a question to the user.
      String askUserWithDefaultAnswer​(String msg, String defaultAnswer)
      This will ask a question to the user and return the default answer if the user does not answer.
      protected void cleanHistory()
      Clean history from FileSystem
      boolean getConfigBoolean​(String name)
      Get config for property name, in case the property doesn't exist on file system config file Best effort will be made to prompt question in karaf shell to get the needed information
      String getConfigString​(String name)
      Get config for property name, in case the property doesn't exist on file system config file Best effort will be made to prompt question in karaf shell to get the needed information
      org.apache.http.impl.client.CloseableHttpClient getHttpClient()
      This HTTP client is configured to be used for ElasticSearch requests to be able to perform migrations requests.
      void performMigrationStep​(String stepKey, MigrationContext.MigrationStep step)
      this method allow for migration step execution: - in case the history already contains the given stepKey as COMPLETED, then the step won't be executed - in case the history doesn't contain the given stepKey, then the step will be executed Also this method is keeping track of the history by persisting it on the FileSystem.
      void printException​(String msg)
      Same as above without stacktrace
      void printException​(String msg, Throwable t)
      Print an exception along with a message in the console.
      void printMessage​(String msg)
      Print a message in the console.
      String promptMessageToUser​(String msg)
      This method allow you to prompt a message to the user.
      protected void setHttpClient​(org.apache.http.impl.client.CloseableHttpClient httpClient)  
      protected void tryRecoverFromHistory()
      Try to recover from a previous run I case we found an existing history we will ask if we want to recover or if we want to restart from the beginning (it is also configurable using the conf: recoverFromHistory)
    • Constructor Detail

      • MigrationContext

        protected MigrationContext​(org.apache.karaf.shell.api.console.Session session,
                                   MigrationConfig migrationConfig)
    • Method Detail

      • tryRecoverFromHistory

        protected void tryRecoverFromHistory()
                                      throws IOException
        Try to recover from a previous run I case we found an existing history we will ask if we want to recover or if we want to restart from the beginning (it is also configurable using the conf: recoverFromHistory)
        Throws:
        IOException
      • performMigrationStep

        public void performMigrationStep​(String stepKey,
                                         MigrationContext.MigrationStep step)
                                  throws Exception
        this method allow for migration step execution: - in case the history already contains the given stepKey as COMPLETED, then the step won't be executed - in case the history doesn't contain the given stepKey, then the step will be executed Also this method is keeping track of the history by persisting it on the FileSystem.
        Parameters:
        stepKey - the key of the given step
        step - the step to be performed
        Throws:
        IOException
        Exception
      • cleanHistory

        protected void cleanHistory()
                             throws IOException
        Clean history from FileSystem
        Throws:
        IOException
      • askUserWithDefaultAnswer

        public String askUserWithDefaultAnswer​(String msg,
                                               String defaultAnswer)
                                        throws IOException
        This will ask a question to the user and return the default answer if the user does not answer.
        Parameters:
        msg - String message to ask
        defaultAnswer - String default answer
        Returns:
        the user's answer
        Throws:
        IOException - if there was a problem reading input from the console
      • askUserWithAuthorizedAnswer

        public String askUserWithAuthorizedAnswer​(String msg,
                                                  List<String> authorizedAnswer)
                                           throws IOException
        This method allow you to ask a question to the user. The answer is controlled before being return so the question will be ask until the user enter one the authorized answer
        Parameters:
        msg - String message to ask
        authorizedAnswer - Array of possible answer, all answer must be in lower case
        Returns:
        the user answer
        Throws:
        IOException - if there was an error retrieving an answer from the user on the console
      • promptMessageToUser

        public String promptMessageToUser​(String msg)
        This method allow you to prompt a message to the user. No control is done on the answer provided by the user.
        Parameters:
        msg - String message to prompt
        Returns:
        the user answer
      • printMessage

        public void printMessage​(String msg)
        Print a message in the console.
        Parameters:
        msg - the message to print out with a newline
      • printException

        public void printException​(String msg,
                                   Throwable t)
        Print an exception along with a message in the console.
        Parameters:
        msg - the message to print out with a newline
        t - the exception to dump in the shell console after the message
      • printException

        public void printException​(String msg)
        Same as above without stacktrace
        Parameters:
        msg - the message to print out with a newline
      • getConfigString

        public String getConfigString​(String name)
                               throws IOException
        Get config for property name, in case the property doesn't exist on file system config file Best effort will be made to prompt question in karaf shell to get the needed information
        Parameters:
        name - the name of the property
        Returns:
        the value of the property
        Throws:
        IOException
      • getConfigBoolean

        public boolean getConfigBoolean​(String name)
                                 throws IOException
        Get config for property name, in case the property doesn't exist on file system config file Best effort will be made to prompt question in karaf shell to get the needed information
        Parameters:
        name - the name of the property
        Returns:
        the value of the property
        Throws:
        IOException
      • getHttpClient

        public org.apache.http.impl.client.CloseableHttpClient getHttpClient()
        This HTTP client is configured to be used for ElasticSearch requests to be able to perform migrations requests.
        Returns:
        the http client.
      • setHttpClient

        protected void setHttpClient​(org.apache.http.impl.client.CloseableHttpClient httpClient)