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

Using service loader mediator annotations (see #630). #632

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@
<artifactId>angus-activation</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bnd.annotation</artifactId>
<version>6.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
<version>8.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -359,10 +371,6 @@
<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>
</instructions>
</configuration>
<executions>
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/jakarta/mail/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
* @author Max Spivak
*/

@aQute.bnd.annotation.spi.ServiceConsumer(value = Provider.class)
Copy link
Contributor

@lukasj lukasj Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is currently a restriction on using 3rd party libraries in the APIs and only Java SE/Jakarta EE packages can be used.

public final class Session {

// Support legacy @DefaultProvider
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
1 change: 1 addition & 0 deletions api/src/main/java/jakarta/mail/util/StreamProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @since JavaMail 2.1
*/
@aQute.bnd.annotation.spi.ServiceConsumer(value = StreamProvider.class)
public interface StreamProvider {

/**
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
uses jakarta.mail.util.StreamProvider;
//reflective call to java.beans.Beans.instantiate
requires static java.desktop;
//annotations for OSGi service loader mediator
requires static biz.aQute.bnd.annotation;
}