openamf/src/java/org/openamf/io AMFSerializer.java,1.38,1.39
[email protected] Tue, 19 Aug 2003 16:07:23 -0700
| Newsgroups | gmane.comp.java.openamf.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/openamf/openamf/src/java/org/openamf/io In directory sc8-pr-cvs1:/tmp/cvs-serv7108/src/java/org/openamf/io Modified Files: AMFSerializer.java Log Message: preserve references when serializing Index: AMFSerializer.java =================================================================== RCS file: /cvsroot/openamf/openamf/src/java/org/openamf/io/AMFSerializer.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** AMFSerializer.java 16 Aug 2003 13:11:16 -0000 1.38 --- AMFSerializer.java 19 Aug 2003 23:07:21 -0000 1.39 *************** *** 16,19 **** --- 16,20 ---- import java.util.*; + import flashgateway.io.ASObject; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; *************** *** 22,26 **** import org.openamf.AMFHeader; import org.openamf.AMFMessage; - import org.openamf.ASObject; import org.openamf.config.OpenAMFConfig; import org.openamf.recordset.ASRecordSet; --- 23,26 ---- *************** *** 31,35 **** /** * AMF Serializer ! * * @author Jason Calabrese <[email protected]> * @author Pat Maddox <[email protected]> --- 31,35 ---- /** * AMF Serializer ! * * @author Jason Calabrese <[email protected]> * @author Pat Maddox <[email protected]> *************** *** 51,54 **** --- 51,58 ---- */ protected DataOutputStream outputStream; + + private Map storedObjects = new IdentityHashMap(); + private int storedObjectCount = 0; + /** * Constructor *************** *** 66,72 **** */ public void serializeMessage(AMFMessage message) throws IOException { ! if (!log.isDebugEnabled()) { log.info("Serializing Message, for more info turn on debug level"); } outputStream.writeShort(message.getVersion()); // write header --- 70,77 ---- */ public void serializeMessage(AMFMessage message) throws IOException { ! if (log.isInfoEnabled()) { log.info("Serializing Message, for more info turn on debug level"); } + clearStoredObjects(); outputStream.writeShort(message.getVersion()); // write header *************** *** 125,129 **** /** * Writes Data ! * * @param value * @throws IOException --- 130,134 ---- /** * Writes Data ! * * @param value * @throws IOException *************** *** 151,188 **** int offset = TimeZone.getDefault().getRawOffset(); outputStream.writeShort(offset / MILLS_PER_HOUR); - } else if (value instanceof Object[]) { - // write Object Array - writeArray((Object[]) value); - } else if (value instanceof Iterator) { - write((Iterator) value); - } else if (value instanceof Collection) { - write((Collection) value); - } else if (value instanceof Map) { - writeMap((Map) value); - } else if (value instanceof ResultSet) { - ASRecordSet asRecordSet = new ASRecordSet(); - asRecordSet.populate((ResultSet) value); - writeData(asRecordSet); - } else if (value instanceof Document) { - write((Document) value); } else { ! /* ! MM's gateway requires all objects to be marked with the ! Serializable interface in order to be serialized ! That should still be followed if possible, but there is ! no good reason to inforce it. ! */ ! writeObject(value); ! } } /** * Writes Object ! * * @param object * @throws IOException */ protected void writeObject(Object object) throws IOException { ! log.debug("Writting Object"); String customClassName = OpenAMFConfig.getInstance().getCustomClassName( --- 156,202 ---- int offset = TimeZone.getDefault().getRawOffset(); outputStream.writeShort(offset / MILLS_PER_HOUR); } else { ! ! if (storedObjects.containsKey(value)) { ! writeStoredObject(value); ! return; ! } ! storeObject(value); ! ! if (value instanceof Object[]) { ! // write Object Array ! writeArray((Object[]) value); ! } else if (value instanceof Iterator) { ! write((Iterator) value); ! } else if (value instanceof Collection) { ! write((Collection) value); ! } else if (value instanceof Map) { ! writeMap((Map) value); ! } else if (value instanceof ResultSet) { ! ASRecordSet asRecordSet = new ASRecordSet(); ! asRecordSet.populate((ResultSet) value); ! writeData(asRecordSet); ! } else if (value instanceof Document) { ! write((Document) value); ! } else { ! /* ! MM's gateway requires all objects to be marked with the ! Serializable interface in order to be serialized ! That should still be followed if possible, but there is ! no good reason to inforce it. ! */ ! writeObject(value); ! } ! } } /** * Writes Object ! * * @param object * @throws IOException */ protected void writeObject(Object object) throws IOException { ! log.debug("Writing Object"); String customClassName = OpenAMFConfig.getInstance().getCustomClassName( *************** *** 207,211 **** "unable to find readMethod for : " + propertyName ! + " writting null!"); } else { propertyValue = --- 221,225 ---- "unable to find readMethod for : " + propertyName ! + " writing null!"); } else { propertyValue = *************** *** 228,233 **** /** * Writes Array Object - call <code>writeData</code> foreach element ! * ! * @param array * @throws IOException */ --- 242,247 ---- /** * Writes Array Object - call <code>writeData</code> foreach element ! * ! * @param array * @throws IOException */ *************** *** 269,282 **** * Writes Object Map * ! * @param value * @throws IOException */ protected void writeMap(Map map) throws IOException { if (map instanceof ASObject && ((ASObject) map).getType() != null) { ! log.debug("Writting Custom Class: " + ((ASObject) map).getType()); outputStream.writeByte(AMFBody.DATA_TYPE_CUSTOM_CLASS); outputStream.writeUTF(((ASObject) map).getType()); } else { ! log.debug("Writting Map"); outputStream.writeByte(AMFBody.DATA_TYPE_OBJECT); } --- 283,296 ---- * Writes Object Map * ! * @param map * @throws IOException */ protected void writeMap(Map map) throws IOException { if (map instanceof ASObject && ((ASObject) map).getType() != null) { ! log.debug("Writing Custom Class: " + ((ASObject) map).getType()); outputStream.writeByte(AMFBody.DATA_TYPE_CUSTOM_CLASS); outputStream.writeUTF(((ASObject) map).getType()); } else { ! log.debug("Writing Map"); outputStream.writeByte(AMFBody.DATA_TYPE_OBJECT); } *************** *** 293,297 **** /** * Writes XML Document ! * * @param document * @throws IOException --- 307,311 ---- /** * Writes XML Document ! * * @param document * @throws IOException *************** *** 302,306 **** String xmlData = XMLUtils.convertDOMToString(docElement); if (log.isDebugEnabled()) { ! log.debug("Writting xmlData: \n" + xmlData); } ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream(); --- 316,320 ---- String xmlData = XMLUtils.convertDOMToString(docElement); if (log.isDebugEnabled()) { ! log.debug("Writing xmlData: \n" + xmlData); } ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream(); *************** *** 309,312 **** --- 323,342 ---- baOutputStream.writeTo(outputStream); } + + private void writeStoredObject(Object obj) throws IOException { + log.debug("Writing object reference for " + obj); + outputStream.write(AMFBody.DATA_TYPE_REFERENCE_OBJECT); + outputStream.writeShort(((Integer) storedObjects.get(obj)).intValue()); + } + + private void storeObject(Object obj) + { + storedObjects.put(obj, new Integer(storedObjectCount++)); + } + + private void clearStoredObjects() + { + storedObjects.clear(); + } } ------------------------------------------------------- This SF.net email is sponsored by Dice.com. Did you know that Dice has over 25,000 tech jobs available today? From careers in IT to Engineering to Tech Sales, Dice has tech jobs from the best hiring companies. http://www.dice.com/index.epl?rel_code=104