Added basic namespace support.

This commit is contained in:
Mark Fisher
2007-12-10 23:09:42 +00:00
parent c4a72f1436
commit 0e9a93ef42
13 changed files with 545 additions and 0 deletions

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration=org.springframework.integration.config.IntegrationNamespaceHandler

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/spring-integration-1.0.xsd=org/springframework/integration/config/spring-integration-1.0.xsd

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2007 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.integration.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.util.StringUtils;
/**
* Parser for the <em>channel</em> element of the integration namespace.
*
* @author Mark Fisher
*/
public class ChannelParser implements BeanDefinitionParser {
private static final String ID_ATTRIBUTE = "id";
private static final String CAPACITY_ATTRIBUTE = "capacity";
public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition channelDef = new RootBeanDefinition(PointToPointChannel.class);
channelDef.setSource(parserContext.extractSource(element));
String capacity = element.getAttribute(CAPACITY_ATTRIBUTE);
if (StringUtils.hasText(capacity)) {
channelDef.getConstructorArgumentValues().addGenericArgumentValue(Integer.parseInt(capacity));
}
String beanName = element.getAttribute(ID_ATTRIBUTE);
if (!StringUtils.hasText(beanName)) {
beanName = parserContext.getReaderContext().generateBeanName(channelDef);
}
parserContext.registerBeanComponent(new BeanComponentDefinition(channelDef, beanName));
return channelDef;
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2002-2007 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.integration.config;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.bus.ConsumerPolicy;
import org.springframework.integration.endpoint.GenericMessageEndpoint;
import org.springframework.integration.endpoint.MessageHandlerAdapter;
import org.springframework.util.StringUtils;
/**
* Parser for the <em>endpoint</em> element of the integration namespace.
*
* @author Mark Fisher
*/
public class EndpointParser implements BeanDefinitionParser {
private static final String ID_ATTRIBUTE = "id";
private static final String INPUT_CHANNEL_ATTRIBUTE = "input-channel";
private static final String INPUT_CHANNEL_PROPERTY = "inputChannelName";
private static final String DEFAULT_OUTPUT_CHANNEL_ATTRIBUTE = "default-output-channel";
private static final String DEFAULT_OUTPUT_CHANNEL_PROPERTY = "defaultOutputChannelName";
private static final String HANDLER_REF_ATTRIBUTE = "handler";
private static final String HANDLER_METHOD_ATTRIBUTE = "handler-method";
private static final String HANDLER_PROPERTY = "handler";
private static final String OBJECT_PROPERTY = "object";
private static final String METHOD_PROPERTY = "method";
private static final String PERIOD_ATTRIBUTE = "period";
private static final String PERIOD_PROPERTY = "period";
private static final String CONSUMER_ELEMENT = "consumer";
private static final String CONSUMER_POLICY_PROPERTY = "consumerPolicy";
public BeanDefinition parse(Element element, ParserContext parserContext) {
RootBeanDefinition endpointDef = new RootBeanDefinition(GenericMessageEndpoint.class);
endpointDef.setSource(parserContext.extractSource(element));
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
if (StringUtils.hasText(inputChannel)) {
endpointDef.getPropertyValues().addPropertyValue(INPUT_CHANNEL_PROPERTY, inputChannel);
}
String defaultOutputChannel = element.getAttribute(DEFAULT_OUTPUT_CHANNEL_ATTRIBUTE);
if (StringUtils.hasText(defaultOutputChannel)) {
endpointDef.getPropertyValues().addPropertyValue(DEFAULT_OUTPUT_CHANNEL_PROPERTY, defaultOutputChannel);
}
String handlerRef = element.getAttribute(HANDLER_REF_ATTRIBUTE);
if (StringUtils.hasText(handlerRef)) {
String handlerMethod = element.getAttribute(HANDLER_METHOD_ATTRIBUTE);
if (StringUtils.hasText(handlerMethod)) {
BeanDefinition handlerAdapterDef = new RootBeanDefinition(MessageHandlerAdapter.class);
handlerAdapterDef.getPropertyValues().addPropertyValue(OBJECT_PROPERTY, new RuntimeBeanReference(handlerRef));
handlerAdapterDef.getPropertyValues().addPropertyValue(METHOD_PROPERTY, handlerMethod);
String adapterBeanName = parserContext.getReaderContext().generateBeanName(handlerAdapterDef);
parserContext.registerBeanComponent(new BeanComponentDefinition(handlerAdapterDef, adapterBeanName));
endpointDef.getPropertyValues().addPropertyValue(HANDLER_PROPERTY, new RuntimeBeanReference(adapterBeanName));
}
else {
endpointDef.getPropertyValues().addPropertyValue(HANDLER_PROPERTY, new RuntimeBeanReference(handlerRef));
}
}
String beanName = element.getAttribute(ID_ATTRIBUTE);
if (!StringUtils.hasText(beanName)) {
beanName = parserContext.getReaderContext().generateBeanName(endpointDef);
}
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
String localName = child.getLocalName();
if (CONSUMER_ELEMENT.equals(localName)) {
String consumerBeanName = parseConsumer((Element) child, parserContext);
endpointDef.getPropertyValues().addPropertyValue(
CONSUMER_POLICY_PROPERTY, new RuntimeBeanReference(consumerBeanName));
}
}
}
parserContext.registerBeanComponent(new BeanComponentDefinition(endpointDef, beanName));
return endpointDef;
}
private String parseConsumer(Element element, ParserContext parserContext) {
RootBeanDefinition consumerDef = new RootBeanDefinition(ConsumerPolicy.class);
String period = element.getAttribute(PERIOD_ATTRIBUTE);
if (StringUtils.hasText(period)) {
consumerDef.getPropertyValues().addPropertyValue(PERIOD_PROPERTY, Integer.parseInt(period));
}
String beanName = parserContext.getReaderContext().generateBeanName(consumerDef);
parserContext.registerBeanComponent(new BeanComponentDefinition(consumerDef, beanName));
return beanName;
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2007 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.integration.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* Handler for the integration namespace.
*
* @author Mark Fisher
*/
public class IntegrationNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("channel", new ChannelParser());
registerBeanDefinitionParser("endpoint", new EndpointParser());
}
}

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the configuration elements for Spring Integration.
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="channel">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a message channel.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="capacity" type="xsd:integer"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="endpoint">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a message endpoint.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:sequence>
<xsd:element ref="consumer" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="input-channel" type="xsd:string"/>
<xsd:attribute name="default-output-channel" type="xsd:string"/>
<xsd:attribute name="handler" type="xsd:string"/>
<xsd:attribute name="handler-method" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="consumer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a consumer policy.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="period" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2007 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.integration.config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.DocumentMessage;
/**
* @author Mark Fisher
*/
public class ChannelParserTests {
@Test
public void testSimpleChannelWithDefaults() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
for (int i = 0; i < 10; i++) {
boolean result = channel.send(new DocumentMessage(1, "test"), 10);
assertTrue(result);
}
assertFalse(channel.send(new DocumentMessage(1, "test"), 3));
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2007 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.integration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.DocumentMessage;
/**
* @author Mark Fisher
*/
public class EndpointParserTests {
@Test
public void testSimpleEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"genericEndpointTests.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestHandler handler = (TestHandler) context.getBean("testHandler");
assertNull(handler.getMessageString());
channel.send(new DocumentMessage(1, "test"));
handler.getLatch().await(50, TimeUnit.MILLISECONDS);
assertEquals("test", handler.getMessageString());
}
@Test
public void testHandlerAdapterEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAdapterEndpointTests.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestBean bean = (TestBean) context.getBean("testBean");
assertNull(bean.getMessage());
channel.send(new DocumentMessage(1, "test"));
bean.getLatch().await(50, TimeUnit.MILLISECONDS);
assertEquals("test", bean.getMessage());
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2002-2007 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.integration.config;
import java.util.concurrent.CountDownLatch;
/**
* @author Mark Fisher
*/
public class TestBean {
private String message;
private CountDownLatch latch;
public TestBean(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public CountDownLatch getLatch() {
return this.latch;
}
public void store(String message) {
this.message = message;
latch.countDown();
}
public String getMessage() {
return this.message;
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2007 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.integration.config;
import java.util.concurrent.CountDownLatch;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestHandler implements MessageHandler {
private String messageString;
private CountDownLatch latch;
public TestHandler(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public Message handle(Message message) {
this.messageString = (String) message.getPayload();
this.latch.countDown();
return null;
}
public String getMessageString() {
return this.messageString;
}
public CountDownLatch getLatch() {
return this.latch;
}
}

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<channel id="testChannel" capacity="10"/>
</beans:beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<beans:bean class="org.springframework.integration.bus.MessageBus"/>
<channel id="testChannel" capacity="50"/>
<endpoint input-channel="testChannel" handler="testHandler">
<consumer period="100"/>
</endpoint>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<beans:bean class="org.springframework.integration.bus.MessageBus"/>
<channel id="testChannel" capacity="50"/>
<endpoint input-channel="testChannel" handler="testBean" handler-method="store">
<consumer period="100"/>
</endpoint>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>