From 18b662e19c5783f3cca10547544575002e0929af Mon Sep 17 00:00:00 2001 From: jmehrens Date: Sun, 7 Jan 2024 23:56:49 -0600 Subject: [PATCH 1/3] Session.getService does not use proper classloader in OSGI environment Signed-off-by: jmehrens --- api/src/main/java/jakarta/mail/Session.java | 57 +++++++++------------ 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/api/src/main/java/jakarta/mail/Session.java b/api/src/main/java/jakarta/mail/Session.java index 32224691..ae927bf9 100644 --- a/api/src/main/java/jakarta/mail/Session.java +++ b/api/src/main/java/jakarta/mail/Session.java @@ -816,43 +816,37 @@ private T getService(Provider provider, URLName url, Class acl; if (authenticator != null) - cl = authenticator.getClass().getClassLoader(); + acl = authenticator.getClass(); else - cl = this.getClass().getClassLoader(); + acl = streamProvider.getClass(); - // now load the class Class serviceClass = null; - try { - // 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 - } - if (serviceClass == null || !type.isAssignableFrom(serviceClass)) - serviceClass = - Class.forName(provider.getClassName(), false, cl); - - if (!type.isAssignableFrom(serviceClass)) - throw new ClassCastException( - type.getName() + " " + serviceClass.getName()); - } catch (Exception ex1) { + for (ClassLoader l : getClassLoaders(Thread.class, + provider.getClass(), + acl, + streamProvider.getClass(), + getClass(), + System.class)) { + try { + //load and verify provider is compatible in this classloader + serviceClass = Class.forName(provider.getClassName(), + false, l).asSubclass(type); + break; + } catch (ClassNotFoundException | ClassCastException ex) { + // ignore it + } + } + + if (serviceClass == null) { // That didn't work, now try the "system" class loader. // (Need both of these because JDK 1.1 class loaders // may not delegate to their parent class loader.) try { - serviceClass = Class.forName(provider.getClassName()); - if (!type.isAssignableFrom(serviceClass)) - throw new ClassCastException( - type.getName() + " " + serviceClass.getName()); + serviceClass = Class.forName(provider.getClassName()) + .asSubclass(type); } catch (Exception ex) { // Nothing worked, give up. logger.log(Level.FINE, "Exception loading provider", ex); @@ -866,14 +860,11 @@ private T getService(Provider provider, URLName url, Class cons = serviceClass.getConstructor(c); Object[] o = {this, url}; - service = cons.newInstance(o); - + return type.cast(cons.newInstance(o)); } catch (Exception ex) { logger.log(Level.FINE, "Exception loading provider", ex); throw new NoSuchProviderException(provider.getProtocol()); } - - return type.cast(service); } /** @@ -991,7 +982,7 @@ public void load(InputStream is) throws IOException { } else { gcl = getContextClassLoader(); //Fail safe } - + // next, add all the non-default services ServiceLoader sl = ServiceLoader.load(Provider.class, gcl); for (Provider p : sl) { From 5e8f68eb4c63cf09bf39219c5030f1cac7b1f54a Mon Sep 17 00:00:00 2001 From: jmehrens Date: Mon, 8 Jan 2024 00:57:29 -0600 Subject: [PATCH 2/3] update changes.txt --- doc/release/CHANGES.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/release/CHANGES.txt b/doc/release/CHANGES.txt index 214ba6c2..52e87b2a 100644 --- a/doc/release/CHANGES.txt +++ b/doc/release/CHANGES.txt @@ -21,8 +21,10 @@ longer available. CHANGES IN THE 2.1.3 RELEASE ---------------------------- -E 695 SharedFileInputStream should comply with spec +E 631 Session.getService does not use proper classloader in OSGI environment E 665 Jakarta Mail erroneously assumes that classes can be loaded from Thread#getContextClassLoader +E 695 SharedFileInputStream should comply with spec + CHANGES IN THE 2.1.2 RELEASE From 21368958b272f5c5d49b7d0a7097e43fa3781c39 Mon Sep 17 00:00:00 2001 From: jmehrens Date: Mon, 8 Jan 2024 00:59:23 -0600 Subject: [PATCH 3/3] whitespace --- doc/release/CHANGES.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/release/CHANGES.txt b/doc/release/CHANGES.txt index 52e87b2a..8d1675ee 100644 --- a/doc/release/CHANGES.txt +++ b/doc/release/CHANGES.txt @@ -26,7 +26,6 @@ E 665 Jakarta Mail erroneously assumes that classes can be loaded from Thread# E 695 SharedFileInputStream should comply with spec - CHANGES IN THE 2.1.2 RELEASE ---------------------------- E 629 jakarta.mail-api-2.1.0.jar does not work in OSGi environment (hk2servicelocator)