Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix630 2 #633

Merged
merged 6 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,16 @@
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
<Implementation-Build-Id>${buildNumber}</Implementation-Build-Id>
<Import-Package>
!org.glassfish.hk2.osgiresourcelocator,
*
</Import-Package>
<Require-Capability><![CDATA[
osgi.extender;filter:="(&(osgi.extender=osgi.serviceloader.processor)
(version>=1.0.0)(!(version>=2.0.0)))",
osgi.serviceloader;
filter:="(osgi.serviceloader=jakarta.mail.util.StreamProvider)";
cardinality:=multiple,
osgi.serviceloader;
filter:="(osgi.serviceloader=jakarta.mail.Provider)";
cardinality:=multiple]]>
</Require-Capability>
</instructions>
</configuration>
<executions>
Expand Down
34 changes: 0 additions & 34 deletions api/src/main/java/jakarta/mail/util/FactoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ static <T> T find(Class<T> factoryClass) throws RuntimeException {
return factory;
}

// handling Glassfish/OSGi (platform specific default)
if (isOsgi()) {
T result = lookupUsingOSGiServiceLoader(factoryId);
if (result != null) {
return result;
}
}
throw new IllegalStateException("Not provider of " + factoryClass.getName() + " was found");
}

Expand All @@ -86,33 +79,6 @@ private static String fromSystemProperty(String factoryId) {
return systemProp;
}

private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader";

private static boolean isOsgi() {
try {
Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
return true;
} catch (ClassNotFoundException ignored) {
}
return false;
}

@SuppressWarnings({"unchecked"})
private static <T> T lookupUsingOSGiServiceLoader(String factoryId) {
try {
// Use reflection to avoid having any dependency on HK2 ServiceLoader class
Class<?> serviceClass = Class.forName(factoryId);
Class<?>[] args = new Class<?>[]{serviceClass};
Class<?> target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
Method m = target.getMethod("lookupProviderInstances", Class.class);
Iterator<?> iter = ((Iterable<?>) m.invoke(null, (Object[]) args)).iterator();
return iter.hasNext() ? (T) iter.next() : null;
} catch (Exception ignored) {
// log and continue
return null;
}
}

private static <T> T factoryFromServiceLoader(Class<T> factory) {
try {
ServiceLoader<T> sl = ServiceLoader.load(factory);
Expand Down