+ add instantiator factory bean (registration of generated instantiator)
This commit is contained in:
@@ -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<Collection<Instantiator>>,
|
||||
InitializingBean {
|
||||
|
||||
private InstantiatorGenerator generator;
|
||||
private Collection<Instantiator> list;
|
||||
private ClassLoader classLoader;
|
||||
private boolean autoRegister = true;
|
||||
|
||||
private Map<Class<? extends DataSerializable>, 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<Instantiator>(types.size());
|
||||
|
||||
for (Entry<Class<? extends DataSerializable>, 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<Instantiator> 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<Class<? extends DataSerializable>, 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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Instantiator> list = (List<Instantiator>) ctx.getBean("instantiator-factory");
|
||||
assertNotNull(list);
|
||||
assertEquals(2, list.size());
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,13 @@
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.data.gemfire.serialization.WiringInstantiatorTest$TemplateWiringBean" abstract="true" p:beans-ref="beans"/>
|
||||
|
||||
<bean id="instantiator-factory" class="org.springframework.data.gemfire.serialization.InstantiatorFactoryBean">
|
||||
<property name="customTypes">
|
||||
<map>
|
||||
<entry key="org.springframework.data.gemfire.serialization.WiringInstantiatorTest$TypeA" value="1025"/>
|
||||
<entry key="org.springframework.data.gemfire.serialization.WiringInstantiatorTest$TypeB" value="1026"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user