Improvement of the AggregatedClassLoader implementation to use one iterator for each type of TcclLookupPrecedence

This commit is contained in:
Cédric Tabin 2016-11-23 23:43:17 +01:00 committed by Andrea Boriero
parent 346941dcb7
commit 6cae4adabb
1 changed files with 106 additions and 40 deletions

View File

@ -149,51 +149,37 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
}
private Iterator<ClassLoader> newClassLoaderIterator() {
final ClassLoader sysClassLoader = locateSystemClassLoader();
final ClassLoader threadClassLoader = locateTCCL();
final TcclLookupPrecedence behavior;
if ( threadClassLoader == null ) {
behavior = TcclLookupPrecedence.NEVER;
if ( tcclLookupPrecedence == TcclLookupPrecedence.NEVER || threadClassLoader == null ) {
return newTcclNeverIterator();
}
else if ( tcclLookupPrecedence == TcclLookupPrecedence.AFTER ) {
return newTcclAfterIterator(threadClassLoader);
}
else if ( tcclLookupPrecedence == TcclLookupPrecedence.BEFORE ) {
return newTcclBeforeIterator(threadClassLoader);
}
else {
behavior = tcclLookupPrecedence;
throw new RuntimeException( "Unknown precedence: "+tcclLookupPrecedence );
}
}
private Iterator<ClassLoader> newTcclBeforeIterator(final ClassLoader threadContextClassLoader) {
final ClassLoader systemClassLoader = locateSystemClassLoader();
return new Iterator<ClassLoader>() {
private boolean tcclReturned = false;
private boolean sysclReturned = false;
private int currentIndex = 0;
private ClassLoader nextClassLoader;
private boolean tcCLReturned = false;
private boolean sysCLReturned = false;
@Override
public boolean hasNext() {
if ( nextClassLoader != null ) {
if ( !tcCLReturned ) {
return true;
}
if ( currentIndex == 0 && behavior == TcclLookupPrecedence.BEFORE && !tcclReturned ) {
tcclReturned = true;
nextClassLoader = threadClassLoader;
else if ( currentIndex < individualClassLoaders.length ) {
return true;
}
if ( currentIndex < individualClassLoaders.length ) {
nextClassLoader = individualClassLoaders[ currentIndex ];
++currentIndex;
return true;
}
if ( behavior == TcclLookupPrecedence.AFTER && !tcclReturned ) {
tcclReturned = true;
nextClassLoader = threadClassLoader;
return true;
}
if ( !sysclReturned ) {
sysclReturned = true;
nextClassLoader = sysClassLoader;
else if ( !sysCLReturned && systemClassLoader != null ) {
return true;
}
@ -202,13 +188,93 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
@Override
public ClassLoader next() {
if ( nextClassLoader == null ) {
throw new IllegalStateException( "No more ClassLoader to return" );
if ( !tcCLReturned ) {
tcCLReturned = true;
return threadContextClassLoader;
}
else if ( currentIndex < individualClassLoaders.length ) {
currentIndex += 1;
return individualClassLoaders[ currentIndex - 1 ];
}
else if ( !sysCLReturned && systemClassLoader != null ) {
sysCLReturned = true;
return systemClassLoader;
}
throw new IllegalStateException( "No more item" );
}
};
}
ClassLoader result = nextClassLoader;
nextClassLoader = null;
return result;
private Iterator<ClassLoader> newTcclAfterIterator(final ClassLoader threadContextClassLoader) {
final ClassLoader systemClassLoader = locateSystemClassLoader();
return new Iterator<ClassLoader>() {
private int currentIndex = 0;
private boolean tcCLReturned = false;
private boolean sysCLReturned = false;
@Override
public boolean hasNext() {
if ( currentIndex < individualClassLoaders.length ) {
return true;
}
else if ( !tcCLReturned ) {
return true;
}
else if ( !sysCLReturned && systemClassLoader != null ) {
return true;
}
return false;
}
@Override
public ClassLoader next() {
if ( currentIndex < individualClassLoaders.length ) {
currentIndex += 1;
return individualClassLoaders[ currentIndex - 1 ];
}
else if ( !tcCLReturned ) {
tcCLReturned = true;
return threadContextClassLoader;
}
else if ( !sysCLReturned && systemClassLoader != null ) {
sysCLReturned = true;
return systemClassLoader;
}
throw new IllegalStateException( "No more item" );
}
};
}
private Iterator<ClassLoader> newTcclNeverIterator() {
final ClassLoader systemClassLoader = locateSystemClassLoader();
return new Iterator<ClassLoader>() {
private int currentIndex = 0;
private boolean sysCLReturned = false;
@Override
public boolean hasNext() {
if ( currentIndex < individualClassLoaders.length ) {
return true;
}
else if ( !sysCLReturned && systemClassLoader != null ) {
return true;
}
return false;
}
@Override
public ClassLoader next() {
if ( currentIndex < individualClassLoaders.length ) {
currentIndex += 1;
return individualClassLoaders[ currentIndex - 1 ];
}
else if ( !sysCLReturned && systemClassLoader != null ) {
sysCLReturned = true;
return systemClassLoader;
}
throw new IllegalStateException( "No more item" );
}
};
}