Author: swd847
Date: 2010-07-22 05:24:25 -0400 (Thu, 22 Jul 2010)
New Revision: 13473
Removed:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProducer.java
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/SeamManaged.java
Log:
clean up managed persistence context implementation
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java 2010-07-22
08:36:20 UTC (rev 13472)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java 2010-07-22
09:24:25 UTC (rev 13473)
@@ -29,13 +29,14 @@
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.jboss.weld.extensions.bean.BeanImpl;
import org.jboss.weld.extensions.bean.BeanLifecycle;
+import org.jboss.weld.extensions.util.BeanResolutionException;
+import org.jboss.weld.extensions.util.BeanResolver;
/**
* Class that is responsible for creating and destroying the seam managed
@@ -104,20 +105,15 @@
{
if (emf == null)
{
- Set<Bean<?>> beans = manager.getBeans(EntityManagerFactory.class,
qualifiers);
- if (beans.size() == 0)
+ try
{
- throw new RuntimeException("No bean found with type EntityManagerFactory
and qualifiers " + qualifiers);
+ emf = BeanResolver.getReference(EntityManagerFactory.class, qualifiers,
manager);
}
- if (beans.size() != 1)
+ catch (BeanResolutionException e)
{
- throw new RuntimeException("More than 1 bean found with type
EntityManagerFactory and qualifiers " + qualifiers);
+ throw new RuntimeException(e);
}
- Bean<?> emfBean = beans.iterator().next();
- CreationalContext<?> emfCreationalContext =
manager.createCreationalContext(emfBean);
- emf = (EntityManagerFactory) manager.getReference(emfBean,
EntityManagerFactory.class, emfCreationalContext);
}
return emf;
}
-
}
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java 2010-07-22
08:36:20 UTC (rev 13472)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java 2010-07-22
09:24:25 UTC (rev 13473)
@@ -45,11 +45,10 @@
* Extension the wraps producer methods/fields that produce an entity manager to
* turn them into Seam Managed Persistence Contexts.
*
- * At present this happens automatically, in future we will need some way to
- * configure it
*
- * @author stuart
*
+ * @author Stuart Douglas
+ *
*/
public class ManagedPersistenceContextExtension implements Extension
{
Deleted:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProducer.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProducer.java 2010-07-22
08:36:20 UTC (rev 13472)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProducer.java 2010-07-22
09:24:25 UTC (rev 13473)
@@ -1,75 +0,0 @@
-package org.jboss.seam.persistence;
-
-import java.io.Serializable;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Proxy;
-import java.util.Set;
-
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.inject.spi.InjectionPoint;
-import javax.enterprise.inject.spi.Producer;
-import javax.persistence.EntityManager;
-
-/**
- * implementation of Producer that proxies a produced EntityManager
- *
- * @author stuart
- *
- */
-public class ManagedPersistenceContextProducer implements Producer<EntityManager>
-{
- static final Class<?> proxyClass =
Proxy.getProxyClass(EntityManager.class.getClassLoader(), EntityManager.class,
Serializable.class);
-
- static final Constructor<?> proxyConstructor;
-
- static
- {
- try
- {
- proxyConstructor = proxyClass.getConstructor(InvocationHandler.class);
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- private final Producer<EntityManager> delegate;
-
- private final BeanManager beanManager;
-
- public ManagedPersistenceContextProducer(Producer<EntityManager> delegate,
BeanManager beanManager)
- {
- this.delegate = delegate;
- this.beanManager = beanManager;
- }
-
- public void dispose(EntityManager instance)
- {
- delegate.dispose(instance);
- }
-
- public Set<InjectionPoint> getInjectionPoints()
- {
- return delegate.getInjectionPoints();
- }
-
- public EntityManager produce(CreationalContext<EntityManager> ctx)
- {
- try
- {
- EntityManager entityManager = delegate.produce(ctx);
- ManagedPersistenceContextProxyHandler handler = new
ManagedPersistenceContextProxyHandler(entityManager, beanManager);
- EntityManager proxy = (EntityManager) proxyConstructor.newInstance(handler);
- return proxy;
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
-
- }
-
-}
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java 2010-07-22
08:36:20 UTC (rev 13472)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java 2010-07-22
09:24:25 UTC (rev 13473)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
package org.jboss.seam.persistence;
import java.io.Serializable;
@@ -3,8 +24,5 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
-import java.util.Set;
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.persistence.EntityManager;
@@ -13,14 +31,24 @@
import javax.transaction.SystemException;
import org.jboss.seam.persistence.transaction.UserTransaction;
-import org.jboss.weld.extensions.beanManager.BeanManagerAccessor;
-import org.jboss.weld.extensions.literal.DefaultLiteral;
+import org.jboss.weld.extensions.util.BeanResolutionException;
+import org.jboss.weld.extensions.util.BeanResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Proxy handler for the seam managed persistence context. This handler makes
+ * sure that the EntityManager is enrolled in the current transaction before
+ * passing the call through to the delegate
+ *
+ * @author Stuart Douglas
+ *
+ */
public class ManagedPersistenceContextProxyHandler implements InvocationHandler,
Serializable, Synchronization
{
+ private static final long serialVersionUID = -6539267789786229774L;
+
private final EntityManager delegate;
private transient BeanManager beanManager;
@@ -67,31 +95,18 @@
}
}
- private BeanManager getBeanManager()
- {
- if (beanManager == null)
- {
- beanManager = BeanManagerAccessor.getManager();
- }
- return beanManager;
- }
-
private UserTransaction getUserTransaction()
{
if (userTransaction == null)
{
- Set<Bean<?>> beans = beanManager.getBeans(UserTransaction.class,
DefaultLiteral.INSTANCE);
- if (beans.size() == 0)
+ try
{
- throw new RuntimeException("No bean with class" +
UserTransaction.class.getName() + " and qualifiers Default found");
+ userTransaction = BeanResolver.getDefaultReference(UserTransaction.class,
beanManager);
}
- else if (beans.size() != 1)
+ catch (Exception e)
{
- throw new RuntimeException("More than 1 bean with class" +
UserTransaction.class.getName() + " and qualifiers Default found");
+ throw new RuntimeException(e);
}
- Bean<UserTransaction> userTransactionBean = (Bean<UserTransaction>)
beans.iterator().next();
- CreationalContext<UserTransaction> ctx =
beanManager.createCreationalContext(userTransactionBean);
- userTransaction = (UserTransaction)
beanManager.getReference(userTransactionBean, UserTransaction.class, ctx);
}
return userTransaction;
}
@@ -103,7 +118,6 @@
public void beforeCompletion()
{
- // TODO Auto-generated method stub
}
Modified:
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/SeamManaged.java
===================================================================
---
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/SeamManaged.java 2010-07-22
08:36:20 UTC (rev 13472)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/SeamManaged.java 2010-07-22
09:24:25 UTC (rev 13473)
@@ -21,13 +21,43 @@
*/
package org.jboss.seam.persistence;
+import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+import javax.enterprise.context.Dependent;
+import javax.persistence.EntityManagerFactory;
+
+/**
+ * Signifies that a resource producer field or producer method that produces an
+ * {@link EntityManagerFactory} should also produce a Seam managed persistence
+ * context. For example:
+ *
+ * <pre>
+ * @SeamManaged
+ * @Produces
+ * @PersistenceUnit
+ * @ConversationScoped
+ * @SomeQualifier
+ * EntityManagerFactory emf;
+ * </pre>
+ *
+ * will create a conversation scoped seam managed persistence context that is
+ * conversation scoped with the qualifier @SomeQualifier.
+ *
+ * This field still produces the EntityManagerFactory with qualifier
+ * @SomeQualifier, however the scope for the producer field is changed to
+ * {@link Dependent}, as the specification does not allow resource producer
+ * fields to have a scope other than Depedent
+ *
+ * @author Stuart Douglas
+ *
+ */
@Retention(RetentionPolicy.RUNTIME)
-@Target( { ElementType.FIELD })
+@Target( { ElementType.FIELD, ElementType.METHOD })
+@Documented
public @interface SeamManaged
{