[Red5devs] [red5 commit] r3099 - in java/server/trunk/src/org/red5: io/amf io/amf3 io/object server server/net/rtmp/codec ...
codesite-noreply at google.com
codesite-noreply at google.com
Wed Sep 24 22:26:08 PDT 2008
Author: mondain
Date: Wed Sep 24 22:25:45 2008
New Revision: 3099
Modified:
java/server/trunk/src/org/red5/io/amf/Input.java
java/server/trunk/src/org/red5/io/amf/Output.java
java/server/trunk/src/org/red5/io/amf3/Output.java
java/server/trunk/src/org/red5/io/object/Deserializer.java
java/server/trunk/src/org/red5/io/object/Serializer.java
java/server/trunk/src/org/red5/server/PersistableAttributeStore.java
java/server/trunk/src/org/red5/server/net/rtmp/codec/RTMPProtocolDecoder.java
java/server/trunk/src/org/red5/server/so/SharedObject.java
Log:
APPSERVER-277 patch applied from Harald Radi
Modified: java/server/trunk/src/org/red5/io/amf/Input.java
==============================================================================
--- java/server/trunk/src/org/red5/io/amf/Input.java (original)
+++ java/server/trunk/src/org/red5/io/amf/Input.java Wed Sep 24 22:25:45
2008
@@ -21,12 +21,14 @@
import java.io.IOException;
import java.lang.reflect.Field;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
@@ -389,17 +391,20 @@
Class theClass = bean.getClass();
while (hasMoreProperties()) {
String name = readPropertyName();
- Class t = getPropertyType(bean, name);
+ Type type = getPropertyType(bean, name);
log.debug("property: {}", name);
- Object property = deserializer.deserialize(this, t);
+ Object property = deserializer.deserialize(this, type);
log.debug("val: {}", property);
//log.debug("val: "+property.getClass().getName());
try {
if (property != null) {
try {
- if (!t.isAssignableFrom(property.getClass())) {
- property = ConversionUtils.convert(property, t);
- }
+ if (type instanceof Class) {
+ Class t = (Class) type;
+ if (!t.isAssignableFrom(property.getClass())) {
+ property =
ConversionUtils.convert(property, t);
+ }
+ }
final Field field = theClass.getField(name);
field.set(bean, property);
} catch (Exception ex2) {
@@ -579,11 +584,11 @@
reset(ReferenceMode.MODE_RTMP);
}
- protected Class getPropertyType(Object instance, String propertyName) {
+ protected Type getPropertyType(Object instance, String propertyName) {
try {
if (instance != null) {
Field field = instance.getClass().getField(propertyName);
- return field.getType();
+ return field.getGenericType();
} else {
// instance is null for anonymous class, use default type
}
@@ -591,7 +596,8 @@
try {
BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance();
PropertyUtilsBean propertyUtils =
beanUtilsBean.getPropertyUtils();
- return propertyUtils.getPropertyType(instance,
propertyName);
+ PropertyDescriptor propertyDescriptor =
propertyUtils.getPropertyDescriptor(instance, propertyName);
+ return
propertyDescriptor.getReadMethod().getGenericReturnType();
} catch (Exception e2) {
// nothing
}
Modified: java/server/trunk/src/org/red5/io/amf/Output.java
==============================================================================
--- java/server/trunk/src/org/red5/io/amf/Output.java (original)
+++ java/server/trunk/src/org/red5/io/amf/Output.java Wed Sep 24 22:25:45
2008
@@ -22,11 +22,8 @@
import java.lang.reflect.Array;
import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
@@ -37,7 +34,6 @@
import org.apache.commons.collections.map.LRUMap;
import org.apache.mina.common.ByteBuffer;
import org.red5.annotations.Anonymous;
-import org.red5.annotations.DontSerialize;
import org.red5.io.amf3.ByteArray;
import org.red5.io.object.BaseOutput;
import org.red5.io.object.ICustomSerializable;
@@ -298,17 +294,16 @@
// Iterate thru entries and write out property names with
separators
for (BeanMap.Entry<?, ?> entry: set) {
- if (entry.getKey().toString().equals("class")) {
- continue;
- }
+ String fieldName = entry.getKey().toString();
+ Field field = getField(objectClass, fieldName);
- String keyName = entry.getKey().toString();
- // Check if the Field corresponding to the getter/setter pair is
transient
- if (dontSerializeField(objectClass, keyName)) {
+ // Check if the Field corresponding to the getter/setter pair
is transient
+ if (!serializer.serializeField(field)) {
continue;
}
- putString(buf, keyName);
- serializer.serialize(this, entry.getValue());
+
+ putString(buf, fieldName);
+ serializer.serialize(this, field, entry.getValue());
}
// Write out end of object mark
buf.put((byte) 0x00);
@@ -316,27 +311,16 @@
buf.put(AMF.TYPE_END_OF_OBJECT);
}
- protected boolean dontSerializeField(Class<?> objectClass, String
keyName) {
+ protected Field getField(Class<?> objectClass, String keyName) {
for (Class<?> clazz = objectClass; !clazz.equals(Object.class);
clazz = clazz.getSuperclass()) {
try {
- Field field = clazz.getDeclaredField(keyName);
- boolean dontSerialize =
field.isAnnotationPresent(DontSerialize.class);
- boolean isTransient =
Modifier.isTransient(field.getModifiers());
-
- if (dontSerialize && log.isDebugEnabled()) {
- log.debug("Skipping {} because its marked with
@DontSerialize", field.getName());
- }
- if (isTransient) {
- log.warn("Using \"transient\" to declare fields not to be
serialized is deprecated and will be removed in Red5 0.8, use
\"@DontSerialize\" instead.");
- }
- return dontSerialize || isTransient;
+ return clazz.getDeclaredField(keyName);
} catch (NoSuchFieldException nfe) {
// Ignore this exception and use the default behaviour
log.debug("writeObject caught NoSuchFieldException");
}
}
-
- return false;
+ return null;
}
/** {@inheritDoc} */
@@ -378,23 +362,12 @@
buf.put(AMF.TYPE_OBJECT);
}
- // Get public field values
- Map<String, Object> values = new HashMap<String, Object>();
// Iterate thru fields of an object to build "name-value" map from
it
for (Field field : objectClass.getFields()) {
- if (field.isAnnotationPresent(DontSerialize.class)) {
- if (log.isDebugEnabled()) {
- log.debug("Skipping " + field.getName() + " because its marked with
@DontSerialize");
- }
- continue;
- } else {
- int modifiers = field.getModifiers();
- if (Modifier.isTransient(modifiers)) {
- log.warn("Using \"transient\" to declare fields not to be serialized
is " +
- "deprecated and will be removed in Red5 0.8, use \"@DontSerialize\"
instead.");
- continue;
- }
- }
+ // Check if the Field corresponding to the getter/setter pair
is transient
+ if (!serializer.serializeField(field)) {
+ continue;
+ }
Object value;
try {
@@ -404,19 +377,10 @@
// Swallow on private and protected properties access
exception
continue;
}
- // Put field to the map of "name-value" pairs
- values.put(field.getName(), value);
- }
-
- // Output public values
- Iterator<Map.Entry<String, Object>> it = values.entrySet().iterator();
- // Iterate thru map and write out properties with separators
- while (it.hasNext()) {
- Map.Entry<String, Object> entry = it.next();
// Write out prop name
- putString(buf, entry.getKey());
+ putString(buf, field.getName());
// Write out
- serializer.serialize(this, entry.getValue());
+ serializer.serialize(this, field, value);
}
// Write out end of object marker
buf.put((byte) 0x00);
Modified: java/server/trunk/src/org/red5/io/amf3/Output.java
==============================================================================
--- java/server/trunk/src/org/red5/io/amf3/Output.java (original)
+++ java/server/trunk/src/org/red5/io/amf3/Output.java Wed Sep 24 22:25:45
2008
@@ -21,11 +21,8 @@
import java.lang.reflect.Array;
import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -34,7 +31,6 @@
import org.apache.commons.collections.BeanMap;
import org.apache.mina.common.ByteBuffer;
import org.red5.annotations.Anonymous;
-import org.red5.annotations.DontSerialize;
import org.red5.compatibility.flex.messaging.io.ObjectProxy;
import org.red5.io.amf.AMF;
import org.red5.io.object.RecordSet;
@@ -84,7 +80,7 @@
public void enforceAMF3() {
amf3_mode++;
}
-
+
/**
* Provide access to raw data.
*
@@ -161,7 +157,7 @@
}
return encoded;
}
-
+
/** {@inheritDoc} */
protected void putString(String str, byte[] encoded) {
final int len = encoded.length;
@@ -385,7 +381,7 @@
// Strip compatibility prefix from classname
className = className.substring(23);
}
-
+
// If we need to serialize class information...
if (!objectClass.isAnnotationPresent(Anonymous.class)) {
putString(className);
@@ -395,23 +391,12 @@
// Store key/value pairs
amf3_mode += 1;
- // Get public field values
- Map<String, Object> values = new HashMap<String, Object>();
// Iterate thru fields of an object to build "name-value" map from
it
for (Field field : objectClass.getFields()) {
- if (field.isAnnotationPresent(DontSerialize.class)) {
- if (log.isDebugEnabled()) {
- log.debug("Skipping " + field.getName() + " because its marked with
@DontSerialize");
- }
- continue;
- } else {
- int modifiers = field.getModifiers();
- if (Modifier.isTransient(modifiers)) {
- log.warn("Using \"transient\" to declare fields not to be serialized
is " +
- "deprecated and will be removed in Red5 0.8, use \"@DontSerialize\"
instead.");
- continue;
- }
- }
+ // Check if the Field corresponding to the getter/setter pair
is transient
+ if (!serializer.serializeField(field)) {
+ continue;
+ }
Object value;
try {
@@ -421,19 +406,11 @@
// Swallow on private and protected properties access
exception
continue;
}
- // Put field to the map of "name-value" pairs
- values.put(field.getName(), value);
- }
- // Output public values
- Iterator<Map.Entry<String, Object>> it = values.entrySet().iterator();
- // Iterate thru map and write out properties with separators
- while (it.hasNext()) {
- Map.Entry<String, Object> entry = it.next();
// Write out prop name
- putString(entry.getKey());
+ putString(field.getName());
// Write out
- serializer.serialize(this, entry.getValue());
+ serializer.serialize(this, field, value);
}
amf3_mode -= 1;
// Write out end of object marker
@@ -455,7 +432,7 @@
// Strip compatibility prefix from classname
className = className.substring(23);
}
-
+
if (object instanceof IExternalizable) {
// The object knows how to serialize itself.
int type = 1 << 1 | 1;
@@ -500,15 +477,13 @@
amf3_mode += 1;
for (BeanMap.Entry<?, ?> entry: set) {
String keyName = entry.getKey().toString();
- if ("class".equals(keyName)) {
- continue;
- }
+ Field field = getField(objectClass, keyName);
// Check if the Field corresponding to the getter/setter pair is
transient
- if (dontSerializeField(objectClass, keyName)) continue;
+ if (!serializer.serializeField(field)) continue;
putString(keyName);
- serializer.serialize(this, entry.getValue());
+ serializer.serialize(this, field, entry.getValue());
}
amf3_mode -= 1;
@@ -567,7 +542,7 @@
buf.put(encoded);
storeReference(xml);
}
-
+
/** {@inheritDoc} */
public void writeByteArray(ByteArray array) {
writeAMF3();
@@ -590,5 +565,5 @@
data.position(old);
}
}
-
+
}
Modified: java/server/trunk/src/org/red5/io/object/Deserializer.java
==============================================================================
--- java/server/trunk/src/org/red5/io/object/Deserializer.java (original)
+++ java/server/trunk/src/org/red5/io/object/Deserializer.java Wed Sep 24
22:25:45 2008
@@ -22,10 +22,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.lang.reflect.Type;
+
/**
- * The Deserializer class reads data input and handles the data
- * according to the core data types
- *
+ * The Deserializer class reads data input and handles the data
+ * according to the core data types
+ *
* @author The Red5 Project (red5 at osflash.org)
* @author Luke Hubbard, Codegent Ltd (luke at codegent.com)
*/
@@ -41,7 +43,7 @@
* @param target
* @return Object
*/
- public <T> T deserialize(Input in, Class<T> target) {
+ public <T> T deserialize(Input in, Type target) {
byte type = in.readDataType();
log.debug("Type: {}", type);
@@ -104,7 +106,7 @@
* @param target
* @return
*/
- protected Object postProcessExtension(Object result, Class target) {
+ protected Object postProcessExtension(Object result, Type target) {
// does nothing at the moment, but will later!
return result;
}
Modified: java/server/trunk/src/org/red5/io/object/Serializer.java
==============================================================================
--- java/server/trunk/src/org/red5/io/object/Serializer.java (original)
+++ java/server/trunk/src/org/red5/io/object/Serializer.java Wed Sep 24
22:25:45 2008
@@ -25,11 +25,14 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
import org.apache.commons.collections.BeanMap;
import org.red5.io.amf3.ByteArray;
import org.red5.io.amf3.IExternalizable;
import org.red5.io.utils.ObjectMap;
+import org.red5.annotations.DontSerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
@@ -54,7 +57,18 @@
* @param out Output writer
* @param any Object to serialize
*/
- public void serialize(Output out, Object any) {
+ public void serialize(Output out, Object any) {
+ serialize(out, null, any);
+ }
+
+ /**
+ * Serializes output to a core data type object
+ *
+ * @param out Output writer
+ * @param field The field to serialize
+ * @param any Object to serialize
+ */
+ public void serialize(Output out, Field field, Object any) {
log.debug("serialize");
if (any instanceof IExternalizable) {
// Make sure all IExternalizable objects are serialized as objects
@@ -293,4 +307,24 @@
}
}
+ /**
+ * Checks whether the field should be serialized or not
+ *
+ * @param field The field to be serialized
+ * @return <code>true</code> if the field should be serialized,
otherwise <code>false</code>
+ */
+ public boolean serializeField(Field field) {
+ boolean dontSerialize =
field.isAnnotationPresent(DontSerialize.class);
+ boolean isTransient = Modifier.isTransient(field.getModifiers());
+ boolean isClass = "class".equals(field.getName());
+
+ if (dontSerialize && log.isDebugEnabled()) {
+ log.debug("Skipping {} because its marked with
@DontSerialize", field.getName());
+ }
+ if (isTransient) {
+ log.warn("Using \"transient\" to declare fields not to be
serialized is deprecated and will be removed in Red5 0.8, use
\"@DontSerialize\" instead.");
+ }
+
+ return !(dontSerialize || isTransient || isClass);
+ }
}
Modified:
java/server/trunk/src/org/red5/server/PersistableAttributeStore.java
==============================================================================
--- java/server/trunk/src/org/red5/server/PersistableAttributeStore.java
(original)
+++ java/server/trunk/src/org/red5/server/PersistableAttributeStore.java
Wed Sep 24 22:25:45 2008
@@ -188,7 +188,7 @@
@SuppressWarnings("unchecked")
public void deserialize(Input input) throws IOException {
Deserializer deserializer = new Deserializer();
- setAttributes(deserializer.deserialize(input, Map.class));
+ setAttributes(deserializer.<Map>deserialize(input, Map.class));
}
/**
Modified:
java/server/trunk/src/org/red5/server/net/rtmp/codec/RTMPProtocolDecoder.java
==============================================================================
---
java/server/trunk/src/org/red5/server/net/rtmp/codec/RTMPProtocolDecoder.java
(original)
+++
java/server/trunk/src/org/red5/server/net/rtmp/codec/RTMPProtocolDecoder.java
Wed Sep 24 22:25:45 2008
@@ -818,8 +818,7 @@
log.debug("Action {}", action);
if (header == null || header.getStreamId() == 0) {
- int invokeId = deserializer.deserialize(input, Number.class)
- .intValue();
+ int invokeId = deserializer.<Number>deserialize(input,
Number.class).intValue();
notify.setInvokeId(invokeId);
}
@@ -937,7 +936,7 @@
Input input = new org.red5.io.amf.Input(in);
String action = deserializer.deserialize(input, String.class);
- int invokeId = deserializer.deserialize(input, Number.class).intValue();
+ int invokeId = deserializer.<Number>deserialize(input,
Number.class).intValue();
FlexMessage msg = new FlexMessage();
msg.setInvokeId(invokeId);
Object[] params = new Object[] {};
Modified: java/server/trunk/src/org/red5/server/so/SharedObject.java
==============================================================================
--- java/server/trunk/src/org/red5/server/so/SharedObject.java (original)
+++ java/server/trunk/src/org/red5/server/so/SharedObject.java Wed Sep 24
22:25:45 2008
@@ -648,7 +648,7 @@
Deserializer deserializer = new Deserializer();
name = deserializer.deserialize(input, String.class);
persistentSO = persistent = true;
- super.setAttributes(deserializer.deserialize(input, Map.class));
+ super.setAttributes(deserializer.<Map>deserialize(input, Map.class));
ownerMessage.setName(name);
ownerMessage.setIsPersistent(true);
}
More information about the Red5devs
mailing list