moved org.springframework.integration.samples module to spring-integration-samples
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.samples.xml;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class BookOrderProcessingSample {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("orderProcessingSample.xml",
|
||||
BookOrderProcessingSample.class);
|
||||
MessageChannel messageChannel = (MessageChannel) applicationContext.getBean("ordersChannel");
|
||||
GenericMessage<Document> orderMessage = createXmlMessageFromResource("org/springframework/integration/samples/xml/order.xml");
|
||||
messageChannel.send(orderMessage);
|
||||
applicationContext.close();
|
||||
}
|
||||
|
||||
private static GenericMessage<Document> createXmlMessageFromResource(String path) throws Exception {
|
||||
Resource orderRes = new ClassPathResource(path);
|
||||
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
builderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
|
||||
Document orderDoc = builder.parse(orderRes.getInputStream());
|
||||
return new GenericMessage<Document>(orderDoc);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.samples.xml;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class ExternalResupply {
|
||||
|
||||
public void orderResupply(Document resupplyOrder){
|
||||
System.out.println("Placing resupply order: " + XmlUtil.docAsString(resupplyOrder));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.samples.xml;
|
||||
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class StockChecker {
|
||||
|
||||
private final XPathExpression isbnSelectingXPath;
|
||||
|
||||
public StockChecker(XPathExpression isbnSelectingXPath) {
|
||||
this.isbnSelectingXPath = isbnSelectingXPath;
|
||||
}
|
||||
|
||||
public Document checkStockLevel(Document doc) {
|
||||
String isbn = isbnSelectingXPath.evaluateAsString(doc);
|
||||
boolean inStock = false;
|
||||
|
||||
// we only carry stock of one book currently
|
||||
if ("0321200683".equals(isbn)) {
|
||||
inStock = true;
|
||||
}
|
||||
doc.getDocumentElement().setAttribute("in-stock", String.valueOf(inStock));
|
||||
return doc;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.samples.xml;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class WarehouseDispatch {
|
||||
|
||||
public void dispatch(Document orderItem){
|
||||
System.out.println("Warehouse dispatching orderItem: " + XmlUtil.docAsString(orderItem));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.samples.xml;
|
||||
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class XmlUtil {
|
||||
|
||||
public static String docAsString(Document doc) {
|
||||
try {
|
||||
StringResult res = new StringResult();
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
transformer.transform(new DOMSource(doc), res);
|
||||
return res.toString();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<xsl:stylesheet version="1.0"
|
||||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
||||
xmlns:sb="http://www.example.org/orders"
|
||||
xmlns:bb="http://www.example.org/orders-bigbooks">
|
||||
<xsl:template match="/sb:orderItem" >
|
||||
<bb:bigBooksOrder>
|
||||
<bb:order>
|
||||
<bb:purchaser>smallbooks</bb:purchaser>
|
||||
<bb:quantity>5</bb:quantity>
|
||||
<bb:isbn>
|
||||
<xsl:value-of select="./sb:isbn/text()"/>
|
||||
</bb:isbn>
|
||||
</bb:order>
|
||||
|
||||
</bb:bigBooksOrder>
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<order xmlns="http://www.example.org/orders">
|
||||
<orderItem>
|
||||
<isbn>0321200683</isbn>
|
||||
<quantity>2</quantity>
|
||||
</orderItem>
|
||||
<orderItem>
|
||||
<isbn>1590596439</isbn>
|
||||
<quantity>1</quantity>
|
||||
</orderItem>
|
||||
</order>
|
||||
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:si-xml="http://www.springframework.org/schema/integration/xml"
|
||||
xmlns:si="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/integration/xml
|
||||
http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd
|
||||
http://www.springframework.org/schema/util
|
||||
http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<si:channel id="ordersChannel" />
|
||||
<si:channel id="stockCheckerChannel" />
|
||||
<si:channel id="orderRoutingChannel" />
|
||||
<si:channel id="warehouseDispatchChannel" />
|
||||
<si:channel id="outOfStockChannel" />
|
||||
<si:channel id="resupplyOrderChannel" />
|
||||
|
||||
<!-- map of namespace prefix to URI -->
|
||||
<util:map id="orderNamespaceMap">
|
||||
<entry key="orderNs" value="http://www.example.org/orders" />
|
||||
<entry key="productNs" value="http://www.example.org/prodcuts" />
|
||||
</util:map>
|
||||
|
||||
<!--
|
||||
split the inbound order into a number of orderItem documents that can
|
||||
be processed separately
|
||||
-->
|
||||
<si-xml:xpath-splitter id="orderItemSplitter" input-channel="ordersChannel" output-channel="stockCheckerChannel" create-documents="true">
|
||||
<si-xml:xpath-expression expression="/orderNs:order/orderNs:orderItem" namespace-map="orderNamespaceMap" />
|
||||
</si-xml:xpath-splitter>
|
||||
|
||||
<!-- if each order -->
|
||||
<si:service-activator input-channel="stockCheckerChannel" output-channel="orderRoutingChannel" ref="stockChecker" />
|
||||
<si-xml:xpath-expression id="selectIsbnXpath" namespace-map="orderNamespaceMap" expression="/orderNs:orderItem/orderNs:isbn/text()" />
|
||||
<bean id="stockChecker" class="org.springframework.integration.samples.xml.StockChecker">
|
||||
<constructor-arg ref="selectIsbnXpath" />
|
||||
</bean>
|
||||
|
||||
<!-- if in stock route to the warehouse else route to the out of stock channel -->
|
||||
<si-xml:xpath-router id="instockRouter" channel-resolver="mapChannelResolver"
|
||||
input-channel="orderRoutingChannel" resolution-required="true">
|
||||
<si-xml:xpath-expression expression="/orderNs:orderItem/@in-stock" namespace-map="orderNamespaceMap" />
|
||||
</si-xml:xpath-router>
|
||||
|
||||
<bean id="mapChannelResolver"
|
||||
class="org.springframework.integration.channel.MapBasedChannelResolver">
|
||||
<property name="channelMap">
|
||||
<map>
|
||||
<entry key="true" value-ref="warehouseDispatchChannel" />
|
||||
<entry key="false" value-ref="outOfStockChannel" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<!-- dispatch order if in stock -->
|
||||
<si:outbound-channel-adapter ref="warehouseDispatch" method="dispatch" channel="warehouseDispatchChannel" />
|
||||
<bean id="warehouseDispatch" class="org.springframework.integration.samples.xml.WarehouseDispatch" />
|
||||
|
||||
|
||||
<!-- convert the order item to a format that can be understood by BigBooks the wholesaler -->
|
||||
<si-xml:xslt-transformer input-channel="outOfStockChannel" output-channel="resupplyOrderChannel"
|
||||
xsl-resource="classpath:org/springframework/integration/samples/xml/bigBooksSupplierTransformer.xsl"/>
|
||||
|
||||
<!-- send the resupply order -->
|
||||
<si:outbound-channel-adapter ref="externalResupply" method="orderResupply" channel="resupplyOrderChannel" />
|
||||
<bean id="externalResupply" class="org.springframework.integration.samples.xml.ExternalResupply" />
|
||||
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user