1 28 package org.objectweb.carol.rmi.jrmp.server; 29 30 import java.io.IOException ; 32 import java.io.ObjectInput ; 33 import java.io.ObjectOutput ; 34 import java.rmi.MarshalException ; 35 import java.rmi.Remote ; 36 import java.rmi.RemoteException ; 37 import java.rmi.UnmarshalException ; 38 import java.rmi.server.Operation ; 39 import java.rmi.server.RemoteCall ; 40 import java.rmi.server.RemoteObject ; 41 import java.rmi.server.UID ; 42 import java.util.Arrays ; 43 44 import org.objectweb.carol.rmi.jrmp.interceptor.JClientInterceptorHelper; 45 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor; 46 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorHelper; 47 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore; 48 49 import sun.rmi.server.UnicastRef; 50 import sun.rmi.transport.Connection; 51 import sun.rmi.transport.LiveRef; 52 import sun.rmi.transport.StreamRemoteCall; 53 54 60 public class JUnicastRef extends UnicastRef { 61 62 65 private transient boolean localRef = false; 66 67 70 private transient byte[] raddr = null; 71 72 75 private transient UID ruid = null; 76 77 80 protected transient JClientRequestInterceptor[] cis = null; 81 82 85 protected transient String [] initializers = null; 86 87 private transient int localId = -2; 88 89 92 public JUnicastRef() { 93 } 94 95 99 public JUnicastRef(LiveRef liveRef) { 100 super(liveRef); 101 } 102 103 109 public JUnicastRef(LiveRef liveRef, JClientRequestInterceptor[] cis, String [] initial, int local) { 110 super(liveRef); 111 this.initializers = initial; 112 this.cis = cis; 113 this.raddr = JInterceptorHelper.getInetAddress(); 114 this.ruid = JInterceptorHelper.getSpaceID(); 115 this.localId = local; 116 } 117 118 122 public String getRefClass(ObjectOutput out) { 123 super.getRefClass(out); 124 return null; 125 } 126 127 134 public Object invoke(Remote obj, java.lang.reflect.Method method, Object [] params, long opnum) throws Exception { 135 if ((localRef) && (localId != -2)) { 136 return method.invoke(JLocalObjectStore.getObject(localId), params); 139 } else { 140 Connection conn = ref.getChannel().newConnection(); 143 java.rmi.server.RemoteCall call = null; 144 boolean reuse = true; 145 boolean alreadyFreed = false; 146 147 try { 148 call = new JRemoteCall(conn, ref.getObjID(), -1, opnum, cis); 149 try { 150 ObjectOutput out = call.getOutputStream(); 151 marshalCustomCallData(out); 152 Class [] types = method.getParameterTypes(); 153 for (int i = 0; i < types.length; i++) { 154 marshalValue(types[i], params[i], out); 155 } 156 } catch (IOException e) { 157 throw new MarshalException ("error marshalling arguments" + e); 158 } 159 160 call.executeCall(); 162 163 try { 164 Class rtype = method.getReturnType(); 165 if (rtype == void.class) { 166 return null; 167 } 168 ObjectInput in = call.getInputStream(); 169 Object returnValue = unmarshalValue(rtype, in); 170 alreadyFreed = true; 171 ref.getChannel().free(conn, true); 172 173 return returnValue; 174 175 } catch (IOException e) { 176 throw new UnmarshalException ("IOException unmarshalling return" + e); 177 } catch (ClassNotFoundException e) { 178 throw new UnmarshalException ("ClassNotFoundException unmarshalling return" + e); 179 } finally { 180 try { 181 call.done(); 182 } catch (IOException e) { 183 reuse = false; 184 } 185 } 186 187 } catch (RuntimeException e) { 188 if ((call == null) || (((StreamRemoteCall) call).getServerException() != e)) { 189 reuse = false; 190 } 191 throw e; 192 193 } catch (RemoteException e) { 194 reuse = false; 195 throw e; 196 197 } catch (Error e) { 198 reuse = false; 199 throw e; 200 201 } finally { 202 if (!alreadyFreed) { 203 ref.getChannel().free(conn, reuse); 204 } 205 } 206 } 207 } 208 209 214 public void invoke(java.rmi.server.RemoteCall call) throws Exception { 215 super.invoke(call); 216 } 217 218 227 public RemoteCall newCall(RemoteObject obj, Operation [] ops, int opnum, long hash) throws RemoteException { 228 229 Connection conn = ref.getChannel().newConnection(); 230 try { 231 RemoteCall call = new JRemoteCall(conn, ref.getObjID(), opnum, hash, cis); 232 try { 233 marshalCustomCallData(call.getOutputStream()); 234 } catch (IOException e) { 235 throw new MarshalException ("error marshaling " + "custom call data"); 236 } 237 return call; 238 } catch (RemoteException e) { 239 ref.getChannel().free(conn, false); 240 throw e; 241 } 242 } 243 244 249 protected void marshalCustomCallData(ObjectOutput out) throws IOException { 250 JClientInterceptorHelper.send_request(out, cis, localRef); 251 super.marshalCustomCallData(out); 252 } 253 254 259 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 260 readExternal(in, false); 261 } 262 263 268 public void writeExternal(ObjectOutput out) throws IOException { 269 writeExternal(out, false); 270 } 271 272 278 public void readExternal(ObjectInput in, boolean newFormat) throws IOException , ClassNotFoundException { 279 raddr = new byte[in.readInt()]; 280 in.read(raddr); 281 ruid = UID.read(in); 282 localRef = ((Arrays.equals(raddr, JInterceptorHelper.getInetAddress())) && (ruid.equals(JInterceptorHelper 283 .getSpaceID()))); 284 String [] ia = new String [in.readInt()]; 286 for (int i = 0; i < ia.length; i++) { 287 ia[i] = in.readUTF(); 288 } 289 this.initializers = ia; 290 cis = JInterceptorStore.setRemoteInterceptors(raddr, ruid, ia); 291 localId = in.readInt(); 292 ref = LiveRef.read(in, newFormat); 293 } 294 295 301 public void writeExternal(ObjectOutput out, boolean newFormat) throws IOException { 302 out.writeInt(raddr.length); 303 out.write(raddr); 304 ruid.write(out); 305 String [] ia = this.initializers; 307 out.writeInt(ia.length); 308 for (int i = 0; i < ia.length; i++) { 309 out.writeUTF(ia[i]); 310 } 311 out.writeInt(getLocalId()); 313 ref.write(out, newFormat); 314 out.flush(); 315 } 316 317 320 public int getLocalId() { 321 return localId; 323 } 324 } | Popular Tags |