📄 rmiutilities.java
字号:
r[p++] = (Field) it.next(); } return r; } /** * The method is called for Remotes that are not Stubs. It is assumed, that * the Remote is an implementation. The method searches for the suitable tie * and, if found, exports it by creating and connecting the stub. Such export * is supported since jdk 1.5. */ void exportTie(org.omg.CORBA_2_3.portable.OutputStream output, Object implementation, Class interfaceClass) { try { // Remote, but non - stub class (implementation) // must be replaced by stub. Tie t = Util.getTie((Remote) implementation); if (t instanceof Servant) { POA rootPoa = POAHelper.narrow(output.orb().resolve_initial_references( "RootPOA")); org.omg.CORBA.Object co = rootPoa.servant_to_reference((Servant) t); Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass); writeRemoteObject(output, stub); if (rootPoa.the_POAManager().get_state().value() == State._HOLDING) rootPoa.the_POAManager().activate(); } else if (t instanceof org.omg.CORBA.Object) { org.omg.CORBA.Object co = (org.omg.CORBA.Object) t; output.orb().connect(co); Stub stub = (Stub) PortableRemoteObject.narrow(co, interfaceClass); writeRemoteObject(output, stub); } } catch (Exception ex) { MARSHAL m = new MARSHAL("Unable to export " + implementation); m.minor = Minor.TargetConversion; m.initCause(ex); throw m; } } /** * Start the ORB, if it is not already runnning. */ void ensureOrbRunning(org.omg.CORBA_2_3.portable.OutputStream output) { // Ensure ORB is running. if (output.orb() instanceof OrbFunctional) { ((OrbFunctional) output.orb()).ensureRunning(); } } /** * Write data to the CORBA output stream. Writes the object contents only; the * header must be already written. For object, containing objects, may be * called recursively. * * @param an_output a stream to write to, must be * org.omg.CORBA_2_3.portable.OutputStream * @param object an object to write. */ public void writeRemoteObject(OutputStream an_output, Object object) { org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output; if (isTieRequired(object)) { // Find the interface that is implemented by the object and extends // Remote. Class fc = getExportedInterface(object); exportTie(output, object, fc); } else if (object instanceof org.omg.CORBA.Object) { ensureOrbRunning(output); an_output.write_Object((org.omg.CORBA.Object) object); } else if (object != null && object instanceof Serializable) writeFields(an_output, (Serializable) object); } /** * Write data to the CORBA output stream. Writes the object contents only; the * header must be already written. For object, containing objects, may be * called recursively. * * @param an_output a stream to write to, must be * org.omg.CORBA_2_3.portable.OutputStream * @param object an object to write. */ public void writeValue(OutputStream an_output, Serializable object) { org.omg.CORBA_2_3.portable.OutputStream output = (org.omg.CORBA_2_3.portable.OutputStream) an_output; if (isTieRequired(object)) { // Find the interface that is implemented by the object and extends // Remote. Class fc = getExportedInterface(object); exportTie(output, object, fc); } else if (object instanceof org.omg.CORBA.Object) { ensureOrbRunning(output); an_output.write_Object((org.omg.CORBA.Object) object); } else if (object instanceof Externalizable) { try { ObjectOutputStream stream = new CorbaOutput(output, object, this); stream.write(VERSION); ((Externalizable) object).writeExternal(stream); } catch (Exception ex) { MARSHAL m = new MARSHAL("writeExternal failed"); m.minor = Minor.Value; m.initCause(ex); throw m; } } else if (object instanceof Serializable) { Object mode = null; synchronized (io_format) { mode = io_format.get(object.getClass()); if (mode == STANDARD) { writeFields(an_output, (Serializable) object); return; } } try { Method m = object.getClass().getDeclaredMethod("writeObject", WRITE_OBJECT_ARGS); m.setAccessible(true); // May be private. try { ObjectOutputStream stream = new CorbaOutput(output, object, this); // Write version. stream.write(VERSION); if (mode == CUSTOM_DWO) // Write true, supposing that the defaultWriteObject // has been called. stream.write(1); else if (mode == CUSTOM_NO_DWO) // Write false (has not been called) stream.write(0); else { // Measure. DefaultWriteObjectTester tester = new DefaultWriteObjectTester(object); m.invoke(object, new Object[] { tester }); synchronized (io_format) { io_format.put(object.getClass(), tester.dwo_called ? CUSTOM_DWO : CUSTOM_NO_DWO); stream.write(tester.dwo_called ? 1 : 0); } } m.invoke(object, new Object[] { stream }); stream.flush(); } catch (Exception ex) { MARSHAL mx = new MARSHAL(object.getClass().getName() + ".writeObject failed"); mx.initCause(ex); throw mx; } } catch (NoSuchMethodException e) { // Write in a standard way. writeFields(an_output, (Serializable) object); synchronized (io_format) { io_format.put(object.getClass(), STANDARD); } } } } /** * Read data from the CDR input stream. Reads the object contents only; the * header must be already read (the repository id or ids ara passed). For * object, containing objects, may be called recursively. * * @param an_input the stream to read from, must be * org.omg.CORBA_2_3.portable.InputStream * @param object the instance of the object being read. * @param id the repository Id from the stream in the case when single id was * specified. * @param ids the repository Ids from the stream in the case when multiple ids * were specified. * @param codebase the codebase, if it was included in the header of the value * type. Null if not codebase was included. * * @return the object, extracted from the stream. */ /** * Read value from the input stream in the case when the value is not * Streamable or CustomMarshalled. */ public Serializable readValue(InputStream in, int offset, Class clz, String repositoryID, RunTime sender) { if (in instanceof HeadlessInput) ((HeadlessInput) in).subsequentCalls = true; gnuRuntime g; Serializable object = null; try { g = (gnuRuntime) sender; object = g.target; } catch (ClassCastException e) { // Working with the other CORBA implementation. g = null; } org.omg.CORBA_2_3.portable.InputStream input = (org.omg.CORBA_2_3.portable.InputStream) in; if (Remote.class.isAssignableFrom(clz) || ValueBase.class.isAssignableFrom(clz)) { // Interface is narrowed into Stub. if (clz.isInterface()) try { clz = Util.loadClass( PortableRemoteObjectDelegateImpl.getStubClassName(clz.getName()), null, clz.getClassLoader()); } catch (ClassNotFoundException e) { MARSHAL m = new MARSHAL("Cannot get stub from interface " + clz.getClass().getName()); m.minor = Minor.TargetConversion; m.initCause(e); throw m; } // Remote needs special handling. if (ObjectImpl.class.isAssignableFrom(clz)) { // First read CORBA object reference. Object ro = input.read_Object(); ObjectImpl obj = (ObjectImpl) ro; if (obj == null) return null; Delegate delegate = obj._get_delegate(); object = instantiate(offset, clz, g); ((ObjectImpl) object)._set_delegate(delegate); } // The object - specific data follows. } else if (org.omg.CORBA.Object.class.isAssignableFrom(clz)) object = (Serializable) input.read_Object(); if (object == null) object = instantiate(offset, clz, g); // The sentence below prevents attempt to read the internal fields of the // ObjectImpl (or RMI Stub) that might follow the object definition. // Sun's jre 1.5 does not write this information. The stubs, generated // by rmic, does not contain such fields. if (object instanceof ObjectImpl) return object; if (object instanceof Externalizable) { try { CorbaInput stream = new CorbaInput(input, object, this, offset, repositoryID, g); byte version = stream.readByte(); if (version != 1) throw new MARSHAL("Unsuported RMI-IIOP version " + version); ((Externalizable) object).readExternal(stream); } catch (Exception ex) { MARSHAL m = new MARSHAL("readExternal failed"); m.initCause(ex); throw m; } } else { Object mode = null; synchronized (io_format) { mode = io_format.get(object.getClass()); } if (mode == STANDARD) { readFields(offset, repositoryID, object, input, g); } else { try { Method m = object.getClass().getDeclaredMethod("readObject", READ_OBJECT_ARGS); try { m.setAccessible(true); // May be private. CorbaInput stream = new CorbaInput(input, object, this, offset, repositoryID, g); byte version = stream.readByte(); if (version != 1) throw new MARSHAL("Unsuported RMI-IIOP version " + version); // This would indicate is defaultWriteObject has been // called, // but the readObject method normally takes care about this. boolean dwo = stream.readByte() != 0; m.invoke(object, new Object[] { stream }); synchronized (io_format) { io_format.put(object.getClass(), dwo ? CUSTOM_DWO : CUSTOM_NO_DWO); } } catch (Exception ex) { ex.printStackTrace(); MARSHAL mx = new MARSHAL(object.getClass().getName() + ".readObject failed"); mx.initCause(ex); throw mx; } } catch (NoSuchMethodException e) { // Read in a standard way. synchronized (io_format) { io_format.put(object.getClass(), STANDARD); readFields(offset, repositoryID, object, input, g); } } } } return object; } /** * Create an instance. */ Serializable instantiate(int offset, Class clz, gnuRuntime g) throws MARSHAL { Serializable object; try { object = (Serializable) Vio.instantiateAnyWay(clz); g.objectWritten(object, offset); } catch (Exception e) { MARSHAL m = new MARSHAL("Unable to instantiate " + clz); m.minor = Minor.Instantiation; m.initCause(e); throw m; } return object; } /** * Read fields of the object. */ void readFields(int offset, String repositoryID, Serializable object, org.omg.CORBA_2_3.portable.InputStream input, gnuRuntime r) throws MARSHAL { Field f = null; Class o_class = object.getClass(); try { // The returned field array must already be in canonical order. Field[] fields = getWritableFields(o_class); Class fc; for (int i = 0; i < fields.length; i++) { // Full value type header expected ahead. if (input instanceof HeadlessInput) ((HeadlessInput) input).subsequentCalls = true; f = fields[i]; fc = f.getType(); Object v; if (fc == String.class) { v = input.read_value(wStringValueHelper); } else if (fc == int.class) v = new Integer(input.read_long()); else if (fc == long.class) v = new Long(input.read_longlong()); else if (fc == double.class) v = new Double(input.read_double()); else if (fc == float.class) v = new Float(input.read_float()); else if (fc == boolean.class) v = input.read_boolean() ? Boolean.TRUE : Boolean.FALSE; else if (fc == short.class) v = new Short(input.read_short()); else if (fc == byte.class) v = new Byte(input.read_octet()); else if (fc == char.class) v = new Character(input.read_char()); else if (org.omg.CORBA.Object.class.isAssignableFrom(fc) || Remote.class.isAssignableFrom(fc)) { v = readValue(input, offset, fc, null, r); } else { v = Vio.read(input, fc); } f.set(object, v); } } catch (Exception ex) { MARSHAL m = new MARSHAL("Cannot read " + o_class.getName() + " field " + f); m.initCause(ex); m.minor = Minor.ValueFields; throw m; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -