Skip to content

Commit

Permalink
Add gradle build file with aws s3 sdk dependency, skeleton S3Resource…
Browse files Browse the repository at this point in the history
…Reference and config
  • Loading branch information
jonesde committed Jul 20, 2018
1 parent 574aea7 commit 1da2fe3
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

# runtime added files
build/
lib/

# IntelliJ IDEA files
.idea/
out/
*.ipr
*.iws
*.iml

# Eclipse files (and some general ones also used by Eclipse)
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath

# NetBeans files
nbproject/private/
nbbuild/
.nb-gradle/
dist/
nbdist/
nbactions.xml
nb-configuration.xml

# OSX auto files
.DS_Store
.AppleDouble
.LSOverride
._*

# Windows auto files
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux auto files
*~
14 changes: 14 additions & 0 deletions MoquiConf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 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 -->
<default-property name="AWS_REGION" value=""/>
<default-property name="AWS_ACCESS_KEY_ID" value=""/>
<default-property name="AWS_SECRET_ACCESS_KEY" value=""/>

<resource-facade>
<resource-reference scheme="s3" class="org.moqui.aws.S3ResourceReference"/>
</resource-facade>
</moqui-conf>
60 changes: 60 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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/>.
*/

apply plugin: 'groovy'

sourceCompatibility = '1.8'
version = '1.0.0'
def jarBaseName = 'moqui-aws'
def moquiDir = projectDir.parentFile.parentFile.parentFile
def frameworkDir = file(moquiDir.absolutePath + '/framework')

