KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > usertx > client > ClientUserTransactionObjectFactory


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.usertx.client;
23
24 import org.jboss.tm.TransactionPropagationContextUtil;
25
26 import java.util.Hashtable JavaDoc;
27
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.naming.spi.ObjectFactory JavaDoc;
34
35 import javax.transaction.UserTransaction JavaDoc;
36
37 /**
38  * This is an object factory for producing client
39  * UserTransactions.
40  * usage for standalone clients.
41  *
42  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
43  * @version $Revision: 37459 $
44  */

45 public class ClientUserTransactionObjectFactory
46    implements ObjectFactory JavaDoc
47 {
48    /**
49     * The <code>UserTransaction</code> this factory will return.
50     * This is evaluated lazily in {@link #getUserTransaction()}.
51     */

52    static private UserTransaction JavaDoc userTransaction = null;
53
54    /**
55     * Get the <code>UserTransaction</code> this factory will return.
56     * This may return a cached value from a previous call.
57     */

58    static private UserTransaction JavaDoc getUserTransaction()
59    {
60       if (userTransaction == null) {
61          // See if we have a local TM
62
try {
63             new InitialContext JavaDoc().lookup("java:/TransactionManager");
64
65             // We execute in the server.
66
userTransaction = ServerVMClientUserTransaction.getSingleton();
67          } catch (NamingException JavaDoc ex) {
68             // We execute in a stand-alone client.
69
ClientUserTransaction cut = ClientUserTransaction.getSingleton();
70
71             // Tell the proxy that this is the factory for
72
// transaction propagation contexts.
73
TransactionPropagationContextUtil.setTPCFactory(cut);
74             userTransaction = cut;
75          }
76       }
77       return userTransaction;
78    }
79
80    public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name,
81                                    Context JavaDoc nameCtx, Hashtable JavaDoc environment)
82       throws Exception JavaDoc
83    {
84       Reference JavaDoc ref = (Reference JavaDoc)obj;
85  
86       if (!ref.getClassName().equals(ClientUserTransaction.class.getName()))
87          return null;
88
89       return getUserTransaction();
90    }
91 }
92
93
Popular Tags