1 22 package org.jboss.invocation.jrmp.interfaces; 23 24 import java.io.IOException ; 25 import java.io.Externalizable ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.rmi.ConnectException ; 29 import java.rmi.MarshalledObject ; 30 import java.rmi.NoSuchObjectException ; 31 import java.rmi.RemoteException ; 32 import java.rmi.ServerException ; 33 import java.rmi.server.RemoteObject ; 34 import java.rmi.server.RemoteStub ; 35 import javax.transaction.TransactionRolledbackException ; 36 import javax.transaction.SystemException ; 37 38 import org.jboss.invocation.Invocation; 39 import org.jboss.invocation.Invoker; 40 import org.jboss.invocation.MarshalledInvocation; 41 import org.jboss.tm.TransactionPropagationContextFactory; 42 import org.jboss.tm.TransactionPropagationContextUtil; 43 44 52 public class JRMPInvokerProxy 53 implements Invoker, Externalizable 54 { 55 56 private static final long serialVersionUID = -3713605626489646730L; 57 59 protected Invoker remoteInvoker; 61 62 65 public static int MAX_RETRIES = 10; 66 67 70 public JRMPInvokerProxy() 71 { 72 super(); 73 } 74 75 81 public JRMPInvokerProxy(final Invoker remoteInvoker) 82 { 83 this.remoteInvoker = remoteInvoker; 84 } 85 86 89 public String getServerHostName() throws Exception 90 { 91 return remoteInvoker.getServerHostName(); 92 } 93 94 104 public Object getTransactionPropagationContext() 105 throws SystemException 106 { 107 TransactionPropagationContextFactory tpcFactory = TransactionPropagationContextUtil.getTPCFactoryClientSide(); 108 return (tpcFactory == null) ? null : tpcFactory.getTransactionPropagationContext(); 109 } 110 111 116 public Object invoke(Invocation invocation) 117 throws Exception 118 { 119 MarshalledInvocation mi = new MarshalledInvocation(invocation); 121 122 mi.setTransactionPropagationContext(getTransactionPropagationContext()); 125 126 for (int i = 0; i < MAX_RETRIES; i++) 130 { 131 try 132 { 133 MarshalledObject result = (MarshalledObject ) remoteInvoker.invoke(mi); 134 return result.get(); 135 } 136 catch (ConnectException ce) 137 { 138 if (i + 1 < MAX_RETRIES) 139 { 140 Thread.sleep(1); 141 continue; 142 } 143 throw ce; 144 } 145 catch (ServerException ex) 146 { 147 if (ex.detail instanceof NoSuchObjectException ) 151 { 152 throw (NoSuchObjectException ) ex.detail; 153 } 154 if (ex.detail instanceof TransactionRolledbackException ) 155 { 156 throw (TransactionRolledbackException ) ex.detail; 157 } 158 if (ex.detail instanceof RemoteException ) 159 { 160 throw (RemoteException ) ex.detail; 161 } 162 throw ex; 163 } 164 } 165 throw new Exception ("Unreachable statement"); 166 } 167 168 171 public void writeExternal(final ObjectOutput out) 172 throws IOException 173 { 174 178 if( remoteInvoker instanceof RemoteStub ) 179 { 180 out.writeObject(remoteInvoker); 181 } 182 else 183 { 184 Object replacement = RemoteObject.toStub(remoteInvoker); 185 out.writeObject(replacement); 186 } 187 } 188 189 193 public void readExternal(final ObjectInput in) 194 throws IOException , ClassNotFoundException 195 { 196 remoteInvoker = (Invoker) in.readObject(); 197 } 198 } 199 200 | Popular Tags |