SGF-15
+ finished parsing support for locators and servers
+ improved schema a bit by adding more up-front validation
This commit is contained in:
costin
2010-09-01 12:04:21 +03:00
parent bdb7a4669e
commit d736c302dd
6 changed files with 96 additions and 29 deletions

View File

@@ -73,7 +73,7 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
private int socketBufferSize = PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE;
private int statisticInterval = PoolFactory.DEFAULT_STATISTIC_INTERVAL;
private int subscriptionAckInterval = PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL;
private boolean enableSubscription = PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED;
private boolean subscriptionEnabled = PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED;
private int subscriptionMessageTrackingTimeout = PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT;
private int subscriptionRedundancy = PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY;
private boolean threadLocalConnections = PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS;
@@ -139,7 +139,7 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
poolFactory.setServerGroup(serverGroup);
poolFactory.setSocketBufferSize(socketBufferSize);
poolFactory.setStatisticInterval(statisticInterval);
poolFactory.setSubscriptionEnabled(enableSubscription);
poolFactory.setSubscriptionEnabled(subscriptionEnabled);
poolFactory.setSubscriptionAckInterval(subscriptionAckInterval);
poolFactory.setSubscriptionMessageTrackingTimeout(subscriptionMessageTrackingTimeout);
poolFactory.setSubscriptionRedundancy(subscriptionRedundancy);
@@ -284,10 +284,10 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
}
/**
* @param enableSubscription the enableSubscription to set
* @param subscriptionEnabled the subscriptionEnabled to set
*/
public void setEnableSubscription(boolean enableSubscription) {
this.enableSubscription = enableSubscription;
public void setSubscriptionEnabled(boolean subscriptionEnabled) {
this.subscriptionEnabled = subscriptionEnabled;
}
/**

View File

@@ -16,12 +16,19 @@
package org.springframework.data.gemfire.config;
import java.util.List;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.client.PoolConnection;
import org.springframework.data.gemfire.client.PoolFactoryBean;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
@@ -29,12 +36,53 @@ import org.w3c.dom.Element;
*
* @author Costin Leau
*/
class PoolParser extends AbstractSingleBeanDefinitionParser {
class PoolParser extends AbstractSimpleBeanDefinitionParser {
protected Class<?> getBeanClass(Element element) {
return PoolFactoryBean.class;
}
protected void postProcess(BeanDefinitionBuilder builder, Element element) {
List<Element> subElements = DomUtils.getChildElements(element);
ManagedList<Object> locators = new ManagedList<Object>(subElements.size());
ManagedList<Object> servers = new ManagedList<Object>(subElements.size());
// parse nested locator/server elements
for (Element subElement : subElements) {
String name = subElement.getLocalName();
if ("locator".equals(name)) {
locators.add(parseLocator(subElement));
}
if ("server".equals(name)) {
servers.add(parseServer(subElement));
}
}
if (!locators.isEmpty()) {
builder.addPropertyValue("locators", locators);
}
if (!servers.isEmpty()) {
builder.addPropertyValue("servers", servers);
}
}
private Object parseServer(Element subElement) {
return parseConnection(subElement);
}
private Object parseLocator(Element subElement) {
return parseConnection(subElement);
}
private BeanDefinition parseConnection(Element element) {
BeanDefinitionBuilder defBuilder = BeanDefinitionBuilder.genericBeanDefinition(PoolConnection.class);
ParsingUtils.setPropertyValue(element, defBuilder, "host", "host");
ParsingUtils.setPropertyValue(element, defBuilder, "port", "port");
return defBuilder.getBeanDefinition();
}
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
throws BeanDefinitionStoreException {
@@ -44,4 +92,4 @@ class PoolParser extends AbstractSingleBeanDefinitionParser {
}
return name;
}
}
}

View File

@@ -425,7 +425,7 @@ The name of the pool used by this client.
</xsd:complexType>
</xsd:element>
<xsd:complexType name="connectionType" abstract="true">
<xsd:complexType name="connectionType">
<xsd:attribute name="host" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
@@ -460,12 +460,10 @@ Defines a pool for connections from a client to a set of GemFire Cache Servers.
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="locator" type="connectionType"/>
<xsd:element name="server" type="connectionType"/>
</xsd:choice>
</xsd:sequence>
<xsd:choice minOccurs="1" maxOccurs="1">
<xsd:element name="locator" type="connectionType" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element name="server" type="connectionType" minOccurs="1" maxOccurs="unbounded"/>
</xsd:choice>
<xsd:attribute name="id" type="xsd:ID" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@@ -36,6 +36,9 @@ public abstract class TestUtils {
clazz = clazz.getSuperclass();
} while (field == null && !clazz.equals(Object.class));
if (field == null)
throw new IllegalArgumentException("Cannot find field '" + name + "' in the class hierarchy of "
+ target.getClass());
field.setAccessible(true);
return (T) field.get(target);
}