// to run use "gradle dependencyUpdates"
apply plugin: 'com.github.ben-manes.versions'
buildscript {
repositories { jcenter() }
dependencies { classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0' }
}
repositories {
flatDir name: 'localLib', dirs: frameworkDir.absolutePath + '/lib'
jcenter()
}

// Log4J has annotation processors, disable to avoid warning
tasks.withType(JavaCompile) { options.compilerArgs << "-proc:none" }
tasks.withType(GroovyCompile) { options.compilerArgs << "-proc:none" }

dependencies {
compile project(':framework')
// AWS Java SDK for S3 (depends on core and kms jars)
compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.371' // Apache 2.0
}

// by default the Java plugin runs test on build, change to not do that (only run test if explicit task)
// no longer workds as of gradle 4.8 or possibly earlier, use clear() instead: check.dependsOn.remove(test)
check.dependsOn.clear()

task cleanLib(type: Delete) { delete fileTree(dir: projectDir.absolutePath+'/lib', include: '*') }
clean.dependsOn cleanLib

jar {
destinationDir = file(projectDir.absolutePath + '/lib')
baseName = jarBaseName
}
task copyDependencies { doLast {
copy { from (configurations.runtime - project(':framework').configurations.runtime - project(':framework').jar.archivePath)
into file(projectDir.absolutePath + '/lib') }
} }
copyDependencies.dependsOn cleanLib
jar.dependsOn copyDependencies
189 changes: 189 additions & 0 deletions src/main/groovy/org/moqui/aws/S3ResourceReference.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* 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 org.moqui.BaseArtifactException
import org.moqui.impl.context.ExecutionContextFactoryImpl
import org.moqui.impl.context.reference.BaseResourceReference
import org.moqui.resource.ResourceReference
import org.moqui.util.ObjectUtilities
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.sql.rowset.serial.SerialBlob
import java.nio.charset.StandardCharsets

class S3ResourceReference extends BaseResourceReference {
protected final static Logger logger = LoggerFactory.getLogger(S3ResourceReference.class)
public final static String locationPrefix = "s3://"

String location

S3ResourceReference() { }

@Override ResourceReference init(String location, ExecutionContextFactoryImpl ecf) {
this.ecf = ecf
this.location = location
return this
}

@Override ResourceReference createNew(String location) {
S3ResourceReference resRef = new S3ResourceReference()
resRef.init(location, ecf)
return resRef
}
@Override String getLocation() { location }

String getBucketName() {
if (!location) return ""
// should have a prefix of "s3://" and then first path segment is bucket name
String fullPath = location.substring(locationPrefix.length())
int slashIdx = fullPath.indexOf("/")
if (slashIdx) {
return fullPath.substring(0, slashIdx)
} else {
return fullPath
}
}
String getPath() {
if (!location) return ""
// should have a prefix of "s3://" and then first path segment is bucket name
String fullPath = location.substring(locationPrefix.length())
int slashIdx = fullPath.indexOf("/")
if (slashIdx) {
return fullPath.substring(slashIdx + 1)
} else {
return ""
}
}

@Override InputStream openStream() {
// TODO
return null
}

@Override OutputStream getOutputStream() {
// TODO can support this?
throw new UnsupportedOperationException("The getOutputStream method is not supported for s3 resources, use putStream() instead")
}

@Override String getText() { return ObjectUtilities.getStreamText(openStream()) }

@Override boolean supportsAll() { true }

@Override boolean supportsUrl() { false }
@Override URL getUrl() { return null }

@Override boolean supportsDirectory() { true }
@Override boolean isFile() {
// TODO
return true
}
@Override boolean isDirectory() {
if (!getPath()) return true // consider root a directory
// TODO
return false
}
@Override List<ResourceReference> getDirectoryEntries() {
// TODO
return null
}

@Override boolean supportsExists() { true }
@Override boolean getExists() { return getDbResource(true) != null }

@Override boolean supportsLastModified() { true }
@Override long getLastModified() {
// TODO
return 0
}

@Override boolean supportsSize() { true }
@Override long getSize() {
// TODO
return 0
}

@Override boolean supportsWrite() { true }
@Override void putText(String text) {
// TODO: use diff from last version for text
SerialBlob sblob = text ? new SerialBlob(text.getBytes(StandardCharsets.UTF_8)) : null
// TODO
}
@Override void putStream(InputStream stream) {
if (stream == null) return
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ObjectUtilities.copyStream(stream, baos)
SerialBlob sblob = new SerialBlob(baos.toByteArray())
// TODO
}

@Override void move(String newLocation) {
if (!newLocation) throw new BaseArtifactException("No location specified, not moving resource at ${getLocation()}")
if (!newLocation.startsWith(locationPrefix))
throw new BaseArtifactException("Location [${newLocation}] is not a s3 location, not moving resource at ${getLocation()}")

List<String> filenameList = new ArrayList<>(Arrays.asList(newLocation.substring(locationPrefix.length()).split("/")))
if (filenameList) {
String newFilename = filenameList.get(filenameList.size()-1)
filenameList.remove(filenameList.size()-1)
// TODO
}
}

@Override ResourceReference makeDirectory(String name) {
// TODO can make directory with no files in S3?
return new S3ResourceReference().init("${location}/${name}", ecf)
}
@Override ResourceReference makeFile(String name) {
S3ResourceReference newRef = new S3ResourceReference().init("${location}/${name}", ecf)
// TODO make empty file
return newRef
}
@Override boolean delete() {
// TODO
// if not exists: return false
return true
}

@Override boolean supportsVersion() { return true }
@Override Version getVersion(String versionName) {
// TODO
return null
}
@Override Version getCurrentVersion() {
// TODO
return null
}
@Override Version getRootVersion() {
// TODO
return null
}
@Override ArrayList<Version> getVersionHistory() {
// TODO
ArrayList<Version> verList = new ArrayList<>()
return verList
}
@Override ArrayList<Version> getNextVersions(String versionName) {
// TODO
ArrayList<Version> verList = new ArrayList<>()
return verList
}
@Override InputStream openStream(String versionName) {
if (versionName == null || versionName.isEmpty()) return openStream()
// TODO
return null
}
@Override String getText(String versionName) { return ObjectUtilities.getStreamText(openStream(versionName)) }
}

0 comments on commit 1da2fe3

Please sign in to comment.