participate


JavaMail - NoSuchProviderException -> bug in Session.getSession
<<   Back to Forum  |   Give us Feedback
This topic has 1 reply on 1 page.
rzo
Posts:4
Registered: 9/17/05
NoSuchProviderException -> bug in Session.getSession   
Jun 10, 2006 4:57 AM

 
Session.getSession Method throws NoSuchProviderException.
Problem arises when working with different classloaders.

Looking at the source code: It is not taken into account that the same classes may have been loaded in different class loaders. We may find the class but not the constructor.

The class may be available in both the "application" class loader and the "system" class loader. The current implementation first tries the application CL then the system CL.

However when searching for the Constructor the classloader is no longer taken into account. The code:

Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
	    Constructor cons = serviceClass.getConstructor(c);
 
	    Object[] o = {this, url};
	    service = cons.newInstance(o);


will not find the constructor. since the classes in "c" were loaded in a different class loader.

to solve the issue: if the consturctor was not found with the serviceClass from the application class loader try the serviceClass from the system class loader before throwing an exception:

I propose to change the code to something like:


           // find the provider class
 
	    ClassLoader ccl = getContextClassLoader();
	    if (ccl != null)
		try {
		          serviceClass1 = ccl.loadClass(provider.getClassName());
		     } catch (ClassNotFoundException ex) { }
 	    try {       
		        serviceClass2 = cl.loadClass(provider.getClassName());
	          } catch (Exception ex1) {}
 
            try {
		serviceClass3 = Class.forName(provider.getClassName());
	          } catch (Exception ex) {}
 
 
	// construct an instance of the class
 
	if (serviceClass1 != null)
	try {
	    Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
	    Constructor cons = serviceClass1.getConstructor(c);
 
	    Object[] o = {this, url};
	    service = cons.newInstance(o);
 
	} catch (Exception ex) {}
	
	if (service == null && serviceClass2 != null)
	try {
	    Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
	    Constructor cons = serviceClass2.getConstructor(c);
 
	    Object[] o = {this, url};
	    service = cons.newInstance(o);
 
	} catch (Exception ex) {}	
	
		if (service == null && serviceClass3 != null)
	try {
	    Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
	    Constructor cons = serviceClass3.getConstructor(c);
 
	    Object[] o = {this, url};
	    service = cons.newInstance(o);
 
	} catch (Exception ex) {}
	
if (service == null)
	throw new NoSuchProviderException(provider.getProtocol());
 
bshannon
Posts:4,225
Registered: 10/27/97
Re: NoSuchProviderException -> bug in Session.getSession   
Nov 22, 2006 11:49 AM (reply 1 of 1)  (In reply to original post )

 
I'm finally getting back to this issue...

Can you explain how this happens?

Do you have two versions of the same class in your CLASSPATH,
and one version has the required constructor but the other version
does not?

Of course, it's generally bad practice to have two versions of the same
class on your CLASSPATH.

And it's also generally good practice that the application class loader
will first delegate requests to the system class loader before trying to
load the class itself.

It sounds like the only way to get into the situation where you see
this problem is to have a broken application class loader and a
poorly configured CLASSPATH.
 
This topic has 1 reply on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 29
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums