Author: julien_viet
Date: 2010-01-16 18:15:12 -0500 (Sat, 16 Jan 2010)
New Revision: 1335
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ConvertedTypeModel.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A1.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A2.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A3.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/api/annotations/Converted.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeModel.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/DataKind.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java
Log:
implement and test type converter
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/api/annotations/Converted.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/api/annotations/Converted.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/api/annotations/Converted.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -22,11 +22,25 @@
import org.exoplatform.webui.application.replication.api.TypeConverter;
import org.exoplatform.webui.application.replication.impl.api.DefaultTypeConverter;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
/**
+ * Annotates a type to be converted to another type for serialization.
+ *
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
+(a)Target(ElementType.TYPE)
+(a)Retention(RetentionPolicy.RUNTIME)
public @interface Converted
{
+ /**
+ * Specify the converter class.
+ *
+ * @return the converter class
+ */
Class<? extends TypeConverter<?, ?>> value();
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -44,7 +44,7 @@
ClassTypeModel(Class<O> type, ClassTypeModel<? super O> superType,
Map<String, FieldModel<O, ?>> fields, boolean serialized)
{
- super(type, superType, fields);
+ super(type, superType);
//
this.superType = superType;
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ConvertedTypeModel.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ConvertedTypeModel.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ConvertedTypeModel.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.application.replication.model;
+
+import org.exoplatform.webui.application.replication.api.TypeConverter;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class ConvertedTypeModel<O, T> extends TypeModel<O>
+{
+
+ /** . */
+ private final TypeModel<T> targetType;
+
+ /** . */
+ private final Class<? extends TypeConverter<O, T>> converterJavaType;
+
+ ConvertedTypeModel(
+ Class<O> javaType,
+ TypeModel<? super O> superType,
+ TypeModel<T> targetType,
+ Class<? extends TypeConverter<O, T>> converterJavaType)
+ {
+ super(javaType, superType);
+
+ //
+ this.targetType = targetType;
+ this.converterJavaType = converterJavaType;
+ }
+
+ public TypeModel<T> getTargetType()
+ {
+ return targetType;
+ }
+
+ public Class<? extends TypeConverter<O, T>> getConverterJavaType()
+ {
+ return converterJavaType;
+ }
+}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -19,10 +19,13 @@
package org.exoplatform.webui.application.replication.model;
+import org.exoplatform.webui.application.replication.api.TypeConverter;
+import org.exoplatform.webui.application.replication.api.annotations.Converted;
import org.exoplatform.webui.application.replication.api.annotations.Serialized;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -49,13 +52,13 @@
}
/** . */
- private final Map<String, TypeModel> typeModelMap;
+ private final Map<String, TypeModel<?>> typeModelMap;
/** . */
- private final Map<String, TypeModel> immutableTypeModelMap;
+ private final Map<String, TypeModel<?>> immutableTypeModelMap;
/** . */
- private final Collection<TypeModel> immutableTypeModelSet;
+ private final Collection<TypeModel<?>> immutableTypeModelSet;
/** . */
private final boolean buildIfAbsent;
@@ -67,9 +70,9 @@
public TypeDomain(boolean buildIfAbsent)
{
- ConcurrentHashMap<String, TypeModel> typeModelMap = new
ConcurrentHashMap<String, TypeModel>();
- Map<String, TypeModel> immutableTypeModelMap =
Collections.unmodifiableMap(typeModelMap);
- Collection<TypeModel> immutableTypeModelSet =
Collections.unmodifiableCollection(typeModelMap.values());
+ ConcurrentHashMap<String, TypeModel<?>> typeModelMap = new
ConcurrentHashMap<String, TypeModel<?>>();
+ Map<String, TypeModel<?>> immutableTypeModelMap =
Collections.unmodifiableMap(typeModelMap);
+ Collection<TypeModel<?>> immutableTypeModelSet =
Collections.unmodifiableCollection(typeModelMap.values());
//
this.typeModelMap = typeModelMap;
@@ -78,7 +81,7 @@
this.buildIfAbsent = buildIfAbsent;
}
- public Map<String, TypeModel> getTypeModelMap()
+ public Map<String, TypeModel<?>> getTypeModelMap()
{
return immutableTypeModelMap;
}
@@ -88,12 +91,12 @@
return buildIfAbsent;
}
- public Collection<TypeModel> getTypeModels()
+ public Collection<TypeModel<?>> getTypeModels()
{
return immutableTypeModelSet;
}
- public TypeModel getTypeModel(String typeName)
+ public TypeModel<?> getTypeModel(String typeName)
{
if (typeName == null)
{
@@ -102,15 +105,15 @@
return typeModelMap.get(typeName);
}
- public TypeModel getTypeModel(Class<?> javaType)
+ public <O> TypeModel<O> getTypeModel(Class<O> javaType)
{
if (javaType == null)
{
throw new NullPointerException();
}
- //
- TypeModel typeModel = typeModelMap.get(javaType.getName());
+ // Cast OK
+ TypeModel<O> typeModel =
(TypeModel<O>)typeModelMap.get(javaType.getName());
//
if (typeModel == null && buildIfAbsent)
@@ -133,6 +136,8 @@
// Build the missing types required to have knowledge about the
// provided java type
Map<String, TypeModel<?>> addedTypeModels = new HashMap<String,
TypeModel<?>>();
+
+ //
TypeModel<O> model = build(javaType, addedTypeModels);
// Perform merge
@@ -157,58 +162,131 @@
throw new IllegalArgumentException("No primitive type accepted");
}
- // Cast OK
- TypeModel<O> typeModel = (TypeModel<O>)get(javaType, addedTypeModels);
+ //
+ TypeModel<O> typeModel = get(javaType, addedTypeModels);
//
if (typeModel == null)
{
- boolean replicated = javaType.getAnnotation(Serialized.class) != null;
+ boolean serialized = javaType.getAnnotation(Serialized.class) != null;
+ boolean converted = javaType.getAnnotation(Converted.class) != null;
//
- ClassTypeModel<? super O> superTypeModel = null;
- if (javaType.getSuperclass() != null)
+ if (serialized)
{
- TypeModel<? super O> builtType = build(javaType.getSuperclass(),
addedTypeModels);
- if (builtType instanceof ClassTypeModel)
+ if (converted)
{
- superTypeModel = (ClassTypeModel<? super O>)builtType;
- }
- else
- {
throw new TypeException();
}
+ typeModel = buildClassType(javaType, addedTypeModels, true);
}
+ else if (converted)
+ {
+ typeModel = buildConvertedType(javaType, addedTypeModels);
+ }
+ else
+ {
+ typeModel = buildClassType(javaType, addedTypeModels, false);
+ }
+ }
- //
- TreeMap<String, FieldModel<O, ?>> fieldModels = new
TreeMap<String, FieldModel<O, ?>>();
+ //
+ return typeModel;
+ }
- //
- typeModel = new ClassTypeModel<O>(javaType, superTypeModel, fieldModels,
replicated);
+ private <O> ConvertedTypeModel<O, ?> buildConvertedType(Class<O>
javaType, Map<String, TypeModel<?>> addedTypeModels)
+ {
+ Converted converted = javaType.getAnnotation(Converted.class);
- //
- addedTypeModels.put(javaType.getName(), typeModel);
+ //
+ Class<? extends TypeConverter<?, ?>> converterClass =
converted.value();
+ ParameterizedType converterParameterizedType =
(ParameterizedType)converterClass.getGenericSuperclass();
- // Now build fields
- for (Field field : javaType.getDeclaredFields())
+ //
+ if (!converterParameterizedType.getActualTypeArguments()[0].equals(javaType))
+ {
+ throw new TypeException();
+ }
+
+ //
+ Class<? extends TypeConverter<O, ?>> converterJavaType =
(Class<TypeConverter<O, ?>>)converted.value();
+
+ //
+ return buildConvertedType(javaType, addedTypeModels, converterJavaType);
+ }
+
+ private <O, T> ConvertedTypeModel<O, T> buildConvertedType(
+ Class<O> javaType,
+ Map<String, TypeModel<?>> addedTypeModels,
+ Class<? extends TypeConverter<O, ? /* This is a bit funky and nasty, need to
investigate*/ >> converterJavaType)
+ {
+ Class<T> outputClass =
(Class<T>)((ParameterizedType)converterJavaType.getGenericSuperclass()).getActualTypeArguments()[1];
+
+ //
+ ClassTypeModel<T> targetType = (ClassTypeModel<T>)build(outputClass,
addedTypeModels);
+
+ //
+ TypeModel<? super O> superType = null;
+ Class<? super O> superJavaType = javaType.getSuperclass();
+ if (superJavaType != null)
+ {
+ superType = build(superJavaType, addedTypeModels);
+ }
+
+ //
+ ConvertedTypeModel<O, T> typeModel = new ConvertedTypeModel<O,
T>(javaType, superType, targetType, (Class<TypeConverter<O, T>>)
converterJavaType);
+
+ //
+ addedTypeModels.put(typeModel.getName(), typeModel);
+
+ //
+ return typeModel;
+ }
+
+ private <O> ClassTypeModel<O> buildClassType(Class<O> javaType,
Map<String, TypeModel<?>> addedTypeModels, boolean serialized)
+ {
+ ClassTypeModel<? super O> superTypeModel = null;
+ if (javaType.getSuperclass() != null)
+ {
+ TypeModel<? super O> builtType = build(javaType.getSuperclass(),
addedTypeModels);
+ if (builtType instanceof ClassTypeModel)
{
- if (!Modifier.isStatic(field.getModifiers()))
- {
- field.setAccessible(true);
- Class<?> fieldJavaType = field.getType();
+ superTypeModel = (ClassTypeModel<? super O>)builtType;
+ }
+ else
+ {
+ throw new TypeException();
+ }
+ }
- // Replace if a primitive
- if (fieldJavaType.isPrimitive())
- {
- fieldJavaType = primitiveToWrapperMap.get(fieldJavaType);
- }
+ //
+ TreeMap<String, FieldModel<O, ?>> fieldModels = new TreeMap<String,
FieldModel<O, ?>>();
- TypeModel<?> fieldTypeModel = build(fieldJavaType,
addedTypeModels);
- if (fieldTypeModel != null)
- {
- fieldModels.put(field.getName(), createField(typeModel, field,
fieldTypeModel));
- }
+ //
+ ClassTypeModel<O> typeModel = new ClassTypeModel<O>(javaType,
superTypeModel, fieldModels, serialized);
+
+ //
+ addedTypeModels.put(javaType.getName(), typeModel);
+
+ // Now build fields
+ for (Field field : javaType.getDeclaredFields())
+ {
+ if (!Modifier.isStatic(field.getModifiers()))
+ {
+ field.setAccessible(true);
+ Class<?> fieldJavaType = field.getType();
+
+ // Replace if a primitive
+ if (fieldJavaType.isPrimitive())
+ {
+ fieldJavaType = primitiveToWrapperMap.get(fieldJavaType);
}
+
+ TypeModel<?> fieldTypeModel = build(fieldJavaType, addedTypeModels);
+ if (fieldTypeModel != null)
+ {
+ fieldModels.put(field.getName(), createField(typeModel, field,
fieldTypeModel));
+ }
}
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeModel.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeModel.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeModel.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -19,10 +19,6 @@
package org.exoplatform.webui.application.replication.model;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
@@ -36,7 +32,7 @@
/** . */
private final TypeModel<? super O> superType;
- TypeModel(Class<O> javaType, TypeModel<? super O> superType,
Map<String, FieldModel<O, ?>> fields)
+ TypeModel(Class<O> javaType, TypeModel<? super O> superType)
{
this.javaType = javaType;
this.superType = superType;
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/DataKind.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/DataKind.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/DataKind.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -29,6 +29,6 @@
public static final int OBJECT = 0;
public static final int NULL_VALUE = 1;
public static final int OBJECT_REF = 2;
- public static final int SERIALIZABLE_OBJECT = 3;
+ public static final int CONVERTED_OBJECT = 3;
}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -20,8 +20,10 @@
package org.exoplatform.webui.application.replication.serial;
import org.exoplatform.webui.application.replication.SerializationContext;
+import org.exoplatform.webui.application.replication.api.TypeConverter;
import org.exoplatform.webui.application.replication.api.factory.ObjectFactory;
import org.exoplatform.webui.application.replication.model.ClassTypeModel;
+import org.exoplatform.webui.application.replication.model.ConvertedTypeModel;
import org.exoplatform.webui.application.replication.model.FieldModel;
import java.io.*;
@@ -177,33 +179,81 @@
}
}
+ private Object read(DataContainer container) throws IOException
+ {
+ int sw = container.readInt();
+ switch (sw)
+ {
+ case DataKind.OBJECT_REF:
+ {
+ int id = container.readInt();
+ Object o1 = idToObject.get(id);
+ if (o1 == null)
+ {
+ throw new AssertionError();
+ }
+ return o1;
+ }
+ case DataKind.OBJECT:
+ {
+ int id = container.readInt();
+ Class<?> clazz = (Class)container.readObject();
+ ClassTypeModel<?> typeModel =
(ClassTypeModel<?>)context.getTypeDomain().getTypeModel(clazz);
+ return instantiate(id, container, typeModel);
+ }
+ case DataKind.CONVERTED_OBJECT:
+ {
+ Class<?> tclazz = (Class<?>)container.readObject();
+ ConvertedTypeModel<?, ?> ctm = (ConvertedTypeModel<?,?>)
context.getTypeDomain().getTypeModel(tclazz);
+ return convertObject(container, ctm);
+ }
+ default:
+ throw new StreamCorruptedException("Unrecognized data " + sw);
+ }
+ }
+
+ private <O, T> O convertObject(DataContainer container, ConvertedTypeModel<O,
T> convertedType) throws IOException
+ {
+ Object inner = resolveObject(container);
+ T t = convertedType.getTargetType().getJavaType().cast(inner);
+
+ TypeConverter<O, T> converter;
+ try
+ {
+ converter = convertedType.getConverterJavaType().newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new AssertionError(e);
+ }
+
+ //
+ O o;
+ try
+ {
+ o = converter.read(t);
+ }
+ catch (Exception e)
+ {
+ InvalidObjectException ioe = new InvalidObjectException("The object "
+ t + " conversion throw an exception ");
+ ioe.initCause(e);
+ throw ioe;
+ }
+ if (o == null)
+ {
+ throw new InvalidObjectException("The object " + t + " was
converted to null by converter " + converter);
+ }
+
+ //
+ return o;
+ }
+
@Override
protected Object resolveObject(Object obj) throws IOException
{
if (obj instanceof DataContainer)
{
- DataContainer container = (DataContainer) obj;
-
- int id;
- int sw = container.readInt();
- switch (sw)
- {
- case DataKind.OBJECT_REF:
- id = container.readInt();
- Object o1 = idToObject.get(id);
- if (o1 == null)
- {
- throw new AssertionError();
- }
- return o1;
- case DataKind.OBJECT:
- id = container.readInt();
- Class<?> clazz = (Class) container.readObject();
- ClassTypeModel<?> typeModel =
(ClassTypeModel<?>)context.getTypeDomain().getTypeModel(clazz);
- return instantiate(id, container, typeModel);
- default:
- throw new StreamCorruptedException("Unrecognized data " + sw);
- }
+ return read((DataContainer)obj);
}
else
{
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java 2010-01-16
14:24:10 UTC (rev 1334)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -20,8 +20,11 @@
package org.exoplatform.webui.application.replication.serial;
import org.exoplatform.webui.application.replication.SerializationContext;
+import org.exoplatform.webui.application.replication.api.TypeConverter;
import org.exoplatform.webui.application.replication.model.ClassTypeModel;
+import org.exoplatform.webui.application.replication.model.ConvertedTypeModel;
import org.exoplatform.webui.application.replication.model.FieldModel;
+import org.exoplatform.webui.application.replication.model.TypeModel;
import java.io.*;
import java.util.IdentityHashMap;
@@ -58,8 +61,70 @@
return nextId;
}
- private <O> void write(DataContainer output, ClassTypeModel<O> typeModel,
O obj) throws IOException
+ private void write(Object obj, DataContainer output) throws IOException
{
+ Class objClass = obj.getClass();
+ TypeModel typeModel = context.getTypeDomain().getTypeModel(objClass);
+
+ //
+ if (typeModel == null)
+ {
+ throw new NotSerializableException("Object " + obj + " does not
have its type described");
+ }
+
+ //
+ if (typeModel instanceof ClassTypeModel)
+ {
+ // This is obviously unchecked because of the fact that Object.getClass()
returns Class<?>
+ // need to find a work around for that
+ write((ClassTypeModel)typeModel, obj, output);
+ }
+ else
+ {
+ write((ConvertedTypeModel)typeModel, obj, output);
+ }
+ }
+
+ private <O, T> void write(ConvertedTypeModel<O, T> typeModel, O obj,
DataContainer output) throws IOException
+ {
+ Class<? extends TypeConverter<O, T>> converterClass =
typeModel.getConverterJavaType();
+
+ //
+ TypeConverter<O, T> converter;
+ try
+ {
+ converter = converterClass.newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new AssertionError(e);
+ }
+
+ //
+ T target;
+ try
+ {
+ target = converter.write(obj);
+ }
+ catch (Exception e)
+ {
+ InvalidObjectException ioe = new InvalidObjectException("The object "
+ obj + " conversion threw an exception ");
+ ioe.initCause(e);
+ throw ioe;
+ }
+ if (target == null)
+ {
+ throw new InvalidObjectException("The object " + obj + " was
converted to null by converter " + converter);
+ }
+
+ //
+ output.writeInt(DataKind.CONVERTED_OBJECT);
+ output.writeObject(typeModel.getJavaType());
+ write(target, output);
+ }
+
+ private <O> void write(ClassTypeModel<O> typeModel, O obj, DataContainer
output) throws IOException
+ {
if (!typeModel.isSerialized())
{
throw new NotSerializableException("Type " + typeModel + " cannot
be serialized");
@@ -154,23 +219,13 @@
Integer id = objectToId.get(obj);
if (id != null)
{
+ output = new DataContainer();
output.writeInt(DataKind.OBJECT_REF);
output.writeObject(id);
}
else
{
- Class objClass = obj.getClass();
- ClassTypeModel typeModel =
(ClassTypeModel)context.getTypeDomain().getTypeModel(objClass);
-
- //
- if (typeModel == null)
- {
- throw new NotSerializableException("Object " + obj + " does
not have its type described");
- }
-
- // This is obviously unchecked because of the fact that Object.getClass()
returns Class<?>
- // need to find a work around for that
- write(output, typeModel, obj);
+ write(obj, output);
}
//
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A1.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A1.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A1.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.replication.converter;
+
+import org.exoplatform.webui.application.replication.api.annotations.Converted;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+(a)Converted(A3.class)
+public class A1
+{
+
+ String state;
+
+ public A1()
+ {
+ }
+
+ public A1(String state)
+ {
+ this.state = state;
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A2.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A2.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A2.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.replication.converter;
+
+import org.exoplatform.webui.application.replication.api.annotations.Serialized;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+@Serialized
+public class A2
+{
+
+ String state;
+
+ public A2()
+ {
+ }
+
+ public A2(String state)
+ {
+
+ this.state = state;
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A3.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A3.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/A3.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.replication.converter;
+
+import junit.framework.AssertionFailedError;
+import org.exoplatform.webui.application.replication.api.TypeConverter;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class A3 extends TypeConverter<A1, A2>
+{
+
+ /** . */
+ public static TypeConverter<A1, A2> delegate;
+
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ if (delegate == null)
+ {
+ throw new AssertionFailedError();
+ }
+ return delegate.write(input);
+ }
+
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ if (delegate == null)
+ {
+ throw new AssertionFailedError();
+ }
+ return delegate.read(output);
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java 2010-01-16
23:15:12 UTC (rev 1335)
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.replication.converter;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+import org.exoplatform.webui.application.replication.SerializationContext;
+import org.exoplatform.webui.application.replication.api.TypeConverter;
+import org.exoplatform.webui.application.replication.model.TypeDomain;
+
+import java.io.IOException;
+import java.io.InvalidObjectException;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestConverter extends TestCase
+{
+
+ public void testConvert() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A1.class);
+ A1 a = new A1("foo");
+
+ //
+ A3.delegate = new TypeConverter<A1, A2>()
+ {
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ return new A2(input.state);
+ }
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ return new A1(output.state);
+ }
+ };
+ SerializationContext context = new SerializationContext(domain);
+ a = context.clone(a);
+ assertEquals("foo", a.state);
+ }
+
+ public void testConverterWriteThrowsException() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A1.class);
+ A1 a = new A1("foo");
+
+ //
+ final Exception e = new Exception();
+ A3.delegate = new TypeConverter<A1, A2>()
+ {
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ throw e;
+ }
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ throw new AssertionFailedError();
+ }
+ };
+ SerializationContext context = new SerializationContext(domain);
+ try
+ {
+ a = context.clone(a);
+ fail();
+ }
+ catch (InvalidObjectException ioe)
+ {
+ assertSame(e, ioe.getCause());
+ }
+ }
+
+ public void testConverterWriteReturnsNull() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A1.class);
+ A1 a = new A1("foo");
+
+ //
+ A3.delegate = new TypeConverter<A1, A2>()
+ {
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ return null;
+ }
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ throw new AssertionFailedError();
+ }
+ };
+ SerializationContext context = new SerializationContext(domain);
+ try
+ {
+ a = context.clone(a);
+ fail();
+ }
+ catch (InvalidObjectException e)
+ {
+ }
+ }
+
+ public void testConverterReadReturnsNull() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A1.class);
+ A1 a = new A1("foo");
+
+ //
+ A3.delegate = new TypeConverter<A1, A2>()
+ {
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ return new A2(input.state);
+ }
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ return null;
+ }
+ };
+ SerializationContext context = new SerializationContext(domain);
+ try
+ {
+ a = context.clone(a);
+ fail();
+ }
+ catch (InvalidObjectException e)
+ {
+ }
+ }
+
+ public void testConverterReadThrowsException() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A1.class);
+ A1 a = new A1("foo");
+
+ //
+ final Exception e = new Exception();
+ A3.delegate = new TypeConverter<A1, A2>()
+ {
+ @Override
+ public A2 write(A1 input) throws Exception
+ {
+ return new A2(input.state);
+ }
+ @Override
+ public A1 read(A2 output) throws Exception
+ {
+ throw e;
+ }
+ };
+ SerializationContext context = new SerializationContext(domain);
+ try
+ {
+ a = context.clone(a);
+ fail();
+ }
+ catch (InvalidObjectException ioe)
+ {
+ assertSame(e, ioe.getCause());
+ }
+ }
+}