View File

@@ -19,11 +19,15 @@ package org.springframework.data.gemfire.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.Iterator;
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.TestUtils;
import org.springframework.data.gemfire.client.PoolConnection;
import org.springframework.data.gemfire.client.PoolFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -44,14 +48,32 @@ public class PoolNamespaceTest {
public void testBasicClient() throws Exception {
assertTrue(context.containsBean("gemfire-pool"));
assertEquals(context.getBean("gemfire-pool"), PoolManager.find("gemfire-pool"));
PoolFactoryBean pfb = (PoolFactoryBean) context.getBean("&gemfire-pool");
Collection<PoolConnection> locators = TestUtils.readField("locators", pfb);
assertEquals(1, locators.size());
PoolConnection locator = locators.iterator().next();
assertEquals("localhost", locator.getHost());
assertEquals(40403, locator.getPort());
}
@Test
public void testComplexPool() throws Exception {
assertTrue(context.containsBean("complex"));
PoolFactoryBean pfb = (PoolFactoryBean) context.getBean("&complex");
assertEquals(30, TestUtils.readField("retry-attempts", pfb));
assertEquals(6000, TestUtils.readField("free-connection-timeout", pfb));
assertEquals(5000, TestUtils.readField("ping-interval", pfb));
assertEquals(30, TestUtils.readField("retryAttempts", pfb));
assertEquals(6000, TestUtils.readField("freeConnectionTimeout", pfb));
assertEquals(5000l, TestUtils.readField("pingInterval", pfb));
assertTrue((Boolean) TestUtils.readField("subscriptionEnabled", pfb));
Collection<PoolConnection> servers = TestUtils.readField("servers", pfb);
assertEquals(2, servers.size());
Iterator<PoolConnection> iterator = servers.iterator();
PoolConnection server = iterator.next();
assertEquals("localhost", server.getHost());
assertEquals(40404, server.getPort());
server = iterator.next();
assertEquals("localhost", server.getHost());
assertEquals(40405, server.getPort());
}
}

View File

@@ -3,7 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:p="http://www.springframework.org/schema/p"
default-lazy-init="true"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
@@ -14,16 +13,13 @@
<gfe:cache />
<gfe:pool/>
<gfe:pool>
<gfe:locator host="localhost" port="40403"/>
</gfe:pool>
<!--
<gfe:locator host="localhost" port="0"/>
-->
<gfe:pool id="complex" free-connection-timeout="6000" retry-attempts="30" ping-interval="5000"/>
<!--
<gfe:server host="localhost" port="0"/>
<gfe:locator host="localhost" port="0"/>
-->
<gfe:pool id="complex" free-connection-timeout="6000" retry-attempts="30" ping-interval="5000" subscription-enabled="true">
<gfe:server host="localhost" port="40404"/>
<gfe:server host="localhost" port="40405"/>
</gfe:pool>
</beans>