Author: julien_viet
Date: 2010-01-16 19:17:36 -0500 (Sat, 16 Jan 2010)
New Revision: 1336
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/SerializationMode.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B1.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B2.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B3.java
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/TypeDomain.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
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java
Log:
implement converter type that converts to java.io.Serializable
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
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -40,9 +40,9 @@
private final Map<String, FieldModel<O, ?>> immutableFields;
/** . */
- private final boolean serialized;
+ private final SerializationMode serializationMode;
- ClassTypeModel(Class<O> type, ClassTypeModel<? super O> superType,
Map<String, FieldModel<O, ?>> fields, boolean serialized)
+ ClassTypeModel(Class<O> type, ClassTypeModel<? super O> superType,
Map<String, FieldModel<O, ?>> fields, SerializationMode serializationMode)
{
super(type, superType);
@@ -50,7 +50,7 @@
this.superType = superType;
this.fields = fields;
this.immutableFields = Collections.unmodifiableMap(fields);
- this.serialized = serialized;
+ this.serializationMode = serializationMode;
}
@Override
@@ -59,9 +59,9 @@
return superType;
}
- public boolean isSerialized()
+ public SerializationMode getSerializationMode()
{
- return serialized;
+ return serializationMode;
}
public Collection<FieldModel<O, ?>> getFields()
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/SerializationMode.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/SerializationMode.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/SerializationMode.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public enum SerializationMode
+{
+
+ SERIALIZED,
+
+ SERIALIZABLE,
+
+ NONE
+
+
+}
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
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -23,6 +23,7 @@
import org.exoplatform.webui.application.replication.api.annotations.Converted;
import org.exoplatform.webui.application.replication.api.annotations.Serialized;
+import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
@@ -263,9 +264,24 @@
TreeMap<String, FieldModel<O, ?>> fieldModels = new TreeMap<String,
FieldModel<O, ?>>();
//
- ClassTypeModel<O> typeModel = new ClassTypeModel<O>(javaType,
superTypeModel, fieldModels, serialized);
+ SerializationMode serializationMode;
+ if (serialized)
+ {
+ serializationMode = SerializationMode.SERIALIZED;
+ }
+ else if (Serializable.class.isAssignableFrom(javaType))
+ {
+ serializationMode = SerializationMode.SERIALIZABLE;
+ }
+ else
+ {
+ serializationMode = SerializationMode.NONE;
+ }
//
+ ClassTypeModel<O> typeModel = new ClassTypeModel<O>(javaType,
superTypeModel, fieldModels, serializationMode);
+
+ //
addedTypeModels.put(javaType.getName(), typeModel);
// Now build fields
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
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/DataKind.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -30,5 +30,6 @@
public static final int NULL_VALUE = 1;
public static final int OBJECT_REF = 2;
public static final int CONVERTED_OBJECT = 3;
+ public static final int SERIALIZED_OBJECT = 4;
}
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
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -109,7 +109,6 @@
Object o = container.readObject();
state.put(fieldModel, o);
break;
-
}
}
}
@@ -207,6 +206,8 @@
ConvertedTypeModel<?, ?> ctm = (ConvertedTypeModel<?,?>)
context.getTypeDomain().getTypeModel(tclazz);
return convertObject(container, ctm);
}
+ case DataKind.SERIALIZED_OBJECT:
+ return container.readObject();
default:
throw new StreamCorruptedException("Unrecognized data " + sw);
}
@@ -228,14 +229,14 @@
}
//
- O o;
+ O o = null;
try
{
o = converter.read(t);
}
catch (Exception e)
{
- InvalidObjectException ioe = new InvalidObjectException("The object "
+ t + " conversion throw an exception ");
+ InvalidObjectException ioe = new InvalidObjectException("The object "
+ t + " conversion throw an exception " + converter);
ioe.initCause(e);
throw ioe;
}
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
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -21,10 +21,7 @@
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 org.exoplatform.webui.application.replication.model.*;
import java.io.*;
import java.util.IdentityHashMap;
@@ -125,79 +122,86 @@
private <O> void write(ClassTypeModel<O> typeModel, O obj, DataContainer
output) throws IOException
{
- if (!typeModel.isSerialized())
+ if (typeModel.getSerializationMode() == SerializationMode.SERIALIZED)
{
- throw new NotSerializableException("Type " + typeModel + " cannot
be serialized");
- }
+ //
+ output.writeInt(DataKind.OBJECT);
+ output.writeInt(register(obj));
+ output.writeObject(typeModel.getJavaType());
- //
- output.writeInt(DataKind.OBJECT);
- output.writeInt(register(obj));
- output.writeObject(typeModel.getJavaType());
-
- //
- SerializationStatus status = SerializationStatus.NONE;
- for (ClassTypeModel<? super O> currentTypeModel = typeModel;currentTypeModel
!= null;currentTypeModel = currentTypeModel.getSuperType())
- {
- if (currentTypeModel instanceof ClassTypeModel<?>)
+ //
+ SerializationStatus status = SerializationStatus.NONE;
+ for (ClassTypeModel<? super O> currentTypeModel =
typeModel;currentTypeModel != null;currentTypeModel = currentTypeModel.getSuperType())
{
- for (FieldModel<?, ?> fieldModel : currentTypeModel.getFields())
+ if (currentTypeModel instanceof ClassTypeModel<?>)
{
- if (!fieldModel.isTransient())
+ for (FieldModel<?, ?> fieldModel : currentTypeModel.getFields())
{
- Object fieldValue = fieldModel.get(obj);
- if (fieldValue == null)
+ if (!fieldModel.isTransient())
{
- output.writeObject(DataKind.NULL_VALUE);
- }
- else
- {
- Integer fieldValueId = objectToId.get(fieldValue);
- if (fieldValueId != null)
+ Object fieldValue = fieldModel.get(obj);
+ if (fieldValue == null)
{
- output.writeObject(DataKind.OBJECT_REF);
- output.writeInt(fieldValueId);
+ output.writeObject(DataKind.NULL_VALUE);
}
else
{
- output.writeObject(DataKind.OBJECT);
- output.writeObject(fieldValue);
+ Integer fieldValueId = objectToId.get(fieldValue);
+ if (fieldValueId != null)
+ {
+ output.writeObject(DataKind.OBJECT_REF);
+ output.writeInt(fieldValueId);
+ }
+ else
+ {
+ output.writeObject(DataKind.OBJECT);
+ output.writeObject(fieldValue);
+ }
}
}
}
- }
- switch (status)
- {
- case NONE:
- status = SerializationStatus.FULL;
- break;
- }
- }
- else
- {
- if (!currentTypeModel.getFields().isEmpty())
- {
switch (status)
{
- case FULL:
- status = SerializationStatus.PARTIAL;
+ case NONE:
+ status = SerializationStatus.FULL;
break;
}
}
+ else
+ {
+ if (!currentTypeModel.getFields().isEmpty())
+ {
+ switch (status)
+ {
+ case FULL:
+ status = SerializationStatus.PARTIAL;
+ break;
+ }
+ }
+ }
}
+
+ //
+ switch (status)
+ {
+ case FULL:
+ break;
+ case PARTIAL:
+ System.out.println("Partial serialization of object " + obj);
+ break;
+ case NONE:
+ throw new NotSerializableException("Type " + typeModel + "
is not serializable");
+ }
}
-
- //
- switch (status)
+ else if (typeModel.getSerializationMode() == SerializationMode.SERIALIZABLE)
{
- case FULL:
- break;
- case PARTIAL:
- System.out.println("Partial serialization of object " + obj);
- break;
- case NONE:
- throw new NotSerializableException("Type " + typeModel + " is
not serializable");
+ output.writeInt(DataKind.SERIALIZED_OBJECT);
+ output.writeObject(obj);
}
+ else
+ {
+ throw new NotSerializableException("Type " + typeModel + " is not
serializable");
+ }
}
@Override
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B1.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B1.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B1.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -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(B3.class)
+public class B1
+{
+
+ String state;
+
+ public B1()
+ {
+ }
+
+ public B1(String state)
+ {
+ this.state = state;
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B2.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B2.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B2.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -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;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class B2 implements Serializable
+{
+
+ String state;
+
+ public B2()
+ {
+ }
+
+ public B2(String state)
+ {
+ this.state = state;
+ }
+}
\ No newline at end of file
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B3.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B3.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/B3.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -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 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 B3 extends TypeConverter<B1, B2>
+{
+
+ @Override
+ public B2 write(B1 input) throws Exception
+ {
+ return new B2(input.state);
+ }
+
+ @Override
+ public B1 read(B2 output) throws Exception
+ {
+ return new B1(output.state);
+ }
+}
\ No newline at end of file
Modified:
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 2010-01-16
23:15:12 UTC (rev 1335)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/converter/TestConverter.java 2010-01-17
00:17:36 UTC (rev 1336)
@@ -35,7 +35,7 @@
public class TestConverter extends TestCase
{
- public void testConvert() throws Exception
+ public void testConvertSerializedType() throws Exception
{
TypeDomain domain = new TypeDomain();
domain.add(A1.class);
@@ -60,6 +60,18 @@
assertEquals("foo", a.state);
}
+ public void testConvertSerializableType() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(B1.class);
+ B1 b = new B1("foo");
+
+ //
+ SerializationContext context = new SerializationContext(domain);
+ b = context.clone(b);
+ assertEquals("foo", b.state);
+ }
+
public void testConverterWriteThrowsException() throws Exception
{
TypeDomain domain = new TypeDomain();