Class OSGiScriptEngineManager


  • public class OSGiScriptEngineManager
    extends javax.script.ScriptEngineManager
    This class acts as a delegate for all the available ScriptEngineManagers. Unluckily, the standard did not define it as an interface, so we need to extend it to allow polymorphism. However, no calls to super are used. It wraps all available ScriptEngineManagers in the OSGi ServicePlatform into a merged ScriptEngineManager. Internally, what this class does is creating ScriptEngineManagers for each bundle that contains a ScriptEngineFactory and includes a META-INF/services/javax.script.ScriptEngineFactory file. It assumes that the file contains a list of @link ScriptEngineFactory classes. For each bundle, it creates a ScriptEngineManager, then merges them. @link ScriptEngineFactory objects are wrapped into @link OSGiScriptEngineFactory objects to deal with problems of context class loader: Those scripting engines that rely on the ContextClassloader for finding resources need to use this wrapper and the @link OSGiScriptFactory. Mainly, jruby does. Note that even if no context classloader issues arose, it would still be needed to search manually for the factories and either use them directly (losing the mimeType/extension/shortName mechanisms for finding engines or manually registering them) or still use this class, which would be smarter. In the latter case, it would only be needed to remove the hack that temporarily sets the context classloader to the appropriate, bundle-related, class loader. Caveats:
    • All factories are wrapped with an OSGiScriptEngineFactory. As Engines are not wrapped, calls like ScriptEngineManager osgiManager=new OSGiScriptEngineManager(context);
      ScriptEngine engine=osgiManager.getEngineByName("ruby"); ScriptEngineFactory factory=engine.getFactory() //this does not return the OSGiFactory wrapper factory.getScriptEngine(); //this might fail, as it does not use OSGiScriptEngineFactory wrapper
      might result in unexpected errors. Future versions may wrap the ScriptEngine with a OSGiScriptEngine to solve this issue, but for the moment it is not needed.
    • Constructor Summary

      Constructors 
      Constructor Description
      OSGiScriptEngineManager​(org.osgi.framework.BundleContext context)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object get​(java.lang.String key)  
      javax.script.Bindings getBindings()  
      javax.script.ScriptEngine getEngineByExtension​(java.lang.String extension)  
      javax.script.ScriptEngine getEngineByMimeType​(java.lang.String mimeType)  
      javax.script.ScriptEngine getEngineByName​(java.lang.String shortName)  
      java.util.List<javax.script.ScriptEngineFactory> getEngineFactories()  
      void put​(java.lang.String key, java.lang.Object value)  
      void registerEngineExtension​(java.lang.String extension, javax.script.ScriptEngineFactory factory)  
      void registerEngineMimeType​(java.lang.String type, javax.script.ScriptEngineFactory factory)  
      void registerEngineName​(java.lang.String name, javax.script.ScriptEngineFactory factory)  
      void reloadManagers()
      This method is the only one that is visible and not part of the ScriptEngineManager class.
      void setBindings​(javax.script.Bindings bindings)
      Follows the same behavior of @link javax.script.ScriptEngineManager#setBindings(Bindings) This means that the same bindings are applied to all the underlying managers.
      • Methods inherited from class java.lang.Object

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

      • OSGiScriptEngineManager

        public OSGiScriptEngineManager​(org.osgi.framework.BundleContext context)
    • Method Detail

      • reloadManagers

        public void reloadManagers()
        This method is the only one that is visible and not part of the ScriptEngineManager class. Its purpose is to find new managers that weren't available before, but keeping the globalScope bindings set. If you want to clean the bindings you can either get a fresh instance of OSGiScriptManager or setting up a new bindings object. This can be done with: ScriptEngineManager manager=new OSGiScriptEngineManager(context); (...)//do stuff osgiManager=(OSGiScriptEngineManager)manager;//cast to ease reading osgiManager.reloadManagers(); manager.setBindings(new OSGiBindings());//or you can use your own bindings implementation
      • get

        public java.lang.Object get​(java.lang.String key)
        Overrides:
        get in class javax.script.ScriptEngineManager
      • getBindings

        public javax.script.Bindings getBindings()
        Overrides:
        getBindings in class javax.script.ScriptEngineManager
      • getEngineByExtension

        public javax.script.ScriptEngine getEngineByExtension​(java.lang.String extension)
        Overrides:
        getEngineByExtension in class javax.script.ScriptEngineManager
      • getEngineByMimeType

        public javax.script.ScriptEngine getEngineByMimeType​(java.lang.String mimeType)
        Overrides:
        getEngineByMimeType in class javax.script.ScriptEngineManager
      • getEngineByName

        public javax.script.ScriptEngine getEngineByName​(java.lang.String shortName)
        Overrides:
        getEngineByName in class javax.script.ScriptEngineManager
      • getEngineFactories

        public java.util.List<javax.script.ScriptEngineFactory> getEngineFactories()
        Overrides:
        getEngineFactories in class javax.script.ScriptEngineManager
      • put

        public void put​(java.lang.String key,
                        java.lang.Object value)
        Overrides:
        put in class javax.script.ScriptEngineManager
      • registerEngineExtension

        public void registerEngineExtension​(java.lang.String extension,
                                            javax.script.ScriptEngineFactory factory)
        Overrides:
        registerEngineExtension in class javax.script.ScriptEngineManager
      • registerEngineMimeType

        public void registerEngineMimeType​(java.lang.String type,
                                           javax.script.ScriptEngineFactory factory)
        Overrides:
        registerEngineMimeType in class javax.script.ScriptEngineManager
      • registerEngineName

        public void registerEngineName​(java.lang.String name,
                                       javax.script.ScriptEngineFactory factory)
        Overrides:
        registerEngineName in class javax.script.ScriptEngineManager
      • setBindings

        public void setBindings​(javax.script.Bindings bindings)
        Follows the same behavior of @link javax.script.ScriptEngineManager#setBindings(Bindings) This means that the same bindings are applied to all the underlying managers.
        Overrides:
        setBindings in class javax.script.ScriptEngineManager
        Parameters:
        bindings -