KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > iiop > client > IIOPClientUserTransactionObjectFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.tm.iiop.client;
23
24 import java.util.Hashtable JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.Name JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.naming.spi.ObjectFactory JavaDoc;
31 import javax.transaction.UserTransaction JavaDoc;
32
33 import org.omg.CORBA.BAD_PARAM JavaDoc;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.tm.iiop.TransactionFactoryExtHelper;
37 import org.jboss.tm.usertx.client.ServerVMClientUserTransaction;
38      
39 /**
40  * This is an object factory for producing client-side UserTransactions
41  * for standalone RMI/IIOP clients.
42  *
43  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
44  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
45  * @version $Revision: 37459 $
46  */

47 public class IIOPClientUserTransactionObjectFactory
48       implements ObjectFactory JavaDoc
49 {
50    private static final Logger log =
51       Logger.getLogger(IIOPClientUserTransactionObjectFactory.class);
52    private static final boolean traceEnabled = log.isTraceEnabled();
53
54    /**
55     * The <code>UserTransaction</code> this factory will return.
56     * This is evaluated lazily in {@link #getUserTransaction()}.
57     */

58    private static UserTransaction JavaDoc userTransaction = null;
59
60    /**
61     * Get the <code>UserTransaction</code> this factory will return.
62     * This may return a cached value from a previous call.
63     */

64    private static UserTransaction JavaDoc getUserTransaction()
65    {
66       if (userTransaction == null)
67       {
68          // See if we have a local TM
69
try
70          {
71             new InitialContext JavaDoc().lookup("java:/TransactionManager");
72             
73             // We execute in the server.
74
userTransaction = ServerVMClientUserTransaction.getSingleton();
75          }
76          catch (NamingException JavaDoc ex)
77          {
78             // We execute in a stand-alone client.
79
userTransaction = IIOPClientUserTransaction.getSingleton();
80          }
81       }
82       return userTransaction;
83    }
84    
85    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name,
86                                    Context JavaDoc nameCtx, Hashtable JavaDoc environment)
87       throws Exception JavaDoc
88    {
89
90       if (traceEnabled)
91          log.trace("getObjectInstance: obj=" + obj +
92                    "\n name=" + name +
93                    "\n nameCtx= " + nameCtx);
94       
95       if (!name.toString().equals("UserTransaction"))
96          return null;
97       try
98       {
99          TransactionFactoryExtHelper.narrow(obj);
100       }
101       catch (BAD_PARAM JavaDoc e)
102       {
103          return null;
104       }
105       return getUserTransaction();
106    }
107 }
108
109
Popular Tags