Author: swd847
Date: 2010-07-19 18:31:04 -0400 (Mon, 19 Jul 2010)
New Revision: 13444
Added:
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/ManagedPersistenceContextProducer.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
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/Hotel.java
Removed:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/Hotel.java
Modified:
modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionScopedTest.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java
modules/persistence/trunk/impl/src/test/resources/META-INF/persistence.xml
Log:
very quick and dirty impl of the SMPC
Added:
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
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextBeanLifecycle.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,115 @@
+/*
+ * 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;
+import java.lang.annotation.Annotation;
+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.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;
+
+public class ManagedPersistenceContextBeanLifecycle implements
BeanLifecycle<EntityManager>
+{
+
+ private EntityManagerFactory emf;
+ private final Annotation[] qualifiers;
+ private final BeanManager manager;
+
+ static final Class<?> proxyClass =
Proxy.getProxyClass(EntityManager.class.getClassLoader(), EntityManager.class,
Serializable.class);
+
+ public ManagedPersistenceContextBeanLifecycle(Set<Annotation> qualifiers,
BeanManager manager)
+ {
+ this.qualifiers = new Annotation[qualifiers.size()];
+ int i = 0;
+ for (Annotation a : qualifiers)
+ {
+ this.qualifiers[i++] = a;
+ }
+ this.manager = manager;
+ }
+
+ static final Constructor<?> proxyConstructor;
+
+ static
+ {
+ try
+ {
+ proxyConstructor = proxyClass.getConstructor(InvocationHandler.class);
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public EntityManager create(BeanImpl<EntityManager> bean,
CreationalContext<EntityManager> arg0)
+ {
+ try
+ {
+ EntityManagerFactory emf = getEntityManagerFactory();
+ EntityManager entityManager = emf.createEntityManager();
+ ManagedPersistenceContextProxyHandler handler = new
ManagedPersistenceContextProxyHandler(entityManager, manager);
+ EntityManager proxy = (EntityManager) proxyConstructor.newInstance(handler);
+ return proxy;
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void destroy(BeanImpl<EntityManager> bean, EntityManager arg0,
CreationalContext<EntityManager> arg1)
+ {
+ arg1.release();
+ }
+
+ private EntityManagerFactory getEntityManagerFactory()
+ {
+ if (emf == null)
+ {
+ Set<Bean<?>> beans = manager.getBeans(EntityManagerFactory.class,
qualifiers);
+ if (beans.size() == 0)
+ {
+ throw new RuntimeException("No bean found with type EntityManagerFactory
and qualifiers " + qualifiers);
+ }
+ if (beans.size() != 1)
+ {
+ throw new RuntimeException("More than 1 bean found with type
EntityManagerFactory and qualifiers " + qualifiers);
+ }
+ Bean<?> emfBean = beans.iterator().next();
+ CreationalContext<?> emfCreationalContext =
manager.createCreationalContext(emfBean);
+ emf = (EntityManagerFactory) manager.getReference(emfBean,
EntityManagerFactory.class, emfCreationalContext);
+ }
+ return emf;
+ }
+
+}
Added:
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
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextExtension.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,136 @@
+/*
+ * 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.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessProducer;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
+import org.jboss.weld.extensions.bean.BeanBuilder;
+import org.jboss.weld.extensions.literal.DefaultLiteral;
+
+/**
+ * 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
+ *
+ */
+public class ManagedPersistenceContextExtension implements Extension
+{
+
+ Set<Bean<?>> beans = new HashSet<Bean<?>>();
+
+ public <T> void processProducer(@Observes ProcessProducer<T,
EntityManagerFactory> event, BeanManager manager)
+ {
+ if (!event.getAnnotatedMember().isAnnotationPresent(SeamManaged.class))
+ {
+ return;
+ }
+ Set<Annotation> qualifiers = new HashSet<Annotation>();
+ Class scope = Dependent.class;
+ for (Annotation a : event.getAnnotatedMember().getAnnotations())
+ {
+ if (manager.isQualifier(a.annotationType()))
+ {
+ qualifiers.add(a);
+ }
+ else if (manager.isScope(a.annotationType()))
+ {
+ scope = a.annotationType();
+ }
+ }
+ if (qualifiers.isEmpty())
+ {
+ qualifiers.add(new DefaultLiteral());
+ }
+ AnnotatedTypeBuilder<EntityManager> typeBuilder =
AnnotatedTypeBuilder.newInstance(EntityManager.class);
+ BeanBuilder<EntityManager> builder = new
BeanBuilder<EntityManager>(typeBuilder.create(), manager);
+ builder.defineBeanFromAnnotatedType();
+ builder.setQualifiers(qualifiers);
+ builder.setScope(scope);
+ builder.setInjectionTarget(new NoOpInjectionTarget());
+ builder.setBeanLifecycle(new ManagedPersistenceContextBeanLifecycle(qualifiers,
manager));
+ beans.add(builder.create());
+ }
+
+ public void afterBeanDiscovery(@Observes AfterBeanDiscovery event)
+ {
+ for (Bean<?> i : beans)
+ {
+ event.addBean(i);
+ }
+ }
+
+ private static class NoOpInjectionTarget implements
InjectionTarget<EntityManager>
+ {
+
+ public EntityManager produce(CreationalContext<EntityManager> ctx)
+ {
+ return null;
+ }
+
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return Collections.emptySet();
+ }
+
+ public void dispose(EntityManager instance)
+ {
+
+ }
+
+ public void preDestroy(EntityManager instance)
+ {
+
+ }
+
+ public void postConstruct(EntityManager instance)
+ {
+
+ }
+
+ public void inject(EntityManager instance, CreationalContext<EntityManager>
ctx)
+ {
+
+ }
+
+ }
+}
Added:
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
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProducer.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,75 @@
+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);
+ }
+
+ }
+
+}
Added:
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
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/ManagedPersistenceContextProxyHandler.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,110 @@
+package org.jboss.seam.persistence;
+
+import java.io.Serializable;
+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;
+import javax.transaction.Synchronization;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ManagedPersistenceContextProxyHandler implements InvocationHandler,
Serializable, Synchronization
+{
+
+ private final EntityManager delegate;
+
+ private transient BeanManager beanManager;
+
+ private transient UserTransaction userTransaction;
+
+ private transient boolean synchronizationRegistered;
+
+ static final Logger log =
LoggerFactory.getLogger(ManagedPersistenceContextProxyHandler.class);
+
+ public ManagedPersistenceContextProxyHandler(EntityManager delegate, BeanManager
beanManager)
+ {
+ this.delegate = delegate;
+ this.beanManager = beanManager;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ if (!synchronizationRegistered)
+ {
+ joinTransaction();
+ }
+ return method.invoke(delegate, args);
+ }
+
+ private void joinTransaction() throws SystemException
+ {
+ UserTransaction transaction = getUserTransaction();
+ if (transaction.isActive())
+ {
+ transaction.enlist(delegate);
+ try
+ {
+ transaction.registerSynchronization(this);
+ synchronizationRegistered = true;
+ }
+ catch (Exception e)
+ {
+ // synchronizationRegistered =
+ // PersistenceProvider.instance().registerSynchronization(this,
+ // entityManager);
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ 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)
+ {
+ throw new RuntimeException("No bean with class" +
UserTransaction.class.getName() + " and qualifiers Default found");
+ }
+ else if (beans.size() != 1)
+ {
+ throw new RuntimeException("More than 1 bean with class" +
UserTransaction.class.getName() + " and qualifiers Default found");
+ }
+ Bean<UserTransaction> userTransactionBean = (Bean<UserTransaction>)
beans.iterator().next();
+ CreationalContext<UserTransaction> ctx =
beanManager.createCreationalContext(userTransactionBean);
+ userTransaction = (UserTransaction)
beanManager.getReference(userTransactionBean, UserTransaction.class, ctx);
+ }
+ return userTransaction;
+ }
+
+ public void afterCompletion(int status)
+ {
+ synchronizationRegistered = false;
+ }
+
+ public void beforeCompletion()
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
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
(rev 0)
+++
modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/SeamManaged.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,34 @@
+/*
+ * 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.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+(a)Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.FIELD })
+public @interface SeamManaged
+{
+
+}
Modified:
modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
===================================================================
---
modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 2010-07-19
22:31:04 UTC (rev 13444)
@@ -1,3 +1,4 @@
org.jboss.seam.persistence.PersistenceContextExtension
org.jboss.seam.persistence.transaction.TransactionExtension
-org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension
\ No newline at end of file
+org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension
+org.jboss.seam.persistence.ManagedPersistenceContextExtension
\ No newline at end of file
Added:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java
(rev 0)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/persistence/test/ManagedPersistenceContextTest.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,76 @@
+package org.jboss.seam.persistence.test;
+
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+import junit.framework.Assert;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.seam.persistence.PersistenceContextExtension;
+import org.jboss.seam.persistence.transaction.Transaction;
+import org.jboss.seam.persistence.transaction.UserTransaction;
+import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.EntityManagerProvider;
+import org.jboss.seam.transactions.test.util.Hotel;
+import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(Arquillian.class)
+public class ManagedPersistenceContextTest
+{
+ @Deployment
+ public static Archive<?> createTestArchive()
+ {
+ WebArchive war =
ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class,
"test.war");
+ war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
+
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
+ war.addPackage(Transaction.class.getPackage());
+ war.addPackage(PersistenceContextExtension.class.getPackage());
+ war.addClasses(ManagedPersistenceContextTest.class, Hotel.class,
EntityManagerProvider.class);
+ war.addWebResource("META-INF/persistence.xml",
"classes/META-INF/persistence.xml");
+ war.addWebResource(new ByteArrayAsset(new byte[0]), "beans.xml");
+
war.addWebResource("META-INF/services/javax.enterprise.inject.spi.Extension",
"classes/META-INF/services/javax.enterprise.inject.spi.Extension");
+ return war;
+ }
+
+ @Inject
+ UserTransaction transaction;
+
+ @Inject
+ EntityManager em;
+
+ @Test
+ public void testManagedPsersistenceContext() throws NotSupportedException,
SystemException, SecurityException, IllegalStateException, RollbackException,
HeuristicMixedException, HeuristicRollbackException
+ {
+ transaction.begin();
+ Hotel h = new Hotel("test", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.commit();
+
+ transaction.begin();
+ h = new Hotel("test2", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
+ em.persist(h);
+ em.flush();
+ transaction.rollback();
+
+ transaction.begin();
+ List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
+ Assert.assertTrue(hotels.size() == 1);
+ transaction.rollback();
+ }
+
+}
Deleted:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/Hotel.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/Hotel.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/Hotel.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -1,205 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat Middleware LLC, 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.transactions.test;
-
-import java.io.Serializable;
-import java.math.BigDecimal;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Id;
-import javax.persistence.Table;
-import javax.persistence.Transient;
-import javax.validation.constraints.Max;
-import javax.validation.constraints.Min;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
-import org.jboss.weld.extensions.core.Veto;
-
-/**
- * <p>
- * <strong>Hotel</strong> is the model/entity class that represents a hotel.
- * </p>
- *
- * @author Gavin King
- * @author Dan Allen
- */
-@Entity
-@Table(name = "hotel")
-@Veto
-public class Hotel implements Serializable
-{
- private Long id;
- private String name;
- private String address;
- private String city;
- private String state;
- private String zip;
- private String country;
- private Integer stars;
- private BigDecimal price;
-
- public Hotel()
- {
- }
-
- public Hotel(final String name, final String address, final String city, final String
state, final String zip, final String country)
- {
- this.name = name;
- this.address = address;
- this.city = city;
- this.state = state;
- this.zip = zip;
- this.country = country;
- }
-
- public Hotel(final int price, final int stars, final String name, final String
address, final String city, final String state, final String zip, final String country)
- {
- this.price = new BigDecimal(price);
- this.stars = stars;
- this.name = name;
- this.address = address;
- this.city = city;
- this.state = state;
- this.zip = zip;
- this.country = country;
- }
-
- @Id
- @GeneratedValue
- public Long getId()
- {
- return id;
- }
-
- public void setId(final Long id)
- {
- this.id = id;
- }
-
- @Size(max = 50)
- @NotNull
- public String getName()
- {
- return name;
- }
-
- public void setName(final String name)
- {
- this.name = name;
- }
-
- @Size(max = 100)
- @NotNull
- public String getAddress()
- {
- return address;
- }
-
- public void setAddress(final String address)
- {
- this.address = address;
- }
-
- @Size(max = 40)
- @NotNull
- public String getCity()
- {
- return city;
- }
-
- public void setCity(final String city)
- {
- this.city = city;
- }
-
- @Size(min = 3, max = 6)
- @NotNull
- public String getZip()
- {
- return zip;
- }
-
- public void setZip(final String zip)
- {
- this.zip = zip;
- }
-
- @Size(min = 2, max = 10)
- public String getState()
- {
- return state;
- }
-
- public void setState(final String state)
- {
- this.state = state;
- }
-
- @Size(min = 2, max = 40)
- @NotNull
- public String getCountry()
- {
- return country;
- }
-
- public void setCountry(final String country)
- {
- this.country = country;
- }
-
- @Min(1)
- @Max(5)
- public Integer getStars()
- {
- return stars;
- }
-
- public void setStars(final Integer stars)
- {
- this.stars = stars;
- }
-
- @Column(precision = 6, scale = 2)
- public BigDecimal getPrice()
- {
- return price;
- }
-
- public void setPrice(final BigDecimal price)
- {
- this.price = price;
- }
-
- @Transient
- public String getLocation()
- {
- return city + ", " + state + ", " + country;
- }
-
- @Override
- public String toString()
- {
- return "Hotel(" + name + "," + address + "," + city +
"," + zip + ")";
- }
-}
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionInterceptorTest.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -21,6 +21,7 @@
import org.jboss.seam.transactions.test.util.ArtifactNames;
import org.jboss.seam.transactions.test.util.DontRollBackException;
import org.jboss.seam.transactions.test.util.EntityManagerProvider;
+import org.jboss.seam.transactions.test.util.Hotel;
import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -36,7 +37,7 @@
public static Archive<?> createTestArchive()
{
- WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ WebArchive war =
ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class,
"test.war");
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
war.addPackage(Transaction.class.getPackage());
@@ -87,6 +88,7 @@
catch (DontRollBackException e)
{
}
+ observer.setEnabled(false);
assertHotels(2);
observer.verify();
}
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionManagedBean.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -6,6 +6,7 @@
import org.jboss.seam.persistence.transaction.TransactionPropagation;
import org.jboss.seam.persistence.transaction.Transactional;
import org.jboss.seam.transactions.test.util.DontRollBackException;
+import org.jboss.seam.transactions.test.util.Hotel;
@Transactional(TransactionPropagation.REQUIRED)
public class TransactionManagedBean
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionObserver.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -47,9 +47,12 @@
public void observeBeforeTransactionCommit(@Observes AfterTransactionCompletion
event)
{
afterTransaction = true;
- if (expectSuccess != event.success())
+ if (enabled)
{
- throw new RuntimeException("Expected success to be " +
expectSuccess);
+ if (expectSuccess != event.success())
+ {
+ throw new RuntimeException("Expected success to be " +
expectSuccess);
+ }
}
}
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionScopedTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionScopedTest.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/TransactionScopedTest.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -17,6 +17,7 @@
import org.jboss.seam.persistence.transaction.UserTransaction;
import org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension;
import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.Hotel;
import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -31,8 +32,7 @@
@Deployment
public static Archive<?> createTestArchive()
{
-
- WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ WebArchive war =
ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class,
"test.war");
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
war.addPackage(Transaction.class.getPackage());
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/UserTransactionTest.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -4,8 +4,7 @@
import javax.inject.Inject;
import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.PersistenceUnit;
+import javax.persistence.PersistenceContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
@@ -19,6 +18,7 @@
import org.jboss.seam.persistence.transaction.Transaction;
import org.jboss.seam.persistence.transaction.UserTransaction;
import org.jboss.seam.transactions.test.util.ArtifactNames;
+import org.jboss.seam.transactions.test.util.Hotel;
import org.jboss.seam.transactions.test.util.MavenArtifactResolver;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -33,8 +33,7 @@
@Deployment
public static Archive<?> createTestArchive()
{
-
- WebArchive war = ShrinkWrap.create("test.war", WebArchive.class);
+ WebArchive war =
ShrinkWrap.createDomain().getArchiveFactory().create(WebArchive.class,
"test.war");
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.WELD_EXTENSIONS));
war.addLibraries(MavenArtifactResolver.resolve(ArtifactNames.SEAM_PERSISTENCE_API));
war.addPackage(Transaction.class.getPackage());
@@ -48,34 +47,35 @@
@Inject
UserTransaction transaction;
- @PersistenceUnit
- EntityManagerFactory emf;
+ @PersistenceContext
+ EntityManager em;
@Test
public void userTransactionTest() throws NotSupportedException, SystemException,
SecurityException, IllegalStateException, RollbackException, HeuristicMixedException,
HeuristicRollbackException
{
transaction.begin();
- EntityManager em = emf.createEntityManager();
em.joinTransaction();
Hotel h = new Hotel("test", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
em.persist(h);
em.flush();
transaction.commit();
+ em.clear();
transaction.begin();
- em = emf.createEntityManager();
em.joinTransaction();
h = new Hotel("test2", "Fake St", "Wollongong",
"NSW", "2518", "Australia");
em.persist(h);
em.flush();
transaction.rollback();
+ em.clear();
transaction.begin();
- em = emf.createEntityManager();
em.joinTransaction();
List<Hotel> hotels = em.createQuery("select h from Hotel
h").getResultList();
Assert.assertTrue(hotels.size() == 1);
transaction.rollback();
+ em.clear();
+
}
}
Modified:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java 2010-07-19
11:45:15 UTC (rev 13443)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/EntityManagerProvider.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -1,12 +1,17 @@
package org.jboss.seam.transactions.test.util;
+import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+import org.jboss.seam.persistence.SeamManaged;
+
public class EntityManagerProvider
{
- @PersistenceContext
+ @PersistenceUnit
+ @RequestScoped
@Produces
- EntityManager entityManager;
+ @SeamManaged
+ EntityManagerFactory emf;
}
Copied:
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/Hotel.java
(from rev 13431,
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/Hotel.java)
===================================================================
---
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/Hotel.java
(rev 0)
+++
modules/persistence/trunk/impl/src/test/java/org/jboss/seam/transactions/test/util/Hotel.java 2010-07-19
22:31:04 UTC (rev 13444)
@@ -0,0 +1,205 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, 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.transactions.test.util;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Transient;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+import org.jboss.weld.extensions.core.Veto;
+
+/**
+ * <p>
+ * <strong>Hotel</strong> is the model/entity class that represents a hotel.
+ * </p>
+ *
+ * @author Gavin King
+ * @author Dan Allen
+ */
+@Entity
+@Table(name = "hotel")
+@Veto
+public class Hotel implements Serializable
+{
+ private Long id;
+ private String name;
+ private String address;
+ private String city;
+ private String state;
+ private String zip;
+ private String country;
+ private Integer stars;
+ private BigDecimal price;
+
+ public Hotel()
+ {
+ }
+
+ public Hotel(final String name, final String address, final String city, final String
state, final String zip, final String country)
+ {
+ this.name = name;
+ this.address = address;
+ this.city = city;
+ this.state = state;
+ this.zip = zip;
+ this.country = country;
+ }
+
+ public Hotel(final int price, final int stars, final String name, final String
address, final String city, final String state, final String zip, final String country)
+ {
+ this.price = new BigDecimal(price);
+ this.stars = stars;
+ this.name = name;
+ this.address = address;
+ this.city = city;
+ this.state = state;
+ this.zip = zip;
+ this.country = country;
+ }
+
+ @Id
+ @GeneratedValue
+ public Long getId()
+ {
+ return id;
+ }
+
+ public void setId(final Long id)
+ {
+ this.id = id;
+ }
+
+ @Size(max = 50)
+ @NotNull
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(final String name)
+ {
+ this.name = name;
+ }
+
+ @Size(max = 100)
+ @NotNull
+ public String getAddress()
+ {
+ return address;
+ }
+
+ public void setAddress(final String address)
+ {
+ this.address = address;
+ }
+
+ @Size(max = 40)
+ @NotNull
+ public String getCity()
+ {
+ return city;
+ }
+
+ public void setCity(final String city)
+ {
+ this.city = city;
+ }
+
+ @Size(min = 3, max = 6)
+ @NotNull
+ public String getZip()
+ {
+ return zip;
+ }
+
+ public void setZip(final String zip)
+ {
+ this.zip = zip;
+ }
+
+ @Size(min = 2, max = 10)
+ public String getState()
+ {
+ return state;
+ }
+
+ public void setState(final String state)
+ {
+ this.state = state;
+ }
+
+ @Size(min = 2, max = 40)
+ @NotNull
+ public String getCountry()
+ {
+ return country;
+ }
+
+ public void setCountry(final String country)
+ {
+ this.country = country;
+ }
+
+ @Min(1)
+ @Max(5)
+ public Integer getStars()
+ {
+ return stars;
+ }
+
+ public void setStars(final Integer stars)
+ {
+ this.stars = stars;
+ }
+
+ @Column(precision = 6, scale = 2)
+ public BigDecimal getPrice()
+ {
+ return price;
+ }
+
+ public void setPrice(final BigDecimal price)
+ {
+ this.price = price;
+ }
+
+ @Transient
+ public String getLocation()
+ {
+ return city + ", " + state + ", " + country;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "Hotel(" + name + "," + address + "," + city +
"," + zip + ")";
+ }
+}
Modified: modules/persistence/trunk/impl/src/test/resources/META-INF/persistence.xml
===================================================================
--- modules/persistence/trunk/impl/src/test/resources/META-INF/persistence.xml 2010-07-19
11:45:15 UTC (rev 13443)
+++ modules/persistence/trunk/impl/src/test/resources/META-INF/persistence.xml 2010-07-19
22:31:04 UTC (rev 13444)
@@ -9,7 +9,7 @@
-->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
- <class>org.jboss.seam.transactions.test.Hotel</class>
+ <class>org.jboss.seam.transactions.test.util.Hotel</class>
<exclude-unlisted-classes/>
<properties>
<!-- Properties for Hibernate (default provider for JBoss AS) -->