1 27 28 package org.objectweb.fractal.rmi.stub; 29 30 import org.objectweb.fractal.api.Component; 31 import org.objectweb.fractal.api.Interface; 32 import org.objectweb.fractal.api.Type; 33 34 import org.objectweb.fractal.rmi.RemoteException; 35 36 import org.objectweb.jonathan.apis.binding.Identifier; 37 import org.objectweb.jonathan.apis.binding.Reference; 38 import org.objectweb.jonathan.apis.kernel.JonathanException; 39 import org.objectweb.jonathan.apis.presentation.MarshalException; 40 import org.objectweb.jonathan.apis.presentation.Marshaller; 41 import org.objectweb.jonathan.apis.presentation.MarshallerFactory; 42 import org.objectweb.jonathan.apis.presentation.UnMarshaller; 43 import org.objectweb.jonathan.apis.protocols.ReplyInterface; 44 import org.objectweb.jonathan.apis.protocols.ServerException; 45 import org.objectweb.jonathan.apis.protocols.SessionIdentifier; 46 import org.objectweb.jonathan.apis.protocols.Session_High; 47 48 58 59 public class Stub implements Reference, Interface { 60 61 64 65 protected Identifier id; 66 67 70 71 protected MarshallerFactory marshallerFactory; 72 73 76 77 protected SessionIdentifier sessionId; 78 79 83 84 protected Session_High session; 85 86 89 90 public Stub () { 91 } 92 93 97 103 104 public Identifier[] getIdentifiers () { 105 return new Identifier[] { id }; 106 } 107 108 114 115 public void setIdentifiers (Identifier[] ids) { 116 this.id = ids[0]; 117 } 118 119 123 public Component getFcItfOwner () { 124 try { 125 Marshaller marshaller = request(); 126 ReplyInterface reply = prepareInvocation(marshaller); 127 marshaller.writeInt(-1); 128 invoke(marshaller); 129 UnMarshaller unmarshaller = reply.listen(); 130 Component result = (Component)unmarshaller.readValue(); 131 unmarshaller.close(); 132 return result; 133 } catch (Exception e) { 134 e = handleException(e); 135 if (e instanceof RuntimeException ) { 136 throw (RuntimeException )e; 137 } else { 138 throw new RemoteException("server side exception", e); 139 } 140 } 141 } 142 143 public String getFcItfName () { 144 try { 145 Marshaller marshaller = request(); 146 ReplyInterface reply = prepareInvocation(marshaller); 147 marshaller.writeInt(-2); 148 invoke(marshaller); 149 UnMarshaller unmarshaller = reply.listen(); 150 String result = (String )unmarshaller.readValue(); 151 unmarshaller.close(); 152 return result; 153 } catch (Exception e) { 154 e = handleException(e); 155 if (e instanceof RuntimeException ) { 156 throw (RuntimeException )e; 157 } else { 158 throw new RemoteException("server side exception", e); 159 } 160 } 161 } 162 163 public Type getFcItfType () { 164 try { 165 Marshaller marshaller = request(); 166 ReplyInterface reply = prepareInvocation(marshaller); 167 marshaller.writeInt(-3); 168 invoke(marshaller); 169 UnMarshaller unmarshaller = reply.listen(); 170 Type result = (Type)unmarshaller.readValue(); 171 unmarshaller.close(); 172 return result; 173 } catch (Exception e) { 174 e = handleException(e); 175 if (e instanceof RuntimeException ) { 176 throw (RuntimeException )e; 177 } else { 178 throw new RemoteException("server side exception", e); 179 } 180 } 181 } 182 183 public boolean isFcInternalItf () { 184 try { 185 Marshaller marshaller = request(); 186 ReplyInterface reply = prepareInvocation(marshaller); 187 marshaller.writeInt(-4); 188 invoke(marshaller); 189 UnMarshaller unmarshaller = reply.listen(); 190 boolean result = unmarshaller.readBoolean(); 191 unmarshaller.close(); 192 return result; 193 } catch (Exception e) { 194 e = handleException(e); 195 if (e instanceof RuntimeException ) { 196 throw (RuntimeException )e; 197 } else { 198 throw new RemoteException("server side exception", e); 199 } 200 } 201 } 202 203 207 213 214 protected Marshaller request () throws MarshalException { 215 if (marshallerFactory == null) { 216 throw new MarshalException("null marshaller factory"); 217 } 218 return marshallerFactory.newMarshaller(); 219 } 220 221 232 233 protected ReplyInterface prepareInvocation (final Marshaller marshaller) 234 throws MarshalException 235 { 236 try { 237 synchronized(this) { 238 if (session == null) { 239 session = sessionId.bind(null); 240 } 241 } 242 return session.prepareInvocation(marshaller); 243 } catch (Exception e) { 244 throw new MarshalException("exception preparing marshaller: " + e); 245 } 246 } 247 248 254 255 protected void invoke (final Marshaller marshaller) throws MarshalException { 256 try { 257 synchronized(this) { 258 if (session == null) { 259 throw new MarshalException("null session"); 260 } 261 } 262 session.send(marshaller); 263 } catch (Exception e) { 264 throw new MarshalException("exception at invocation: " + e); 265 } 266 } 267 268 279 280 protected Exception handleException (final Exception e) { 281 if (e instanceof ServerException) { 282 try { 283 UnMarshaller unmarshaller = ((ServerException)e).unmarshaller; 284 Exception f = (Exception )unmarshaller.readValue(); 285 unmarshaller.close(); 286 return f; 287 } catch (JonathanException je) { 288 return new RemoteException( 289 "error during exception unmarshalling by stub", je); 290 } 291 } else { 292 return new RemoteException( 293 "error during marshalling/unmarshalling by stub", e); 294 } 295 } 296 297 protected Object replaceClassName (Object o) throws ClassNotFoundException { 298 if (o instanceof String ) { 299 return Class.forName((String )o); 300 } else if (o instanceof Object []) { 301 Object [] desc = (Object [])o; 302 if (desc.length == 2 && desc[1] instanceof String ) { 303 return new Object [] { desc[0], Class.forName((String )desc[1]) }; 304 } 305 } 306 return o; 307 } 308 309 protected Object replaceClassValue (Object o) { 310 if (o instanceof Class ) { 311 return ((Class )o).getName(); 312 } else if (o instanceof Object []) { 313 Object [] desc = (Object [])o; 314 if (desc.length == 2 && desc[1] instanceof Class ) { 315 return new Object [] { desc[0], ((Class )desc[1]).getName() }; 316 } 317 } 318 return o; 319 } 320 321 325 332 333 public boolean equals (final Object o) { 334 if (o instanceof Stub) { 335 return id.equals(((Stub)o).id); 336 } 337 return false; 338 } 339 340 345 346 public int hashCode () { 347 return id.hashCode(); 348 } 349 350 354 355 protected void finalize () { 356 if (session != null) { 357 session.close(); 358 session = null; 359 } 360 } 361 } 362 | Popular Tags |