From 1da2fe37d3cb26d346ba364b5aea63ad8e03e941 Mon Sep 17 00:00:00 2001 From: David E Jones Date: Thu, 19 Jul 2018 23:48:04 -0700 Subject: [PATCH] Add gradle build file with aws s3 sdk dependency, skeleton S3ResourceReference and config --- .gitignore | 47 +++++ MoquiConf.xml | 14 ++ build.gradle | 60 ++++++ .../org/moqui/aws/S3ResourceReference.groovy | 189 ++++++++++++++++++ 4 files changed, 310 insertions(+) create mode 100644 .gitignore create mode 100644 MoquiConf.xml create mode 100644 build.gradle create mode 100644 src/main/groovy/org/moqui/aws/S3ResourceReference.groovy diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f44aad --- /dev/null +++ b/.gitignore @@ -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 +*~ diff --git a/MoquiConf.xml b/MoquiConf.xml new file mode 100644 index 0000000..b9dac2a --- /dev/null +++ b/MoquiConf.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..4e8799e --- /dev/null +++ b/build.gradle @@ -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 + * . + */ + +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 diff --git a/src/main/groovy/org/moqui/aws/S3ResourceReference.groovy b/src/main/groovy/org/moqui/aws/S3ResourceReference.groovy new file mode 100644 index 0000000..ccf0617 --- /dev/null +++ b/src/main/groovy/org/moqui/aws/S3ResourceReference.groovy @@ -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 + * . + */ +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 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 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 getVersionHistory() { + // TODO + ArrayList verList = new ArrayList<>() + return verList + } + @Override ArrayList getNextVersions(String versionName) { + // TODO + ArrayList 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)) } +}