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.Log;
25  
26  /**
27   * Transaction
28   *
29   * Class to represent a JTA UserTransaction impl.
30   */
31  public class Transaction extends NamingEntry
32  {
33      public static final String USER_TRANSACTION = "UserTransaction";
34      
35  
36      public static void bindToENC ()
37      throws NamingException
38      {
39          Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
40  
41          if ( txEntry != null )
42          {
43              txEntry.bindToComp();
44          }
45          else
46          {
47              throw new NameNotFoundException( USER_TRANSACTION + " not found" );
48          }
49      }
50   
51      
52      
53      public Transaction (UserTransaction userTransaction)
54      throws NamingException
55      {
56          super (USER_TRANSACTION, 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 }