Skip to content

Commit

Permalink
Add ToolFactory, first pass and partly tested ResourceReference impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesde committed Jul 21, 2018
1 parent 1da2fe3 commit 3f13ba0
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 62 deletions.
12 changes: 9 additions & 3 deletions MoquiConf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
<!-- No copyright or license for configuration file, details here are not considered a creative work. -->
<moqui-conf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/moqui-conf-2.1.xsd">
<!-- set these in System env vars or in a Moqui Conf XML file like this -->
<!-- see https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html -->
<!-- see https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html -->
<!-- https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html -->
<!-- https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html -->
<!-- https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-region-selection.html -->
<!-- NOTE: moqui makes these available as Java system properties, from here or from env vars;
AWS SDK looks at different keys for Java sys props but S3ClientToolFactory looks at these keys, the env var keys -->
<default-property name="AWS_REGION" value=""/>
<default-property name="AWS_ACCESS_KEY_ID" value=""/>
<default-property name="AWS_SECRET_ACCESS_KEY" value=""/>

<tools>
<tool-factory class="org.moqui.aws.S3ClientToolFactory" init-priority="40" disabled="false"/>
</tools>
<resource-facade>
<resource-reference scheme="s3" class="org.moqui.aws.S3ResourceReference"/>
<resource-reference scheme="aws3" class="org.moqui.aws.S3ResourceReference"/>
</resource-facade>
</moqui-conf>
73 changes: 73 additions & 0 deletions src/main/groovy/org/moqui/aws/S3ClientToolFactory.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This software is in the public domain under CC0 1.0 Universal plus a
* Grant of Patent License.
*
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software (see the LICENSE.md file). If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package org.moqui.aws

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import groovy.transform.CompileStatic
import org.moqui.context.ExecutionContextFactory
import org.moqui.context.ToolFactory
import org.moqui.util.SystemBinding
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/** A ToolFactory for AWS S3 Client */
@CompileStatic
class S3ClientToolFactory implements ToolFactory<AmazonS3> {
protected final static Logger logger = LoggerFactory.getLogger(S3ClientToolFactory.class)
final static String TOOL_NAME = "AwsS3Client"

protected ExecutionContextFactory ecf = null
protected AmazonS3 s3Client = null

/** Default empty constructor */
S3ClientToolFactory() { }

@Override String getName() { return TOOL_NAME }

@Override
void init(ExecutionContextFactory ecf) {
this.ecf = ecf
// NOTE: minimal explicit configuration here, see:
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html
// https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-region-selection.html

// There is no Java sys prop key for region, and env var vs Java sys prop keys are different for access key ID and secret
// so normalize here to the standard SDK env var keys and support from Java sys props as well
String awsRegion = SystemBinding.getPropOrEnv("AWS_REGION")
String awsAccessKeyId = SystemBinding.getPropOrEnv("AWS_ACCESS_KEY_ID")
String awsSecret = SystemBinding.getPropOrEnv("AWS_SECRET_ACCESS_KEY")
if (awsAccessKeyId && awsSecret) {
System.setProperty("aws.accessKeyId", awsAccessKeyId)
System.setProperty("aws.secretKey", awsSecret)
}

logger.info("Starting AWS S3 Client with region ${awsRegion} access ID ${awsAccessKeyId}")

AmazonS3ClientBuilder cb = AmazonS3ClientBuilder.standard()
if (awsRegion) cb.withRegion(awsRegion)
s3Client = cb.build()
}

@Override AmazonS3 getInstance(Object... parameters) { return s3Client }

@Override
void destroy() {
// stop Camel to prevent more calls coming in
if (s3Client != null) try {
s3Client.shutdown()
logger.info("AWS S3 Client shut down")
} catch (Throwable t) { logger.error("Error in AWS S3 Client shut down", t) }
}
}
Loading

0 comments on commit 3f13ba0

Please sign in to comment.