Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.handlers » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.concurrent.impl.handlers;
   18   
   19   import java.util.ArrayList;
   20   import java.util.Iterator;
   21   import java.util.List;
   22   
   23   import javax.naming.Binding;
   24   import javax.naming.Context;
   25   import javax.naming.Name;
   26   import javax.naming.NameClassPair;
   27   import javax.naming.NamingEnumeration;
   28   import javax.naming.NamingException;
   29   import javax.naming.NotContextException;
   30   import javax.transaction.UserTransaction;
   31   
   32   import org.apache.geronimo.transaction.GeronimoUserTransaction;
   33   import org.apache.xbean.naming.context.ContextFlyweight;
   34   import org.apache.xbean.naming.context.ContextUtil;
   35   import org.apache.xbean.naming.reference.SimpleReference;
   36   
   37   /**
   38    * A wrapper around an existing java:comp/ context that exposes
   39    * UserTransaction entry. 
   40    */
   41   public class UserTransactionContext extends ContextFlyweight {
   42   
   43       private static final String USER_TRANSACTION = "UserTransaction";
   44       
   45       private Context componentContext;
   46   
   47       public UserTransactionContext(Context componentContext) {
   48           this.componentContext = componentContext;
   49       }
   50       
   51       public static boolean hasUserTransaction(Context componentContext) {  
   52           UserTransaction userTransaction = getUserTransaction(componentContext);
   53           return (userTransaction != null);
   54       }
   55       
   56       public static UserTransaction getUserTransaction(Context componentContext) {
   57           try {
   58               return (UserTransaction)componentContext.lookup(USER_TRANSACTION);
   59           } catch (NamingException e) {
   60               return null;
   61           } 
   62       }
   63       
   64       protected Context getContext() throws NamingException {
   65           return this.componentContext;
   66       }
   67      
   68       public Object lookup(Name name) throws NamingException {
   69           if (name != null && name.size() == 1 && name.get(0).equals(USER_TRANSACTION)) {
   70               return getUserTransaction();
   71           } else {
   72               return super.lookup(name);
   73           }
   74       }
   75   
   76       public Object lookup(String name) throws NamingException {
   77           if (name != null && name.equals(USER_TRANSACTION)) {
   78               return getUserTransaction();
   79           } else {
   80               return super.lookup(name);
   81           }
   82       }
   83       
   84       public NamingEnumeration list(String name) throws NamingException {
   85           if (name == null) {
   86               throw new NullPointerException("name is null");
   87           }
   88           return list(getNameParser(name).parse(name));
   89       }
   90       
   91       public NamingEnumeration list(Name name) throws NamingException {
   92           if (name == null) {
   93               throw new NullPointerException("name is null");
   94           }
   95          
   96           if (name.isEmpty()) {
   97               return list();
   98           }
   99           
  100           Object target = null;
  101           try {
  102               target = super.lookup(name);
  103           } catch (NamingException e) {
  104               throw new NotContextException(name.toString());
  105           }
  106   
  107           if (target == this) {
  108               return list();
  109           } else if (target instanceof Context) {
  110               return ((Context) target).list("");
  111           } else {
  112               throw new NotContextException("The name " + name + " cannot be listed");
  113           }
  114       }
  115       
  116       public NamingEnumeration listBindings(String name) throws NamingException {
  117           if (name == null) {
  118               throw new NullPointerException("name is null");
  119           }
  120           return listBindings(getNameParser(name).parse(name));
  121       }
  122       
  123       public NamingEnumeration listBindings(Name name) throws NamingException {
  124           if (name == null) {
  125               throw new NullPointerException("name is null");
  126           }
  127           
  128           if (name.isEmpty()) {
  129               return listBindings();
  130           }
  131           
  132           Object target = null;
  133           try {
  134               target = super.lookup(name);
  135           } catch (NamingException e) {
  136               throw new NotContextException(name.toString());
  137           }
  138   
  139           if (target == this) {
  140               return listBindings();
  141           } else if (target instanceof Context) {
  142               return ((Context) target).listBindings("");
  143           } else {
  144               throw new NotContextException("The name " + name + " cannot be listed");
  145           }
  146       }
  147           
  148       private NamingEnumeration listBindings() throws NamingException {
  149           NamingEnumeration e = super.listBindings("");
  150           List<Binding> list = new ArrayList<Binding>();
  151           while(e.hasMore()) {
  152               Binding binding = (Binding)e.next();
  153               list.add(binding);
  154           }
  155           list.add(new ContextUtil.ReadOnlyBinding(USER_TRANSACTION, new UserTransactionReference(), this));
  156           return new ListNamingEnumeration(list.iterator());
  157       }
  158       
  159       private NamingEnumeration list() throws NamingException {
  160           NamingEnumeration e = super.list("");
  161           List<NameClassPair> list = new ArrayList<NameClassPair>();
  162           while(e.hasMore()) {
  163               NameClassPair nameClassPair = (NameClassPair)e.next();
  164               list.add(nameClassPair);
  165           }
  166           list.add(new NameClassPair(USER_TRANSACTION, UserTransaction.class.getName()));
  167           return new ListNamingEnumeration(list.iterator());
  168       }
  169       
  170       private Object getUserTransaction() throws NamingException {
  171           return new GeronimoUserTransaction(TransactionContextHandler.getTransactionManager());
  172       }
  173       
  174       private class UserTransactionReference extends SimpleReference {
  175           
  176           public Object getContent() throws NamingException {
  177               return getUserTransaction();
  178           }
  179           
  180           public String getClassName() {
  181               return UserTransaction.class.getName();
  182           }
  183           
  184       }
  185       
  186       private static class ListNamingEnumeration implements NamingEnumeration {
  187   
  188           private Iterator iter;
  189           
  190           public ListNamingEnumeration(Iterator iter){
  191               this.iter = iter;
  192           }
  193           
  194           public void close() throws NamingException {
  195           }
  196   
  197           public boolean hasMore() throws NamingException {
  198               return hasMoreElements();
  199           }
  200   
  201           public Object next() throws NamingException {
  202               return nextElement();
  203           }
  204           
  205           public boolean hasMoreElements() {
  206               return this.iter.hasNext();
  207           }
  208               
  209           public Object nextElement() {
  210               return this.iter.next();
  211           }
  212           
  213       }
  214       
  215   }

Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.handlers » [javadoc | source]