magnetronnie
magnetronnie

Reputation: 505

Thread.currentThread().getContextClassLoader() returns multiple object instances

I have a stateless session bean that creates a RuntimeFactory and Application object. Both classes are part of the Social Business Toolkit. Application is used to read the properties and managed-beans files, but this didn't happen because the RuntimeFactory was unable to get the Application object.

AbstractRuntimeFactory has a Map with Application objects:

private Map<ClassLoader,AbstractApplication> applications = new HashMap<ClassLoader, AbstractApplication>();

ClassLoader is set using this method:

protected ClassLoader getContextClassLoader() {
    return Thread.currentThread().getContextClassLoader();
}

The Application object is retrieved with this method:

public Application getApplicationUnchecked() {
    ClassLoader cl = getContextClassLoader();
    return applications.get(cl);
}

During debugging I noticed the Thread id stays the same, but there are two different instances of ClassLoader. How does this happen? There's only one session bean, RuntimeFactory and Application. Shouldn't getContextClassLoader() always give me the same object back?

As I work around I now use:

ClassLoader cl = this.getClass().getClassLoader();

Where this is the RuntimeFactory, but I'm not sure if this is a good solution.. it feels more like a workaround to the actual problem.

ps: I'm using WebSphere Portal as application server.

Upvotes: 0

Views: 766

Answers (1)

Mark Wallace
Mark Wallace

Reputation: 146

You don't need to use the RuntimeFactory stuff in this case and it's may be easier to avoid it. I worked with another customer last year who was also using EJB's and they opted to just instantiate the Endpoint they needed directly and manage the endpoint settings themselves. The code would look something like this:

    BasicEndpoint endpoint = new ConnectionsBasicEndpoint();
    endpoint.setUrl(url);
    endpoint.setUser(user);
    endpoint.setPassword(password);
    endpoint.setForceTrustSSLCertificate(true);

    BlogService blogService = new BlogService(endpoint);

Upvotes: 3

Related Questions