Page 1 of 1

Class loading issue for plugin

Posted: Thu Aug 09, 2012 7:55 pm
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

Re: Class loading issue for plugin

Posted: Fri Aug 10, 2012 10:58 am
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

Re: Class loading issue for plugin

Posted: Fri Aug 17, 2012 7:02 pm
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.