Author: epbernard
Date: 2009-07-25 01:24:55 -0400 (Sat, 25 Jul 2009)
New Revision: 17201
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java
jpa-api/trunk/src/main/java/javax/persistence/Persistence.java
jpa-api/trunk/src/main/java/javax/persistence/spi/PersistenceProviderResolverHolder.java
Log:
EJB-454 implements the default PPR strategy
EJB-453 use resolver held by PPRH
fix issues with Hibernate's isLoaded
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java 2009-07-24
19:05:04 UTC (rev 17200)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java 2009-07-25
05:24:55 UTC (rev 17201)
@@ -170,13 +170,21 @@
final boolean isInitialized = interceptor == null || interceptor.isInitialized(
property );
LoadState state;
if (isInitialized && interceptor != null) {
- state = LoadState.LOADED;
+ //property is loaded according to bytecode enhancement, but is it loaded as far as
association?
+ //it's ours, we can read
+ state = isLoaded( get( entity, property ) );
+ //it's ours so we know it's loaded
+ if (state == LoadState.UNKNOWN) state = LoadState.LOADED;
}
else if ( interceptor != null && (! isInitialized)) {
state = LoadState.NOT_LOADED;
}
else if ( sureFromUs ) { //interceptor == null
- state = LoadState.LOADED;
+ //property is loaded according to bytecode enhancement, but is it loaded as far as
association?
+ //it's ours, we can read
+ state = isLoaded( get( entity, property ) );
+ //it's ours so we know it's loaded
+ if (state == LoadState.UNKNOWN) state = LoadState.LOADED;
}
else {
state = LoadState.UNKNOWN;
Modified: jpa-api/trunk/src/main/java/javax/persistence/Persistence.java
===================================================================
--- jpa-api/trunk/src/main/java/javax/persistence/Persistence.java 2009-07-24 19:05:04 UTC
(rev 17200)
+++ jpa-api/trunk/src/main/java/javax/persistence/Persistence.java 2009-07-25 05:24:55 UTC
(rev 17201)
@@ -2,18 +2,13 @@
// EJB3 Specification Copyright 2004-2009 Sun Microsystems, Inc.
package javax.persistence;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.Enumeration;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import javax.persistence.spi.LoadState;
import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceProviderResolverHolder;
/**
* Bootstrap class that provides access to an EntityManagerFactory.
@@ -46,10 +41,7 @@
*/
public static EntityManagerFactory createEntityManagerFactory(String
persistenceUnitName, Map properties) {
EntityManagerFactory emf = null;
-
- if ( providers.size() == 0 ) {
- findAllProviders();
- }
+ List<PersistenceProvider> providers = getProviders();
for ( PersistenceProvider provider : providers ) {
emf = provider.createEntityManagerFactory( persistenceUnitName, properties );
if ( emf != null ) {
@@ -62,72 +54,45 @@
return emf;
}
+ private static List<PersistenceProvider> getProviders() {
+ return PersistenceProviderResolverHolder
+ .getPersistenceProviderResolver()
+ .getPersistenceProviders();
+ }
+
/**
* @return Returns a <code>PersistenceUtil</code> instance.
*/
public static PersistenceUtil getPersistenceUtil() {
- // return a dummy implementation of PersistenceUtil. Introduced for Hibernate Validator
(HV-104).
- // Needs to be changed once we start updating EntityManager and introduce JPA2
- return new PersistenceUtil() {
+ return util;
+ }
+ private static PersistenceUtil util =
+ //TODO add an Hibernate specific optimization
+ new PersistenceUtil() {
public boolean isLoaded(Object entity, String attributeName) {
+ List<PersistenceProvider> providers = Persistence.getProviders();
+ for ( PersistenceProvider provider : providers ) {
+ final LoadState state = provider.isLoadedWithoutReference( entity, attributeName );
+ if ( state == LoadState.UNKNOWN ) continue;
+ return state == LoadState.LOADED;
+ }
+ for ( PersistenceProvider provider : providers ) {
+ final LoadState state = provider.isLoadedWithReference( entity, attributeName );
+ if ( state == LoadState.UNKNOWN ) continue;
+ return state == LoadState.LOADED;
+ }
return true;
}
public boolean isLoaded(Object object) {
+ List<PersistenceProvider> providers = Persistence.getProviders();
+ for ( PersistenceProvider provider : providers ) {
+ final LoadState state = provider.isLoaded( object );
+ if ( state == LoadState.UNKNOWN ) continue;
+ return state == LoadState.LOADED;
+ }
return true;
}
};
- }
-
-
- // Helper methods
- private static void findAllProviders() {
- try {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- Enumeration<URL> resources = loader.getResources( "META-INF/services/"
+ PersistenceProvider.class.getName() );
- Set<String> names = new HashSet<String>();
- while ( resources.hasMoreElements() ) {
- URL url = resources.nextElement();
- InputStream is = url.openStream();
- try {
- names.addAll( providerNamesFromReader( new BufferedReader( new InputStreamReader( is
) ) ) );
- }
- finally {
- is.close();
- }
- }
- for ( String s : names ) {
- Class providerClass = loader.loadClass( s );
- providers.add( ( PersistenceProvider ) providerClass.newInstance() );
- }
- }
- catch ( IOException e ) {
- throw new PersistenceException( e );
- }
- catch ( InstantiationException e ) {
- throw new PersistenceException( e );
- }
- catch ( IllegalAccessException e ) {
- throw new PersistenceException( e );
- }
- catch ( ClassNotFoundException e ) {
- throw new PersistenceException( e );
- }
- }
-
- private static final Pattern nonCommentPattern = Pattern.compile( "^([^#]+)"
);
-
- private static Set<String> providerNamesFromReader(BufferedReader reader) throws
IOException {
- Set<String> names = new HashSet<String>();
- String line;
- while ( ( line = reader.readLine() ) != null ) {
- line = line.trim();
- Matcher m = nonCommentPattern.matcher( line );
- if ( m.find() ) {
- names.add( m.group().trim() );
- }
- }
- return names;
- }
}
Modified:
jpa-api/trunk/src/main/java/javax/persistence/spi/PersistenceProviderResolverHolder.java
===================================================================
---
jpa-api/trunk/src/main/java/javax/persistence/spi/PersistenceProviderResolverHolder.java 2009-07-24
19:05:04 UTC (rev 17200)
+++
jpa-api/trunk/src/main/java/javax/persistence/spi/PersistenceProviderResolverHolder.java 2009-07-25
05:24:55 UTC (rev 17201)
@@ -14,6 +14,9 @@
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.PersistenceException;
@@ -88,7 +91,6 @@
private final List<WeakReference<Class<? extends
PersistenceProvider>>> resolverClasses;
public CachingPersistenceProviderResolver(ClassLoader cl) {
- //creates a hard reference to the cl :(
resolverClasses = new ArrayList<WeakReference<Class<? extends
PersistenceProvider>>>();
try {
Enumeration<URL> resources = cl.getResources( "META-INF/services/" +
PersistenceProvider.class.getName() );