Class DefaultScriptingExecutor

java.lang.Object
org.openspaces.remoting.scripting.DefaultScriptingExecutor
All Implemented Interfaces:
EventListener, ClusterInfoAware, ScriptingExecutor, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener

public class DefaultScriptingExecutor extends Object implements ScriptingExecutor, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener, ClusterInfoAware
A Default "server" side script executor. Can execute Scripts based on a set of registered LocalScriptExecutors.

Will automatically register groovy (GroovyLocalScriptExecutor) if it exists within the classpath. Will also automatically register ruby (JRubyLocalScriptExecutor) if the jruby jars exists within the classpath.

If working under Java 6, or adding JSR 223 jars into the classpath, will use its scripting support as a fall-back if no local script executors are found for a given type. The JSR allows for a unified API on top of different scripting libraries with pluggable types.

The executor will automatically add the Spring application context as a parameter to the script under the name applicationContext allowing to get any beans defined on the "server side". Since GigaSpace instances are often used within the script, it will automatically add all the different GigaSpace instances defined within the application context under their respective bean names.

Another parameter that will be passed to the script is the ClusterInfo allowing the script to be "aware" of which cluster instance it is executed on, and the cluster size.

This executor also supports caching of compiled scripts using the CompiledScriptCache abstraction. There are two special caches, one for compiled scripts that are thread safe (the same compiled script can be executed by several threads) and one for compiled scripts that are not thread safe (the same compiled scripts can only be executed by a single thread). Note, caching is done based on the script name (Script.getName(), this means that changed scripts should change their name in order to recompile them and insert them into the cache.

Author:
kimchy
  • Field Details

  • Constructor Details

    • DefaultScriptingExecutor

      public DefaultScriptingExecutor()
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setClusterInfo

      public void setClusterInfo(ClusterInfo clusterInfo)
      Description copied from interface: ClusterInfoAware
      Sets the cluster information.

      Note, the cluster information is obtained externally from the application context which means that this feature need to be supported by specific containers (and is not supported by plain Spring application context). This means that beans that implement ClusterInfoAware should take into account the fact that the cluster info provided might be null.

      Specified by:
      setClusterInfo in interface ClusterInfoAware
      Parameters:
      clusterInfo - The cluster information to be injected
    • setParameters

      public void setParameters(Map<String,Object> parameters)
      Sets parameters that will be added to each script. The key of the map is the parameter name (a String) and the value if a free Object as the parameter value.
    • setParameterTypes

      public void setParameterTypes(Map<String,Class<?>> parameterTypes)
      Sets parameter types for parameters that will be added to each script. The key of the map is the parameter name (a String) and the value is a class representing that static type for the key name.
    • setExecutors

      public void setExecutors(Map<String,LocalScriptExecutor> executors)
    • setThreadSafeCompiledScriptCache

      public void setThreadSafeCompiledScriptCache(CompiledScriptCache threadSafeCompiledScriptCache)
      Sets a compiled script cache for compiled scripts taht are thread safe (the same script can be executed by different threads). Defaults to LRUThreadSafeCompiledScriptCache.
      See Also:
    • setNonThreadSafeCopmiledScriptCache

      public void setNonThreadSafeCopmiledScriptCache(CompiledScriptCache nonThreadSafeCopmiledScriptCache)
      Sets a compiled script cache for compiled scripts taht are not thread safe (the same script can not be executed by different threads). Defaults to LRUNonThreadSafeCompiledScriptCache.
      See Also:
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • onApplicationEvent

      public void onApplicationEvent(org.springframework.context.ApplicationEvent applicationEvent)
      On applicaiton context refresh event get all the GigaSpace beans and put them in a map that will later be appeneded to any script parameters.
      Specified by:
      onApplicationEvent in interface org.springframework.context.ApplicationListener
    • execute

      public Object execute(Script script) throws ScriptingException
      Description copied from interface: ScriptingExecutor
      Executes the given script and returns a response.
      Specified by:
      execute in interface ScriptingExecutor
      Throws:
      ScriptingException
    • asyncExecute

      public Future asyncExecute(Script script) throws ScriptingException
      Description copied from interface: ScriptingExecutor
      Executes the given script and return a future that can be used to read the response at a later stage.

      Note, this only works with "async" remoting.

      Specified by:
      asyncExecute in interface ScriptingExecutor
      Throws:
      ScriptingException