Class loading issue for plugin

Oxygen general issues.
jwalker
Posts: 10
Joined: Thu Aug 09, 2012 3:27 pm

Class loading issue for plugin

Post by jwalker »

I am developing a custom plugin and am running into issues with class loading it looks like? I placed the following into my plugin.xml file:

Code: Select all


classLoaderType="preferReferencedResources"
These are the issues:
I am using a ScriptEngineFactory class from a script-api.jar library to use for beanshell scripting. This is the following error that I get:

Code: Select all


"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.sun.script.javascript.RhinoScriptEngineFactory cannot be cast to javax.script.ScriptEngineFactory"
From code:

Code: Select all


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

private ScriptEngineManager mgr = new ScriptEngineManager();;
private ScriptEngine bshEngine = mgr.getEngineByName("beanshell");

Trying to place saxon9 jars on the runtime path is resulting in the following warnings:

Code: Select all


Warning: external object model net.sf.saxon.dom.DOMEnvelope has been loaded, but is not an instance of net.sf.saxon.om.ExternalObjectModel
Warning: external object model net.sf.saxon.dom.DOMObjectModel has been loaded, but is not an instance of net.sf.saxon.om.ExternalObjectModel
Radu
Posts: 9446
Joined: Fri Jul 09, 2004 5:18 pm

Re: Class loading issue for plugin

Post by Radu »

Hi,

I need to know the exact libraries you are referencing.
Maybe you can paste the entire plugin.xml content in the reply.

Oxygen creates a separate class loader for each loaded plugin.
If you set preferReferencedResources then your referenced JARs will be preferred, for example your Saxon 9 jars will be preferred even if Oxygen comes with its own Saxon 9 jars.
But:
Your plugin's code is usually called by Oxygen from a user interface thread which has as a class loader Oxygen's main class loader.
Some libraries use the context class loader set on the thread to load certain resources.
So when calling code which works with these libraries you should do the following:

Code: Select all


        ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(CustomWorkspaceAccessPluginExtension.class.getClassLoader());
//AND YOUR CODE SHOULD GO HERE
} finally {
Thread.currentThread().setContextClassLoader(currentCL);
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
jwalker
Posts: 10
Joined: Thu Aug 09, 2012 3:27 pm

Re: Class loading issue for plugin

Post by jwalker »

That's what I needed for the saxon issue thanks!

The other issue was due to using a jar file that had a duplicate namespace. I removed the jar file from our code and it worked.
Post Reply