Class RhinoScriptProcessor

java.lang.Object
org.alfresco.repo.processor.BaseProcessor
org.alfresco.repo.jscript.RhinoScriptProcessor
All Implemented Interfaces:
org.alfresco.processor.Processor, org.alfresco.scripts.ScriptResourceLoader, ScriptProcessor, org.springframework.beans.factory.InitializingBean

public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcessor, org.alfresco.scripts.ScriptResourceLoader, org.springframework.beans.factory.InitializingBean
Implementation of the ScriptProcessor using the Rhino JavaScript library.
Author:
Kevin Roast
  • Field Details

  • Constructor Details

    • RhinoScriptProcessor

      public RhinoScriptProcessor()
  • Method Details

    • setStoreUrl

      public void setStoreUrl(String storeRef)
      Set the default store reference
      Parameters:
      storeRef - The default store reference
    • setStorePath

      public void setStorePath(String storePath)
      Parameters:
      storePath - The store path to set.
    • setCompile

      public void setCompile(boolean compile)
      Parameters:
      compile - the compile flag to set
    • setShareSealedScopes

      public void setShareSealedScopes(boolean shareSealedScopes)
      Parameters:
      shareSealedScopes - true to allow sharing of sealed scopes between script executions - set to false to disable this feature and ensure that a new scope is created for each executed script.
    • setOptimizationLevel

      public void setOptimizationLevel(int optimizationLevel)
      Parameters:
      optimizationLevel - -1 interpretive mode, 0 no optimizations, 1-9 optimizations performed
    • setMaxScriptExecutionSeconds

      public void setMaxScriptExecutionSeconds(int maxScriptExecutionSeconds)
      Parameters:
      maxScriptExecutionSeconds - the number of seconds a script is allowed to run
    • setMaxStackDepth

      public void setMaxStackDepth(int maxStackDepth)
      Parameters:
      maxStackDepth - the number of call stack depth allowed
    • setMaxMemoryUsedInBytes

      public void setMaxMemoryUsedInBytes(long maxMemoryUsedInBytes)
      Parameters:
      maxMemoryUsedInBytes - the number of memory a script can use
    • setObserverInstructionCount

      public void setObserverInstructionCount(int observerInstructionCount)
      Parameters:
      observerInstructionCount - the number of instructions that will trigger ContextFactory.observeInstructionCount(org.mozilla.javascript.Context, int)
    • setCleanScope

      public void setCleanScope(boolean cleanScope)
      Parameters:
      cleanScope - true to enable scope cleaning at the end of each script execution - set to false to disable this feature.
    • reset

      public void reset()
      Description copied from interface: ScriptProcessor
      Reset the processor - such as clearing any internal caches etc.
      Specified by:
      reset in interface ScriptProcessor
      See Also:
    • execute

      public Object execute(ScriptLocation location, Map<String,Object> model)
      Description copied from interface: ScriptProcessor
      Execute script
      Specified by:
      execute in interface ScriptProcessor
      Parameters:
      location - the location of the script
      model - context model
      Returns:
      Object the result of the script
      See Also:
    • execute

      public Object execute(String location, Map<String,Object> model)
      Description copied from interface: ScriptProcessor
      Execute script
      Specified by:
      execute in interface ScriptProcessor
      Parameters:
      location - the classpath string locating the script
      model - the context model
      Returns:
      Object the result of the script
      See Also:
    • execute

      public Object execute(NodeRef nodeRef, QName contentProp, Map<String,Object> model)
      Description copied from interface: ScriptProcessor
      Execute script
      Specified by:
      execute in interface ScriptProcessor
      Parameters:
      nodeRef - the script node reference
      contentProp - the content property of the script
      model - the context model
      Returns:
      Object the result of the script
      See Also:
    • executeString

      public Object executeString(String source, Map<String,Object> model)
      Description copied from interface: ScriptProcessor
      Execute script string
      Specified by:
      executeString in interface ScriptProcessor
      Parameters:
      source - the script string
      model - the context model
      Returns:
      Object the result of the script
      See Also:
    • executeString

      public Object executeString(String source, Map<String,Object> model, boolean secure)
      Description copied from interface: ScriptProcessor
      Execute script string
      Specified by:
      executeString in interface ScriptProcessor
      Parameters:
      source - the script string
      model - the context model
      secure - the flag that indicates if string is considered secure to be executed, i.e., it will have access to the full execution context instead of being executed in a sandbox context
      Returns:
      Object the result of the script
      See Also:
    • loadScriptResource

      public String loadScriptResource(String resource)
      Load a script content from the specific resource path.
      Specified by:
      loadScriptResource in interface org.alfresco.scripts.ScriptResourceLoader
      Parameters:
      resource - Resources can be of the form:
       classpath:alfresco/includeme.js
       workspace://SpacesStore/6f73de1b-d3b4-11db-80cb-112e6c2ea048
       /Company Home/Data Dictionary/Scripts/includeme.js
                  
      Returns:
      the content from the resource, null if not recognised format
      Throws:
      org.alfresco.error.AlfrescoRuntimeException - on any IO or ContentIO error
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Pre initializes two scope objects (one secure and one not) with the standard objects preinitialised. This saves on very expensive calls to reinitialize a new scope on every web script execution. See http://www.mozilla.org/rhino/scopes.html
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
      See Also:
      • InitializingBean.afterPropertiesSet()
    • initScope

      protected org.mozilla.javascript.Scriptable initScope(org.mozilla.javascript.Context cx, boolean secure, boolean sealed)
      Initializes a scope for script execution. The easiest way to embed Rhino is just to create a new scope this way whenever you need one. However, initStandardObjects() is an expensive method to call and it allocates a fair amount of memory.
      Parameters:
      cx - the thread execution context
      secure - Do we consider the script secure? When false this ensures the script may not access insecure java.* libraries or import any other classes for direct access - only the configured root host objects will be available to the script writer.
      sealed - Should the scope be sealed, making it immutable? This should be true if a scope is to be reused.
      Returns:
      the scope object