INTSAMPLES-86 - Add XQuery Sample
For reference see: https://jira.springsource.org/browse/INTSAMPLES-86
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.service.CustomerService;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
|
||||
/**
|
||||
* Starts the Spring Context and will initialize the Spring Integration routes.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @version 1.0
|
||||
*
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Main.class);
|
||||
|
||||
private static final String HORIZONTAL_LINE = "=========================================================\n";
|
||||
|
||||
private Main() { }
|
||||
|
||||
/**
|
||||
* Load the Spring Integration Application Context
|
||||
*
|
||||
* @param args - command line arguments
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void main(final String... args) throws IOException {
|
||||
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
System.out.println("\n"
|
||||
+ HORIZONTAL_LINE
|
||||
+ "\n "
|
||||
+ "\n Welcome to the Spring Integration XQuery Sample! "
|
||||
+ "\n "
|
||||
+ "\n For more information please visit: "
|
||||
+ "\n http://www.springintegration.org/ "
|
||||
+ "\n "
|
||||
+ "\n"
|
||||
+ HORIZONTAL_LINE);
|
||||
|
||||
System.out.println("Which XQuery Processor would you like to use? <enter>: ");
|
||||
System.out.println("\t1. Use Saxon");
|
||||
System.out.println("\t2. Use Sedna");
|
||||
System.out.println("\t3. Use BaseX");
|
||||
|
||||
System.out.println("\tq. Quit the application");
|
||||
System.out.print("Enter your choice: ");
|
||||
|
||||
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
|
||||
|
||||
while (true) {
|
||||
final String input = scanner.nextLine();
|
||||
|
||||
if("1".equals(input.trim())) {
|
||||
context.getEnvironment().setActiveProfiles("saxon");
|
||||
System.out.println("\nUsing Saxon...");
|
||||
break;
|
||||
} else if("2".equals(input.trim())) {
|
||||
context.getEnvironment().setActiveProfiles("sedna");
|
||||
System.out.println("\nUsing Sedna...");
|
||||
break;
|
||||
} else if("3".equals(input.trim())) {
|
||||
context.getEnvironment().setActiveProfiles("basex");
|
||||
System.out.println("\nUsing BaseX...");
|
||||
|
||||
if(ClassUtils.isPresent("net.xqj.sedna.SednaXQDataSource", ClassUtils.getDefaultClassLoader())) {
|
||||
LOGGER.error("Detected the Sedna library to be present. This "
|
||||
+ "conflicts with BaseX. Please start the application "
|
||||
+ "from the command line using:\n\n"
|
||||
+ "mvn exec:java -Dbasex\n\nExiting...");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
break;
|
||||
} else if("q".equals(input.trim())) {
|
||||
System.out.println("Exiting application...bye.");
|
||||
System.exit(0);
|
||||
} else {
|
||||
System.out.println("Invalid choice\n\n");
|
||||
System.out.print("Enter you choice: ");
|
||||
}
|
||||
}
|
||||
|
||||
context.load("classpath:META-INF/spring/integration/*-context.xml");
|
||||
context.registerShutdownHook();
|
||||
context.refresh();
|
||||
|
||||
final CustomerService service = context.getBean(CustomerService.class);
|
||||
|
||||
final Resource resource = context.getResource("classpath:data/customers.xml");
|
||||
final InputStream is = resource.getInputStream();
|
||||
final String customers = new String(FileCopyUtils.copyToByteArray(is));
|
||||
|
||||
System.out.println("\n\nExtracting Customer Names from:\n"
|
||||
+ HORIZONTAL_LINE
|
||||
+ customers + "\n"
|
||||
+ HORIZONTAL_LINE
|
||||
+ "\n");
|
||||
final String customernames = service.getCustomerNames(customers);
|
||||
System.out.println("Extracted Customer Names: \n"
|
||||
+ HORIZONTAL_LINE
|
||||
+ customernames + "\n"
|
||||
+ HORIZONTAL_LINE
|
||||
+ "\n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.service;
|
||||
|
||||
/**
|
||||
* XML Extraction Service.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @version 1.0
|
||||
*
|
||||
*/
|
||||
public interface CustomerService {
|
||||
|
||||
/**
|
||||
* For a given customer XML file, this method extracts all customer names and
|
||||
* returns them as a new XML document.
|
||||
*
|
||||
* @param sourceXml The source XML file to extract the customer names from
|
||||
* @return The String-based XML document containing the customer names
|
||||
*/
|
||||
String getCustomerNames(String sourceXml);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-xquery="http://www.springframework.org/schema/integration/xquery"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration/xquery http://www.springframework.org/schema/integration/xquery/spring-integration-xquery.xsd">
|
||||
|
||||
<int:gateway id="gateway" default-request-timeout="5000"
|
||||
default-reply-timeout="5000" default-request-channel="requestChannel"
|
||||
service-interface="org.springframework.integration.service.CustomerService">
|
||||
<int:method name="getCustomerNames" />
|
||||
</int:gateway>
|
||||
|
||||
<int:channel id="requestChannel" />
|
||||
|
||||
<int-xquery:xquery-transformer id="xqueryTransformer"
|
||||
data-source="xqDataSource" input-channel="requestChannel"
|
||||
format-output="true" xquery-file-resource="classpath:data/xquery.xql">
|
||||
</int-xquery:xquery-transformer>
|
||||
|
||||
<beans profile="default, saxon">
|
||||
<bean id="xqDataSource" class="net.sf.saxon.xqj.SaxonXQDataSource" />
|
||||
</beans>
|
||||
|
||||
<beans profile="sedna">
|
||||
<bean id="xqDataSource" class="net.xqj.sedna.SednaXQDataSource">
|
||||
<property name="serverName" value="localhost" />
|
||||
<property name="databaseName" value="test" />
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
<beans profile="basex">
|
||||
<bean id="xqDataSource" class="net.xqj.basex.BaseXXQDataSource">
|
||||
<property name="user" value="admin" />
|
||||
<property name="password" value="admin" />
|
||||
<property name="serverName" value="localhost" />
|
||||
<property name="port" value="1984" />
|
||||
</bean>
|
||||
</beans>
|
||||
|
||||
</beans>
|
||||
18
basic/xquery/src/main/resources/data/customers.xml
Normal file
18
basic/xquery/src/main/resources/data/customers.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<customers>
|
||||
<customer id="1">
|
||||
<name>Foo Industries</name>
|
||||
<industry>Chemical</industry>
|
||||
<city>Glowing City</city>
|
||||
</customer>
|
||||
<customer id="2">
|
||||
<name>Bar Refreshments</name>
|
||||
<industry>Beverage</industry>
|
||||
<city>Desert Town</city>
|
||||
</customer>
|
||||
<customer id="3">
|
||||
<name>Hello World Services</name>
|
||||
<industry>Travel</industry>
|
||||
<city>Coral Sands</city>
|
||||
</customer>
|
||||
</customers>
|
||||
4
basic/xquery/src/main/resources/data/xquery.xql
Normal file
4
basic/xquery/src/main/resources/data/xquery.xql
Normal file
@@ -0,0 +1,4 @@
|
||||
(: The XQuery Script that will extract the customer names from the source XML file :)
|
||||
<customers>
|
||||
{ //customers/customer/name }
|
||||
</customers>
|
||||
28
basic/xquery/src/main/resources/log4j.xml
Normal file
28
basic/xquery/src/main/resources/log4j.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- Appenders -->
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<!-- Loggers -->
|
||||
<logger name="org.springframework.integration">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.integration">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
<root>
|
||||
<priority value="warn" />
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.service.CustomerService;
|
||||
|
||||
/**
|
||||
* Verify that the Spring Integration Application Context starts successfully.
|
||||
*
|
||||
* @author Gunnar Hillert
|
||||
* @since 1.0
|
||||
*
|
||||
*/
|
||||
public class CustomerServiceTest {
|
||||
|
||||
@Test
|
||||
public void testStartupOfSpringInegrationContext() throws Exception{
|
||||
new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml",
|
||||
CustomerServiceTest.class);
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractCustomerNames() {
|
||||
final ApplicationContext context
|
||||
= new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml",
|
||||
CustomerServiceTest.class);
|
||||
|
||||
final CustomerService service = context.getBean(CustomerService.class);
|
||||
|
||||
final String sourceXml = "<customers><customer id=\"1\"><name>Foo Industries</name>"
|
||||
+ "<industry>Chemical</industry><city>Glowing City</city></customer></customers>";
|
||||
final String expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
||||
+ "<customers>\n"
|
||||
+ " <name>Foo Industries</name>\n"
|
||||
+ "</customers>\n";
|
||||
|
||||
final String extractedCustomerNames = service.getCustomerNames(sourceXml);
|
||||
|
||||
System.out.println(">>" + expectedResult + "<<");
|
||||
System.out.println(">>" + extractedCustomerNames + "<<");
|
||||
|
||||
Assert.assertEquals("The expected list of customer names did not match.",
|
||||
expectedResult, extractedCustomerNames);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user