View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.jndi;
20  
21  import javax.naming.Context;
22  import javax.naming.InitialContext;
23  import javax.naming.LinkRef;
24  import javax.naming.NameNotFoundException;
25  import javax.naming.NamingException;
26  import javax.transaction.UserTransaction;
27  
28  import org.eclipse.jetty.jndi.NamingUtil;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  /**
32   * Transaction
33   *
34   * Class to represent a JTA UserTransaction impl.
35   */
36  public class Transaction extends NamingEntry
37  {
38      private static Logger __log = NamingUtil.__log;
39      public static final String USER_TRANSACTION = "UserTransaction";
40      
41  
42      public static void bindToENC ()
43      throws NamingException
44      {
45          Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
46  
47          if ( txEntry != null )
48          {
49              txEntry.bindToComp();
50          }
51          else
52          {
53              throw new NameNotFoundException( USER_TRANSACTION + " not found" );
54          }
55      }
56      
57      public Transaction (UserTransaction userTransaction)
58      throws NamingException
59      {
60          super (USER_TRANSACTION);
61          save(userTransaction);
62      }
63      
64      
65      /** 
66       * Allow other bindings of UserTransaction.
67       * 
68       * These should be in ADDITION to java:comp/UserTransaction
69       * @see 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 }