View Javadoc

    1   // ========================================================================
    2   // $Id: Transaction.java 3680 2008-09-21 10:37:13Z janb $
    3   // Copyright 2006 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.jetty.plus.naming;
    17
    18  import javax.naming.*;
    19  import javax.transaction.UserTransaction;
    20
    21  import org.mortbay.log.Log;
    22  import org.mortbay.naming.NamingUtil;
    23
    24  /**
    25   * Transaction
    26   *
    27   * Class to represent a JTA UserTransaction impl.
    28   *
    29   *
    30   */
    31  /**
    32   * Transaction
    33   *
    34   *
    35   */
    36  public class Transaction extends NamingEntry
    37  {
    38      public static final String USER_TRANSACTION = "UserTransaction";
    39
    40
    41      public static void bindToENC ()
    42      throws NamingException
    43      {
    44          Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
    45
    46          if ( txEntry != null )
    47          {
    48              txEntry.bindToComp();
    49          }
    50          else
    51          {
    52              throw new NameNotFoundException( USER_TRANSACTION + " not found" );
    53          }
    54      }
    55
    56
    57
    58      public Transaction (UserTransaction userTransaction)
    59      throws NamingException
    60      {
    61          super (USER_TRANSACTION, userTransaction);
    62      }
    63
    64
    65      /**
    66       * Allow other bindings of UserTransaction.
    67       *
    68       * These should be in ADDITION to java:comp/UserTransaction
    69       * @see org.mortbay.jetty.plus.naming.NamingEntry#bindToENC(java.lang.String)
    70       */
    71      public void bindToENC (String localName)
    72      throws NamingException
    73      {
    74          InitialContext ic = new InitialContext();
    75          Context env = (Context)ic.lookup("java:comp/env");
    76          Log.debug("Binding java:comp/env"+getJndiName()+" to "+objectNameString);
    77          NamingUtil.bind(env, localName, new LinkRef(objectNameString));
    78      }
    79
    80      /**
    81       * Insist on the java:comp/UserTransaction binding
    82       * @throws NamingException
    83       */
    84      private void bindToComp ()
    85      throws NamingException
    86      {
    87          //ignore the name, it is always bound to java:comp
    88          InitialContext ic = new InitialContext();
    89          Context env = (Context)ic.lookup("java:comp");
    90          Log.debug("Binding java:comp/"+getJndiName()+" to "+objectNameString);
    91          NamingUtil.bind(env, getJndiName(), new LinkRef(objectNameString));
    92      }
    93
    94      /**
    95       * Unbind this Transaction from a java:comp
    96       */
    97      public void unbindENC ()
    98      {
    99          try
    100         {
    101             InitialContext ic = new InitialContext();
    102             Context env = (Context)ic.lookup("java:comp");
    103             Log.debug("Unbinding java:comp/"+getJndiName());
    104             env.unbind(getJndiName());
    105         }
    106         catch (NamingException e)
    107         {
    108             Log.warn(e);
    109         }
    110     }
    111 }