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

Updated FactoryFinder for graal vm native images #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -67,7 +69,7 @@ public interface ObjectFactory {

private static ObjectFactory objectFactory = new StandaloneObjectFactory();

private final ConcurrentHashMap<String, T> cachedFactories = new ConcurrentHashMap<String, T>();
public final ConcurrentHashMap<String, T> cachedFactories = new ConcurrentHashMap<String, T>();
private final String path;
private final Class<T> factoryType;

Expand Down Expand Up @@ -123,7 +125,7 @@ public static void setObjectFactory(ObjectFactory objectFactory) {
* @throws NoSuchMethodException if the factory class found does not have a suitable constructor
* @throws SecurityException if a security error occurs trying to create the factory instance.
*/
public T newInstance(String key) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, ClassCastException, ResourceNotFoundException, InvocationTargetException, NoSuchMethodException, SecurityException {
public T newInstance(String key) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, ClassCastException, FileNotFoundException, ResourceNotFoundException, InvocationTargetException, NoSuchMethodException, SecurityException {
T factory = cachedFactories.get(key);
if (factory == null) {
Object found = objectFactory.create(path + key);
Expand Down Expand Up @@ -163,7 +165,8 @@ protected static class StandaloneObjectFactory implements ObjectFactory {
final ConcurrentHashMap<String, Properties> propertiesMap = new ConcurrentHashMap<String, Properties>();

@Override
public Object create(final String path) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, ResourceNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
public Object create(final String path) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException, FileNotFoundException, ResourceNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

Class<?> clazz = classMap.get(path);
Properties properties = propertiesMap.get(path);

Expand Down Expand Up @@ -216,14 +219,36 @@ static public Class<?> loadClass(Properties properties) throws ClassNotFoundExce

return clazz;
}

static public Properties loadProperties(String uri) throws IOException, ResourceNotFoundException {


/**
* Change Log : If QPID client is being run from a GRAALVM native image then its unable to the classloader and pick up resources from within /META-INF
* Added system variable to indicate that if the execution is for a native image build [ NATIVE_IMAGE]
* Poll on NATIVE_IMAGE and if true pull the location of the schema files META-INF/services/ location.
*
**/
static public Properties loadProperties(String uri) throws IOException, ResourceNotFoundException, FileNotFoundException {

//determine if native image is running
String nativeImageVar = System.getProperty("NATIVE_IMAGE");

boolean nativeImageInd = ("Y".equals(nativeImageVar));

// lets try the thread context class loader first
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = StandaloneObjectFactory.class.getClassLoader();
}
InputStream in = classLoader.getResourceAsStream(uri);

InputStream in = null;
//if native image is running them pull resources from FileInputSteam from -DQPID_RESOURCE_DIRECTORY variable.
if(nativeImageInd) {
String qpidResourceDirectory = System.getProperty("QPID_RESOURCE_DIRECTORY");
String modifiedUri = qpidResourceDirectory + "/" + uri;
in = new FileInputStream(modifiedUri);
} else {
in = classLoader.getResourceAsStream(uri);
}
if (in == null) {
in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
if (in == null) {
Expand Down