Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Classloading improvements + log fixes #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 23 additions & 11 deletions jaxb-api/src/main/java/javax/xml/bind/ContextFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ public Object run() throws Exception {
/**
* Create an instance of a class using the thread context ClassLoader
*/
static JAXBContext newInstance(Class[] classes, Map properties, String className) throws JAXBException {
static JAXBContext newInstance(Class[] classes, Map properties, String className, ClassLoader loader) throws JAXBException {

Class spi;
try {
spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, getContextClassLoader());
spi = ServiceLoaderUtil.safeLoadClass(className, PLATFORM_DEFAULT_FACTORY_CLASS, loader);
} catch (ClassNotFoundException e) {
throw new JAXBException(Messages.format(Messages.DEFAULT_PROVIDER_NOT_FOUND), e);
}
Expand Down Expand Up @@ -341,7 +341,7 @@ static JAXBContext find(String factoryId,
if (factoryName != null) return newInstance(contextPath, contextPathClasses, factoryName, classLoader, properties);

JAXBContextFactory obj = ServiceLoaderUtil.firstByServiceLoader(
JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
JAXBContextFactory.class, classLoader, logger, EXCEPTION_HANDLER);

if (obj != null) {
ModuleUtil.delegateAddOpensToImplModule(contextPathClasses, obj.getClass());
Expand Down Expand Up @@ -388,25 +388,33 @@ static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JA
jaxbPropertiesUrl,
JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_DEPRECATED);

return newInstance(classes, properties, factoryClassName);
return newInstance(classes, properties, factoryClassName, getContextClassLoader());
}

}

String factoryClassName = classNameFromSystemProperties();
if (factoryClassName != null) return newInstance(classes, properties, factoryClassName);
if (factoryClassName != null) return newInstance(classes, properties, factoryClassName, getContextClassLoader());

JAXBContextFactory factory =
ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, logger, EXCEPTION_HANDLER);
ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, getContextClassLoader(), logger, EXCEPTION_HANDLER);
if (factory == null) {
factory = ServiceLoaderUtil.firstByServiceLoader(JAXBContextFactory.class, ContextFinder.class.getClassLoader(), logger, EXCEPTION_HANDLER);
}

if (factory != null) {
ModuleUtil.delegateAddOpensToImplModule(classes, factory.getClass());
return factory.createContext(classes, properties);
}

// to ensure backwards compatibility
String className = firstByServiceLoaderDeprecated(JAXBContext.class, getContextClassLoader());
if (className != null) return newInstance(classes, properties, className);
ClassLoader cl = getContextClassLoader();
String className = firstByServiceLoaderDeprecated(JAXBContext.class, cl);
if (className == null) {
cl = ContextFinder.class.getClassLoader();
className = firstByServiceLoaderDeprecated(JAXBContext.class, cl);
}
if (className != null) return newInstance(classes, properties, className, cl);

logger.fine("Trying to create the platform default provider");
Class ctxFactoryClass =
Expand All @@ -418,7 +426,7 @@ static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JA

// else no provider found
logger.fine("Trying to create the platform default provider");
return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS);
return newInstance(classes, properties, PLATFORM_DEFAULT_FACTORY_CLASS, null);
}


Expand All @@ -429,7 +437,9 @@ static JAXBContext find(Class<?>[] classes, Map<String, ?> properties) throws JA
private static String classNameFromPackageProperties(URL packagePropertiesUrl,
String ... factoryIds) throws JAXBException {

logger.log(Level.FINE, "Trying to locate {0}", packagePropertiesUrl.toString());
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Trying to locate {0}", packagePropertiesUrl.toString());
}
Properties props = loadJAXBProperties(packagePropertiesUrl);
for(String factoryId : factoryIds) {
if (props.containsKey(factoryId)) {
Expand Down Expand Up @@ -492,7 +502,9 @@ private static Properties loadJAXBProperties(URL url) throws JAXBException {
is.close();
return props;
} catch (IOException ioe) {
logger.log(Level.FINE, "Unable to load " + url.toString(), ioe);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Unable to load " + url.toString(), ioe);
}
throw new JAXBException(ioe.toString(), ioe);
}
}
Expand Down
20 changes: 14 additions & 6 deletions jaxb-api/src/main/java/javax/xml/bind/ServiceLoaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ class ServiceLoaderUtil {
private static final String OSGI_SERVICE_LOADER_METHOD_NAME = "lookupProviderClasses";

static <P, T extends Exception> P firstByServiceLoader(Class<P> spiClass,
ClassLoader loader,
Logger logger,
ExceptionHandler<T> handler) throws T {
// service discovery
try {
ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
ServiceLoader<P> serviceLoader = loader != null ? ServiceLoader.load(spiClass, loader) : ServiceLoader.load(spiClass);

for (P impl : serviceLoader) {
logger.fine("ServiceProvider loading Facility used; returning object [" +
impl.getClass().getName() + "]");
if (logger.isLoggable(Level.FINE))
{
logger.fine(
"ServiceProvider loading Facility used; returning object [" + impl.getClass().getName() + "]");
}

return impl;
}
Expand All @@ -88,8 +92,10 @@ static Object lookupUsingOSGiServiceLoader(String factoryId, Logger logger) {
Iterator iter = ((Iterable) m.invoke(null, serviceClass)).iterator();
if (iter.hasNext()) {
Object next = iter.next();
logger.fine("Found implementation using OSGi facility; returning object [" +
next.getClass().getName() + "].");
if (logger.isLoggable(Level.FINE)) {
logger.fine("Found implementation using OSGi facility; returning object [" +
next.getClass().getName() + "].");
}
return next;
} else {
return null;
Expand All @@ -99,7 +105,9 @@ static Object lookupUsingOSGiServiceLoader(String factoryId, Logger logger) {
ClassNotFoundException |
NoSuchMethodException ignored) {

logger.log(Level.FINE, "Unable to find from OSGi: [" + factoryId + "]", ignored);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Unable to find from OSGi: [" + factoryId + "]", ignored);
}
return null;
}
}
Expand Down