Author: julien_viet
Date: 2010-01-14 08:42:30 -0500 (Thu, 14 Jan 2010)
New Revision: 1291
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/CreateException.java
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/test/java/org/exoplatform/webui/replication/factory/
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/A2.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/TestObjectFactory.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/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/TestSerialization.java
Log:
need to add support for object factory
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/CreateException.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/CreateException.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/CreateException.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,46 @@
+/*
+ * 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.factory;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class CreateException extends Exception
+{
+ public CreateException()
+ {
+ }
+
+ public CreateException(String message)
+ {
+ super(message);
+ }
+
+ public CreateException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+ public CreateException(Throwable cause)
+ {
+ super(cause);
+ }
+}
Added:
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
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/DefaultObjectFactory.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,39 @@
+/*
+ * 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.factory;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class DefaultObjectFactory extends ObjectFactory<Object, Object>
+{
+ public <E> E create(Class<E> type, Object context) throws CreateException
+ {
+ try
+ {
+ return type.newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new CreateException(e);
+ }
+ }
+}
Added:
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
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/factory/ObjectFactory.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,31 @@
+/*
+ * 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.factory;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public abstract class ObjectFactory<O, C>
+{
+
+ public abstract <E extends O> E create(Class<E> type, C context) throws
CreateException;
+
+}
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-14
13:23:00 UTC (rev 1290)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/ClassTypeModel.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -19,7 +19,7 @@
package org.exoplatform.webui.application.replication.model;
-import org.exoplatform.webui.application.replication.serial.ObjectWriter;
+import org.exoplatform.webui.application.replication.factory.ObjectFactory;
import java.util.Collection;
import java.util.Collections;
@@ -29,35 +29,72 @@
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public class ClassTypeModel extends TypeModel {
+public class ClassTypeModel<O, C> extends TypeModel
+{
- /** . */
- private final TypeModel superType;
+ /** . */
+ private final Class<O> objectType;
- /** . */
- private final Map<String, FieldModel> fields;
+ /** . */
+ private final TypeModel superType;
- /** . */
- private final Map<String, FieldModel> immutableFields;
+ /** . */
+ private final Map<String, FieldModel> fields;
- ClassTypeModel(Class<?> javaType, TypeModel superType, Map<String,
FieldModel> fields) {
- super(javaType);
+ /** . */
+ private final Map<String, FieldModel> immutableFields;
- //
- this.superType = superType;
- this.fields = fields;
- this.immutableFields = Collections.unmodifiableMap(fields);
- }
+ /** . */
+ private final ObjectFactory<? super O, C> factory;
- public TypeModel getSuperType() {
- return superType;
- }
+ /** . */
+ private final Class<C> contextType;
- public Collection<FieldModel> getFields() {
- return immutableFields.values();
- }
+ ClassTypeModel(
+ Class<O> javaType,
+ TypeModel superType,
+ Map<String, FieldModel> fields,
+ ObjectFactory<? super O, C> factory,
+ Class<C> contextType)
+ {
+ super(javaType);
- public Map<String, FieldModel> getFieldMap() {
- return immutableFields;
- }
+ //
+ this.objectType = javaType;
+ this.superType = superType;
+ this.fields = fields;
+ this.immutableFields = Collections.unmodifiableMap(fields);
+ this.factory = factory;
+ this.contextType = contextType;
+ }
+
+ public Class<O> getObjectType()
+ {
+ return objectType;
+ }
+
+ public Class<C> getContextType()
+ {
+ return contextType;
+ }
+
+ public ObjectFactory<? super O, C> getFactory()
+ {
+ return factory;
+ }
+
+ public TypeModel getSuperType()
+ {
+ return superType;
+ }
+
+ public Collection<FieldModel> getFields()
+ {
+ return immutableFields.values();
+ }
+
+ public Map<String, FieldModel> getFieldMap()
+ {
+ return immutableFields;
+ }
}
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-14
13:23:00 UTC (rev 1290)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/model/TypeDomain.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -19,11 +19,14 @@
package org.exoplatform.webui.application.replication.model;
+import org.exoplatform.webui.application.replication.annotations.Factory;
import org.exoplatform.webui.application.replication.annotations.ReplicatedType;
+import org.exoplatform.webui.application.replication.factory.DefaultObjectFactory;
+import org.exoplatform.webui.application.replication.factory.ObjectFactory;
import java.io.Serializable;
-import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
import java.util.*;
/**
@@ -123,7 +126,7 @@
return null;
}
- private ClassTypeModel buildClassTypeModel(Class<?> javaType, Map<String,
TypeModel> addedTypeModels)
+ private <O> ClassTypeModel<O, ?> buildClassTypeModel(Class<O>
javaType, Map<String, TypeModel> addedTypeModels)
{
ClassTypeModel typeModel = (ClassTypeModel) get(javaType, addedTypeModels);
if (typeModel == null)
@@ -142,10 +145,41 @@
TreeMap<String, FieldModel> fieldModels = new TreeMap<String,
FieldModel>();
//
- typeModel = new ClassTypeModel(javaType, superTypeModel, fieldModels);
+ Class<? extends ObjectFactory<?, ?>> declaredFactoryType = null;
+ Factory factoryAnn = javaType.getAnnotation(Factory.class);
+ if (factoryAnn != null)
+ {
+ declaredFactoryType = factoryAnn.type();
+ }
+
+ //
+ Class<? extends ObjectFactory<? super O, Object>> factoryType =
null;
+ if (declaredFactoryType != null)
+ {
+ if
(((Class<Object>)((ParameterizedType)declaredFactoryType.getGenericSuperclass()).getActualTypeArguments()[0]).isAssignableFrom(javaType))
+ {
+ factoryType = (Class<ObjectFactory<? super
O,Object>>)declaredFactoryType;
+ }
+ else
+ {
+ throw new TypeException();
+ }
+ }
+
+ //
+ if (factoryType != null)
+ {
+ typeModel = createClassType(javaType, fieldModels, superTypeModel,
factoryType);
+ }
+ else
+ {
+ typeModel = createClassType(javaType, fieldModels, superTypeModel);
+ }
+
+ //
addedTypeModels.put(javaType.getName(), typeModel);
- //
+ // Now build fields
for (Field field : javaType.getDeclaredFields())
{
field.setAccessible(true);
@@ -157,9 +191,36 @@
}
}
}
- return typeModel;
+
+ // It must be good
+ return (ClassTypeModel<O,?>)typeModel;
}
+ private <O, C> ClassTypeModel<O, C> createClassType(
+ Class<O> javaType,
+ Map<String, FieldModel> fieldModels,
+ TypeModel superTypeModel,
+ Class<? extends ObjectFactory<? super O, C>> factoryType) {
+ Class<C> contextType =
(Class<C>)((ParameterizedType)factoryType.getGenericSuperclass()).getActualTypeArguments()[1];
+ ObjectFactory<? super O, C> factory;
+ try
+ {
+ factory = factoryType.newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new TypeException();
+ }
+ return new ClassTypeModel<O, C>(javaType, superTypeModel, fieldModels,
factory, contextType);
+ }
+
+ private <O> ClassTypeModel<O, Object> createClassType(
+ Class<O> javaType,
+ Map<String, FieldModel> fieldModels,
+ TypeModel superTypeModel) {
+ return new ClassTypeModel<O, Object>(javaType, superTypeModel, fieldModels,
new DefaultObjectFactory(), Object.class);
+ }
+
private SerializableTypeModel buildSerializable(Class<?> javaType,
Map<String, TypeModel> addedTypeModels)
{
SerializableTypeModel typeModel = (SerializableTypeModel) get(javaType,
addedTypeModels);
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
13:23:00 UTC (rev 1290)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectReader.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -24,9 +24,7 @@
import org.exoplatform.webui.application.replication.model.TypeDomain;
import org.exoplatform.webui.application.replication.model.TypeModel;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
+import java.io.*;
import java.util.*;
/**
@@ -66,7 +64,8 @@
DataContainer container = (DataContainer) obj;
int id;
- switch (container.readInt())
+ int sw = container.readInt();
+ switch (sw)
{
case DataKind.OBJECT_REF:
id = container.readInt();
@@ -79,20 +78,27 @@
case DataKind.OBJECT:
id = container.readInt();
Class clazz = (Class) container.readObject();
+
+ ClassTypeModel<?, ?> typeModel = (ClassTypeModel)
domain.getTypeModel(clazz);
+
+ //
Object instance;
try
{
- instance = clazz.newInstance();
+ instance = typeModel.getFactory().create(clazz, null);
}
catch (Exception e)
{
- throw new AssertionError(e);
+ InvalidClassException ice = new InvalidClassException("Cannot
instantiate object from class " + clazz.getName());
+ ice.initCause(e);
+ throw ice;
}
- ClassTypeModel typeModel = (ClassTypeModel) domain.getTypeModel(clazz);
+
+ //
idToObject.put(id, instance);
//
- ClassTypeModel currentTypeModel = typeModel;
+ ClassTypeModel<?, ?> currentTypeModel = typeModel;
while (true)
{
for (FieldModel fieldModel : (currentTypeModel).getFields())
@@ -155,7 +161,7 @@
//
return instance;
default:
- throw new AssertionError();
+ throw new StreamCorruptedException("Unrecognized data " + sw);
}
}
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-14
13:23:00 UTC (rev 1290)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/application/replication/serial/ObjectWriter.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -81,7 +81,7 @@
}
else
{
- ClassTypeModel typeModel = (ClassTypeModel)
domain.getTypeModel(obj.getClass());
+ ClassTypeModel<?, ?> typeModel = (ClassTypeModel<?,
?>)domain.getTypeModel(obj.getClass());
//
if (typeModel == null)
@@ -95,7 +95,7 @@
output.writeObject(obj.getClass());
//
- ClassTypeModel currentTypeModel = typeModel;
+ ClassTypeModel<?, ?> currentTypeModel = typeModel;
while (true)
{
for (FieldModel fieldModel : (currentTypeModel).getFields())
Modified:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/TestSerialization.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/TestSerialization.java 2010-01-14
13:23:00 UTC (rev 1290)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/TestSerialization.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -89,4 +89,9 @@
assertSame(e.left.left, e.right.left);
assertSame(e.left.right, e.right.right);
}
+
+ public void testCustomFactory() throws Exception
+ {
+
+ }
}
Added:
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
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A1.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,46 @@
+/*
+ * 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.factory;
+
+import org.exoplatform.webui.application.replication.factory.ObjectFactory;
+import org.exoplatform.webui.application.replication.factory.CreateException;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class A1 extends ObjectFactory<A2, Object>
+{
+
+ static A2 instance = new A2();
+
+ @Override
+ public <E extends A2> E create(Class<E> type, Object context) throws
CreateException
+ {
+ if (type == A2.class)
+ {
+ return type.cast(instance);
+ }
+ else
+ {
+ throw new CreateException();
+ }
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A2.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A2.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/A2.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,33 @@
+/*
+ * 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.factory;
+
+import org.exoplatform.webui.application.replication.annotations.Factory;
+import org.exoplatform.webui.application.replication.annotations.ReplicatedType;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+@ReplicatedType
+@Factory(type = A1.class)
+public class A2
+{
+}
\ No newline at end of file
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/TestObjectFactory.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/TestObjectFactory.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/replication/factory/TestObjectFactory.java 2010-01-14
13:42:30 UTC (rev 1291)
@@ -0,0 +1,41 @@
+/*
+ * 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.factory;
+
+import junit.framework.TestCase;
+import org.exoplatform.webui.application.replication.ReplicationContext;
+import org.exoplatform.webui.application.replication.model.TypeDomain;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestObjectFactory extends TestCase
+{
+
+ public void testCustomFactory() throws Exception
+ {
+ TypeDomain domain = new TypeDomain();
+ domain.add(A2.class);
+ ReplicationContext context = new ReplicationContext(domain);
+ A2 a2 = new A2();
+ assertSame(A1.instance, context.clone(a2));
+ }
+}