Hi, using the MemoryContext, simplejndi throws a ClassCastException
during the close() call as explained in the issue
http://issues.osjava.org/jira/browse/SJN-77 .
I traced the problem and saw that the code assumes that there're
Thread objects bound to JNDI. I wonder why this is so.
Can anybody comment on how to fix this issue ?
Regards,
Bulent Erdemir
Comment from the JIRA:
==================
Hi, the problem is in the AbstractContext's close() method. This
method iterates through the internal HashTable (called table) in which
it holds the JNDI bound objects. And while iterating it casts each
object from this table to a Thread and tries to see it the thread's
are alive by calling isAlive() on them.
So, obviously, if you've bound an object to the JNDI which is NOT a
Thread, then the cast operation fails. Here's the code:
while(table.size() > 0 || subContexts.size() > 0) {
it = table.keySet().iterator();
while(it.hasNext()) {
Name name = (Name)it.next();
if(!((Thread)table.get(name)).isAlive()) {
table.remove(name);
}
}
I don't understand why the author does this (Thread casting and
calling isAlive()), however, this is the cause of your problem. And
mine, too... :)
Here's a JUnit 4 test to reproduce the problem:
package org.falez.falez.jndi.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.junit.Before;
import org.junit.Test;
public class TestMemoryContext {
InitialContext ic;
@Before
public void setup() throws NamingException {
System.setProperty("java.naming.factory.initial",
"org.osjava.sj.memory.MemoryContextFactory");
ic = new InitialContext();
}
@Test
public void testPut() throws NamingException {
String name = "falez:/hoba";
String value = "hoba1";
ic.bind(name, value);
String result = (String) ic.lookup(name);
ic.close();
}
}