Class IrisDbGroovySession

java.lang.Object
com.illumon.iris.db.util.IrisDbGroovySession
All Implemented Interfaces:
ScriptSession

public class IrisDbGroovySession extends Object implements ScriptSession
Note: This is *clearly* only intended to be used from one thread at a time.
  • Field Details

  • Constructor Details

    • IrisDbGroovySession

      public IrisDbGroovySession(com.fishlib.io.logger.Logger log, Database db, boolean runInitScripts) throws IOException
      Throws:
      IOException
  • Method Details

    • findScript

      public static InputStream findScript(String relativePath) throws IOException
      Throws:
      IOException
    • runScript

      public void runScript(String script) throws IOException
      Throws:
      IOException
    • hasExecutedScript

      public boolean hasExecutedScript(String scriptName)
    • runScriptOnce

      public void runScriptOnce(String script) throws IOException
      Throws:
      IOException
    • getVariable

      public Object getVariable(String name) throws QueryScope.MissingVariableException
      Description copied from interface: ScriptSession
      Retrieve a variable from the script session's bindings.
      Specified by:
      getVariable in interface ScriptSession
      Parameters:
      name - the variable to retrieve
      Returns:
      the variable
      Throws:
      QueryScope.MissingVariableException - if the variable does not exist
    • getVariable

      public <T> T getVariable(String name, T defaultValue)
      Description copied from interface: ScriptSession
      Retrieve a variable from the script session's bindings. If the variable is not present, return defaultValue. If the variable is present, but is not of type (T), a ClassCastException may result.
      Specified by:
      getVariable in interface ScriptSession
      Type Parameters:
      T - the type of the variable
      Parameters:
      name - the variable to retrieve
      defaultValue - the value to use when no value is present in the session's scope
      Returns:
      the value of the variable, or defaultValue if not present
    • evaluate

      public void evaluate(String command)
      Description copied from interface: ScriptSession
      Evaluates command in the context of the current ScriptSession.
      Specified by:
      evaluate in interface ScriptSession
      Parameters:
      command - the command to evaluate
    • evaluate

      public void evaluate(String command, String scriptName)
      Description copied from interface: ScriptSession
      Evaluates command in the context of the current ScriptSession.
      Specified by:
      evaluate in interface ScriptSession
      Parameters:
      command - the command to evaluate
      scriptName - an optional script name, which may be ignored by the implementation, or used improve error messages or for other internal purposes
    • removeComments

      public static String removeComments(String s)
      Remove comments from an import statement. /* comments take precedence over eol (//) comments. This ignores escaping and quoting, as they are not valid in an import statement.
      Parameters:
      s - import statement string from which to remove comments
      Returns:
      the input string with comments removed, and whitespace trimmed
    • isValidImportString

      public static String isValidImportString(com.fishlib.io.logger.Logger log, String importString)
      Ensure that the given importString is valid. Return a canonical version of the import string if it is valid.
      Parameters:
      importString - the string to check. importString is "[import] [static] package.class[.innerclass...][.field|.method][.*][;]".
      Returns:
      null if importString is not valid, else a string of the form "import [static] package.class.part.part[.*];"
    • addScriptImportClass

      public void addScriptImportClass(String c)
    • addScriptImportClass

      public void addScriptImportClass(Class<?> c)
    • addScriptImportStatic

      public void addScriptImportStatic(String c)
    • addScriptImportStatic

      public void addScriptImportStatic(Class<?> c)
    • getDynamicClass

      public static byte[] getDynamicClass(String name)
    • getVariables

      public Map<String,Object> getVariables()
      Description copied from interface: ScriptSession
      Retrieves all of the variables present in the session's scope (e.g., Groovy binding, Python globals()).
      Specified by:
      getVariables in interface ScriptSession
      Returns:
      an unmodifiable map with variable names as the keys, and the Objects as the result
    • getVariableNames

      public Set<String> getVariableNames()
      Description copied from interface: ScriptSession
      Retrieves all of the variable names present in the session's scope
      Specified by:
      getVariableNames in interface ScriptSession
      Returns:
      an unmodifiable set of variable names
    • hasVariableName

      public boolean hasVariableName(@NotNull String name)
      Description copied from interface: ScriptSession
      Check if the scope has the given variable name
      Specified by:
      hasVariableName in interface ScriptSession
      Parameters:
      name - the variable name
      Returns:
      True iff the scope has the given variable name
    • setVariable

      public void setVariable(@NotNull String name, @Nullable Object value)
      Description copied from interface: ScriptSession
      Inserts a value into the script's scope.
      Specified by:
      setVariable in interface ScriptSession
      Parameters:
      name - the variable name to set
      value - the new value of the variable
    • getBinding

      public groovy.lang.Binding getBinding()
    • getShell

      public groovy.lang.GroovyShell getShell()
    • scriptType

      public String scriptType()
      Specified by:
      scriptType in interface ScriptSession
      Returns:
      a textual description of this script session's language for use in messages.
    • sanitizeThrowable

      public Throwable sanitizeThrowable(Throwable e)
      Description copied from interface: ScriptSession
      If this script session can throw unserializable exceptions, this method is responsible for turning those exceptions into something suitable for sending back to a client.
      Specified by:
      sanitizeThrowable in interface ScriptSession
      Parameters:
      e - the exception to (possibly) sanitize
      Returns:
      the sanitized exception
    • onPersistentQueryInitializationBegin

      public void onPersistentQueryInitializationBegin(@NotNull Supplier<ScriptPathLoader> pathLoaderSupplier, @Nullable ScriptPathLoaderState scriptLoaderState)
      Description copied from interface: ScriptSession
      Called before PersistentQuery initialization, should setup sourcing from the controller (as required).
      Specified by:
      onPersistentQueryInitializationBegin in interface ScriptSession
    • onPersistentQueryInitializationEnd

      public void onPersistentQueryInitializationEnd()
      Description copied from interface: ScriptSession
      Called after PersistentQuery initialization.
      Specified by:
      onPersistentQueryInitializationEnd in interface ScriptSession
    • setScriptPathLoader

      public void setScriptPathLoader(@NotNull Supplier<ScriptPathLoader> pathLoaderSupplier, boolean caching)
      Description copied from interface: ScriptSession
      Sets the scriptPathLoader that is in use for this session.
      Specified by:
      setScriptPathLoader in interface ScriptSession
      Parameters:
      pathLoaderSupplier - a supplier of a script path loader
      caching - whether the source operation should cache results
    • setUseOriginalScriptLoaderState

      public boolean setUseOriginalScriptLoaderState(boolean useOriginal)
      Description copied from interface: ScriptSession
      Informs the session whether or not we should be using the original ScriptLoaderState for source commands.
      Specified by:
      setUseOriginalScriptLoaderState in interface ScriptSession
      Parameters:
      useOriginal - whether to use the script loader state at persistent query initialization
    • clearScriptPathLoader

      public void clearScriptPathLoader()
      Description copied from interface: ScriptSession
      Removes the currently configured script path loader from this script.
      Specified by:
      clearScriptPathLoader in interface ScriptSession