Class ScriptVars


  • public final class ScriptVars
    extends java.lang.Object
    Manages global and script variables.

    The global variables are meant to be accessible by all scripts and the script variables are meant to be accessible by just a given script (identified by its name).

    Note: While it's possible to a script to access the variables of another script that usage is discouraged.

    The keys and (string) values of the variables have restrictions on its character length:

    There's a maximum number of global/script variables:

    • Global: 50;
    • Script: 20;
    Since:
    2.4.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clear()
      Clears all variables, global and script.
      static void clearGlobalVars()
      Clears the global variables.
      static void clearScriptVars​(java.lang.String scriptName)
      Clears the variables of the script with the given name.
      static java.lang.Object getGlobalCustomVar​(java.lang.String key)
      Gets a global custom variable.
      static java.util.Map<java.lang.String,​java.lang.Object> getGlobalCustomVars()
      Gets an unmodifiable map, variable key to value, containing the global custom variables.
      static java.lang.String getGlobalVar​(java.lang.String key)
      Gets a global variable.
      static java.util.Map<java.lang.String,​java.lang.String> getGlobalVars()
      Gets an unmodifiable map, variable key to value, containing the global variables.
      static java.lang.Object getScriptCustomVar​(java.lang.String scriptName, java.lang.String key)
      Gets a custom variable from the script with the given name.
      static java.lang.Object getScriptCustomVar​(javax.script.ScriptContext context, java.lang.String key)
      Gets a script custom variable.
      static java.util.Map<java.lang.String,​java.lang.Object> getScriptCustomVars​(java.lang.String scriptName)
      Gets an unmodifiable map, variable key to value, containing the custom variables of the script with the given name.
      static java.lang.String getScriptVar​(java.lang.String scriptName, java.lang.String key)
      Gets a variable from the script with the given name.
      static java.lang.String getScriptVar​(javax.script.ScriptContext context, java.lang.String key)
      Gets a script variable.
      static java.util.Map<java.lang.String,​java.lang.String> getScriptVars​(java.lang.String scriptName)
      Gets an unmodifiable map, variable key to value, containing the variables of the script with the given name.
      static void setGlobalCustomVar​(java.lang.String key, java.lang.Object value)
      Sets or removes a global custom variable.
      static void setGlobalVar​(java.lang.String key, java.lang.String value)
      Sets or removes a global variable.
      static void setScriptCustomVar​(java.lang.String scriptName, java.lang.String key, java.lang.Object value)
      Sets or removes a script custom variable.
      static void setScriptCustomVar​(javax.script.ScriptContext context, java.lang.String key, java.lang.Object value)
      Sets or removes a script custom variable.
      static void setScriptVar​(java.lang.String scriptName, java.lang.String key, java.lang.String value)
      Sets or removes a script variable.
      static void setScriptVar​(javax.script.ScriptContext context, java.lang.String key, java.lang.String value)
      Sets or removes a script variable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setGlobalVar

        public static void setGlobalVar​(java.lang.String key,
                                        java.lang.String value)
        Sets or removes a global variable.

        The variable is removed when the value is null.

        Parameters:
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The key is null or its length is higher than the maximum allowed (30);
        • The value's length is higher than the maximum allowed (1048576);
        • The number of global variables is higher than the maximum allowed (50);
      • getGlobalVar

        public static java.lang.String getGlobalVar​(java.lang.String key)
        Gets a global variable.
        Parameters:
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
      • getGlobalVars

        public static java.util.Map<java.lang.String,​java.lang.String> getGlobalVars()
        Gets an unmodifiable map, variable key to value, containing the global variables.

        Iterations should be done in a synchronised block using the returned map.

        Returns:
        an unmodifiable map containing the global variables, never null.
        Since:
        2.8.0
      • setGlobalCustomVar

        public static void setGlobalCustomVar​(java.lang.String key,
                                              java.lang.Object value)
        Sets or removes a global custom variable.

        The variable is removed when the value is null.

        Parameters:
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The key is null or its length is higher than the maximum allowed (30);
        • The number of global variables is higher than the maximum allowed (50);
        Since:
        2.9.0
      • getGlobalCustomVar

        public static java.lang.Object getGlobalCustomVar​(java.lang.String key)
        Gets a global custom variable.
        Parameters:
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
        Since:
        2.9.0
      • getGlobalCustomVars

        public static java.util.Map<java.lang.String,​java.lang.Object> getGlobalCustomVars()
        Gets an unmodifiable map, variable key to value, containing the global custom variables.

        Iterations should be done in a synchronised block using the returned map.

        Returns:
        an unmodifiable map containing the global variables, never null.
        Since:
        2.9.0
      • setScriptVar

        public static void setScriptVar​(javax.script.ScriptContext context,
                                        java.lang.String key,
                                        java.lang.String value)
        Sets or removes a script variable.

        The variable is removed when the value is null.

        Parameters:
        context - the context of the script.
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The context is null or it does not contain the name of the script;
        • The key is null or its length is higher than the maximum allowed (30);
        • The value's length is higher than the maximum allowed (1048576);
        • The number of script variables is higher than the maximum allowed (20);
      • setScriptVar

        public static void setScriptVar​(java.lang.String scriptName,
                                        java.lang.String key,
                                        java.lang.String value)
        Sets or removes a script variable.

        The variable is removed when the value is null.

        Parameters:
        scriptName - the name of the script.
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The scriptName is null;
        • The key is null or its length is higher than the maximum allowed (30);
        • The value's length is higher than the maximum allowed (1048576);
        • The number of script variables is higher than the maximum allowed (20);
        Since:
        2.8.0
      • getScriptVar

        public static java.lang.String getScriptVar​(javax.script.ScriptContext context,
                                                    java.lang.String key)
        Gets a script variable.
        Parameters:
        context - the context of the script.
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
        Throws:
        java.lang.IllegalArgumentException - if the context is null or it does not contain the name of the script.
      • getScriptVar

        public static java.lang.String getScriptVar​(java.lang.String scriptName,
                                                    java.lang.String key)
        Gets a variable from the script with the given name.
        Parameters:
        scriptName - the name of the script.
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
        Throws:
        java.lang.IllegalArgumentException - if the scriptName is null.
        Since:
        2.8.0
      • getScriptVars

        public static java.util.Map<java.lang.String,​java.lang.String> getScriptVars​(java.lang.String scriptName)
        Gets an unmodifiable map, variable key to value, containing the variables of the script with the given name.

        Iterations should be done in a synchronised block using the returned map.

        Parameters:
        scriptName - the name of the script.
        Returns:
        an unmodifiable map containing the script variables, never null.
        Since:
        2.8.0
      • setScriptCustomVar

        public static void setScriptCustomVar​(javax.script.ScriptContext context,
                                              java.lang.String key,
                                              java.lang.Object value)
        Sets or removes a script custom variable.

        The variable is removed when the value is null.

        Parameters:
        context - the context of the script.
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The context is null or it does not contain the name of the script;
        • The key is null or its length is higher than the maximum allowed (30);
        • The number of script variables is higher than the maximum allowed (20);
        Since:
        2.9.0
      • setScriptCustomVar

        public static void setScriptCustomVar​(java.lang.String scriptName,
                                              java.lang.String key,
                                              java.lang.Object value)
        Sets or removes a script custom variable.

        The variable is removed when the value is null.

        Parameters:
        scriptName - the name of the script.
        key - the key of the variable.
        value - the value of the variable.
        Throws:
        java.lang.IllegalArgumentException - if one of the following conditions is met:
        • The scriptName is null;
        • The key is null or its length is higher than the maximum allowed (30);
        • The number of script variables is higher than the maximum allowed (20);
        Since:
        2.9.0
      • getScriptCustomVar

        public static java.lang.Object getScriptCustomVar​(javax.script.ScriptContext context,
                                                          java.lang.String key)
        Gets a script custom variable.
        Parameters:
        context - the context of the script.
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
        Throws:
        java.lang.IllegalArgumentException - if the context is null or it does not contain the name of the script.
        Since:
        2.9.0
      • getScriptCustomVar

        public static java.lang.Object getScriptCustomVar​(java.lang.String scriptName,
                                                          java.lang.String key)
        Gets a custom variable from the script with the given name.
        Parameters:
        scriptName - the name of the script.
        key - the key of the variable.
        Returns:
        the value of the variable, might be null if no value was previously set.
        Throws:
        java.lang.IllegalArgumentException - if the scriptName is null.
        Since:
        2.9.0
      • getScriptCustomVars

        public static java.util.Map<java.lang.String,​java.lang.Object> getScriptCustomVars​(java.lang.String scriptName)
        Gets an unmodifiable map, variable key to value, containing the custom variables of the script with the given name.

        Iterations should be done in a synchronised block using the returned map.

        Parameters:
        scriptName - the name of the script.
        Returns:
        an unmodifiable map containing the script variables, never null.
        Since:
        2.9.0
      • clearScriptVars

        public static void clearScriptVars​(java.lang.String scriptName)
        Clears the variables of the script with the given name.
        Parameters:
        scriptName - the name of the script.
        Since:
        2.8.0
        See Also:
        clear(), clearGlobalVars()