[seam-commits] Seam SVN: r8527 - in trunk/src/main/org/jboss/seam: framework and 4 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Jul 28 11:45:00 EDT 2008
Author: pete.muir at jboss.org
Date: 2008-07-28 11:44:59 -0400 (Mon, 28 Jul 2008)
New Revision: 8527
Added:
trunk/src/main/org/jboss/seam/init/EjbEntityDescriptor.java
Removed:
trunk/src/main/org/jboss/seam/persistence/JpaPersistenceProvider.java
Modified:
trunk/src/main/org/jboss/seam/Entity.java
trunk/src/main/org/jboss/seam/Seam.java
trunk/src/main/org/jboss/seam/framework/EntityIdentifier.java
trunk/src/main/org/jboss/seam/persistence/EntityManagerProxy.java
trunk/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java
trunk/src/main/org/jboss/seam/persistence/PersistenceProvider.java
trunk/src/main/org/jboss/seam/security/EntityPermissionChecker.java
trunk/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java
trunk/src/main/org/jboss/seam/util/Reflections.java
Log:
Rollback changes trying to make JPA provider detection at runtime
Modified: trunk/src/main/org/jboss/seam/Entity.java
===================================================================
--- trunk/src/main/org/jboss/seam/Entity.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/Entity.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -1,5 +1,6 @@
package org.jboss.seam;
+import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -12,20 +13,21 @@
import javax.persistence.Version;
import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.init.EjbDescriptor;
+import org.jboss.seam.init.EjbEntityDescriptor;
import org.jboss.seam.util.Reflections;
/**
* Metamodel class for entity classes.
*
- * A class will be identified as an entity class
- * if it has an @Entity annotation.
+ * A class will be identified as an entity class if it has an @Entity annotation.
*
* @author Gavin King
- *
+ *
*/
public class Entity extends Model
{
-
+
private Method preRemoveMethod;
private Method prePersistMethod;
private Method preUpdateMethod;
@@ -36,85 +38,25 @@
private Field versionField;
private String name;
+ /**
+ *
+ * @param beanClass
+ *
+ * Use Entity.forBean() or Entity.forClass
+ */
+ @Deprecated
public Entity(Class<?> beanClass)
{
super(beanClass);
-
- if (beanClass.isAnnotationPresent(javax.persistence.Entity.class))
+ EjbDescriptor descriptor = Seam.getEjbDescriptor(beanClass);
+ if (descriptor instanceof EjbEntityDescriptor)
{
- if (!"".equals(beanClass.getAnnotation(javax.persistence.Entity.class).name()))
- {
- name = beanClass.getAnnotation(javax.persistence.Entity.class).name();
- }
- else
- {
- name = beanClass.getName();
- }
+ mergeAnnotationAndOrmXml((EjbEntityDescriptor) descriptor);
}
-
- for ( Class<?> clazz=beanClass; clazz!=Object.class; clazz = clazz.getSuperclass() )
+ else
{
-
- for ( Method method: clazz.getDeclaredMethods() )
- {
- //TODO: does the spec allow multiple lifecycle method
- // in the entity class heirarchy?
- if ( method.isAnnotationPresent(PreRemove.class) )
- {
- preRemoveMethod = method;
- }
- if ( method.isAnnotationPresent(PrePersist.class) )
- {
- prePersistMethod = method;
- }
- if ( method.isAnnotationPresent(PreUpdate.class) )
- {
- preUpdateMethod = method;
- }
- if ( method.isAnnotationPresent(PostLoad.class) )
- {
- postLoadMethod = method;
- }
- if ( method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class))
- {
- identifierGetter = method;
- }
- if ( method.isAnnotationPresent(Version.class) )
- {
- versionGetter = method;
- }
-
- if ( !method.isAccessible() )
- {
- method.setAccessible(true);
- }
- }
-
- if (identifierGetter==null)
- {
- for ( Field field: clazz.getDeclaredFields() )
- {
- if ( field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class))
- {
- identifierField = field;
- if ( !field.isAccessible() )
- {
- field.setAccessible(true);
- }
- }
- if ( field.isAnnotationPresent(Version.class) )
- {
- versionField = field;
- if ( !field.isAccessible() )
- {
- field.setAccessible(true);
- }
- }
- }
- }
-
+ mergeAnnotationAndOrmXml(null);
}
-
}
public Method getPostLoadMethod()
@@ -137,33 +79,37 @@
return preUpdateMethod;
}
+ @Deprecated
public Field getIdentifierField()
{
return identifierField;
}
+ @Deprecated
public Method getIdentifierGetter()
{
return identifierGetter;
}
-
+
+ @Deprecated
public Field getVersionField()
{
return versionField;
}
+ @Deprecated
public Method getVersionGetter()
{
return versionGetter;
}
-
+
public Object getIdentifier(Object entity)
{
- if (identifierGetter!=null)
+ if (identifierGetter != null)
{
return Reflections.invokeAndWrap(identifierGetter, entity);
}
- else if (identifierField!=null)
+ else if (identifierField != null)
{
return Reflections.getAndWrap(identifierField, entity);
}
@@ -175,11 +121,11 @@
public Object getVersion(Object entity)
{
- if (versionGetter!=null)
+ if (versionGetter != null)
{
return Reflections.invokeAndWrap(versionGetter, entity);
}
- else if (versionField!=null)
+ else if (versionField != null)
{
return Reflections.getAndWrap(versionField, entity);
}
@@ -188,29 +134,33 @@
return null;
}
}
-
+
public String getName()
{
return name;
}
-
+
+ public static Entity forBean(Object bean)
+ {
+ return forClass(bean.getClass());
+ }
+
public static Entity forClass(Class clazz)
{
- if ( !Contexts.isApplicationContextActive() )
+ if (!Contexts.isApplicationContextActive())
{
throw new IllegalStateException("No application context active");
}
-
+
Class entityClass = Seam.getEntityClass(clazz);
- if (entityClass==null)
+ if (entityClass == null)
{
- throw new IllegalArgumentException("Not an entity class: " + clazz.getName());
+ throw new NotEntityException("Not an entity class: " + clazz.getName());
}
-
String name = getModelName(entityClass);
Model model = (Model) Contexts.getApplicationContext().get(name);
- if ( model==null || !(model instanceof Entity) )
+ if (model == null || !(model instanceof Entity))
{
Entity entity = new Entity(entityClass);
Contexts.getApplicationContext().set(name, entity);
@@ -222,4 +172,153 @@
}
}
+ private void mergeAnnotationAndOrmXml(EjbEntityDescriptor descriptor)
+ {
+ // Lookup the name of the Entity from XML, annotation or default
+ this.name = lookupName(getBeanClass(), descriptor);
+ if (this.name == null)
+ {
+ throw new NotEntityException("Unable to establish name of entity " + getBeanClass());
+ }
+
+ if (descriptor != null)
+ {
+ // Set any methods and fields we need metadata for from the XML
+ // descriptor. These take priority over annotations
+
+ this.preRemoveMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPreRemoveMethodName());
+ this.prePersistMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPrePersistMethodName());
+ this.preUpdateMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPreUpdateMethodName());
+ this.postLoadMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPostLoadMethodName());
+
+ this.identifierField = descriptor.getIdentifierFieldName() != null ? Reflections.getField(getBeanClass(), descriptor.getIdentifierFieldName()) : null;
+ this.identifierGetter = descriptor.getIdentifierPropertyName() != null ? Reflections.getGetterMethod(getBeanClass(), descriptor.getIdentifierPropertyName()) : null;
+
+ this.versionField = descriptor.getVersionFieldName() != null ? Reflections.getField(getBeanClass(), descriptor.getVersionFieldName()) : null;
+ this.versionGetter = descriptor.getVersionPropertyName() != null ? Reflections.getGetterMethod(getBeanClass(), descriptor.getVersionPropertyName()) : null;
+ }
+
+ if (descriptor == null || !descriptor.isMetaDataComplete())
+ {
+ for ( Class<?> clazz=getBeanClass(); clazz!=Object.class; clazz = clazz.getSuperclass() )
+ {
+
+ for ( Method method: clazz.getDeclaredMethods() )
+ {
+ //TODO: does the spec allow multiple lifecycle method
+ // in the entity class heirarchy?
+ if (this.preRemoveMethod == null && method.isAnnotationPresent(PreRemove.class))
+ {
+ this.preRemoveMethod = method;
+ }
+ if (this.prePersistMethod == null && method.isAnnotationPresent(PrePersist.class) )
+ {
+ this.prePersistMethod = method;
+ }
+ if (preUpdateMethod == null && method.isAnnotationPresent(PreUpdate.class) )
+ {
+ preUpdateMethod = method;
+ }
+ if (postLoadMethod == null && method.isAnnotationPresent(PostLoad.class) )
+ {
+ postLoadMethod = method;
+ }
+ if (identifierField == null && identifierGetter == null && method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class))
+ {
+ identifierGetter = method;
+ }
+ if (versionField == null && versionGetter == null && method.isAnnotationPresent(Version.class) )
+ {
+ versionGetter = method;
+ }
+ }
+
+ if ( ( identifierGetter == null && identifierField == null ) || ( versionField == null && versionGetter == null ) )
+ {
+ for ( Field field: clazz.getDeclaredFields() )
+ {
+ if ( identifierGetter == null && identifierField == null && (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)))
+ {
+ identifierField = field;
+ }
+ if ( versionGetter == null && versionField == null && field.isAnnotationPresent(Version.class) )
+ {
+ versionField = field;
+ }
+ }
+ }
+ }
+ }
+
+ setAccessible(this.preRemoveMethod);
+ setAccessible(this.prePersistMethod);
+ setAccessible(this.preUpdateMethod);
+ setAccessible(this.postLoadMethod);
+ setAccessible(this.identifierField);
+ setAccessible(this.identifierGetter);
+ setAccessible(this.versionField);
+ setAccessible(this.versionGetter);
+ }
+
+ private void setAccessible(AccessibleObject accessibleObject)
+ {
+ if (accessibleObject != null)
+ {
+ accessibleObject.setAccessible(true);
+ }
+ }
+
+ private static String lookupName(Class<?> beanClass, EjbEntityDescriptor descriptor)
+ {
+ if (descriptor != null && descriptor.getEjbName() != null)
+ {
+ // XML overrides annotations
+ return descriptor.getEjbName();
+ }
+ else if ( (descriptor == null || !descriptor.isMetaDataComplete()) && beanClass.isAnnotationPresent(javax.persistence.Entity.class) && !"".equals(beanClass.getAnnotation(javax.persistence.Entity.class).name()))
+ {
+ // Is a name specified?
+ return beanClass.getAnnotation(javax.persistence.Entity.class).name();
+ }
+ else if (descriptor != null || beanClass.isAnnotationPresent(javax.persistence.Entity.class))
+ {
+ // Use the default name if either a descriptor is specified or the
+ // annotation is present
+ return beanClass.getName();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private static Method getEntityCallbackMethod(Class beanClass, String callbackMethodName)
+ {
+ try
+ {
+ if (callbackMethodName != null)
+ {
+ return Reflections.getMethod(beanClass, callbackMethodName);
+ }
+ else
+ {
+ return null;
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new IllegalArgumentException("Unable to find Entity callback method specified in orm.xml", e);
+ }
+ }
+
+ public static class NotEntityException extends IllegalArgumentException
+ {
+
+ public NotEntityException(String string)
+ {
+ super(string);
+ }
+
+ }
+
}
Modified: trunk/src/main/org/jboss/seam/Seam.java
===================================================================
--- trunk/src/main/org/jboss/seam/Seam.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/Seam.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -21,8 +21,6 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Role;
import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.faces.Converter;
-import org.jboss.seam.annotations.faces.Validator;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.contexts.Lifecycle;
@@ -39,7 +37,6 @@
*/
public class Seam
{
-
private static final Map<Class, String> COMPONENT_NAME_CACHE = new ConcurrentHashMap<Class, String>();
private static final Map<Class, EjbDescriptor> EJB_DESCRIPTOR_CACHE = new ConcurrentHashMap<Class, EjbDescriptor>();
private static final Set<ClassLoader> CLASSLOADERS_LOADED = new HashSet<ClassLoader>();
@@ -68,7 +65,7 @@
EJB_DESCRIPTOR_CACHE.putAll(ejbDescriptors);
CLASSLOADERS_LOADED.add(clazz.getClassLoader());
}
- }
+ }
/**
* Get the default scope
@@ -78,13 +75,6 @@
{
return clazz.isAnnotationPresent(Scope.class) ?
clazz.getAnnotation(Scope.class).value() :
- getDefaultComponentScope(clazz);
- }
-
- public static ScopeType getDefaultComponentScope(Class<?> clazz)
- {
- return ( clazz.isAnnotationPresent(Validator.class) || clazz.isAnnotationPresent(Converter.class) ) ?
- ScopeType.STATELESS :
getComponentType(clazz).getDefaultScope();
}
@@ -166,22 +156,22 @@
/**
* Get the bean class from a container-generated proxy
* class
+ *
*/
- public static Class getEntityClass(Class<?> clazz)
+ public static Class getEntityClass(Class clazz)
{
- while ( clazz!=null && !Object.class.equals(clazz) )
+ while (clazz != null && !Object.class.equals(clazz))
{
- if ( clazz.isAnnotationPresent(Entity.class) )
+ if (clazz.isAnnotationPresent(Entity.class))
{
return clazz;
}
- else
+ else
{
- EjbDescriptor ejbDescriptor = EJB_DESCRIPTOR_CACHE.get(clazz);
- if ( ejbDescriptor!=null )
+ EjbDescriptor ejbDescriptor = Seam.getEjbDescriptor(clazz);
+ if (ejbDescriptor != null)
{
- return ejbDescriptor.getBeanType()==ComponentType.ENTITY_BEAN ?
- clazz : null;
+ return ejbDescriptor.getBeanType() == ComponentType.ENTITY_BEAN ? clazz : null;
}
else
{
@@ -327,7 +317,6 @@
{
COMPONENT_NAME_CACHE.clear();
EJB_DESCRIPTOR_CACHE.clear();
- CLASSLOADERS_LOADED.clear();
}
}
Modified: trunk/src/main/org/jboss/seam/framework/EntityIdentifier.java
===================================================================
--- trunk/src/main/org/jboss/seam/framework/EntityIdentifier.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/framework/EntityIdentifier.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -10,7 +10,7 @@
{
public EntityIdentifier(Object entity, EntityManager entityManager)
{
- super(PersistenceProvider.instance().getBeanClass(entityManager, entity), PersistenceProvider.instance().getId(entity, entityManager));
+ super(PersistenceProvider.instance().getBeanClass(entity), PersistenceProvider.instance().getId(entity, entityManager));
}
Added: trunk/src/main/org/jboss/seam/init/EjbEntityDescriptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/init/EjbEntityDescriptor.java (rev 0)
+++ trunk/src/main/org/jboss/seam/init/EjbEntityDescriptor.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -0,0 +1,138 @@
+package org.jboss.seam.init;
+
+public class EjbEntityDescriptor extends EjbDescriptor
+{
+
+ private boolean metaDataComplete;
+
+ private String preRemoveMethodName;
+ private String prePersistMethodName;
+ private String preUpdateMethodName;
+ private String postLoadMethodName;
+ private String identifierPropertyName;
+ private String identifierFieldName;
+ private String versionPropertyName;
+ private String versionFieldName;
+
+
+ public String getPreRemoveMethodName()
+ {
+ return preRemoveMethodName;
+ }
+
+ public void setPreRemoveMethodName(String preRemoveMethodName)
+ {
+ this.preRemoveMethodName = preRemoveMethodName;
+ }
+
+ public String getPrePersistMethodName()
+ {
+ return prePersistMethodName;
+ }
+
+ public void setPrePersistMethodName(String prePersistMethodName)
+ {
+ this.prePersistMethodName = prePersistMethodName;
+ }
+
+ public String getPreUpdateMethodName()
+ {
+ return preUpdateMethodName;
+ }
+
+ public void setPreUpdateMethodName(String preUpdateMethodName)
+ {
+ this.preUpdateMethodName = preUpdateMethodName;
+ }
+
+ public String getPostLoadMethodName()
+ {
+ return postLoadMethodName;
+ }
+
+ public void setPostLoadMethodName(String postLoadMethodName)
+ {
+ this.postLoadMethodName = postLoadMethodName;
+ }
+
+ public String getIdentifierPropertyName()
+ {
+ return identifierPropertyName;
+ }
+
+ public void setIdentifierPropertyName(String identifierProperty)
+ {
+ this.identifierPropertyName = identifierProperty;
+ }
+
+ public String getVersionPropertyName()
+ {
+ return versionPropertyName;
+ }
+
+ public void setVersionPropertyName(String versionProperty)
+ {
+ this.versionPropertyName = versionProperty;
+ }
+
+ public String getIdentifierFieldName()
+ {
+ return identifierFieldName;
+ }
+
+ public void setIdentifierFieldName(String identifierField)
+ {
+ this.identifierFieldName = identifierField;
+ }
+
+ public String getVersionFieldName()
+ {
+ return versionFieldName;
+ }
+
+ public void setVersionFieldName(String versionField)
+ {
+ this.versionFieldName = versionField;
+ }
+
+ public void setVersionAttribute(String versionAttributeName, String accessType)
+ {
+ if (accessType != null)
+ {
+ if (accessType == "PROPERTY")
+ {
+ this.versionPropertyName = versionAttributeName;
+ }
+ else if (accessType == "FIELD")
+ {
+ this.versionFieldName = versionAttributeName;
+ }
+ }
+ }
+
+ public void setIdentifierAttribute(String identifierAttributeName, String accessType)
+ {
+ if (accessType != null)
+ {
+ if (accessType == "PROPERTY")
+ {
+ this.identifierPropertyName = identifierAttributeName;
+ }
+ else if (accessType == "FIELD")
+ {
+ this.identifierFieldName = identifierAttributeName;
+ }
+ }
+ }
+
+ public boolean isMetaDataComplete()
+ {
+ return metaDataComplete;
+ }
+
+ public void setMetaDataComplete(boolean metaDataComplete)
+ {
+ this.metaDataComplete = metaDataComplete;
+ }
+
+}
Property changes on: trunk/src/main/org/jboss/seam/init/EjbEntityDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/src/main/org/jboss/seam/persistence/EntityManagerProxy.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/EntityManagerProxy.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/persistence/EntityManagerProxy.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -92,7 +92,7 @@
public Object getDelegate()
{
- return PersistenceProvider.instance().proxyDelegate( delegate, delegate.getDelegate() );
+ return PersistenceProvider.instance().proxyDelegate( delegate.getDelegate() );
}
public FlushModeType getFlushMode()
Modified: trunk/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/persistence/HibernatePersistenceProvider.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -1,11 +1,9 @@
package org.jboss.seam.persistence;
-import static org.jboss.seam.ScopeType.STATELESS;
import static org.jboss.seam.annotations.Install.FRAMEWORK;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
-import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
@@ -17,16 +15,12 @@
import org.hibernate.Session;
import org.hibernate.StaleStateException;
import org.hibernate.TransientObjectException;
-import org.hibernate.ejb.event.Callback;
-import org.hibernate.ejb.event.EJB3PostLoadEventListener;
-import org.hibernate.ejb.event.EntityCallbackHandler;
-import org.hibernate.engine.SessionImplementor;
-import org.hibernate.event.PostLoadEventListener;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.VersionType;
import org.jboss.seam.Component;
+import org.jboss.seam.Entity;
import org.jboss.seam.ScopeType;
-import org.jboss.seam.Seam;
+import org.jboss.seam.Entity.NotEntityException;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
@@ -42,11 +36,11 @@
* @author Pete Muir
*
*/
- at Name("org.jboss.seam.persistence.hibernatePersistenceProvider")
+ at Name("org.jboss.seam.persistence.persistenceProvider")
@Scope(ScopeType.STATELESS)
@BypassInterceptors
@Install(precedence=FRAMEWORK, classDependencies={"org.hibernate.Session", "javax.persistence.EntityManager"})
-public class HibernatePersistenceProvider extends AbstractPersistenceProvider
+public class HibernatePersistenceProvider extends PersistenceProvider
{
private static Log log = Logging.getLog(HibernatePersistenceProvider.class);
@@ -123,6 +117,10 @@
{
return proxySession( (Session) delegate );
}
+ catch (NotHibernateException nhe)
+ {
+ return super.proxyDelegate(delegate);
+ }
catch (Exception e)
{
throw new RuntimeException("could not proxy delegate", e);
@@ -132,13 +130,27 @@
@Override
public void setFlushModeManual(EntityManager entityManager)
{
- getSession(entityManager).setFlushMode(FlushMode.MANUAL);
+ try
+ {
+ getSession(entityManager).setFlushMode(FlushMode.MANUAL);
+ }
+ catch (NotHibernateException nhe)
+ {
+ super.setFlushModeManual(entityManager);
+ }
}
@Override
public boolean isDirty(EntityManager entityManager)
{
- return getSession(entityManager).isDirty();
+ try
+ {
+ return getSession(entityManager).isDirty();
+ }
+ catch (NotHibernateException nhe)
+ {
+ return super.isDirty(entityManager);
+ }
}
@Override
@@ -148,6 +160,10 @@
{
return getSession(entityManager).getIdentifier(bean);
}
+ catch (NotHibernateException nhe)
+ {
+ return super.getId(bean, entityManager);
+ }
catch (TransientObjectException e)
{
return super.getId(bean, entityManager);
@@ -157,32 +173,63 @@
@Override
public Object getVersion(Object bean, EntityManager entityManager)
{
- return getVersion( bean, getSession(entityManager) );
+ try
+ {
+ return getVersion( bean, getSession(entityManager) );
+ }
+ catch (NotHibernateException nhe)
+ {
+ return super.getVersion(bean, entityManager);
+ }
}
@Override
public void checkVersion(Object bean, EntityManager entityManager, Object oldVersion, Object version)
{
- checkVersion(bean, getSession(entityManager), oldVersion, version);
+ try
+ {
+ checkVersion(bean, getSession(entityManager), oldVersion, version);
+ }
+ catch (NotHibernateException nhe)
+ {
+ super.checkVersion(bean, entityManager, oldVersion, version);
+ }
}
@Override
public void enableFilter(Filter f, EntityManager entityManager)
{
- org.hibernate.Filter filter = getSession(entityManager).enableFilter( f.getName() );
- for ( Map.Entry<String, ValueExpression> me: f.getParameters().entrySet() )
+ try
{
- filter.setParameter( me.getKey(), me.getValue().getValue() );
+ org.hibernate.Filter filter = getSession(entityManager).enableFilter( f.getName() );
+ for ( Map.Entry<String, ValueExpression> me: f.getParameters().entrySet() )
+ {
+ filter.setParameter( me.getKey(), me.getValue().getValue() );
+ }
+ filter.validate();
}
+ catch (NotHibernateException nhe)
+ {
+ super.enableFilter(f, entityManager);
+ }
+
}
@Override
public boolean registerSynchronization(Synchronization sync, EntityManager entityManager)
{
- //TODO: just make sure that a Hibernate JPA EntityTransaction
- // delegates to the Hibernate Session transaction
- getSession(entityManager).getTransaction().registerSynchronization(sync);
- return true;
+ try
+ {
+ //TODO: just make sure that a Hibernate JPA EntityTransaction
+ // delegates to the Hibernate Session transaction
+ getSession(entityManager).getTransaction().registerSynchronization(sync);
+ return true;
+ }
+ catch (NotHibernateException nhe)
+ {
+ return super.registerSynchronization(sync, entityManager);
+ }
+
}
@Override
@@ -192,6 +239,10 @@
{
return getSession(entityManager).getEntityName(bean);
}
+ catch (NotHibernateException nhe)
+ {
+ return super.getName(bean, entityManager);
+ }
catch (TransientObjectException e)
{
return super.getName(bean, entityManager);
@@ -210,10 +261,10 @@
try
{
return (EntityManager) FULL_TEXT_ENTITYMANAGER_PROXY_CONSTRUCTOR.newInstance(
- FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR.invoke(null, super.proxyEntityManager( entityManager) )
- //TODO is double wrapping the right choice? ie to wrap the session?
- );
- }
+ FULL_TEXT_ENTITYMANAGER_CONSTRUCTOR.invoke(null, super.proxyEntityManager( entityManager) )
+ //TODO is double wrapping the right choice? ie to wrap the session?
+ );
+ }
catch (Exception e)
{
//throw new RuntimeException("could not proxy FullTextEntityManager", e);
@@ -241,7 +292,7 @@
private static ClassMetadata getClassMetadata(Object value, Session session)
{
- Class entityClass = Seam.getEntityClass( value.getClass() );
+ Class entityClass = getEntityClass(value);
ClassMetadata classMetadata = null;
if (entityClass!=null)
{
@@ -263,118 +314,52 @@
@Override
public Class getBeanClass(Object bean)
{
- try
- {
- return super.getBeanClass(bean);
- }
- catch (IllegalArgumentException iae)
- {
- return Hibernate.getClass(bean);
- }
+ return getEntityClass(bean);
}
- /**
- * A nasty hack until we get a nicer method in Hibernate to use instead
- *
- * TODO fix this once Hibernate exposes an API method to return the callback method/s for a
- * given bean class
- *
- * @param entityManager
- * @return
- */
- private EntityCallbackHandler getCallbackHandler(EntityManager entityManager)
+ public static Class getEntityClass(Object bean)
{
- PostLoadEventListener[] listeners = ((SessionImplementor) getSession(entityManager))
- .getListeners().getPostLoadEventListeners();
-
- for (PostLoadEventListener listener : listeners)
- {
- if (listener instanceof EJB3PostLoadEventListener)
- {
- try
- {
- Field callbackHandlerField = EJB3PostLoadEventListener.class.getField("callbackHandler");
- return (EntityCallbackHandler) callbackHandlerField.get(listener);
- }
- catch (Exception ex)
- {
- throw new RuntimeException(ex);
- }
- }
- }
- return null;
- }
-
- /**
- * More nastiness
- *
- * @param handler
- * @param fieldName
- * @return
- */
- private Callback[] getCallbacks(EntityCallbackHandler handler, String fieldName, Class beanClass)
- {
+ Class clazz = null;
try
{
- Field f = EntityCallbackHandler.class.getField(fieldName);
- HashMap<Class,Callback[]> callbacks = (HashMap<Class,Callback[]>) f.get(handler);
- return callbacks.get(beanClass);
+ clazz = Entity.forBean(bean).getBeanClass();
}
- catch (Exception ex)
- {
- throw new RuntimeException(ex);
+ catch (NotEntityException e) {
+ // It's ok, try some other methods
}
- }
-
- private Method getCallbackMethod(EntityManager entityManager, Class beanClass, String callbackFieldName)
- {
- Callback[] callbacks = getCallbacks(getCallbackHandler(entityManager), callbackFieldName, beanClass);
- if (callbacks != null)
+ if (clazz == null)
{
- for (Callback cb : callbacks)
- {
- return cb.getCallbackMethod();
- }
+ clazz = Hibernate.getClass(bean);
}
-
- return null;
+
+ return clazz;
}
- /*
- @Override
- public Method getPostLoadMethod(Class beanClass, EntityManager entityManager)
- {
- return getCallbackMethod(entityManager, beanClass, "postLoads");
- }
-
-
- @Override
- public Method getPrePersistMethod(Class beanClass, EntityManager entityManager)
- {
- return getCallbackMethod(entityManager, beanClass, "preCreates");
- }
-
- @Override
- public Method getPreUpdateMethod(Class beanClass, EntityManager entityManager)
- {
- return getCallbackMethod(entityManager, beanClass, "preUpdates");
- }
-
- @Override
- public Method getPreRemoveMethod(Class beanClass, EntityManager entityManager)
- {
- return getCallbackMethod(entityManager, beanClass, "preRemoves");
- }*/
-
private Session getSession(EntityManager entityManager)
{
- return (Session) entityManager.getDelegate();
+ Object delegate = entityManager.getDelegate();
+ if ( delegate instanceof Session )
+ {
+ return (Session) delegate;
+ }
+ else
+ {
+ throw new NotHibernateException();
+ }
}
+ /**
+ * Occurs when Hibernate is in the classpath, but this particular
+ * EntityManager is not from Hibernate
+ *
+ * @author Gavin King
+ *
+ */
+ static class NotHibernateException extends IllegalArgumentException {}
+
public static HibernatePersistenceProvider instance()
{
- return (HibernatePersistenceProvider) Component.getInstance(HibernatePersistenceProvider.class, STATELESS);
+ return (HibernatePersistenceProvider) Component.getInstance(HibernatePersistenceProvider.class, ScopeType.STATELESS);
}
-
}
Deleted: trunk/src/main/org/jboss/seam/persistence/JpaPersistenceProvider.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/JpaPersistenceProvider.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/persistence/JpaPersistenceProvider.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -1,59 +0,0 @@
-package org.jboss.seam.persistence;
-
-import static org.jboss.seam.ScopeType.STATELESS;
-import static org.jboss.seam.annotations.Install.BUILT_IN;
-
-import javax.persistence.EntityManager;
-import javax.transaction.Synchronization;
-
-import org.jboss.seam.Component;
-import org.jboss.seam.annotations.Install;
-import org.jboss.seam.annotations.Name;
-import org.jboss.seam.annotations.Scope;
-import org.jboss.seam.annotations.intercept.BypassInterceptors;
-
-/**
- * PersistenceProvider for any JPA implementation. Many methods are unusable
- * or use naive implementations
- *
- * @author Gavin King
- * @author Pete Muir
- *
- */
- at Name("org.jboss.seam.persistence.jpaPersistenceProvider")
- at Scope(STATELESS)
- at BypassInterceptors
- at Install(precedence=BUILT_IN, classDependencies="javax.persistence.EntityManager")
-public class JpaPersistenceProvider extends AbstractPersistenceProvider
-{
-
- @Override
- public void enableFilter(Filter filter, EntityManager entityManager)
- {
- throw new UnsupportedOperationException("You must use Hibernate to use Filters");
- }
-
- @Override
- public boolean isDirty(EntityManager entityManager)
- {
- return true;
- }
-
- @Override
- public boolean registerSynchronization(Synchronization sync, EntityManager entityManager)
- {
- return false;
- }
-
- @Override
- public void setFlushModeManual(EntityManager entityManager)
- {
- throw new UnsupportedOperationException("You must use Hibernate to use Manual Flush Mode");
- }
-
- public static JpaPersistenceProvider instance()
- {
- return (JpaPersistenceProvider) Component.getInstance(JpaPersistenceProvider.class, STATELESS);
- }
-
-}
Modified: trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/persistence/ManagedEntityIdentityInterceptor.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -140,7 +140,7 @@
return value instanceof List ||
value instanceof Map ||
value instanceof Set ||
- Seam.isEntityClass( value.getClass() );
+ Seam.getEntityClass(value.getClass()) != null;
}
private Object getFieldValue(Object bean, Field field) throws Exception
Modified: trunk/src/main/org/jboss/seam/persistence/PersistenceProvider.java
===================================================================
--- trunk/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/persistence/PersistenceProvider.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -1,10 +1,11 @@
package org.jboss.seam.persistence;
import static org.jboss.seam.annotations.Install.BUILT_IN;
-import static org.jboss.seam.util.Reflections.isInstanceOf;
import java.lang.reflect.Method;
+import java.util.Date;
import javax.persistence.EntityManager;
+import javax.persistence.OptimisticLockException;
import javax.transaction.Synchronization;
import org.jboss.seam.Component;
@@ -16,8 +17,9 @@
import org.jboss.seam.annotations.intercept.BypassInterceptors;
/**
* Abstraction layer for persistence providers (JPA implementations).
- * This class delegates to either the generic JpaPersistenceProvider or a more
- * specific one if available.
+ * This class provides a working base implementation that can be
+ * optimized for performance and non-standardized features by extending
+ * and overriding the methods.
*
* The methods on this class are a great todo list for the next rev
* of the JPA spec ;-)
@@ -38,7 +40,7 @@
*/
public void setFlushModeManual(EntityManager entityManager)
{
- getPersistenceProvider(entityManager).setFlushModeManual(entityManager);
+ throw new UnsupportedOperationException("For use of FlushMode.MANUAL, please use Hibernate as the persistence provider or use a custom PersistenceProvider");
}
/**
* Does the persistence context have unflushed changes? If
@@ -49,7 +51,7 @@
*/
public boolean isDirty(EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).isDirty(entityManager);
+ return true; //best we can do!
}
/**
@@ -59,7 +61,7 @@
*/
public Object getId(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getId(bean, entityManager);
+ return Entity.forBean( bean ).getIdentifier(bean);
}
/**
@@ -72,7 +74,7 @@
*/
public String getName(Object bean, EntityManager entityManager) throws IllegalArgumentException
{
- return getPersistenceProvider(entityManager).getName(bean, entityManager);
+ return Entity.forBean( bean ).getName();
}
/**
@@ -82,12 +84,24 @@
*/
public Object getVersion(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getVersion(bean, entityManager);
+ return Entity.forBean( bean ).getVersion(bean);
}
public void checkVersion(Object bean, EntityManager entityManager, Object oldVersion, Object version)
{
- getPersistenceProvider(entityManager).checkVersion(bean, entityManager, oldVersion, version);
+ boolean equal;
+ if (oldVersion instanceof Date)
+ {
+ equal = ( (Date) oldVersion ).getTime() == ( (Date) version ).getTime();
+ }
+ else
+ {
+ equal = oldVersion.equals(version);
+ }
+ if ( !equal )
+ {
+ throw new OptimisticLockException("current database version number does not match passivated version number");
+ }
}
/**
* Enable a Filter. This is here just especially for Hibernate,
@@ -96,7 +110,7 @@
*/
public void enableFilter(Filter filter, EntityManager entityManager)
{
- getPersistenceProvider(entityManager).enableFilter(filter, entityManager);
+ throw new UnsupportedOperationException("For filters, please use Hibernate as the persistence provider");
}
/**
@@ -104,7 +118,7 @@
*/
public boolean registerSynchronization(Synchronization sync, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).registerSynchronization(sync, entityManager);
+ return false; //best we can do!
}
public static PersistenceProvider instance()
@@ -115,22 +129,15 @@
/**
* Wrap the delegate before returning it to the application
*/
- @Deprecated
public Object proxyDelegate(Object delegate)
{
- return getPersistenceProvider(delegate).proxyDelegate(delegate);
+ return delegate;
}
-
- public Object proxyDelegate(EntityManager entityManager, Object delegate)
- {
- return getPersistenceProvider(entityManager).proxyDelegate(delegate);
- }
-
/**
* Wrap the entityManager before returning it to the application
*/
public EntityManager proxyEntityManager(EntityManager entityManager) {
- return getPersistenceProvider(entityManager).proxyEntityManager(entityManager);
+ return new EntityManagerProxy(entityManager);
}
/**
@@ -139,71 +146,55 @@
* @param bean The entity bean instance
* @return The class of the entity bean
*/
- @Deprecated
public Class getBeanClass(Object bean)
{
- return Entity.forClass(bean.getClass()).getBeanClass();
+ return Entity.forBean(bean).getBeanClass();
}
- public Class getBeanClass(EntityManager entityManager, Object bean)
+ public Method getPostLoadMethod(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getBeanClass(bean);
+ return Entity.forBean(bean).getPostLoadMethod();
}
- public Method getPostLoadMethod(Class beanClass, EntityManager entityManager)
+ public Method getPrePersistMethod(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getPostLoadMethod(beanClass, entityManager);
+ return Entity.forBean(bean).getPrePersistMethod();
}
- public Method getPrePersistMethod(Class beanClass, EntityManager entityManager)
+ public Method getPreUpdateMethod(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getPrePersistMethod(beanClass, entityManager);
+ return Entity.forBean(bean).getPreUpdateMethod();
}
- public Method getPreUpdateMethod(Class beanClass, EntityManager entityManager)
+ public Method getPreRemoveMethod(Object bean, EntityManager entityManager)
{
- return getPersistenceProvider(entityManager).getPreUpdateMethod(beanClass, entityManager);
+ return Entity.forBean(bean).getPreRemoveMethod();
}
- public Method getPreRemoveMethod(Class beanClass, EntityManager entityManager)
+ @Deprecated
+ public Method getPreRemoveMethod(Class beanClass)
{
- return getPersistenceProvider(entityManager).getPreRemoveMethod(beanClass, entityManager);
+ return Entity.forClass(beanClass).getPreRemoveMethod();
}
- /**
- * Do runtime detection of PersistenceProvider
- */
- private AbstractPersistenceProvider getPersistenceProvider(EntityManager entityManager)
+ @Deprecated
+ public Method getPostLoadMethod(Class beanClass)
{
- // Work around EJBTHREE-912 (don't you just love random NPEs!)
- if (entityManager != null && isInstanceOf(entityManager.getClass(), "org.jboss.ejb3.entity.HibernateSession"))
- {
- return HibernatePersistenceProvider.instance();
- }
- else if(entityManager != null && isInstanceOf(entityManager.getDelegate().getClass(), "org.hibernate.Session"))
- {
- return HibernatePersistenceProvider.instance();
- }
- else
- {
- return JpaPersistenceProvider.instance();
- }
+ return Entity.forClass(beanClass).getPostLoadMethod();
}
- /**
- * Do runtime detection of PersistenceProvider
- */
@Deprecated
- private AbstractPersistenceProvider getPersistenceProvider(Object delegate)
+ public Method getPrePersistMethod(Class beanClass)
{
- if (isInstanceOf(delegate.getClass(), "org.hibernate.Session"))
- {
- return HibernatePersistenceProvider.instance();
- }
- else
- {
- return JpaPersistenceProvider.instance();
- }
+ return Entity.forClass(beanClass).getPrePersistMethod();
}
+ @Deprecated
+ public Method getPreUpdateMethod(Class beanClass)
+ {
+ return Entity.forClass(beanClass).getPreUpdateMethod();
+ }
+
+
+
}
Modified: trunk/src/main/org/jboss/seam/security/EntityPermissionChecker.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/EntityPermissionChecker.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/security/EntityPermissionChecker.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -87,16 +87,16 @@
switch (action)
{
case READ:
- m = provider.getPostLoadMethod(beanClass, getEntityManager());
+ m = provider.getPostLoadMethod(entity, getEntityManager());
break;
case INSERT:
- m = provider.getPrePersistMethod(beanClass, getEntityManager());
+ m = provider.getPrePersistMethod(entity, getEntityManager());
break;
case UPDATE:
- m = provider.getPreUpdateMethod(beanClass, getEntityManager());
+ m = provider.getPreUpdateMethod(entity, getEntityManager());
break;
case DELETE:
- m = provider.getPreRemoveMethod(beanClass, getEntityManager());
+ m = provider.getPreRemoveMethod(entity, getEntityManager());
}
Restrict restrict = null;
Modified: trunk/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/security/HibernateSecurityInterceptor.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -10,6 +10,7 @@
import org.hibernate.EmptyInterceptor;
import org.hibernate.Interceptor;
import org.hibernate.type.Type;
+import org.jboss.seam.Entity.NotEntityException;
/**
* Facilitates security checks for Hibernate entities
@@ -30,8 +31,15 @@
public boolean onLoad(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- EntityPermissionChecker.instance().checkEntityPermission(entity, READ);
-
+ try
+ {
+ EntityPermissionChecker.instance().checkEntityPermission(entity, READ);
+ }
+ catch (NotEntityException e)
+ {
+ // Not a JPA entity
+ }
+
return wrappedInterceptor != null ?
wrappedInterceptor.onLoad(entity, id, state, propertyNames, types) :
false;
@@ -41,8 +49,15 @@
public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- EntityPermissionChecker.instance().checkEntityPermission(entity, DELETE);
-
+ try
+ {
+ EntityPermissionChecker.instance().checkEntityPermission(entity, DELETE);
+ }
+ catch (NotEntityException e)
+ {
+ // Not a JPA entity
+ }
+
if (wrappedInterceptor != null)
wrappedInterceptor.onDelete(entity, id, state, propertyNames, types);
}
@@ -51,8 +66,15 @@
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types)
{
- EntityPermissionChecker.instance().checkEntityPermission(entity, UPDATE);
-
+ try
+ {
+ EntityPermissionChecker.instance().checkEntityPermission(entity, UPDATE);
+ }
+ catch (NotEntityException e)
+ {
+ // Not a JPA entity
+ }
+
return wrappedInterceptor != null ?
wrappedInterceptor.onFlushDirty(entity, id, currentState,
previousState, propertyNames, types) : false;
@@ -62,8 +84,15 @@
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types)
{
- EntityPermissionChecker.instance().checkEntityPermission(entity, INSERT);
-
+ try
+ {
+ EntityPermissionChecker.instance().checkEntityPermission(entity, INSERT);
+ }
+ catch (NotEntityException e)
+ {
+ // Not a JPA entity
+ }
+
return wrappedInterceptor != null ?
wrappedInterceptor.onSave(entity, id, state, propertyNames, types) :
false;
Modified: trunk/src/main/org/jboss/seam/util/Reflections.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/Reflections.java 2008-07-28 15:44:08 UTC (rev 8526)
+++ trunk/src/main/org/jboss/seam/util/Reflections.java 2008-07-28 15:44:59 UTC (rev 8527)
@@ -316,6 +316,19 @@
}
}
+ public static Method getMethod(Class clazz, String name)
+ {
+ for ( Class superClass = clazz; superClass!=Object.class; superClass=superClass.getSuperclass() )
+ {
+ try
+ {
+ return superClass.getDeclaredMethod(name);
+ }
+ catch (NoSuchMethodException nsme) {}
+ }
+ throw new IllegalArgumentException("no such method: " + clazz.getName() + '.' + name);
+ }
+
/**
* Check to see if clazz is an instance of name
*/
More information about the seam-commits
mailing list