diff --git a/src/main/java/org/springframework/data/gemfire/serialization/InstantiatorFactoryBean.java b/src/main/java/org/springframework/data/gemfire/serialization/InstantiatorFactoryBean.java new file mode 100644 index 00000000..fa95b8db --- /dev/null +++ b/src/main/java/org/springframework/data/gemfire/serialization/InstantiatorFactoryBean.java @@ -0,0 +1,119 @@ +/* + * Copyright 2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.gemfire.serialization; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; + +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +import com.gemstone.gemfire.DataSerializable; +import com.gemstone.gemfire.Instantiator; + +/** + * {@link FactoryBean} that eases registration of custom {@link Instantiator} through + * {@link InstantiatorGenerator}s, inside the Spring container. + * + * By default, the returns {@link Instantiator}s (created through {@link AsmInstantiatorGenerator} if a custom + * generator is not specified) are registered at startup with GemFire. + * + * @author Costin Leau + */ +public class InstantiatorFactoryBean implements BeanClassLoaderAware, FactoryBean>, + InitializingBean { + + private InstantiatorGenerator generator; + private Collection list; + private ClassLoader classLoader; + private boolean autoRegister = true; + + private Map, Integer> types; + + + public void afterPropertiesSet() throws Exception { + Assert.notEmpty(types, "no custom types for generating the Instantiators"); + + if (generator == null) { + generator = new AsmInstantiatorGenerator(classLoader); + } + + list = new ArrayList(types.size()); + + for (Entry, Integer> entry : types.entrySet()) { + Assert.notNull(entry.getKey(), "Invalid/Null class given as custom type"); + Assert.notNull(entry.getValue(), "Invalid/Null int given as user id"); + + list.add(generator.getInstantiator(entry.getKey(), entry.getValue())); + } + + if (autoRegister) { + for (Instantiator instantiator : list) { + Instantiator.register(instantiator); + } + } + } + + public Collection getObject() throws Exception { + return list; + } + + public Class getObjectType() { + return (list == null ? list.getClass() : Collection.class); + } + + public boolean isSingleton() { + return true; + } + + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + + /** + * Sets the custom types and associated user ids for generating the {@link Instantiator}s. + * + * @param types map containing as keys the custom types and values the associated user ids. + */ + public void setCustomTypes(Map, Integer> types) { + this.types = types; + } + + /** + * Sets the generator to use for creating {@link Instantiator}s. + * + * @param generator the generator to set + */ + public void setGenerator(InstantiatorGenerator generator) { + this.generator = generator; + } + + /** + * Sets the auto-registration of this {@link Instantiator} during the container startup. + * Default is true, meaning the registration will occur once this factory is initialized. + * + * @see #register(Instantiator) + * @param autoRegister the autoRegister to set + */ + public void setAutoRegister(boolean autoRegister) { + this.autoRegister = autoRegister; + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/data/gemfire/serialization/AsmInstantiatorFactoryTest.java b/src/test/java/org/springframework/data/gemfire/serialization/AsmInstantiatorFactoryTest.java index 0d01c123..d0693e84 100644 --- a/src/test/java/org/springframework/data/gemfire/serialization/AsmInstantiatorFactoryTest.java +++ b/src/test/java/org/springframework/data/gemfire/serialization/AsmInstantiatorFactoryTest.java @@ -27,7 +27,6 @@ import java.io.Serializable; import org.junit.Before; import org.junit.Test; -import org.springframework.data.gemfire.serialization.AsmInstantiatorGenerator; import com.gemstone.gemfire.DataSerializable; import com.gemstone.gemfire.Instantiator; @@ -87,4 +86,4 @@ public class AsmInstantiatorFactoryTest { Instantiator instance2 = asmFactory.getInstantiator(SomeClass.class, 125); assertSame(instance1, instance2); } -} +} \ No newline at end of file diff --git a/src/test/java/org/springframework/data/gemfire/serialization/WiringInstantiatorTest.java b/src/test/java/org/springframework/data/gemfire/serialization/WiringInstantiatorTest.java index 516699d1..36008fa2 100644 --- a/src/test/java/org/springframework/data/gemfire/serialization/WiringInstantiatorTest.java +++ b/src/test/java/org/springframework/data/gemfire/serialization/WiringInstantiatorTest.java @@ -16,6 +16,7 @@ package org.springframework.data.gemfire.serialization; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -26,17 +27,17 @@ import java.beans.Beans; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.data.gemfire.serialization.AsmInstantiatorGenerator; -import org.springframework.data.gemfire.serialization.WiringInstantiator; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.gemstone.gemfire.DataSerializable; +import com.gemstone.gemfire.Instantiator; /** * @author Costin Leau @@ -83,6 +84,24 @@ public class WiringInstantiatorTest { } } + public static class TypeA implements DataSerializable { + + public void fromData(DataInput arg0) throws IOException, ClassNotFoundException { + } + + public void toData(DataOutput arg0) throws IOException { + } + } + + public static class TypeB implements DataSerializable { + + public void fromData(DataInput arg0) throws IOException, ClassNotFoundException { + } + + public void toData(DataOutput arg0) throws IOException { + } + } + @Test public void testAutowiredBean() throws Exception { Object instance = instantiator.newInstance(); @@ -115,4 +134,10 @@ public class WiringInstantiatorTest { assertSame(bean.beans, ctx.getBean("beans")); } + + public void testInstantiatorFactoryBean() throws Exception { + List list = (List) ctx.getBean("instantiator-factory"); + assertNotNull(list); + assertEquals(2, list.size()); + } } \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/gemfire/serialization/simple-config.xml b/src/test/resources/org/springframework/data/gemfire/serialization/simple-config.xml index 15853681..f2406a60 100644 --- a/src/test/resources/org/springframework/data/gemfire/serialization/simple-config.xml +++ b/src/test/resources/org/springframework/data/gemfire/serialization/simple-config.xml @@ -25,4 +25,13 @@ + + + + + + + + +