ClassCastException - ClassLoader issues

B

Berlin Brown

I am working with servlet code on Websphere and when I take code off
the 'servletContext', I keep getting a ClassCastException. They should
be identical and even print outs show the same name.

But when I extract the object off the servletContext, I get a
classCastException. From reading online, I might have to change the
classloader policy within the server?

Error caused here:

IJythonUtilPlugin plugin = (JythonUtilPlugin)
context.getAttribute(JythonUtilPlugin.PLUGIN_NAME_KEY);

Here is the code I am trying to use to fix the issue.

log.info("=========================================");
Object obj = context.getAttribute(JythonUtilPlugin.PLUGIN_NAME_KEY);
log.info("Current Object Type=" + obj.getClass().getName());
log.info("Instance Of=" + (obj instanceof JythonUtilPlugin));
log.info("Class=" + (obj.getClass()));
String tmp1 = "ClassLoader=" + (obj.getClass().getClassLoader());
log.info(tmp1.substring(0, 20));
log.info("This=" + (this.getClass().getClassLoader()));

// More ClassLoader Checks (from IBM thinkworks)
java.lang.ClassLoader ctxCl =
Thread.currentThread().getContextClassLoader();
java.lang.ClassLoader tcCl = JythonUtilPlugin.class.getClassLoader();

java.lang.ClassLoader soCl = obj.getClass().getClassLoader();
tmp1 = "ctxCl=" + ((ctxCl == null) ? "null" : ctxCl.toString());
log.info(tmp1.substring(0, 40));
tmp1 = "targetCl=" + ((tcCl == null) ? "null" : tcCl.toString());
log.info(tmp1.substring(0, 40));
tmp1 = "sourceCl=" + ((soCl == null) ? "null" : soCl.toString());
log.info(tmp1.substring(0, 40));
log.info("=========================================");
// End of Classloader Check
 
B

Berlin Brown

Berlin said:
I am working with servlet code on Websphere and when I take code off
the 'servletContext', I keep getting a ClassCastException. They should
be identical and even print outs show the same name.

But when I extract the object off the servletContext, I get a
classCastException. From reading online, I might have to change the
classloader policy within the server?

Error caused here:

IJythonUtilPlugin plugin = (JythonUtilPlugin)
context.getAttribute(JythonUtilPlugin.PLUGIN_NAME_KEY);

Here is the code I am trying to use to fix the issue.

log.info("=========================================");
Object obj = context.getAttribute(JythonUtilPlugin.PLUGIN_NAME_KEY);
log.info("Current Object Type=" + obj.getClass().getName());
log.info("Instance Of=" + (obj instanceof JythonUtilPlugin));
log.info("Class=" + (obj.getClass()));
String tmp1 = "ClassLoader=" + (obj.getClass().getClassLoader());
log.info(tmp1.substring(0, 20));
log.info("This=" + (this.getClass().getClassLoader()));

// More ClassLoader Checks (from IBM thinkworks)
java.lang.ClassLoader ctxCl =
Thread.currentThread().getContextClassLoader();
java.lang.ClassLoader tcCl = JythonUtilPlugin.class.getClassLoader();

java.lang.ClassLoader soCl = obj.getClass().getClassLoader();
tmp1 = "ctxCl=" + ((ctxCl == null) ? "null" : ctxCl.toString());
log.info(tmp1.substring(0, 40));
tmp1 = "targetCl=" + ((tcCl == null) ? "null" : tcCl.toString());
log.info(tmp1.substring(0, 40));
tmp1 = "sourceCl=" + ((soCl == null) ? "null" : soCl.toString());
log.info(tmp1.substring(0, 40));
log.info("=========================================");
// End of Classloader Check


Actually, this may be the fix I need. It gives another exception, but
interesting code, otherwise.

Class clJy1 =
this.getClass().getClassLoader().loadClass("org.spirit.util.JythonUtilPlugin");
Method m = clJy1.getMethod("getInterpreter", null);
log.info("Method: " + m);
PythonInterpreter interp = (PythonInterpreter) m.invoke(objJyPlugin,
new Class[]{});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top