[seam-commits] Seam SVN: r13433 - in modules/persistence/trunk: impl/src/main/java/org/jboss/seam/persistence/transaction and 2 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sun Jul 18 06:53:55 EDT 2010


Author: swd847
Date: 2010-07-18 06:53:55 -0400 (Sun, 18 Jul 2010)
New Revision: 13433

Added:
   modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/TransactionScoped.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/ContextualIdentifierStore.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeContext.java
   modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeExtension.java
Modified:
   modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/UserTransaction.java
   modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
Log:
first attempt at @TransactionScoped



Added: modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/TransactionScoped.java
===================================================================
--- modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/TransactionScoped.java	                        (rev 0)
+++ modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/TransactionScoped.java	2010-07-18 10:53:55 UTC (rev 13433)
@@ -0,0 +1,51 @@
+/*
+ * 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.transaction;
+
+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.NormalScope;
+import javax.enterprise.util.AnnotationLiteral;
+
+/**
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+ at Documented
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target( { ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
+ at NormalScope(passivating = false)
+public @interface TransactionScoped
+{
+   public static final TransactionScoped LITERAL = new TransactionScopedLiteral();
+
+   static class TransactionScopedLiteral extends AnnotationLiteral<TransactionScoped> implements TransactionScoped
+   {
+
+   }
+
+}

Modified: modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/UserTransaction.java
===================================================================
--- modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/UserTransaction.java	2010-07-18 10:39:12 UTC (rev 13432)
+++ modules/persistence/trunk/api/src/main/java/org/jboss/seam/persistence/transaction/UserTransaction.java	2010-07-18 10:53:55 UTC (rev 13433)
@@ -29,6 +29,7 @@
  * Extends the standard UserTransaction interface with a couple of helpful
  * methods.
  * 
+ * 
  * @author Gavin King
  * 
  */

