KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > usertx > server > ClientUserTransactionService


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.server;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.Context JavaDoc;
34
35 import org.jboss.system.ServiceMBeanSupport;
36 import org.jboss.tm.usertx.client.ClientUserTransaction;
37 import org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory;
38 import org.jboss.tm.usertx.interfaces.UserTransactionSession;
39 import org.jboss.invocation.Invocation;
40 import org.jboss.invocation.MarshalledInvocation;
41
42 /**
43  * This is a JMX service handling the serverside of UserTransaction
44  * usage for standalone clients.
45  *
46  * @author <a HREF="mailto:osh@sparre.dk">Ole Husgaard</a>
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 37459 $
49  */

50 public class ClientUserTransactionService
51       extends ServiceMBeanSupport
52       implements ClientUserTransactionServiceMBean
53 {
54    // Constants -----------------------------------------------------
55
/** The location where the javax.transaction.UserTransaction is bound on the server */
56    public static String JavaDoc JNDI_NAME = "UserTransaction";
57
58    // Attributes ----------------------------------------------------
59
/** The UserTransactionSession and UserTransactionSessionFactory method hashes */
60    private Map JavaDoc marshalledInvocationMapping = new HashMap JavaDoc();
61    /** The proxy factory service used for the UserTransactionSession */
62    private ObjectName JavaDoc txProxyName;
63    /** The stateless proxy used for all UserTransactionSessions */
64    private Object JavaDoc txProxy;
65
66    /** Set the name of the proxy factory service used for the
67     * UserTransactionSession
68     * @param proxyName
69     */

70    public void setTxProxyName(ObjectName JavaDoc proxyName)
71    {
72       this.txProxyName = proxyName;
73    }
74
75    /** Expose UserTransactionSession and UserTransactionSessionFactory
76     * interfaces via JMX to invokers.
77     *
78     * @jmx:managed-operation
79     *
80     * @param invocation A pointer to the invocation object
81     * @return Return value of method invocation.
82     *
83     * @throws Exception Failed to invoke method.
84     */

85    public Object JavaDoc invoke(Invocation invocation) throws Exception JavaDoc
86    {
87       // Set the method hash to Method mapping
88
if (invocation instanceof MarshalledInvocation)
89       {
90          MarshalledInvocation mi = (MarshalledInvocation) invocation;
91          mi.setMethodMap(marshalledInvocationMapping);
92       }
93       // Invoke the method via reflection
94
Method JavaDoc method = invocation.getMethod();
95       Object JavaDoc[] args = invocation.getArguments();
96       Object JavaDoc value = null;
97       try
98       {
99          if( UserTransactionSessionFactory.class.isAssignableFrom(method.getDeclaringClass()) )
100          {
101             // Just return the UserTransactionSession proxy as its stateless
102
value = txProxy;
103          }
104          else if( method.getName().equals("begin") )
105          {
106             // Begin a new transaction
107
Integer JavaDoc timeout = (Integer JavaDoc) args[0];
108             UserTransactionSession session = UserTransactionSessionImpl.getInstance();
109             value = session.begin(timeout.intValue());
110          }
111          else if( method.getName().equals("destroy"))
112          {
113             /* We do nothing as the tx will timeout and the tx map is shared
114             across all sessions as we have no association with the txs
115             a given client has started.
116             */

117          }
118          else
119          {
120             UserTransactionSession session = UserTransactionSessionImpl.getInstance();
121             value = method.invoke(session, args);
122          }
123       }
124       catch(InvocationTargetException JavaDoc e)
125       {
126          Throwable JavaDoc t = e.getTargetException();
127          if( t instanceof Exception JavaDoc )
128             throw (Exception JavaDoc) t;
129          else
130             throw new UndeclaredThrowableException JavaDoc(t, method.toString());
131       }
132
133       return value;
134    }
135
136    // ServiceMBeanSupport overrides ---------------------------------
137

138    protected void startService()
139          throws Exception JavaDoc
140    {
141       Context JavaDoc ctx = new InitialContext JavaDoc();
142       // Bind the in VM UserTransaction interface
143
ctx.bind(JNDI_NAME, ClientUserTransaction.getSingleton());
144
145       // Get the UserTransactionSession proxy
146
txProxy = getServer().getAttribute(txProxyName, "Proxy");
147
148       // Build the UserTransactionSession interface method map
149
HashMap JavaDoc tmpMap = new HashMap JavaDoc(13);
150       Method JavaDoc[] methods = UserTransactionSession.class.getMethods();
151       for(int m = 0; m < methods.length; m ++)
152       {
153          Method JavaDoc method = methods[m];
154          Long JavaDoc hash = new Long JavaDoc(MarshalledInvocation.calculateHash(method));
155          tmpMap.put(hash, method);
156       }
157       // Add the UserTransactionSessionFactory interface method map
158
methods = UserTransactionSessionFactory.class.getMethods();
159       for(int m = 0; m < methods.length; m ++)
160       {
161          Method JavaDoc method = methods[m];
162          Long JavaDoc hash = new Long JavaDoc(MarshalledInvocation.calculateHash(method));
163          tmpMap.put(hash, method);
164       }
165       marshalledInvocationMapping = Collections.unmodifiableMap(tmpMap);
166    }
167
168    protected void stopService()
169    {
170       try
171       {
172          Context JavaDoc ctx = new InitialContext JavaDoc();
173          ctx.unbind(JNDI_NAME);
174       }
175       catch (Exception JavaDoc e)
176       {
177          log.warn("Failed to unbind "+JNDI_NAME, e);
178       }
179    }
180
181 }
182
Popular Tags