From 9876236e9828f42c64ebba1c1d101ecd5c57d408 Mon Sep 17 00:00:00 2001 From: Gunnar Hillert Date: Wed, 5 Oct 2011 11:04:59 -0400 Subject: [PATCH] First draft of an Oracle Stored Proc Sample Sample show-cases the execution of Oracle Stored Procedures and Functions --- .gitignore | 3 + .../stored-procedures-oracle/README.md | 108 ++++++++++++++++ intermediate/stored-procedures-oracle/pom.xml | 122 ++++++++++++++++++ .../org/springframework/integration/Main.java | 92 +++++++++++++ .../service/StringConversionService.java | 37 ++++++ .../spring-integration-context.xml | 44 +++++++ .../src/main/resources/logback.xml | 24 ++++ .../sts/StringConversionServiceTest.java | 58 +++++++++ 8 files changed, 488 insertions(+) create mode 100644 intermediate/stored-procedures-oracle/README.md create mode 100644 intermediate/stored-procedures-oracle/pom.xml create mode 100644 intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/Main.java create mode 100644 intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/service/StringConversionService.java create mode 100644 intermediate/stored-procedures-oracle/src/main/resources/META-INF/spring/integration/spring-integration-context.xml create mode 100644 intermediate/stored-procedures-oracle/src/main/resources/logback.xml create mode 100644 intermediate/stored-procedures-oracle/src/test/java/org/springframework/integration/sts/StringConversionServiceTest.java diff --git a/.gitignore b/.gitignore index a32372f3..fe5e0208 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ log.roo .project .classpath .DS_Store +*.iml +*.ipr +*.iws diff --git a/intermediate/stored-procedures-oracle/README.md b/intermediate/stored-procedures-oracle/README.md new file mode 100644 index 00000000..c8eac2c8 --- /dev/null +++ b/intermediate/stored-procedures-oracle/README.md @@ -0,0 +1,108 @@ +Spring Integration - Stored Procedure Example - Oracle +================================================================================ + +# Overview + +This example provides a simple example using the stored procedure outbound gateway +adapter. This example will call an Oracle Stored Procedure as well as an Oracle Function using the StoredProc Outbound Gateway. + +This sample was tested against: **Oracle Database Express Edition 11g Release 2** (which can be downloaded and used for free). +Nevertheless, the example should work with other versions as well. + +# Setup + +##Pre-requisites + +- Access to a Oracle or Oracle XE database instance +- Install Oracle JDBC Driver to your local Maven repository (~/.m2) + +##JDBC Driver installation + +- Go to [http://www.oracle.com/technetwork/indexes/downloads/index.html](http://www.oracle.com/technetwork/indexes/downloads/index.html) +- Under "JDBC Drivers", download the appropriate driver relevant to your Oracle and JDK version (This sample was tested using +"Oracle Database 11g Release 2 JDBC Drivers") +- Once downloaded, install the driver to your local Maven repository: + + $ mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=~/dev/ojdbc6.jar -DgeneratePom=true + +- Now you can add the dependency to the Maven pom.xml file. Please check the pom.xml for this project and verify that it matches your installed Oracle JDBC driver + + + com.oracle + ojdbc6 + 11.2.0.3 + + +## Setting Up Oracle + +### Create Tablespace + + CREATE TABLESPACE procedure_test + DATAFILE 'c:/data/procedure_test.dbf' + SIZE 10M + AUTOEXTEND ON NEXT 10M + MAXSIZE 100M; + +### Create User + + CREATE USER storedproc + IDENTIFIED BY storedproc + DEFAULT TABLESPACE procedure_test + TEMPORARY TABLESPAC temp; + +### Grant Rights + + GRANT CREATE SESSION,CREATE TABLE,CREATE VIEW,CREATE SEQUENCE TO storedproc; + ALTER USER storedproc QUOTA unlimited ON procedure_test; + +### Creating the Stored Procedure + +create or replace +PROCEDURE CAPITALIZE_STRING(inoutString IN OUT VARCHAR) IS +BEGIN + SELECT upper(inoutString) INTO inoutString from dual ; +END; + +### Creating the Function + +create or replace +FUNCTION GET_COOL_NUMBER + RETURN NUMBER + IS cool_number NUMBER(11,2); +BEGIN + cool_number := 12345; + RETURN cool_number; +END; + +## Setting Up Oracle + +You may have to update the Oracle DB properties in: + + /src/main/resources/META-INF/spring/integration/spring-integration-context.xml + + + + + + + + + 3 + 20 + + + + +# Run the Sample + +* running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application) +* or from the command line: + - mvn package + - mvn exec:java + +-------------------------------------------------------------------------------- + +For help please take a look at the Spring Integration documentation: + +http://www.springsource.org/spring-integration + diff --git a/intermediate/stored-procedures-oracle/pom.xml b/intermediate/stored-procedures-oracle/pom.xml new file mode 100644 index 00000000..3a885698 --- /dev/null +++ b/intermediate/stored-procedures-oracle/pom.xml @@ -0,0 +1,122 @@ + + 4.0.0 + + org.springframework.integration.samples + oracle-stored-procedures + 1.0-SNAPSHOT + jar + + stored-procedures-oracle + http://www.springsource.org/spring-integration + + + UTF-8 + 2.1.0.BUILD-SNAPSHOT + 1.6.1 + 4.7 + + + + + repository.springframework.maven.release + Spring Framework Maven Release Repository + http://maven.springframework.org/release + + + + + + + maven-eclipse-plugin + 2.8 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2 + + org.springframework.integration.Main + + + + + + + + + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + + org.springframework.integration + spring-integration-jdbc + ${spring.integration.version} + + + + + + ch.qos.logback + logback-classic + 0.9.28 + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + log4j-over-slf4j + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + + com.oracle + ojdbc6 + 11.2.0.3 + + + + diff --git a/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/Main.java b/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/Main.java new file mode 100644 index 00000000..ae802155 --- /dev/null +++ b/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/Main.java @@ -0,0 +1,92 @@ +/* + * Copyright 2002-2011 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.util.Scanner; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import org.springframework.integration.service.StringConversionService; + + +/** + * 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 = LoggerFactory.getLogger(Main.class); + + private Main() { } + + /** + * Load the Spring Integration Application Context + * + * @param args - command line arguments + */ + public static void main(final String... args) { + + LOGGER.info("\n=========================================================" + + "\n " + + "\n Welcome to Spring Integration! " + + "\n " + + "\n For more information please visit: " + + "\n http://www.springsource.org/spring-integration " + + "\n " + + "\n=========================================================" ); + + final AbstractApplicationContext context = + new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/*-context.xml"); + + context.registerShutdownHook(); + + final Scanner scanner = new Scanner(System.in); + + final StringConversionService service = context.getBean(StringConversionService.class); + + LOGGER.info("\n=========================================================" + + "\n " + + "\n Please press 'q + Enter' to quit the application. " + + "\n " + + "\n=========================================================" ); + + System.out.print("Please enter a string and press : "); + + while (!scanner.hasNext("q")) { + String input = scanner.nextLine(); + + System.out.println("Converting String to Uppcase using Stored Procedure..."); + String inputUpperCase = service.convertToUpperCase(input); + + System.out.println("Retrieving Numeric value via Sql Function..."); + Integer number = service.getNumber(); + + System.out.println(String.format("Converted '%s' - End Result: '%s_%s'.", input, inputUpperCase, number)); + System.out.print("To try again, please enter a string and press :"); + } + + LOGGER.info("Exiting application...bye."); + + System.exit(0); + + } +} diff --git a/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/service/StringConversionService.java b/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/service/StringConversionService.java new file mode 100644 index 00000000..aba92fed --- /dev/null +++ b/intermediate/stored-procedures-oracle/src/main/java/org/springframework/integration/service/StringConversionService.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-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.integration.service; + +import org.springframework.integration.annotation.Payload; + +import java.util.Map; + +/** + * Provides string manipulation services. + */ +public interface StringConversionService { + + /** + * Converts a String to Upper Case. + * + * @param stringToConvert The string to convert to upper case + * @return The converted upper case string. + */ + String convertToUpperCase(String stringToConvert); + + Integer getNumber(); + +} diff --git a/intermediate/stored-procedures-oracle/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/intermediate/stored-procedures-oracle/src/main/resources/META-INF/spring/integration/spring-integration-context.xml new file mode 100644 index 00000000..d14f58d2 --- /dev/null +++ b/intermediate/stored-procedures-oracle/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + 3 + 20 + + + + + + + + + + + + + + + + + + + + + diff --git a/intermediate/stored-procedures-oracle/src/main/resources/logback.xml b/intermediate/stored-procedures-oracle/src/main/resources/logback.xml new file mode 100644 index 00000000..eee6b966 --- /dev/null +++ b/intermediate/stored-procedures-oracle/src/main/resources/logback.xml @@ -0,0 +1,24 @@ + + + + + + + %d %5p | %t | %-55logger{55} | %m %n + + + + + + + + + + + + + + + + + diff --git a/intermediate/stored-procedures-oracle/src/test/java/org/springframework/integration/sts/StringConversionServiceTest.java b/intermediate/stored-procedures-oracle/src/test/java/org/springframework/integration/sts/StringConversionServiceTest.java new file mode 100644 index 00000000..25d84c63 --- /dev/null +++ b/intermediate/stored-procedures-oracle/src/test/java/org/springframework/integration/sts/StringConversionServiceTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2002-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.integration.sts; + +import static junit.framework.Assert.*; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import org.springframework.integration.service.StringConversionService; + +/** + * Verify that the Spring Integration Application Context starts successfully. + */ +public class StringConversionServiceTest { + + @Test + public void testStartupOfSpringInegrationContext() throws Exception{ + final ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", + StringConversionServiceTest.class); + Thread.sleep(2000); + } + + @Test + public void testConvertStringToUpperCase() { + final ApplicationContext context + = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", + StringConversionServiceTest.class); + + final StringConversionService service = context.getBean(StringConversionService.class); + + final String stringToConvert = "I love Spring Integration"; + final String expectedResult = "I LOVE SPRING INTEGRATION"; + + // final String convertedString = service.convertToUpperCase(stringToConvert); + + // assertEquals("Expecting that the string is converted to upper case.", + //dd expectedResult, convertedString); + + } + +}