Package tigase.osgi
Class OSGiScriptEngineManager
java.lang.Object
javax.script.ScriptEngineManager
tigase.osgi.OSGiScriptEngineManager
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 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:
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 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 likeScriptEngineManager osgiManager=new OSGiScriptEngineManager(context);
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.
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetEngineByExtension
(String extension) getEngineByMimeType
(String mimeType) getEngineByName
(String shortName) void
void
registerEngineExtension
(String extension, ScriptEngineFactory factory) void
registerEngineMimeType
(String type, ScriptEngineFactory factory) void
registerEngineName
(String name, ScriptEngineFactory factory) void
This method is the only one that is visible and not part of the ScriptEngineManager class.void
setBindings
(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.
-
Constructor Details
-
OSGiScriptEngineManager
public OSGiScriptEngineManager() -
OSGiScriptEngineManager
public OSGiScriptEngineManager(org.osgi.framework.BundleContext context)
-
-
Method Details
-
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
- Overrides:
get
in classScriptEngineManager
-
getBindings
- Overrides:
getBindings
in classScriptEngineManager
-
setBindings
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 classScriptEngineManager
-
getEngineByExtension
- Overrides:
getEngineByExtension
in classScriptEngineManager
-
getEngineByMimeType
- Overrides:
getEngineByMimeType
in classScriptEngineManager
-
getEngineByName
- Overrides:
getEngineByName
in classScriptEngineManager
-
getEngineFactories
- Overrides:
getEngineFactories
in classScriptEngineManager
-
put
- Overrides:
put
in classScriptEngineManager
-
registerEngineExtension
- Overrides:
registerEngineExtension
in classScriptEngineManager
-
registerEngineMimeType
- Overrides:
registerEngineMimeType
in classScriptEngineManager
-
registerEngineName
- Overrides:
registerEngineName
in classScriptEngineManager
-