public AggregatedClassLoader run() { return new AggregatedClassLoader( orderedClassLoaderSet, lookupPrecedence ); } } );
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" ); } }; }
private Iterator<ClassLoader> newClassLoaderIterator() { final ClassLoader threadClassLoader = locateTCCL(); 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 { throw new RuntimeException( "Unknown precedence: "+tcclLookupPrecedence ); } }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); try { return classLoader.loadClass( name ); } catch (Exception ignore) { } catch (LinkageError ignore) { } } throw new ClassNotFoundException( "Could not load requested class : " + name ); }
@Override protected URL findResource(String name) { final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); final URL resource = classLoader.getResource( name ); if ( resource != null ) { return resource; } } return super.findResource( name ); }
private Iterator<ClassLoader> newClassLoaderIterator() { final ClassLoader threadClassLoader = locateTCCL(); 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 { throw new RuntimeException( "Unknown precedence: "+tcclLookupPrecedence ); } }
@Override public Object getService(Bundle requestingBundle, ServiceRegistration registration) { final OsgiClassLoader osgiClassLoader = new OsgiClassLoader(); // First, add the client bundle that's requesting the OSGi services. osgiClassLoader.addBundle( requestingBundle ); // Then, automatically add hibernate-core. These are needed to load resources // contained in the core jar. osgiClassLoader.addBundle( FrameworkUtil.getBundle(SessionFactory.class) ); // Some "boot time" code does still rely on TCCL. "run time" code should all be using // ClassLoaderService now. final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader(); LinkedHashSet<ClassLoader> newTcclDelegates = new LinkedHashSet<>(); newTcclDelegates.add( osgiClassLoader ); newTcclDelegates.add( originalTccl ); final ClassLoader newTccl = new AggregatedClassLoader( newTcclDelegates, TcclLookupPrecedence.NEVER ); Thread.currentThread().setContextClassLoader( newTccl ); try { return buildSessionFactory( requestingBundle, osgiClassLoader ); } finally { Thread.currentThread().setContextClassLoader( originalTccl ); } }
@Override public Enumeration<URL> getResources(String name) throws IOException { final LinkedHashSet<URL> resourceUrls = new LinkedHashSet<URL>(); final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); final Enumeration<URL> urls = classLoader.getResources( name ); while ( urls.hasMoreElements() ) { resourceUrls.add( urls.nextElement() ); } } return new Enumeration<URL>() { final Iterator<URL> resourceUrlIterator = resourceUrls.iterator(); @Override public boolean hasMoreElements() { return resourceUrlIterator.hasNext(); } @Override public URL nextElement() { return resourceUrlIterator.next(); } }; }
private Iterator<ClassLoader> newTcclAfterIterator(final ClassLoader threadContextClassLoader) { final ClassLoader systemClassLoader = locateSystemClassLoader(); return new Iterator<ClassLoader>() { private int currentIndex = 0;
@Override public Object getService(Bundle requestingBundle, ServiceRegistration registration) { final OsgiClassLoader osgiClassLoader = new OsgiClassLoader(); // First, add the client bundle that's requesting the OSGi services. osgiClassLoader.addBundle( requestingBundle ); // Then, automatically add hibernate-core and hibernate-entitymanager. These are needed to load resources // contained in those jars, such as em's persistence.xml schemas. osgiClassLoader.addBundle( FrameworkUtil.getBundle( SessionFactory.class ) ); osgiClassLoader.addBundle( FrameworkUtil.getBundle( HibernateEntityManagerFactory.class ) ); // Some "boot time" code does still rely on TCCL. "run time" code should all be using // ClassLoaderService now. final ClassLoader originalTccl = Thread.currentThread().getContextClassLoader(); LinkedHashSet<ClassLoader> newTcclDelegates = new LinkedHashSet<>(); newTcclDelegates.add( osgiClassLoader ); newTcclDelegates.add( originalTccl ); final ClassLoader newTccl = new AggregatedClassLoader( newTcclDelegates, TcclLookupPrecedence.NEVER ); Thread.currentThread().setContextClassLoader( newTccl ); try { return new OsgiPersistenceProvider( osgiClassLoader, osgiJtaPlatform, osgiServiceUtil, requestingBundle ); } finally { Thread.currentThread().setContextClassLoader( originalTccl ); } }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); try { return classLoader.loadClass( name ); } catch (Exception ignore) { } catch (LinkageError ignore) { } } throw new ClassNotFoundException( "Could not load requested class : " + name ); }
private Iterator<ClassLoader> newTcclBeforeIterator(final ClassLoader threadContextClassLoader) { final ClassLoader systemClassLoader = locateSystemClassLoader(); return new Iterator<ClassLoader>() { private int currentIndex = 0;
public AggregatedClassLoader run() { return new AggregatedClassLoader( orderedClassLoaderSet, lookupPrecedence ); } } );
@Override protected URL findResource(String name) { final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); final URL resource = classLoader.getResource( name ); if ( resource != null ) { return resource; } } return super.findResource( name ); }
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" ); } }; }
@Override public Enumeration<URL> getResources(String name) throws IOException { final LinkedHashSet<URL> resourceUrls = new LinkedHashSet<URL>(); final Iterator<ClassLoader> clIterator = newClassLoaderIterator(); while ( clIterator.hasNext() ) { final ClassLoader classLoader = clIterator.next(); final Enumeration<URL> urls = classLoader.getResources( name ); while ( urls.hasMoreElements() ) { resourceUrls.add( urls.nextElement() ); } } return new Enumeration<URL>() { final Iterator<URL> resourceUrlIterator = resourceUrls.iterator(); @Override public boolean hasMoreElements() { return resourceUrlIterator.hasNext(); } @Override public URL nextElement() { return resourceUrlIterator.next(); } }; }
private Iterator<ClassLoader> newTcclBeforeIterator(final ClassLoader threadContextClassLoader) { final ClassLoader systemClassLoader = locateSystemClassLoader(); return new Iterator<ClassLoader>() { private int currentIndex = 0;
private Iterator<ClassLoader> newTcclAfterIterator(final ClassLoader threadContextClassLoader) { final ClassLoader systemClassLoader = locateSystemClassLoader(); return new Iterator<ClassLoader>() { private int currentIndex = 0;