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   * 
32   */
33  /**
34   * Transaction
35   *
36   *
37   */
38  public class Transaction extends NamingEntry
39  {
40      public static final String USER_TRANSACTION = "UserTransaction";
41      
42  
43      public static void bindToENC ()
44      throws NamingException
45      {
46          Transaction txEntry = (Transaction)NamingEntryUtil.lookupNamingEntry(null, Transaction.USER_TRANSACTION);
47  
48          if ( txEntry != null )
49          {
50              txEntry.bindToComp();
51          }
52          else
53          {
54              throw new NameNotFoundException( USER_TRANSACTION + " not found" );
55          }
56      }
57   
58      
59      
60      public Transaction (UserTransaction userTransaction)
61      throws NamingException
62      {
63          super (USER_TRANSACTION, userTransaction);           
64      }
65      
66      
67      /** 
68       * Allow other bindings of UserTransaction.
69       * 
70       * These should be in ADDITION to java:comp/UserTransaction
71       * @see org.eclipse.jetty.server.server.plus.jndi.NamingEntry#bindToENC(java.lang.String)
72       */
73      public void bindToENC (String localName)
74      throws NamingException
75      {   
76          InitialContext ic = new InitialContext();
77          Context env = (Context)ic.lookup("java:comp/env");
78          Log.debug("Binding java:comp/env"+getJndiName()+" to "+objectNameString);
79          NamingUtil.bind(env, localName, new LinkRef(objectNameString));
80      }
81      
82      /**
83       * Insist on the java:comp/UserTransaction binding
84       * @throws NamingException
85       */
86      private void bindToComp ()
87      throws NamingException
88      {   
89          //ignore the name, it is always bound to java:comp
90          InitialContext ic = new InitialContext();
91          Context env = (Context)ic.lookup("java:comp");
92          Log.debug("Binding java:comp/"+getJndiName()+" to "+objectNameString);
93          NamingUtil.bind(env, getJndiName(), new LinkRef(objectNameString));
94      }
95      
96      /**
97       * Unbind this Transaction from a java:comp
98       */
99      public void unbindENC ()
100     {
101         try
102         {
103             InitialContext ic = new InitialContext();
104             Context env = (Context)ic.lookup("java:comp");
105             Log.debug("Unbinding java:comp/"+getJndiName());
106             env.unbind(getJndiName());
107         }
108         catch (NamingException e)
109         {
110             Log.warn(e);
111         }
112     }
113 }