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

Fix for #543 #544

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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package fr.opensagres.xdocreport.core.internal;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -37,13 +39,13 @@
* <li><b>java.util.ServiceLoader</b> if XDocReport works on Java6. For example :
* <p>
* <code>Iterator<Discovery> discoveries =
ServiceLoader.load( registryType, getClass().getClassLoader() ).iterator();</code>
ServiceLoader.load( registryType, this ).iterator();</code>
* </p>
* </li>
* <li><b>javax.imageio.spi.ServiceRegistry</b> if XDocReport works on Java5. For example :
* <p>
* <code>Iterator<Discovery> discoveries =
ServiceRegistry.lookupProviders( registryType, getClass().getClassLoader() );</code>
ServiceRegistry.lookupProviders( registryType, this );</code>
* </p>
* </li>
* </ul>
Expand Down Expand Up @@ -90,11 +92,17 @@ public abstract class JDKServiceLoader
}
}

public static <T> Iterator<T> lookupProviders( Class<T> providerClass, ClassLoader loader )
public static <T> Iterator<T> lookupProviders( Class<T> providerClass, Object classLoaderObject )
{
try
{
return JDK_SERVICE_LOADER.lookupProvidersFromJDK( providerClass, loader );
ClassLoader[] classLoaders = lookupClassLoader( classLoaderObject );
Iterator<T> providers = JDK_SERVICE_LOADER.lookupProvidersFromJDK( providerClass, classLoaders[0] );
if ( classLoaders.length > 1 )
{
providers = new IteratorPair<>(providers, JDK_SERVICE_LOADER.lookupProvidersFromJDK( providerClass, classLoaders[1] ) );
}
return providers;
}
catch ( Exception e )
{
Expand All @@ -105,6 +113,41 @@ public static <T> Iterator<T> lookupProviders( Class<T> providerClass, ClassLoad
protected abstract <T> Iterator<T> lookupProvidersFromJDK( Class<T> providerClass, ClassLoader loader )
throws Exception;

private static ClassLoader[] lookupClassLoader( Object classLoaderObject ) {
if ( System.getSecurityManager() != null )
{
return AccessController.doPrivileged( new LookupClassLoaderAction( classLoaderObject ) );
}
else
{
return lookupClassLoaderInternal( classLoaderObject );
}
}

private static ClassLoader[] lookupClassLoaderInternal( Object classLoaderObject )
{
// Use thread context class loader by default
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader classClassLoader;
// If not found, use specified object class loader
if ( classLoaderObject != null )
{
classClassLoader = classLoaderObject.getClass().getClassLoader();
}
else
{
classClassLoader = JDKServiceLoader.class.getClassLoader();
}
if ( contextClassLoader == null || contextClassLoader.equals( classClassLoader ) )
{
return new ClassLoader[] { classClassLoader };
}
else
{
return new ClassLoader[] { contextClassLoader, classClassLoader };
}
}

private static class JDK5ServiceLoader
extends JDKServiceLoader
{
Expand Down Expand Up @@ -151,4 +194,72 @@ protected <T> Iterator<T> lookupProvidersFromJDK( Class<T> providerClass, ClassL

}
}

private static class LookupClassLoaderAction
implements PrivilegedAction<ClassLoader[]>
{
private Object classLoaderObject;

LookupClassLoaderAction( Object object )
{
classLoaderObject = object;
}

@Override
public ClassLoader[] run()
{
try
{
return lookupClassLoaderInternal(classLoaderObject);
}
catch ( Exception e )
{
return null;
}
}
}

private static class IteratorPair<T>
implements Iterator<T>
{
private Iterator<T> iterator;
private Iterator<T> secondIterator;

IteratorPair( Iterator<T> firstIterator, Iterator<T> secondIterator )
{
iterator = firstIterator;
this.secondIterator = secondIterator;
}

@Override
public boolean hasNext()
{
boolean result = iterator.hasNext();
if ( !result && secondIterator != null )
{
iterator = secondIterator;
secondIterator = null;
result = iterator.hasNext();
}
return result;
}

@Override
public T next()
{
if ( secondIterator != null && !iterator.hasNext() )
{
iterator = secondIterator;
secondIterator = null;
}
return iterator.next();
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protected void initializeIfNeeded()
onStartInitialization();
// getClass().getClassLoader() to work under OSGi context

Iterator<Discovery> discoveries =
JDKServiceLoader.lookupProviders( registryType, getClass().getClassLoader() );
Iterator<Discovery> discoveries = JDKServiceLoader.lookupProviders( registryType, this );
if ( LOGGER.isLoggable( Level.FINE ) )
{
LOGGER.fine( "discoveries found ? " + discoveries.hasNext() );
Expand Down