Author: julien_viet
Date: 2010-01-14 14:52:25 -0500 (Thu, 14 Jan 2010)
New Revision: 1302
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:
make factory initialize state
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-14
19:39:39 UTC (rev 1301)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java 2010-01-14
19:52:25 UTC (rev 1302)
@@ -19,17 +19,31 @@
package org.exoplatform.webui.application.replication.factory;
+import org.exoplatform.webui.application.replication.model.FieldModel;
+
+import java.util.Map;
+
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
public final class DefaultObjectFactory extends ObjectFactory<Object>
{
- public <O> O create(Class<O> type) throws CreateException
+ @Override
+ public <S> S create(Class<S> type, Map<FieldModel, ?> state) throws
CreateException
{
try
{
- return type.newInstance();
+ S instance = type.newInstance();
+
+ //
+ for (Map.Entry<FieldModel, ?> entry : state.entrySet())
+ {
+ entry.getKey().setValue(instance, entry.getValue());
+ }
+
+ //
+ return instance;
}
catch (Exception e)
{
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-14
19:39:39 UTC (rev 1301)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java 2010-01-14
19:52:25 UTC (rev 1302)
@@ -19,6 +19,10 @@
package org.exoplatform.webui.application.replication.factory;
+import org.exoplatform.webui.application.replication.model.FieldModel;
+
+import java.util.Map;
+
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
@@ -31,10 +35,11 @@
* Instantiate an object based on the provided class.
*
* @param type the type
+ * @param state the state
* @param <S> the sub type of the base type
* @return the S instance
* @throws CreateException anything wrong that could happen during instance creation
*/
- public abstract <S extends B> S create(Class<S> type) throws
CreateException;
+ public abstract <S extends B> S create(Class<S> type, Map<FieldModel,
?> 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-14
19:39:39 UTC (rev 1301)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-14
19:52:25 UTC (rev 1302)
@@ -57,14 +57,14 @@
this.idToResolutions = new HashMap<Integer, List<Resolution>>();
}
- private <O> O instantiate(ClassTypeModel<O> typeModel) throws
InvalidClassException
+ private <O> O instantiate(ClassTypeModel<O> typeModel, Map<FieldModel,
?> state) throws InvalidClassException
{
try
{
ObjectFactory<? super O> factory =
context.getFactory(typeModel.getObjectType());
//
- return factory.create(typeModel.getObjectType());
+ return factory.create(typeModel.getObjectType(), state);
}
catch (Exception e)
{
@@ -100,13 +100,9 @@
ClassTypeModel<?> typeModel =
(ClassTypeModel)context.getTypeDomain().getTypeModel(clazz);
//
- Object instance = instantiate(typeModel);
-
- //
- idToObject.put(id, instance);
-
- //
+ Map<FieldModel, Object> state = new HashMap<FieldModel,
Object>();
ClassTypeModel<?> currentTypeModel = typeModel;
+ List<Bilto> biltos = new ArrayList<Bilto>();
while (true)
{
for (FieldModel fieldModel : (currentTypeModel).getFields())
@@ -114,29 +110,23 @@
switch (container.readInt())
{
case DataKind.NULL_VALUE:
- fieldModel.setValue(instance, null);
+ state.put(fieldModel, null);
break;
case DataKind.OBJECT_REF:
int refId = container.readInt();
Object refO = idToObject.get(refId);
if (refO != null)
{
- fieldModel.setValue(instance, refO);
+ state.put(fieldModel, refO);
}
else
{
- List<Resolution> resolutions =
idToResolutions.get(refId);
- if (resolutions == null)
- {
- resolutions = new ArrayList<Resolution>();
- idToResolutions.put(refId, resolutions);
- }
- resolutions.add(new Resolution(instance, fieldModel));
+ biltos.add(new Bilto(refId, fieldModel));
}
break;
case DataKind.OBJECT:
Object o = container.readObject();
- fieldModel.setValue(instance, o);
+ state.put(fieldModel, o);
break;
}
@@ -157,6 +147,24 @@
}
//
+ 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));
+ }
+
+ //
+ idToObject.put(id, instance);
+
+ //
List<Resolution> resolutions = idToResolutions.remove(id);
if (resolutions != null)
{
@@ -178,6 +186,21 @@
}
}
+ private static class Bilto
+ {
+ /** . */
+ private final int ref;
+
+ /** . */
+ private final FieldModel fieldModel;
+
+ private Bilto(int ref, FieldModel fieldModel)
+ {
+ this.ref = ref;
+ this.fieldModel = fieldModel;
+ }
+ }
+
private static class Resolution
{
/** . */
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-14
19:39:39 UTC (rev 1301)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java 2010-01-14
19:52:25 UTC (rev 1302)
@@ -21,7 +21,10 @@
import org.exoplatform.webui.application.replication.factory.ObjectFactory;
import org.exoplatform.webui.application.replication.factory.CreateException;
+import org.exoplatform.webui.application.replication.model.FieldModel;
+import java.util.Map;
+
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
@@ -32,7 +35,7 @@
static A2 instance = new A2();
@Override
- public <E extends A2> E create(Class<E> type) throws CreateException
+ public <S extends A2> S create(Class<S> type, Map<FieldModel, ?>
state) throws CreateException
{
if (type == A2.class)
{