I am setting up Logback with SMTPAppender for glassfish 4 and having issues with javax.mail.Session's getService(Provider provider, URLName url) method. I have put all the logback jars and javax.mail (javax.mail.jar; jcl-over-slf4j-1.7.5.jar; jul-to-slf4j-1.7.5.jar; logback-classic-1.0.13.jar; logback-core-1.0.13.jar; slf4j-api-1.7.5.jar) in glassfish4\glassfish\lib\endorsed. The getService method, instead of loading the service class by utilizing this.getClass().getClassLoader(), relies on getContextClassLoader(). The service class in my setup is com.sun.mail.smtp.SMTPTransport. Since the javax.mail.Session.class, javax.mail.URLName.class are loaded directly, they resolve to the classes in the endorsed directory. However, the service class is loaded through getContextClassLoader() and it resolves to the class of the jar in glassfish4\glassfish\modules. As a result, the code can't find the constructor:
Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
Constructor cons = serviceClass.getConstructor(c);
I started to wonder, why the code section below exists in Session.getService(...).
// First try the "application's" class loader.
ClassLoader ccl = getContextClassLoader();
if (ccl != null)
try {
serviceClass =
Class.forName(provider.getClassName(), false, ccl);
} catch (ClassNotFoundException ex) {
// ignore it
}
Thanks,
Janos