Skip to content

Commit

Permalink
Support OSGi
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos authored and lukasj committed Nov 2, 2021
1 parent aea9f84 commit 29939fe
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 7 deletions.
5 changes: 5 additions & 0 deletions api/pom.xml
Expand Up @@ -350,6 +350,10 @@
<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 Expand Up @@ -404,6 +408,7 @@ Use is subject to <a href="{@docRoot}/doc-files/speclicense.html" target="_top">
<configuration>
<forkCount>2</forkCount>
<reuseForks>false</reuseForks>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
Expand Down
151 changes: 151 additions & 0 deletions api/src/main/java/jakarta/mail/util/FactoryFinder.java
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package jakarta.mail.util;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.ServiceLoader;

class FactoryFinder {

/**
* Finds the implementation {@code Class} object for the given
* factory type.
* The arguments supplied must be used in order
* This method is package private so that this code can be shared.
*
* @return the {@code Class} object of the specified message factory
*
* @param factoryClass factory abstract class or interface to be found
* @exception RuntimeException if there is an error
*/
static <T> T find(Class<T> factoryClass) throws RuntimeException {

String factoryId = factoryClass.getName();

// Use the system property first
String className = fromSystemProperty(factoryId);
if (className != null) {
T result = newInstance(className);
if (result != null) {
return result;
}
}

// standard services: java.util.ServiceLoader
T factory = factoryFromServiceLoader(factoryClass);
if (factory != null) {
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");
}

private static <T> T newInstance(String className) throws RuntimeException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
checkPackageAccess(className);
Class<T> clazz = null;
try {
if (classLoader == null) {
clazz = (Class<T>) Class.forName(className);
} else {
clazz = (Class<T>) classLoader.loadClass(className);
}
return clazz.getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Cannot instance " + className, e);
}
}

private static String fromSystemProperty(String factoryId) {
String systemProp = getSystemProperty(factoryId);
return systemProp;
}

private static String getSystemProperty(final String property) {
String value = AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.getProperty(property);
}
});
return value;
}

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);
Iterator<T> iter = sl.iterator();
if (iter.hasNext()) {
return iter.next();
} else {
return null;
}
} catch (Throwable t) {
// For example, ServiceConfigurationError can be thrown if the factory class is not declared with 'uses' in module-info
throw new IllegalStateException("Cannot load " + factory + " as ServiceLoader", t);
}
}

private static void checkPackageAccess(String className) {
// make sure that the current thread has an access to the package of the given name.
SecurityManager s = System.getSecurityManager();
if (s != null) {
int i = className.lastIndexOf('.');
if (i != -1) {
s.checkPackageAccess(className.substring(0, i));
}
}
}
}

8 changes: 1 addition & 7 deletions api/src/main/java/jakarta/mail/util/StreamProvider.java
Expand Up @@ -174,12 +174,6 @@ public String getEncoder() {
* @return a stream provider
*/
public static StreamProvider provider() {
ServiceLoader<StreamProvider> sl = ServiceLoader.load(StreamProvider.class);
Iterator<StreamProvider> iter = sl.iterator();
if (iter.hasNext()) {
return iter.next();
} else {
throw new IllegalStateException("Not provider of " + StreamProvider.class.getName() + " was found");
}
return FactoryFinder.find(StreamProvider.class);
}
}
2 changes: 2 additions & 0 deletions api/src/main/java/module-info.java
Expand Up @@ -27,4 +27,6 @@

uses jakarta.mail.Provider;
uses jakarta.mail.util.StreamProvider;
//reflective call to java.beans.Beans.instantiate
requires static java.desktop;
}
62 changes: 62 additions & 0 deletions api/src/test/java/jakarta/mail/util/FactoryFinderTest.java
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package jakarta.mail.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.util.ServiceConfigurationError;

import org.junit.Test;

public class FactoryFinderTest {

@Test
public void specifiedInSystemProperty() {
System.setProperty(Class1.class.getName(), Class2.class.getName());
Class1 impl = FactoryFinder.find(Class1.class);
assertEquals(Class2.class, impl.getClass());
}

@Test
public void specifiedInServiceLoader() {
StreamProvider impl = FactoryFinder.find(StreamProvider.class);
assertEquals(DummyStreamProvider.class, impl.getClass());
}

@Test
public void doesNotExist() {
try {
FactoryFinder.find(Class2.class);
fail("IllegalStateException is expected");
} catch (IllegalStateException e) {
assertNull(e.getCause());
}
try {
FactoryFinder.find(Class3.class);
fail("IllegalStateException is expected");
} catch (IllegalStateException e) {
// java.util.ServiceConfigurationError: jakarta.mail.util.FactoryFinderTest$Class3: module jakarta.mail does not declare `uses`
assertEquals(ServiceConfigurationError.class, e.getCause().getClass());
}
}

public static class Class1 {}
public static class Class2 extends Class1 {}
public static class Class3 {}
}
1 change: 1 addition & 0 deletions api/src/test/java/module-info.java
Expand Up @@ -28,5 +28,6 @@

uses jakarta.mail.Provider;
uses jakarta.mail.util.StreamProvider;
uses jakarta.mail.util.FactoryFinderTest.Class2;
provides jakarta.mail.util.StreamProvider with jakarta.mail.util.DummyStreamProvider;
}

0 comments on commit 29939fe

Please sign in to comment.