From 4426d540aa8fa7caf3d74e55056c43c6c4819ce2 Mon Sep 17 00:00:00 2001 From: Gunnar Hillert Date: Mon, 25 Feb 2013 17:58:03 -0500 Subject: [PATCH] INTEXT-48 - Add sample for AWS S3 support For reference see: https://jira.springsource.org/browse/INTEXT-48 --- samples/aws-s3/README.md | 62 +++++++ samples/aws-s3/pom.xml | 112 +++++++++++++ .../s3-local-storage/polled_files_go_here.txt | 0 .../integration/samples/aws/s3/Main.java | 155 ++++++++++++++++++ .../spring-integration-context.xml | 44 +++++ samples/aws-s3/src/main/resources/log4j.xml | 28 ++++ 6 files changed, 401 insertions(+) create mode 100644 samples/aws-s3/README.md create mode 100644 samples/aws-s3/pom.xml create mode 100644 samples/aws-s3/s3-local-storage/polled_files_go_here.txt create mode 100644 samples/aws-s3/src/main/java/org/springframework/integration/samples/aws/s3/Main.java create mode 100644 samples/aws-s3/src/main/resources/META-INF/spring/integration/spring-integration-context.xml create mode 100644 samples/aws-s3/src/main/resources/log4j.xml diff --git a/samples/aws-s3/README.md b/samples/aws-s3/README.md new file mode 100644 index 0000000..eb97e30 --- /dev/null +++ b/samples/aws-s3/README.md @@ -0,0 +1,62 @@ +Spring Integration - Amazon S3 Sample +===================================== + +## Introduction + +This sample demonstrate the usage of the *Spring Integration* support for +[Amazon Simple Storage Service][] (Amazon S3). The sample covers 2 use-cases: + +* **Upload** a file to Amazon S3 using the *s3-outbound-channel-adapter* +* **Poll** files from Amazon S3 using the *s3-inbound-channel-adapter* + +## How to Run the Sample + +You can execute this sample simply via [Maven][]: + + $ mvn clean package exec:java + +Once executed you should see the following screen: + + ========================================================= + + Welcome to the Spring Integration Amazon S3 Sample + + For more information please visit: + https://github.com/SpringSource/spring-integration-extensions + + ========================================================= + What would you like to do? + 1. Upload a file to Amazon S3 + 2. Poll files from Amazon S3 + q. Quit the application + > + +Once you have selected either **Option 1** or **Option 2**, you will be asked to +provide the **Access Key ID** and the **Secret Access Key** for *Amazon Web Services*. + +You may also consider providing the **Access Key ID** and the **Secret Access Key** +via the command line: + + $ mvn clean package exec:java -DaccessKey=12345 -DsecretKey=12345 + +In that case you will not be asked to provide that information again. + +## s3-outbound-channel-adapter + +When you selected the option to upload a file you will be asked to specify the file you would like to upload: + + Please enter the path to the file you want to upload: + +Enter a path such as `/demo/data/myfile.txt`. + +## s3-inbound-channel-adapter + +The polled files will be stored under `s3-local-storage`. + +-------------------------------------------------------------------------------- + +For help please take a look at the [Spring Integration documentation][] + +[Amazon Simple Storage Service]: http://aws.amazon.com/s3/ +[Maven]: http://maven.apache.org/ +[Spring Integration documentation]: http://www.springsource.org/spring-integration diff --git a/samples/aws-s3/pom.xml b/samples/aws-s3/pom.xml new file mode 100644 index 0000000..256119f --- /dev/null +++ b/samples/aws-s3/pom.xml @@ -0,0 +1,112 @@ + + 4.0.0 + + org.springframework.integration.samples + aws-s3 + 1.0.0.BUILD-SNAPSHOT + jar + + aws-s3 + http://www.springsource.org/spring-integration + + + 2.2.1 + + + + UTF-8 + 2.2.1.RELEASE + 0.5.0.BUILD-SNAPSHOT + 1.2.17 + 4.11 + + + + + repo.springsource.org.milestone + Spring Framework Maven Milestone Repository + https://repo.springsource.org/libs-snapshot + + + + + + + maven-eclipse-plugin + 2.9 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.0 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + org.springframework.integration.samples.aws.s3.Main + + + + + + + + + + + junit + junit + ${junit.version} + test + + + + + + org.springframework.integration + spring-integration-core + ${spring.integration.version} + + + + org.springframework.integration + spring-integration-file + ${spring.integration.version} + + + + org.springframework.integration + spring-integration-aws + ${spring.integration.aws.version} + + + + + + log4j + log4j + ${log4j.version} + + + + diff --git a/samples/aws-s3/s3-local-storage/polled_files_go_here.txt b/samples/aws-s3/s3-local-storage/polled_files_go_here.txt new file mode 100644 index 0000000..e69de29 diff --git a/samples/aws-s3/src/main/java/org/springframework/integration/samples/aws/s3/Main.java b/samples/aws-s3/src/main/java/org/springframework/integration/samples/aws/s3/Main.java new file mode 100644 index 0000000..1e0cb98 --- /dev/null +++ b/samples/aws-s3/src/main/java/org/springframework/integration/samples/aws/s3/Main.java @@ -0,0 +1,155 @@ +/* + * Copyright 2002-2013 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.aws.s3; + +import java.io.File; +import java.util.Scanner; + +import org.apache.log4j.Logger; +import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.integration.MessageChannel; +import org.springframework.integration.support.MessageBuilder; + +/** + * Starts the Spring Context and will initialize the Spring Integration routes. + * + * @author Gunnar Hillert + * @since 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 + */ + public static void main(final String... args) { + + final Scanner scanner = new Scanner(System.in); + + if (LOGGER.isInfoEnabled()) { + LOGGER.info(HORIZONTAL_LINE + + "\n " + + "\n Welcome to the Spring Integration Amazon S3 Sample " + + "\n " + + "\n For more information please visit: " + + "\nhttps://github.com/SpringSource/spring-integration-extensions" + + "\n " + + HORIZONTAL_LINE ); + } + + final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); + final ConfigurableEnvironment environment = context.getEnvironment(); + + System.out.println("What would you like to do?"); + System.out.println("\t1. Upload a file to Amazon S3"); + System.out.println("\t2. Poll files from Amazon S3"); + System.out.println("\tq. Quit the application"); + System.out.print(" > "); + + String filePath; + + while (true) { + final String input = scanner.nextLine(); + + if("1".equals(input.trim())) { + + System.out.println("Uploading to Amazon S3..."); + + environment.setActiveProfiles("upload-to-s3"); + setupCredentials(environment, scanner); + setupS3info(environment, scanner); + + context.load("classpath:META-INF/spring/integration/*-context.xml"); + context.registerShutdownHook(); + context.refresh(); + + System.out.print("\nPlease enter the path to the file you want to upload: "); + filePath = scanner.nextLine(); + + final MessageChannel messageChannel = context.getBean("s3channel", MessageChannel.class); + messageChannel.send(MessageBuilder.withPayload(new File(filePath)).build()); + + break; + + } + else if("2".equals(input.trim())) { + + System.out.println("Polling files from Amazon S3..."); + environment.setActiveProfiles("poll-s3"); + setupCredentials(environment, scanner); + setupS3info(environment, scanner); + + context.load("classpath:META-INF/spring/integration/*-context.xml"); + context.registerShutdownHook(); + context.refresh(); + + } + 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: "); + } + } + + + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Exiting application...bye."); + } + + System.exit(0); + + } + + private static void setupCredentials(ConfigurableEnvironment environment, Scanner scanner) { + if (!environment.containsProperty("accessKey")) { + System.out.print("\nPlease enter your Access Key ID: "); + final String accessKey = scanner.nextLine(); + environment.getSystemProperties().put("accessKey", accessKey); + } + + if (!environment.containsProperty("secretKey")) { + System.out.print("\nPlease enter your Secret Access Key: "); + final String secretKey = scanner.nextLine(); + environment.getSystemProperties().put("secretKey", secretKey); + } + } + + private static void setupS3info(ConfigurableEnvironment environment, Scanner scanner) { + if (!environment.containsProperty("bucket")) { + System.out.print("\nWhich bucket do you want to use? "); + final String bucket = scanner.nextLine(); + environment.getSystemProperties().put("bucket", bucket); + } + + if (!environment.containsProperty("remoteDirectory")) { + System.out.print("\nPlease enter the S3 remote directory to use: "); + final String remoteDirectory = scanner.nextLine(); + environment.getSystemProperties().put("remoteDirectory", remoteDirectory); + } + } +} diff --git a/samples/aws-s3/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/samples/aws-s3/src/main/resources/META-INF/spring/integration/spring-integration-context.xml new file mode 100644 index 0000000..c9262e0 --- /dev/null +++ b/samples/aws-s3/src/main/resources/META-INF/spring/integration/spring-integration-context.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/aws-s3/src/main/resources/log4j.xml b/samples/aws-s3/src/main/resources/log4j.xml new file mode 100644 index 0000000..5ae5943 --- /dev/null +++ b/samples/aws-s3/src/main/resources/log4j.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +