In some situations it may be necessary for an application to intercept the insertion and extraction of Ice objects. For example, the Ice extension for PHP is implemented using Ice for C++ but represents Ice objects as native PHP objects. The PHP extension accomplishes this by manually encoding and decoding Ice objects as directed by the data encoding rules. However, the extension obviously cannot pass a native PHP object to the C++ stream function for writing objects. To bridge this gap between object systems, Ice supplies the helper classes described below.
On this page:
Inserting Objects in Java
The ObjectWriter
base class facilitates the insertion of objects to a stream:
package Ice; public abstract class ObjectWriter extends ObjectImpl { public abstract void write(OutputStream out); // ... }
A foreign Ice object is inserted into a stream using the following technique:
- A Java "wrapper" class is derived from
ObjectWriter
. This class wraps the foreign object and implements thewrite
member function. - An instance of the wrapper class is passed to
writeObject
. (This is possible becauseObjectWriter
derives fromIce.Object
.) Eventually, thewrite
member function is invoked on the wrapper instance. - The implementation of
write
encodes the object's state as directed by the data encoding rules for classes.
It is the application's responsibility to ensure that there is a one-to-one mapping between foreign Ice objects and wrapper objects. This is necessary in order to ensure the proper encoding of object graphs.
Extracting Objects in Java
The ObjectReader
class facilitates the extraction of objects from a stream:
package Ice; public abstract class ObjectReader extends ObjectImpl { public abstract void read(InputStream in); // ... } public interface CompactIdResolver { String resolve(int id); }
Extracting the state of a foreign Ice object is more complicated than insertion:
- A Java "wrapper" class is derived from
ObjectReader
. An instance of this class represents a foreign Ice object. - An object factory is installed that returns instances of the wrapper class. Note that a single object factory can be used for all Slice types if it is registered with an empty Slice type ID.
- A Java callback class implements the
ReadObjectCallback
interface. The implementation ofinvoke
expects its argument to be either null or an instance of the wrapper class as returned by the object factory. - An instance of the callback class is passed to
readObject
. - When the stream is ready to extract the state of an object, it invokes
read
on the wrapper class. The implementation ofread
decodes the object's state as directed by the data encoding rules for classes. - The callback object passed to
readObject
is invoked, passing the instance of the wrapper object. All other callback objects representing the same instance in the stream (in case of object graphs) are invoked with the same wrapper object.
If your class definitions use compact type IDs, you must also supply an implementation of CompactIdResolver
when initializing the communicator. This object is responsible for translating numeric type IDs into their string equivalents.
See Also
- Client-Side Slice-to-PHP Mapping
- Data Encoding
- Data Encoding for Classes
- Class Factories in Java
- Communicator Initialization