View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.jndi;
15  
16  import javax.naming.Context;
17  import javax.naming.InitialContext;
18  import javax.naming.LinkRef;
19  import javax.naming.NameNotFoundException;
20  import javax.naming.NamingException;
21  import javax.transaction.UserTransaction;
22  
23  import org.eclipse.jetty.jndi.NamingUtil;
24  import org.eclipse.jetty.util.log.Logger;
25  
26  /**
27   * Transaction
28   *
29   * Class to represent a JTA UserTransaction impl.
30   */
31  public class Transaction extends NamingEntry
32  {
33      private static Logger __log = NamingUtil.__log;
34      public static final String USER_TRANSACTION = "UserTransaction";
35      
36  
37      public static void bindToENC ()
38      throws NamingException
39      {
40          Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
41  
42          if ( txEntry != null )
43          {
44              txEntry.bindToComp();
45          }
46          else
47          {
48              throw new NameNotFoundException( USER_TRANSACTION + " not found" );
49          }
50      }
51      
52      public Transaction (UserTransaction userTransaction)
53      throws NamingException
54      {
55          super (USER_TRANSACTION);
56          save(userTransaction);
57      }
58      
59      
60      /** 
61       * Allow other bindings of UserTransaction.
62       * 
63       * These should be in ADDITION to java:comp/UserTransaction
64       * @see NamingEntry#bindToENC(java.lang.String)
65       */
66      public void bindToENC (String localName)
67      throws NamingException
68      {   
69          InitialContext ic = new InitialContext();
70          Context env = (Context)ic.lookup("java:comp/env");
71          __log.debug("Binding java:comp/env"+getJndiName()+" to "+_objectNameString);
72          NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
73      }
74      
75      /**
76       * Insist on the java:comp/UserTransaction binding
77       * @throws NamingException
78       */
79      private void bindToComp ()
80      throws NamingException
81      {   
82          //ignore the name, it is always bound to java:comp
83          InitialContext ic = new InitialContext();
84          Context env = (Context)ic.lookup("java:comp");
85          __log.debug("Binding java:comp/"+getJndiName()+" to "+_objectNameString);
86          NamingUtil.bind(env, getJndiName(), new LinkRef(_objectNameString));
87      }
88      
89      /**
90       * Unbind this Transaction from a java:comp
91       */
92      public void unbindENC ()
93      {
94          try
95          {
96              InitialContext ic = new InitialContext();
97              Context env = (Context)ic.lookup("java:comp");
98              __log.debug("Unbinding java:comp/"+getJndiName());
99              env.unbind(getJndiName());
100         }
101         catch (NamingException e)
102         {
103             __log.warn(e);
104         }
105     }
106 }