Skip to content

Commit

Permalink
Jakarta Activation erroneously assumes that classes can be loaded fro…
Browse files Browse the repository at this point in the history
…m Thread#getContextClassLoader jakartaee#144

Signed-off-by: jmehrens <jason_mehrens@hotmail.com>
  • Loading branch information
jmehrens committed Feb 12, 2024
1 parent 31fc414 commit b5c95c5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
67 changes: 35 additions & 32 deletions api/src/main/java/jakarta/activation/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FactoryFinder {
new ServiceLoaderUtil.ExceptionHandler<RuntimeException>() {
@Override
public RuntimeException createException(Throwable throwable, String message) {
return new RuntimeException(message, throwable);
return new IllegalStateException(message, throwable);
}
};

Expand All @@ -52,8 +52,9 @@ static <T> T find(Class<T> 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> T find(Class<T> factoryClass, ClassLoader loader) throws RuntimeException {
Expand Down Expand Up @@ -85,7 +86,9 @@ static <T> T find(Class<T> factoryClass, ClassLoader loader) throws RuntimeExcep
return null;
}

private static <T> T newInstance(String className, Class<? extends T> service, ClassLoader loader) throws RuntimeException {
private static <T> T newInstance(String className,
Class<? extends T> service, ClassLoader loader)
throws RuntimeException {
return ServiceLoaderUtil.newInstance(
className,
service,
Expand Down Expand Up @@ -161,41 +164,41 @@ private static <T> T lookupUsingHk2ServiceLoader(Class<T> factoryClass, ClassLoa

private static ClassLoader[] getClassLoaders(final Class<?>... classes) {
return AccessController.doPrivileged(
new PrivilegedAction<ClassLoader[]>() {
@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<ClassLoader[]>() {
@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;
}
}
);
}
}
12 changes: 6 additions & 6 deletions api/src/main/java/jakarta/activation/MailcapCommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -261,7 +261,7 @@ private void loadAllResources(List<MailcapRegistry> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions api/src/main/java/jakarta/activation/MimetypesFileTypeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -228,7 +228,7 @@ private void loadAllResources(Vector<MimeTypeRegistry> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b5c95c5

Please sign in to comment.