Skip to content

Commit

Permalink
Fix for jakartaee#630.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Oct 23, 2022
1 parent 82dae3a commit 8fc297d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
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)
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;
}

0 comments on commit 8fc297d

Please sign in to comment.