Class RhinoSandboxImpl

java.lang.Object
delight.rhinosandox.internal.RhinoSandboxImpl
All Implemented Interfaces:
RhinoSandbox

public class RhinoSandboxImpl extends Object implements RhinoSandbox
  • Field Details

    • contextFactory

      protected SafeContext contextFactory
    • globalScope

      protected org.mozilla.javascript.ScriptableObject globalScope
    • safeScope

      protected org.mozilla.javascript.ScriptableObject safeScope
    • instructionLimit

      protected int instructionLimit
    • maxDuration

      protected long maxDuration
    • useSafeStandardObjects

      protected boolean useSafeStandardObjects
    • sealScope

      protected boolean sealScope
    • inScope

      protected final Map<String,Object> inScope
    • classShutter

      protected SafeClassShutter classShutter
    • ctxFactoryLock

      protected static final Object ctxFactoryLock
  • Constructor Details

    • RhinoSandboxImpl

      public RhinoSandboxImpl()
  • Method Details

    • getSafeContext

      protected SafeContext getSafeContext()
      Creates a new SafeContext instance. Subclasses can override this method to provide custom SafeContext implementations while preserving all configuration logic.
      Returns:
      a new SafeContext instance
    • assertContextFactory

      public void assertContextFactory()
      see https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scopes_and_Contexts
    • assertSafeScope

      public void assertSafeScope(org.mozilla.javascript.Context context)
    • evalWithGlobalScope

      public Object evalWithGlobalScope(String sourceName, String js)
      Description copied from interface: RhinoSandbox
      Evaluate the given script with the global scope. That is all new global variables written will be available to all other scripts.
      Specified by:
      evalWithGlobalScope in interface RhinoSandbox
    • evalWithGlobalScope

      public Object evalWithGlobalScope(String sourceName, Reader js) throws IOException
      Description copied from interface: RhinoSandbox
      Evaluate the given script with the global scope. That is all new global variables written will be available to all other scripts.
      Specified by:
      evalWithGlobalScope in interface RhinoSandbox
      Throws:
      IOException
    • eval

      public Object eval(String sourceName, String js, Map<String,Object> variables)
      Description copied from interface: RhinoSandbox

      Evaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.

      variables defines variables with Java objects which will be available for the execution of this script.

      Specified by:
      eval in interface RhinoSandbox
    • eval

      public Object eval(String sourceName, String js, org.mozilla.javascript.Scriptable scriptableObject)
      Evaluates JavaScript code with a custom Scriptable object as the execution scope. This method provides more flexibility by allowing custom Scriptable objects while maintaining all security features of the sandbox.
      Specified by:
      eval in interface RhinoSandbox
      Parameters:
      sourceName - the name of the source file or context
      js - the JavaScript code to evaluate
      scriptableObject - the Scriptable object to use as the execution scope
      Returns:
      the result of the JavaScript evaluation
    • evalWithScope

      private Object evalWithScope(String sourceName, String js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer)
      Common evaluation logic that sets up the execution context and delegates scope-specific configuration to a callback function.
      Parameters:
      sourceName - the name of the source file or context
      js - the JavaScript code to evaluate
      scopeConfigurer - callback that configures the execution scope
      Returns:
      the result of the JavaScript evaluation
    • evalWithScope

      private Object evalWithScope(String sourceName, Reader js, RhinoSandboxImpl.ScopeConfigurer scopeConfigurer) throws IOException
      Common evaluation logic for Reader input that sets up the execution context and delegates scope-specific configuration to a callback function.
      Parameters:
      sourceName - the name of the source file or context
      js - the JavaScript reader to evaluate
      scopeConfigurer - callback that configures the execution scope
      Returns:
      the result of the JavaScript evaluation
      Throws:
      IOException - if there's an error reading from the Reader
    • eval

      public Object eval(String sourceName, Reader js, Map<String,Object> variables) throws IOException
      Description copied from interface: RhinoSandbox

      Evaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.

      variables defines variables with Java objects which will be available for the execution of this script.

      Specified by:
      eval in interface RhinoSandbox
      Throws:
      IOException
    • eval

      public Object eval(String sourceName, String js)
      Description copied from interface: RhinoSandbox
      Evaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.
      Specified by:
      eval in interface RhinoSandbox
    • callFunction

      public Object callFunction(org.mozilla.javascript.NativeFunction function, Object[] args)
      Description copied from interface: RhinoSandbox
      executes a javascript function
      Specified by:
      callFunction in interface RhinoSandbox
      Parameters:
      function - a Native function you may got from js
      args - parameters you need to call the function
      Returns:
      the result of the javascript function
    • eval

      public Object eval(String sourceName, Reader js) throws IOException
      Description copied from interface: RhinoSandbox
      Evaluate a script with its own scope. It has access to all objects in the global scope but cannot add new ones.
      Specified by:
      eval in interface RhinoSandbox
      Throws:
      IOException
    • setInstructionLimit

      public RhinoSandbox setInstructionLimit(int limit)
      Description copied from interface: RhinoSandbox
      Sets the maximum instructions allowed for script execution.
      Specified by:
      setInstructionLimit in interface RhinoSandbox
    • setMaxDuration

      public RhinoSandbox setMaxDuration(int limitInMs)
      Sets the maximum allowed duration for scripts.
      Specified by:
      setMaxDuration in interface RhinoSandbox
    • setUseSafeStandardObjects

      public RhinoSandbox setUseSafeStandardObjects(boolean useSafeStandardObjects)
      Description copied from interface: RhinoSandbox
      If .initSafeStandardObjects should be used.
      Specified by:
      setUseSafeStandardObjects in interface RhinoSandbox
    • allow

      public RhinoSandbox allow(Class<?> clazz)
      Description copied from interface: RhinoSandbox

      Will allow access to this class in Rhino scripts.

      Note that for classes in packages which don't start with java., com., net. etc. the class name needs to be prefixed with Packages.

      e.g. mypackage.Myclass will be Packages.mypackage.MyClass

      see

      Specified by:
      allow in interface RhinoSandbox
    • inject

      public RhinoSandbox inject(Class<org.mozilla.javascript.ScriptableObject> clazz)
      Description copied from interface: RhinoSandbox
      Will make this class available to instantiate in Rhino scripts.
      Specified by:
      inject in interface RhinoSandbox
    • inject

      public RhinoSandbox inject(String variableName, Object object)
      Description copied from interface: RhinoSandbox
      Will add a global variable available to all scripts executed with this sandbox.
      Specified by:
      inject in interface RhinoSandbox
    • injectInt

      protected void injectInt(String variableName, Object object)
    • setUseSealedScope

      public RhinoSandbox setUseSealedScope(boolean value)
      Description copied from interface: RhinoSandbox
      If the global scope should be sealed (default: true).
      Specified by:
      setUseSealedScope in interface RhinoSandbox