1 22 package org.jboss.tm.usertx.server; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.UndeclaredThrowableException ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 import java.util.Collections ; 30 31 import javax.management.ObjectName ; 32 import javax.naming.InitialContext ; 33 import javax.naming.Context ; 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 50 public class ClientUserTransactionService 51 extends ServiceMBeanSupport 52 implements ClientUserTransactionServiceMBean 53 { 54 56 public static String JNDI_NAME = "UserTransaction"; 57 58 60 private Map marshalledInvocationMapping = new HashMap (); 61 62 private ObjectName txProxyName; 63 64 private Object txProxy; 65 66 70 public void setTxProxyName(ObjectName proxyName) 71 { 72 this.txProxyName = proxyName; 73 } 74 75 85 public Object invoke(Invocation invocation) throws Exception 86 { 87 if (invocation instanceof MarshalledInvocation) 89 { 90 MarshalledInvocation mi = (MarshalledInvocation) invocation; 91 mi.setMethodMap(marshalledInvocationMapping); 92 } 93 Method method = invocation.getMethod(); 95 Object [] args = invocation.getArguments(); 96 Object value = null; 97 try 98 { 99 if( UserTransactionSessionFactory.class.isAssignableFrom(method.getDeclaringClass()) ) 100 { 101 value = txProxy; 103 } 104 else if( method.getName().equals("begin") ) 105 { 106 Integer timeout = (Integer ) args[0]; 108 UserTransactionSession session = UserTransactionSessionImpl.getInstance(); 109 value = session.begin(timeout.intValue()); 110 } 111 else if( method.getName().equals("destroy")) 112 { 113 117 } 118 else 119 { 120 UserTransactionSession session = UserTransactionSessionImpl.getInstance(); 121 value = method.invoke(session, args); 122 } 123 } 124 catch(InvocationTargetException e) 125 { 126 Throwable t = e.getTargetException(); 127 if( t instanceof Exception ) 128 throw (Exception ) t; 129 else 130 throw new UndeclaredThrowableException (t, method.toString()); 131 } 132 133 return value; 134 } 135 136 138 protected void startService() 139 throws Exception 140 { 141 Context ctx = new InitialContext (); 142 ctx.bind(JNDI_NAME, ClientUserTransaction.getSingleton()); 144 145 txProxy = getServer().getAttribute(txProxyName, "Proxy"); 147 148 HashMap tmpMap = new HashMap (13); 150 Method [] methods = UserTransactionSession.class.getMethods(); 151 for(int m = 0; m < methods.length; m ++) 152 { 153 Method method = methods[m]; 154 Long hash = new Long (MarshalledInvocation.calculateHash(method)); 155 tmpMap.put(hash, method); 156 } 157 methods = UserTransactionSessionFactory.class.getMethods(); 159 for(int m = 0; m < methods.length; m ++) 160 { 161 Method method = methods[m]; 162 Long hash = new Long (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 ctx = new InitialContext (); 173 ctx.unbind(JNDI_NAME); 174 } 175 catch (Exception e) 176 { 177 log.warn("Failed to unbind "+JNDI_NAME, e); 178 } 179 } 180 181 } 182 | Popular Tags |