From b5c95c5d6c732307e6b654e4cd25899f4386863b Mon Sep 17 00:00:00 2001 From: jmehrens Date: Sun, 11 Feb 2024 23:49:53 -0600 Subject: [PATCH] Jakarta Activation erroneously assumes that classes can be loaded from Thread#getContextClassLoader #144 Signed-off-by: jmehrens --- .../jakarta/activation/FactoryFinder.java | 67 ++++++++++--------- .../jakarta/activation/MailcapCommandMap.java | 12 ++-- .../activation/MimetypesFileTypeMap.java | 12 ++-- 3 files changed, 47 insertions(+), 44 deletions(-) diff --git a/api/src/main/java/jakarta/activation/FactoryFinder.java b/api/src/main/java/jakarta/activation/FactoryFinder.java index 53e76d8..57bd290 100644 --- a/api/src/main/java/jakarta/activation/FactoryFinder.java +++ b/api/src/main/java/jakarta/activation/FactoryFinder.java @@ -27,7 +27,7 @@ class FactoryFinder { new ServiceLoaderUtil.ExceptionHandler() { @Override public RuntimeException createException(Throwable throwable, String message) { - return new RuntimeException(message, throwable); + return new IllegalStateException(message, throwable); } }; @@ -52,8 +52,9 @@ static T find(Class factoryClass) throws RuntimeException { return f; } } - throw new RuntimeException( - "Provider for " + factoryClass.getName() + " cannot be found", null); + + throw EXCEPTION_HANDLER.createException((Throwable) null, + "Provider for " + factoryClass.getName() + " cannot be found"); } static T find(Class factoryClass, ClassLoader loader) throws RuntimeException { @@ -85,7 +86,9 @@ static T find(Class factoryClass, ClassLoader loader) throws RuntimeExcep return null; } - private static T newInstance(String className, Class service, ClassLoader loader) throws RuntimeException { + private static T newInstance(String className, + Class service, ClassLoader loader) + throws RuntimeException { return ServiceLoaderUtil.newInstance( className, service, @@ -161,41 +164,41 @@ private static T lookupUsingHk2ServiceLoader(Class factoryClass, ClassLoa private static ClassLoader[] getClassLoaders(final Class... classes) { return AccessController.doPrivileged( - new PrivilegedAction() { - @Override - public ClassLoader[] run() { - ClassLoader[] loaders = new ClassLoader[classes.length]; - int w = 0; - for (Class k : classes) { - ClassLoader cl = null; - if (k == Thread.class) { - try { - cl = Thread.currentThread().getContextClassLoader(); - } catch (SecurityException ex) { - } - } else if (k == System.class) { - try { - cl = ClassLoader.getSystemClassLoader(); - } catch (SecurityException ex) { - } - } else { - try { - cl = k.getClassLoader(); - } catch (SecurityException ex) { - } + new PrivilegedAction() { + @Override + public ClassLoader[] run() { + ClassLoader[] loaders = new ClassLoader[classes.length]; + int w = 0; + for (Class k : classes) { + ClassLoader cl = null; + if (k == Thread.class) { + try { + cl = Thread.currentThread().getContextClassLoader(); + } catch (SecurityException ex) { } - - if (cl != null) { - loaders[w++] = cl; + } else if (k == System.class) { + try { + cl = ClassLoader.getSystemClassLoader(); + } catch (SecurityException ex) { + } + } else { + try { + cl = k.getClassLoader(); + } catch (SecurityException ex) { } } - if (loaders.length != w) { - loaders = Arrays.copyOf(loaders, w); + if (cl != null) { + loaders[w++] = cl; } - return loaders; } + + if (loaders.length != w) { + loaders = Arrays.copyOf(loaders, w); + } + return loaders; } + } ); } } diff --git a/api/src/main/java/jakarta/activation/MailcapCommandMap.java b/api/src/main/java/jakarta/activation/MailcapCommandMap.java index 6fbb48e..3ebbff8 100644 --- a/api/src/main/java/jakarta/activation/MailcapCommandMap.java +++ b/api/src/main/java/jakarta/activation/MailcapCommandMap.java @@ -210,7 +210,7 @@ private MailcapRegistry loadResource(String name) { } catch (IOException | SecurityException e) { if (LogSupport.isLoggable()) LogSupport.log("MailcapCommandMap: can't load " + name, e); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MailcapRegistryProvider. " + "MailcapRegistry: can't load " + name, e); @@ -261,7 +261,7 @@ private void loadAllResources(List v, String name) { if (LogSupport.isLoggable()) LogSupport.log("MailcapCommandMap: can't load " + url, ioex); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MailcapRegistryProvider. " + "MailcapRegistry: can't load " + name, e); @@ -296,7 +296,7 @@ private MailcapRegistry loadFile(String name) { if (LogSupport.isLoggable()) { LogSupport.log("MailcapRegistry: can't load from file - " + name, e); } - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MailcapRegistryProvider. " + "MailcapRegistry: can't load " + name, e); @@ -317,7 +317,7 @@ public MailcapCommandMap(String fileName) throws IOException { if (DB[PROG] == null) { try { DB[PROG] = getImplementation().getByFileName(fileName); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { String message = "Cannot find or load an implementation for MailcapRegistryProvider. " + "MailcapRegistry: can't load " + fileName; if (LogSupport.isLoggable()) { @@ -346,7 +346,7 @@ public MailcapCommandMap(InputStream is) { DB[PROG] = getImplementation().getByInputStream(is); } catch (IOException ex) { // XXX - should throw it - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MailcapRegistryProvider." + "MailcapRegistry: can't load InputStream", e); @@ -547,7 +547,7 @@ public synchronized void addMailcap(String mail_cap) { DB[PROG] = getImplementation().getInMemory(); } DB[PROG].appendToMailcap(mail_cap); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MailcapRegistryProvider. " + "MailcapRegistry: can't load", e); diff --git a/api/src/main/java/jakarta/activation/MimetypesFileTypeMap.java b/api/src/main/java/jakarta/activation/MimetypesFileTypeMap.java index 80a1d01..8dcea9b 100644 --- a/api/src/main/java/jakarta/activation/MimetypesFileTypeMap.java +++ b/api/src/main/java/jakarta/activation/MimetypesFileTypeMap.java @@ -166,7 +166,7 @@ private MimeTypeRegistry loadResource(String name) { } catch (IOException | SecurityException e) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't load " + name, e); @@ -228,7 +228,7 @@ private void loadAllResources(Vector v, String name) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + url, ioex); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't load " + url, e); @@ -270,7 +270,7 @@ private MimeTypeRegistry loadFile(String name) { if (LogSupport.isLoggable()) { LogSupport.log("MimeTypeRegistry: can't load from file - " + name, e); } - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't load " + name, e); @@ -290,7 +290,7 @@ public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException { this(); try { DB[PROG] = getImplementation().getByFileName(mimeTypeFileName); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { String errorMessage = "Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't load " + mimeTypeFileName; if (LogSupport.isLoggable()) { @@ -312,7 +312,7 @@ public MimetypesFileTypeMap(InputStream is) { DB[PROG] = getImplementation().getByInputStream(is); } catch (IOException ex) { // XXX - really should throw it - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't load InputStream", e); @@ -332,7 +332,7 @@ public synchronized void addMimeTypes(String mime_types) { DB[PROG] = getImplementation().getInMemory(); } DB[PROG].appendToRegistry(mime_types); - } catch (NoSuchElementException | ServiceConfigurationError e) { + } catch (NoSuchElementException | IllegalStateException | ServiceConfigurationError e) { if (LogSupport.isLoggable()) { LogSupport.log("Cannot find or load an implementation for MimeTypeRegistryProvider." + "MimeTypeRegistry: can't add " + mime_types, e);