开发者

Scripting Eclipse with Rhino: classloader belongs to the plugin providing Rhino, not the plugin using it

I am using Rhino to script an Eclipse (RCP) application. The problem is that from Javascript I only have access to classes available to the plugin that provides Rhino, and not to all the classes available to the plugin that runs the scripts.

The obvious answer would be to put Rhino in the scripting plugin, but this doesn't work because it's already provided by one of the application's own plugins (which also provides things I need to script) and Eclipse always uses this version instead of the version closer to hand.

  • Is there a way to change the classloader used by Rhino
  • or is it possible to ensure that E开发者_如何学Cclipse loads the Rhino classes from one plugin rather than another?

Thanks to Thilo's answer I used this:

import net.weissmann.tom.rhino.Activator;  // Plugin activator class
import org.mozilla.javascript.tools.shell.Main;

public class JSServer extends Thread {

    //[...]

    public void run() {
        // recent versions of the Main class kindly export
    // the context factory
        Main.shellContextFactory.initApplicationClassLoader(
                Activator.class.getClassLoader()    
            ) ;

        //[...]
    }


Is there a way to change the classloader used by Rhino

Rhino should be using the current Thread's ContextClassLoader. Try Thread.setContextClassLoader (don't forget to restore it).

If that does not work, maybe you can create your own Rhino ContextFactory:

public final void initApplicationClassLoader(java.lang.ClassLoader loader)

Set explicit class loader to use when searching for Java classes.


I don't know Rhino specifics, but you could consider using Eclipse "buddy classloading" with the "registered" policy.

Rhino's plug-in (net.weissmann.tom.rhino, say) would declare itself "open to extension" by specifying Eclipse-BuddyPolicy: registered in its MANIFEST.MF. Plug-ins with classes that Rhino should be able to see would specify Eclipse-RegisterBuddy: net.weissmann.tom.rhino and would need a bundle-level dependency on net.weissmann.tom.rhino.

  • http://www.eclipsezone.com/articles/eclipse-vms/
  • http://wiki.eclipse.org/index.php/Context_Class_Loader_Enhancements#Technical_Solution
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