@ Override public List < PersistenceProvider > getPersistenceProviders ( ) { // try to get the context classloader first, if that fails, use the loader // that loaded this class ClassLoader cl = PrivClassLoader . get ( null ) ; if ( cl == null ) { cl = PrivClassLoader . get ( HybridPersistenceActivator . class ) ; } // Query the provider cache per-classloader List < PersistenceProvider > nonOSGiProviders = providerCache . get ( cl ) ; // Get all providers not registered in OSGi. These will be any third-party providers // available to the application classloader. if ( nonOSGiProviders == null ) { nonOSGiProviders = new ArrayList < PersistenceProvider > ( ) ; try { List < Object > providers = ProviderLocator . getServices ( PersistenceProvider . class . getName ( ) , getClass ( ) , cl ) ; for ( Iterator < Object > provider = providers . iterator ( ) ; provider . hasNext ( ) ; ) { Object o = provider . next ( ) ; if ( o instanceof PersistenceProvider ) { nonOSGiProviders . add ( ( PersistenceProvider ) o ) ; } } // load the providers into the provider cache for the context (or current) classloader providerCache . put ( cl , nonOSGiProviders ) ; } catch ( Exception e ) { throw new PersistenceException ( "Failed to load provider from META-INF/services" , e ) ; } } List < PersistenceProvider > combinedProviders = new ArrayList < PersistenceProvider > ( nonOSGiProviders ) ; combinedProviders . addAll ( super . getPersistenceProviders ( ) ) ; return combinedProviders ; }
This method returns a combination of those persistence providers available from the application classloader in addition to those in the OSGi service registry . OSGi providers are not cached and should not be cached because bundles can be moved in and out of the system and it is the job of the the service tracker to maintain them .