You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/04/21 05:44:16 UTC

svn commit: r650013 - in /geronimo/sandbox/concurrent/geronimo-concurrent: ./ src/main/java/org/apache/geronimo/concurrent/impl/handlers/

Author: gawor
Date: Sun Apr 20 20:44:14 2008
New Revision: 650013

URL: http://svn.apache.org/viewvc?rev=650013&view=rev
Log:
make sure java:comp/UserTransaction is always available to managed tasks

Added:
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java   (with props)
Modified:
    geronimo/sandbox/concurrent/geronimo-concurrent/pom.xml
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/NamingContextHandler.java
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java

Modified: geronimo/sandbox/concurrent/geronimo-concurrent/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/pom.xml?rev=650013&r1=650012&r2=650013&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/pom.xml (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/pom.xml Sun Apr 20 20:44:14 2008
@@ -58,6 +58,11 @@
             <artifactId>geronimo-security</artifactId>
             <version>${version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo.components</groupId>
+            <artifactId>geronimo-transaction</artifactId>
+        </dependency>
    </dependencies>
    
 </project>

Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/NamingContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/NamingContextHandler.java?rev=650013&r1=650012&r2=650013&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/NamingContextHandler.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/NamingContextHandler.java Sun Apr 20 20:44:14 2008
@@ -41,8 +41,12 @@
     public void saveContext(Map<String, Object> context) {
         LOG.debug("saveContext");
         
-        context.put(NEW_CONTEXT,
-                    RootContext.getComponentContext());
+        Context componentContext = RootContext.getComponentContext();
+        if (!UserTransactionContext.hasUserTransaction(componentContext)) {
+            componentContext = new UserTransactionContext(componentContext);
+            LOG.debug("java:comp/UserTransaction not found. Using UserTransactionContext");
+        }
+        context.put(NEW_CONTEXT, componentContext);
     }
 
     public void setContext(Map<String, Object> threadContext) {

Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java?rev=650013&r1=650012&r2=650013&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java Sun Apr 20 20:44:14 2008
@@ -31,22 +31,18 @@
  */
 public class TransactionContextHandler extends org.apache.geronimo.concurrent.handlers.TransactionContextHandler {
 
-    private TransactionManager transactionManager;
-    
-    public void saveContext(Map<String, Object> context) {
-        context.put(TRANSACTION_MANAGER, getTransactionManager());
-    }
-    
-    private TransactionManager getTransactionManager() {
-        if (this.transactionManager == null) {
+    private static TransactionManager transactionManager;
+        
+    public static TransactionManager getTransactionManager() {
+        if (transactionManager == null) {
             try {
                 Kernel kernel = KernelRegistry.getSingleKernel();
                 AbstractNameQuery query = new AbstractNameQuery(TransactionManager.class.getName());
                 Set gbeans = kernel.listGBeans(query);
                 if (gbeans != null && !gbeans.isEmpty()) {
                     AbstractName name = (AbstractName)gbeans.iterator().next();
-                    this.transactionManager = (TransactionManager)kernel.getGBean(name); 
-                    LOG.debug("TransactionManager: " + this.transactionManager);
+                    transactionManager = (TransactionManager)kernel.getGBean(name); 
+                    LOG.debug("TransactionManager: " + transactionManager);
                 } else {
                     LOG.debug("TransactionManager not found");
                 }
@@ -54,7 +50,11 @@
                 LOG.warn("Failed to find TransactionManager", e);
             }
         }
-        return this.transactionManager;
+        return transactionManager;
     }
    
+    public void saveContext(Map<String, Object> context) {
+        context.put(TRANSACTION_MANAGER, getTransactionManager());
+    }
+    
 }

Added: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java?rev=650013&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java Sun Apr 20 20:44:14 2008
@@ -0,0 +1,215 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.concurrent.impl.handlers;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.NotContextException;
+import javax.transaction.UserTransaction;
+
+import org.apache.geronimo.transaction.GeronimoUserTransaction;
+import org.apache.xbean.naming.context.ContextFlyweight;
+import org.apache.xbean.naming.context.ContextUtil;
+import org.apache.xbean.naming.reference.SimpleReference;
+
+/**
+ * A wrapper around an existing java:comp/ context that exposes
+ * UserTransaction entry. 
+ */
+public class UserTransactionContext extends ContextFlyweight {
+
+    private static final String USER_TRANSACTION = "UserTransaction";
+    
+    private Context componentContext;
+
+    public UserTransactionContext(Context componentContext) {
+        this.componentContext = componentContext;
+    }
+    
+    public static boolean hasUserTransaction(Context componentContext) {  
+        UserTransaction userTransaction = getUserTransaction(componentContext);
+        return (userTransaction != null);
+    }
+    
+    public static UserTransaction getUserTransaction(Context componentContext) {
+        try {
+            return (UserTransaction)componentContext.lookup(USER_TRANSACTION);
+        } catch (NamingException e) {
+            return null;
+        } 
+    }
+    
+    protected Context getContext() throws NamingException {
+        return this.componentContext;
+    }
+   
+    public Object lookup(Name name) throws NamingException {
+        if (name != null && name.size() == 1 && name.get(0).equals(USER_TRANSACTION)) {
+            return getUserTransaction();
+        } else {
+            return super.lookup(name);
+        }
+    }
+
+    public Object lookup(String name) throws NamingException {
+        if (name != null && name.equals(USER_TRANSACTION)) {
+            return getUserTransaction();
+        } else {
+            return super.lookup(name);
+        }
+    }
+    
+    public NamingEnumeration list(String name) throws NamingException {
+        if (name == null) {
+            throw new NullPointerException("name is null");
+        }
+        return list(getNameParser(name).parse(name));
+    }
+    
+    public NamingEnumeration list(Name name) throws NamingException {
+        if (name == null) {
+            throw new NullPointerException("name is null");
+        }
+       
+        if (name.isEmpty()) {
+            return list();
+        }
+        
+        Object target = null;
+        try {
+            target = super.lookup(name);
+        } catch (NamingException e) {
+            throw new NotContextException(name.toString());
+        }
+
+        if (target == this) {
+            return list();
+        } else if (target instanceof Context) {
+            return ((Context) target).list("");
+        } else {
+            throw new NotContextException("The name " + name + " cannot be listed");
+        }
+    }
+    
+    public NamingEnumeration listBindings(String name) throws NamingException {
+        if (name == null) {
+            throw new NullPointerException("name is null");
+        }
+        return listBindings(getNameParser(name).parse(name));
+    }
+    
+    public NamingEnumeration listBindings(Name name) throws NamingException {
+        if (name == null) {
+            throw new NullPointerException("name is null");
+        }
+        
+        if (name.isEmpty()) {
+            return listBindings();
+        }
+        
+        Object target = null;
+        try {
+            target = super.lookup(name);
+        } catch (NamingException e) {
+            throw new NotContextException(name.toString());
+        }
+
+        if (target == this) {
+            return listBindings();
+        } else if (target instanceof Context) {
+            return ((Context) target).listBindings("");
+        } else {
+            throw new NotContextException("The name " + name + " cannot be listed");
+        }
+    }
+        
+    private NamingEnumeration listBindings() throws NamingException {
+        NamingEnumeration e = super.listBindings("");
+        List<Binding> list = new ArrayList<Binding>();
+        while(e.hasMore()) {
+            Binding binding = (Binding)e.next();
+            list.add(binding);
+        }
+        list.add(new ContextUtil.ReadOnlyBinding(USER_TRANSACTION, new UserTransactionReference()));
+        return new ListNamingEnumeration(list.iterator());
+    }
+    
+    private NamingEnumeration list() throws NamingException {
+        NamingEnumeration e = super.list("");
+        List<NameClassPair> list = new ArrayList<NameClassPair>();
+        while(e.hasMore()) {
+            NameClassPair nameClassPair = (NameClassPair)e.next();
+            list.add(nameClassPair);
+        }
+        list.add(new NameClassPair(USER_TRANSACTION, UserTransaction.class.getName()));
+        return new ListNamingEnumeration(list.iterator());
+    }
+    
+    private Object getUserTransaction() throws NamingException {
+        return new GeronimoUserTransaction(TransactionContextHandler.getTransactionManager());
+    }
+    
+    private class UserTransactionReference extends SimpleReference {
+        
+        public Object getContent() throws NamingException {
+            return getUserTransaction();
+        }
+        
+        public String getClassName() {
+            return UserTransaction.class.getName();
+        }
+        
+    }
+    
+    private static class ListNamingEnumeration implements NamingEnumeration {
+
+        private Iterator iter;
+        
+        public ListNamingEnumeration(Iterator iter){
+            this.iter = iter;
+        }
+        
+        public void close() throws NamingException {
+        }
+
+        public boolean hasMore() throws NamingException {
+            return hasMoreElements();
+        }
+
+        public Object next() throws NamingException {
+            return nextElement();
+        }
+        
+        public boolean hasMoreElements() {
+            return this.iter.hasNext();
+        }
+            
+        public Object nextElement() {
+            return this.iter.next();
+        }
+        
+    }
+    
+}

Propchange: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/UserTransactionContext.java
------------------------------------------------------------------------------
    svn:eol-style = native