Added: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/ContextualIdentifierStore.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/ContextualIdentifierStore.java	                        (rev 0)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/ContextualIdentifierStore.java	2010-07-18 10:53:55 UTC (rev 13433)
@@ -0,0 +1,81 @@
+/*
+ * 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.transaction.scope;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.enterprise.context.spi.Contextual;
+import javax.enterprise.inject.spi.PassivationCapable;
+
+/**
+ * Class that maps a Contextual to an identifier. If the Contextual is
+ * {@link PassivationCapable} then the id used, otherwise one is generated
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class ContextualIdentifierStore
+{
+   private final Map<Contextual<?>, String> identifiers = new ConcurrentHashMap<Contextual<?>, String>();
+
+   private final Map<String, Contextual<?>> contextualForIdentifier = new ConcurrentHashMap<String, Contextual<?>>();
+
+   private int count = 0;
+
+   private final String PREFIX = "CID-";
+
+   public Contextual<?> getContextual(String id)
+   {
+      return contextualForIdentifier.get(id);
+   }
+
+   public String getId(Contextual<?> contextual)
+   {
+      if (identifiers.containsKey(contextual))
+      {
+         return identifiers.get(contextual);
+      }
+      if (contextual instanceof PassivationCapable)
+      {
+         PassivationCapable p = (PassivationCapable) contextual;
+         String id = p.getId();
+         contextualForIdentifier.put(id, contextual);
+         return id;
+      }
+      else
+      {
+         synchronized (this)
+         {
+            // check again inside the syncronized block
+            if (identifiers.containsKey(contextual))
+            {
+               return identifiers.get(contextual);
+            }
+            String id = PREFIX + getClass().getName() + "-" + (count++);
+            identifiers.put(contextual, id);
+            contextualForIdentifier.put(id, contextual);
+            return id;
+         }
+      }
+   }
+}

Added: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeContext.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeContext.java	                        (rev 0)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeContext.java	2010-07-18 10:53:55 UTC (rev 13433)
@@ -0,0 +1,143 @@
+/*
+ * 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.transaction.scope;
+
+import java.lang.annotation.Annotation;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+
+import javax.enterprise.context.spi.Context;
+import javax.enterprise.context.spi.Contextual;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+
+import org.jboss.seam.persistence.transaction.TransactionScoped;
+import org.jboss.seam.persistence.transaction.UserTransaction;
+import org.jboss.weld.extensions.literal.DefaultLiteral;
+
+/**
+ * Context for the {@link TransactionScoped} scope
+ * 
+ * @author stuart
+ * 
+ */
+public class TransactionScopeContext implements Context, Synchronization
+{
+
+   private final UserTransaction userTransaction;
+
+   private final BeanManager beanManager;
+
+   private final ContextualIdentifierStore identifierStore = new ContextualIdentifierStore();
+
+   public TransactionScopeContext(BeanManager beanManager)
+   {
+      this.beanManager = beanManager;
+      Set<Bean<?>> beans = beanManager.getBeans(UserTransaction.class, new DefaultLiteral());
+      Bean<UserTransaction> userTransactionBean = (Bean<UserTransaction>) beans.iterator().next();
+      CreationalContext<?> ctx = beanManager.createCreationalContext(userTransactionBean);
+      userTransaction = (UserTransaction) beanManager.getReference(userTransactionBean, UserTransaction.class, ctx);
+      userTransaction.registerSynchronization(this);
+
+   }
+
+   private final ThreadLocal<Map<String, Object>> instanceStore = new ThreadLocal<Map<String, Object>>()
+   {
+      protected Map<String, Object> initialValue()
+      {
+
+         return new ConcurrentHashMap<String, Object>();
+      };
+   };
+
+   private final ThreadLocal<Map<String, CreationalContext<?>>> creationalContextStore = new ThreadLocal<Map<String, CreationalContext<?>>>()
+   {
+      protected Map<String, CreationalContext<?>> initialValue()
+      {
+
+         return new ConcurrentHashMap<String, CreationalContext<?>>();
+      };
+   };
+
+   public <T> T get(Contextual<T> contextual)
+   {
+      String id = identifierStore.getId(contextual);
+      Map<String, Object> map = instanceStore.get();
+      return (T) map.get(id);
+   }
+
+   public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext)
+   {
+      String id = identifierStore.getId(contextual);
+      Map<String, Object> map = instanceStore.get();
+      T instance = (T) map.get(id);
+      if (instance == null)
+      {
+         instance = contextual.create(creationalContext);
+         map.put(id, instance);
+      }
+      return instance;
+   }
+
+   public Class<? extends Annotation> getScope()
+   {
+      return TransactionScoped.class;
+   }
+
+   public boolean isActive()
+   {
+      try
+      {
+         return userTransaction.isActive();
+      }
+      catch (SystemException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public void afterCompletion(int status)
+   {
+      Map<String, Object> map = instanceStore.get();
+      Map<String, CreationalContext<?>> creationalContexts = creationalContextStore.get();
+      for (Entry<String, Object> e : map.entrySet())
+      {
+         Contextual contextual = identifierStore.getContextual(e.getKey());
+         CreationalContext<?> ctx = creationalContexts.get(e.getKey());
+         contextual.destroy(e.getValue(), ctx);
+         ctx.release();
+      }
+      instanceStore.remove();
+      creationalContextStore.remove();
+   }
+
+   public void beforeCompletion()
+   {
+
+   }
+
+}

Added: modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeExtension.java
===================================================================
--- modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeExtension.java	                        (rev 0)
+++ modules/persistence/trunk/impl/src/main/java/org/jboss/seam/persistence/transaction/scope/TransactionScopeExtension.java	2010-07-18 10:53:55 UTC (rev 13433)
@@ -0,0 +1,43 @@
+/*
+ * 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.transaction.scope;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterBeanDiscovery;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+
+import org.jboss.seam.persistence.transaction.TransactionScoped;
+
+/**
+ * Extension that registers the context for {@link TransactionScoped} beans
+ * 
+ * @author Stuart Douglas
+ * 
+ */
+public class TransactionScopeExtension implements Extension
+{
+   public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager)
+   {
+      event.addContext(new TransactionScopeContext(manager));
+   }
+}

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-18 10:39:12 UTC (rev 13432)
+++ modules/persistence/trunk/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension	2010-07-18 10:53:55 UTC (rev 13433)
@@ -1,2 +1,3 @@
 org.jboss.seam.persistence.PersistenceContextExtension
-org.jboss.seam.transaction.TransactionExtension
\ No newline at end of file
+org.jboss.seam.transaction.TransactionExtension
+org.jboss.seam.persistence.transaction.scope.TransactionScopeExtension
\ No newline at end of file



More information about the seam-commits mailing list