Skip to content

Commit

Permalink
Modify JNXServiceFinder to also check the service class’s ClassLoader…
Browse files Browse the repository at this point in the history
… (Issue #412)
  • Loading branch information
jesse-gallagher committed Mar 29, 2024
1 parent 42e2d92 commit e307ae4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Utility class to coordinate service loading for JNX.
Expand Down Expand Up @@ -81,9 +82,19 @@ public static <T> T findRequiredService(final Class<T> serviceClass, final Class
@SuppressWarnings("unchecked")
public static <T> Stream<T> findServices(final Class<T> serviceClass) {
return (Stream<T>)SERVICE_CACHE.computeIfAbsent(serviceClass, c -> {
final Iterable<T> services = AccessController
Set<T> converters = new TreeSet<>(Comparator.comparing(o -> o.getClass().getName()));

// Check the context (app) ClassLoader
Iterable<T> services = AccessController
.doPrivileged((PrivilegedAction<Iterable<T>>) () -> ServiceLoader.load(serviceClass));
return StreamSupport.stream(services.spliterator(), false).collect(Collectors.toList());
services.forEach(converters::add);

// Also check the service's own ClassLoader, as it may be distinct
services = AccessController
.doPrivileged((PrivilegedAction<Iterable<T>>) () -> ServiceLoader.load(serviceClass, serviceClass.getClassLoader()));
services.forEach(converters::add);

return converters;
}).stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package it.com.hcl.domino.test.data;

import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -377,4 +378,17 @@ public void testTimeRoundTrip() throws Exception {
Assertions.assertEquals(LocalTime.from(expected), LocalTime.from(date));
});
}

@Test
public void testInstantRoundTrip() throws Exception {
final Instant expected = Instant.ofEpochSecond(System.currentTimeMillis() / 1000);

withTempDb(database -> {
final Document doc = database.createDocument();

doc.replaceItemValue("Foo", expected);
final Instant date = doc.get("Foo", Instant.class, null);
Assertions.assertEquals(Instant.from(expected), Instant.from(date));
});
}
}

0 comments on commit e307ae4

Please sign in to comment.