Author: julien_viet
Date: 2010-01-16 07:38:39 -0500 (Sat, 16 Jan 2010)
New Revision: 1332
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java
Log:
a bit more type safe
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java 2010-01-16
12:08:06 UTC (rev 1331)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java 2010-01-16
12:38:39 UTC (rev 1332)
@@ -30,14 +30,14 @@
public final class DefaultObjectFactory extends ObjectFactory<Object>
{
@Override
- public <S> S create(Class<S> type, Map<FieldModel<?, ?>, ?>
state) throws CreateException
+ public <S> S create(Class<S> type, Map<FieldModel<? super S, ?>,
?> state) throws CreateException
{
try
{
S instance = type.newInstance();
//
- for (Map.Entry<FieldModel<?, ?>, ?> entry : state.entrySet())
+ for (Map.Entry<FieldModel<? super S, ?>, ?> entry :
state.entrySet())
{
FieldModel<?, ?> fieldModel = entry.getKey();
Object value = entry.getValue();
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java 2010-01-16
12:08:06 UTC (rev 1331)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java 2010-01-16
12:38:39 UTC (rev 1332)
@@ -40,6 +40,6 @@
* @return the S instance
* @throws CreateException anything wrong that could happen during instance creation
*/
- public abstract <S extends B> S create(Class<S> type,
Map<FieldModel<?, ?>, ?> state) throws CreateException;
+ public abstract <S extends B> S create(Class<S> type,
Map<FieldModel<? super S, ?>, ?> state) throws CreateException;
}
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
12:08:06 UTC (rev 1331)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-16
12:38:39 UTC (rev 1332)
@@ -42,7 +42,7 @@
private final Map<Integer, Object> idToObject;
/** . */
- private final Map<Integer, List<Resolution>> idToResolutions;
+ private final Map<Integer, List<FutureFieldUpdate<?>>>
idToResolutions;
public ObjectReader(SerializationContext context, InputStream in) throws IOException
{
@@ -54,10 +54,10 @@
//
this.context = context;
this.idToObject = new HashMap<Integer, Object>();
- this.idToResolutions = new HashMap<Integer, List<Resolution>>();
+ this.idToResolutions = new HashMap<Integer,
List<FutureFieldUpdate<?>>>();
}
- private <O> O instantiate(ReplicatableTypeModel<O> typeModel,
Map<FieldModel<?, ?>, ?> state) throws InvalidClassException
+ private <O> O instantiate(ReplicatableTypeModel<O> typeModel,
Map<FieldModel<? super O, ?>, ?> state) throws InvalidClassException
{
try
{
@@ -74,140 +74,141 @@
}
}
- @Override
- protected Object resolveObject(Object obj) throws IOException
+ protected <O> O instantiate(int id, DataContainer container,
ReplicatableTypeModel<O> typeModel) throws IOException
{
- if (obj instanceof DataContainer)
+ Map<FieldModel<? super O, ?>, Object> state = new
HashMap<FieldModel<? super O, ?>, Object>();
+ TypeModel<? super O> currentTypeModel = typeModel;
+ List<FieldUpdate<O>> sets = new
ArrayList<FieldUpdate<O>>();
+ while (currentTypeModel != null)
{
- DataContainer container = (DataContainer) obj;
-
- int id;
- int sw = container.readInt();
- switch (sw)
+ if (currentTypeModel instanceof ReplicatableTypeModel)
{
- case DataKind.OBJECT_REF:
- id = container.readInt();
- Object o1 = idToObject.get(id);
- if (o1 == null)
+ for (FieldModel<? super O, ?> fieldModel :
currentTypeModel.getFields())
+ {
+ if (!fieldModel.isTransient())
{
- throw new AssertionError();
- }
- return o1;
- case DataKind.OBJECT:
- id = container.readInt();
- Class<?> clazz = (Class) container.readObject();
-
- ReplicatableTypeModel<?> typeModel =
(ReplicatableTypeModel)context.getTypeDomain().getTypeModel(clazz);
-
- //
- Map<FieldModel<?, ?>, Object> state = new
HashMap<FieldModel<?, ?>, Object>();
- TypeModel<?> currentTypeModel = typeModel;
- List<Bilto> biltos = new ArrayList<Bilto>();
- while (currentTypeModel != null)
- {
- if (currentTypeModel instanceof ReplicatableTypeModel)
+ switch (container.readInt())
{
- for (FieldModel<?, ?> fieldModel :
currentTypeModel.getFields())
- {
- if (!fieldModel.isTransient())
+ case DataKind.NULL_VALUE:
+ state.put(fieldModel, null);
+ break;
+ case DataKind.OBJECT_REF:
+ int refId = container.readInt();
+ Object refO = idToObject.get(refId);
+ if (refO != null)
{
- switch (container.readInt())
- {
- case DataKind.NULL_VALUE:
- state.put(fieldModel, null);
- break;
- case DataKind.OBJECT_REF:
- int refId = container.readInt();
- Object refO = idToObject.get(refId);
- if (refO != null)
- {
- state.put(fieldModel, refO);
- }
- else
- {
- biltos.add(new Bilto(refId, fieldModel));
- }
- break;
- case DataKind.OBJECT:
- Object o = container.readObject();
- state.put(fieldModel, o);
- break;
-
- }
+ state.put(fieldModel, refO);
}
- }
- }
- currentTypeModel = currentTypeModel.getSuperType();
- }
+ else
+ {
+ sets.add(new FieldUpdate<O>(refId, fieldModel));
+ }
+ break;
+ case DataKind.OBJECT:
+ Object o = container.readObject();
+ state.put(fieldModel, o);
+ break;
- //
- Object instance = instantiate(typeModel, state);
-
- //
- for (Bilto bilto : biltos)
- {
- List<Resolution> resolutions = idToResolutions.get(bilto.ref);
- if (resolutions == null)
- {
- resolutions = new ArrayList<Resolution>();
- idToResolutions.put(bilto.ref, resolutions);
}
- resolutions.add(new Resolution(instance, bilto.fieldModel));
}
+ }
+ }
+ currentTypeModel = currentTypeModel.getSuperType();
+ }
- //
- idToObject.put(id, instance);
+ //
+ O instance = instantiate(typeModel, state);
- //
- List<Resolution> resolutions = idToResolutions.remove(id);
- if (resolutions != null)
- {
- for (Resolution resolution : resolutions)
- {
- resolution.fieldModel.set(resolution.target, instance);
- }
- }
-
- //
- return instance;
- default:
- throw new StreamCorruptedException("Unrecognized data " + sw);
+ // Create future field updates
+ for (FieldUpdate<O> set : sets)
+ {
+ List<FutureFieldUpdate<?>> resolutions =
idToResolutions.get(set.ref);
+ if (resolutions == null)
+ {
+ resolutions = new ArrayList<FutureFieldUpdate<?>>();
+ idToResolutions.put(set.ref, resolutions);
}
+ resolutions.add(new FutureFieldUpdate<O>(instance, set.fieldModel));
}
- else
+
+ //
+ idToObject.put(id, instance);
+
+ // Resolve future field updates
+ List<FutureFieldUpdate<?>> resolutions = idToResolutions.remove(id);
+ if (resolutions != null)
{
- return obj;
+ for (FutureFieldUpdate<?> resolution : resolutions)
+ {
+ resolution.fieldModel.castAndSet(resolution.target, instance);
+ }
}
+
+ //
+ return instance;
}
- private static class Bilto
+ private static class FieldUpdate<O>
{
/** . */
private final int ref;
/** . */
- private final FieldModel fieldModel;
+ private final FieldModel<? super O, ?> fieldModel;
- private Bilto(int ref, FieldModel fieldModel)
+ private FieldUpdate(int ref, FieldModel<? super O, ?> fieldModel)
{
this.ref = ref;
this.fieldModel = fieldModel;
}
}
- private static class Resolution
+ private static class FutureFieldUpdate<O>
{
/** . */
- private final Object target;
+ private final O target;
/** . */
- private final FieldModel fieldModel;
+ private final FieldModel<? super O, ?> fieldModel;
- private Resolution(Object target, FieldModel fieldModel)
+ private FutureFieldUpdate(O target, FieldModel<? super O, ?> fieldModel)
{
this.target = target;
this.fieldModel = fieldModel;
}
}
+ @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();
+ ReplicatableTypeModel<?> typeModel =
(ReplicatableTypeModel)context.getTypeDomain().getTypeModel(clazz);
+ return instantiate(id, container, typeModel);
+ default:
+ throw new StreamCorruptedException("Unrecognized data " + sw);
+ }
+ }
+ else
+ {
+ return obj;
+ }
+ }
}
Modified:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java 2010-01-16
12:08:06 UTC (rev 1331)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java 2010-01-16
12:38:39 UTC (rev 1332)
@@ -35,7 +35,7 @@
static A2 instance = new A2();
@Override
- public <S extends A2> S create(Class<S> type, Map<FieldModel<?,
?>, ?> state) throws CreateException
+ public <S extends A2> S create(Class<S> type, Map<FieldModel<? super
S, ?>, ?> state) throws CreateException
{
if (type == A2.class)
{