Skip to content
Browse files

initial support for grailsw

  • Loading branch information...
1 parent dccba63 commit b7c37925ff1f548fe0148791708fe63f07433daf @jeffbrown jeffbrown committed May 2, 2012
View
1 .gitignore
@@ -63,6 +63,7 @@ target
/grails-test/bin/
/grails-web/bin/
/grails-webflow/bin/
+/grails-wrapper/bin/
/grails-plugin-log4j/bin/
/grails-plugin-logging/bin/
/grails-logging/bin/
View
21 grails-wrapper/build.gradle
@@ -0,0 +1,21 @@
+task runtimeJar(type: Jar) {
+ // TODO
+ from('build/classes/main')
+}
+
+jar {
+ from runtimeJar
+ from "src/main/scripts", {
+ filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [springLoadedVersion: springLoadedVersion, grailsVersion: grailsVersion]
+ }
+ // TODO
+ exclude 'org/**'
+}
+
+//supportJar.appendix = 'wrapper-support'
+jar.appendix = 'wrapper-support'
+runtimeJar.appendix = 'wrapper-runtime'
+
+artifacts {
+ runtime /*supportJar,*/ runtimeJar
+}
View
98 grails-wrapper/src/main/java/org/grails/wrapper/GrailsWrapper.java
@@ -0,0 +1,98 @@
+package org.grails.wrapper;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
+import java.util.ResourceBundle;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+
+public class GrailsWrapper {
+
+ public static void main(final String[] args) throws Exception{
+ ResourceBundle bundle = ResourceBundle.getBundle("grails-wrapper");
+ final String grailsVersion = bundle.getString("wrapper.version");
+ final File grailsCacheDir = new File(System.getProperty("user.home") + "/.grails/");
+ final File grailsVersionDir = new File(grailsCacheDir, grailsVersion);
+ final File wrapperDir = new File(grailsVersionDir, "wrapper");
+ String distUrl = bundle.getString("wrapper.dist.url");
+ if(distUrl == null) {
+ distUrl = "http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/";
+ }
+ if(!distUrl.endsWith("/")) {
+ distUrl += "/";
+ }
+ final String src = distUrl + "grails-" + grailsVersion + ".zip";
+ final URI uri = new URI(src);
+
+ final File file = new File(wrapperDir, "download.zip");
+ new RemoteFileHelper().retrieve(uri, file);
+ final File installDir = new File(wrapperDir, "install");
+ if(!installDir.exists()) {
+ extract(file, installDir);
+ }
+ final File grailsHome = new File(installDir, "grails-" + grailsVersion);
+ final File groovyStartConf = new File(grailsHome, "conf/groovy-starter.conf");
+
+ System.setProperty("grails.home", grailsHome.getAbsolutePath());
+ final URL[] urls = new URL[2];
+ urls[0] = new File(grailsHome, "dist/grails-bootstrap-" + grailsVersion + ".jar").toURI().toURL();
+ File[] groovyJarCandidates = new File(grailsHome, "lib/org.codehaus.groovy/groovy-all/jars/").listFiles(new FilenameFilter() {
+ public boolean accept(final File dir, final String name) {
+ return name.startsWith("groovy-all-") && name.endsWith(".jar");
+ }
+ });
+ urls[1] = groovyJarCandidates[0].toURI().toURL();
+ final URLClassLoader urlClassLoader = new URLClassLoader(urls);
+ final Class<?> loadClass = urlClassLoader.loadClass("org.codehaus.groovy.grails.cli.support.GrailsStarter");
+ final Method mainMethod = loadClass.getMethod("main", String[].class);
+ final String[] args2 = new String[args.length + 2];
+ System.arraycopy(args, 0, args2, 0, args.length);
+ args2[args.length] = "--conf";
+ args2[args.length + 1] = groovyStartConf.getAbsolutePath();
+ mainMethod.invoke(null, new Object[]{args2});
+ }
+
+ public static void extract(final File zip, final File dest) throws IOException {
+ System.out.println("Extracting " + zip.getAbsolutePath() + " to " + dest.getAbsolutePath());
+ Enumeration<?> entries;
+ final ZipFile zipFile = new ZipFile(zip);
+
+ entries = zipFile.entries();
+
+ while (entries.hasMoreElements()) {
+ final ZipEntry entry = (ZipEntry) entries.nextElement();
+
+ if (entry.isDirectory()) {
+ (new File(dest, entry.getName())).mkdirs();
+ continue;
+ }
+
+ copy(zipFile.getInputStream(entry),
+ new BufferedOutputStream(new FileOutputStream(new File(dest, entry.getName()))));
+ }
+ zipFile.close();
+ }
+
+ public static void copy(final InputStream in, final OutputStream out) throws IOException {
+ final byte[] buffer = new byte[1024];
+ int len;
+
+ while ((len = in.read(buffer)) >= 0) {
+ out.write(buffer, 0, len);
+ }
+
+ in.close();
+ out.close();
+ }
+}
View
53 grails-wrapper/src/main/java/org/grails/wrapper/RemoteFileHelper.java
@@ -0,0 +1,53 @@
+package org.grails.wrapper;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
+
+
+public class RemoteFileHelper {
+ private static final int CHUNK_SIZE = 20000;
+ private static final int BUFFER_SIZE = 10000;
+
+ public void retrieve(final URI address, final File destination) throws Exception {
+ if (destination.exists()) {
+ return;
+ }
+ destination.getParentFile().mkdirs();
+
+ System.out.println("Downloading " + address + " to " + destination.getAbsolutePath());
+ OutputStream out = null;
+ InputStream in = null;
+ try {
+ final URL url = address.toURL();
+ out = new BufferedOutputStream(
+ new FileOutputStream(destination));
+ final URLConnection conn = url.openConnection();
+ in = conn.getInputStream();
+ final byte[] buffer = new byte[BUFFER_SIZE];
+ int numRead;
+ long progressCounter = 0;
+ while ((numRead = in.read(buffer)) != -1) {
+ progressCounter += numRead;
+ if (progressCounter / CHUNK_SIZE > 0) {
+ System.out.print(".");
+ progressCounter = progressCounter - CHUNK_SIZE;
+ }
+ out.write(buffer, 0, numRead);
+ }
+ } finally {
+ System.out.println("");
+ if (in != null) {
+ in.close();
+ }
+ if (out != null) {
+ out.close();
+ }
+ }
+ }
+}
View
2 grails-wrapper/src/main/resources/grails-wrapper.properties
@@ -0,0 +1,2 @@
+wrapper.version=@wrapperVersion@
+wrapper.dist.url=@distributationUrl@
View
319 grails-wrapper/src/main/scripts/grailsw
@@ -0,0 +1,319 @@
+##############################################################################
+## ##
+## Grails JVM Bootstrap for UN*X ##
+## ##
+##############################################################################
+
+PROGNAME=`basename "$0"`
+DIRNAME=`dirname "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that
+MAX_FD="maximum"
+
+warn() {
+ echo "${PROGNAME}: $*"
+}
+
+die() {
+ warn "$*"
+ exit 1
+}
+
+earlyInit() {
+ return
+}
+lateInit() {
+ return
+}
+
+GROOVY_STARTUP=~/.groovy/startup
+if [ -r "$GROOVY_STARTUP" ]; then
+ . "$GROOVY_STARTUP"
+fi
+
+earlyInit
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false;
+darwin=false;
+case "`uname`" in
+ CYGWIN*)
+ cygwin=true
+ ;;
+
+ Darwin*)
+ darwin=true
+ ;;
+esac
+
+# Attempt to set JAVA_HOME if it's not already set
+if [ -z "$JAVA_HOME" ]; then
+
+ # Set JAVA_HOME for Darwin
+ if $darwin; then
+
+ [ -z "$JAVA_HOME" -a -d "/Library/Java/Home" ] &&
+ export JAVA_HOME="/Library/Java/Home"
+
+ [ -z "$JAVA_HOME" -a -d "/System/Library/Frameworks/JavaVM.framework/Home" ] &&
+ export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Home"
+
+ fi
+
+fi
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched
+if $cygwin ; then
+ [ -n "$GRAILS_HOME" ] &&
+ GRAILS_HOME=`cygpath --unix "$GRAILS_HOME"`
+ [ -n "$JAVACMD" ] &&
+ JAVACMD=`cygpath --unix "$JAVACMD"`
+ [ -n "$JAVA_HOME" ] &&
+ JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+ [ -n "$CP" ] &&
+ CP=`cygpath --path --unix "$CP"`
+fi
+
+# Remove possible trailing slash (after possible cygwin correction)
+GRAILS_HOME=`echo $GRAILS_HOME | sed -e 's|/$||g'`
+
+# Locate GRAILS_HOME if not it is not set
+if [ -z "$GRAILS_HOME" -o ! -d "$GRAILS_HOME" ] ; then
+ # resolve links - $0 may be a link to groovy's home
+ PRG="$0"
+
+ # need this for relative symlinks
+ while [ -h "$PRG" ] ; do
+ ls=`ls -ld "$PRG"`
+ link=`expr "$ls" : '.*-> \(.*\)$'`
+ if expr "$link" : '/.*' > /dev/null; then
+ PRG="$link"
+ else
+ PRG=`dirname "$PRG"`"/$link"
+ fi
+ done
+
+ SAVED="`pwd`"
+ cd "`dirname \"$PRG\"`/.."
+ GRAILS_HOME="`pwd -P`"
+ cd "$SAVED"
+fi
+
+# Warn the user if JAVA_HOME and/or GRAILS_HOME are not set.
+if [ -z "$JAVA_HOME" ] ; then
+ die "JAVA_HOME environment variable is not set"
+elif [ ! -d "$JAVA_HOME" ] ; then
+ die "JAVA_HOME is not a directory: $JAVA_HOME"
+fi
+
+if [ -z "$GRAILS_HOME" ] ; then
+ warn "GRAILS_HOME environment variable is not set"
+fi
+
+if [ ! -d "$GRAILS_HOME" ] ; then
+ die "GRAILS_HOME is not a directory: $GRAILS_HOME"
+fi
+
+STARTER_CLASSPATH="@wrapperDir@/grails-wrapper-runtime-@grailsVersion@.jar:@wrapperDir@"
+
+# Allow access to Cocoa classes on OS X
+if $darwin; then
+ STARTER_CLASSPATH="$STARTER_CLASSPATH:/System/Library/Java/Support"
+fi
+
+# Create the final classpath
+# Setting a classpath using the -cp or -classpath option means not to use
+# the global classpath. Groovy behaves then the same as the java
+# interpreter
+if [ -n "$CP" ] ; then
+ CP="$CP"
+elif [ -n "$CLASSPATH" ] ; then
+ CP="$CLASSPATH"
+fi
+
+# Determine the Java command to use to start the JVM
+if [ -z "$JAVACMD" ]; then
+ if [ -n "$JAVA_HOME" ]; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ]; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD="$JAVA_HOME/jre/sh/java"
+ else
+ JAVACMD="$JAVA_HOME/bin/java"
+ fi
+ else
+ JAVACMD="java"
+ fi
+fi
+if [ ! -x "$JAVACMD" ]; then
+ die "JAVA_HOME is not defined correctly; can not execute: $JAVACMD"
+fi
+
+# Increase the maximum file descriptors if we can
+if [ "$cygwin" = "false" ]; then
+ MAX_FD_LIMIT=`ulimit -H -n`
+ if [ "$MAX_FD_LIMIT" != "unlimited" ]; then
+ if [ $? -eq 0 ]; then
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
+ # use the businessSystem max
+ MAX_FD="$MAX_FD_LIMIT"
+ fi
+
+ ulimit -n $MAX_FD
+ if [ $? -ne 0 ]; then
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
+ fi
+ else
+ warn "Could not query businessSystem maximum file descriptor limit: $MAX_FD_LIMIT"
+ fi
+ fi
+fi
+
+# Fix the cygwin agent issue
+AGENT_GRAILS_HOME=$GRAILS_HOME
+if $cygwin ; then
+ [ -n "$GRAILS_HOME" ] &&
+ AGENT_GRAILS_HOME=`cygpath --windows "$GRAILS_HOME"`
+fi
+
+SPRINGLOADED_PARAMS="profile=grails"
+if [ -n "$GRAILS_AGENT_CACHE_DIR" ]; then
+ SPRINGLOADED_PARAMS="$SPRINGLOADED_PARAMS;cacheDir=$GRAILS_AGENT_CACHE_DIR"
+ if [ ! -d "$GRAILS_AGENT_CACHE_DIR" ]; then
+ mkdir -p $GRAILS_AGENT_CACHE_DIR
+ fi
+fi
+
+# Process JVM args
+AGENT_STRING="-javaagent:@wrapperDir@/springloaded-core-@springLoadedVersion@.jar -noverify -Dspringloaded=$SPRINGLOADED_PARAMS"
+CMD_LINE_ARGS=""
+DISABLE_RELOADING=false
+
+while true; do
+ if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then
+ CP=$2
+ shift 2
+ break
+ fi
+
+ if [ "$1" = "-reloading" ]; then
+ AGENT=$AGENT_STRING
+ shift
+ break
+ fi
+
+ if [ "$1" = "-noreloading" ]; then
+ DISABLE_RELOADING=true
+ shift
+ break
+ fi
+
+ if [ "$1" != -* ]; then
+ break
+ fi
+
+ CMD_LINE_ARGS="$CMD_LINE_ARGS $1"
+ shift
+done
+
+# Enable agent-based reloading for the 'run-app' command.
+if ! $DISABLE_RELOADING; then
+ for a in "$@"; do
+ if [ "$a" = "run-app" ]; then
+ AGENT=$AGENT_STRING
+ fi
+ done
+
+ if [ $# = 0 ]; then
+ AGENT=$AGENT_STRING
+ fi
+fi
+
+ARGUMENTS="$CMD_LINE_ARGS $@"
+
+# Setup Profiler
+useprofiler=false
+if [ "x$PROFILER" != "x" ]; then
+ if [ -r "$PROFILER" ]; then
+ . $PROFILER
+ useprofiler=true
+ else
+ die "Profiler file not found: $PROFILER"
+ fi
+fi
+
+# For Darwin, use classes.jar for TOOLS_JAR
+TOOLS_JAR="$JAVA_HOME/lib/tools.jar"
+if $darwin; then
+ JAVA_OPTS="-Xdock:name=Grails -Xdock:icon=$GRAILS_HOME/media/icons/grails.icns $JAVA_OPTS"
+# TOOLS_JAR="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Classes/classes.jar"
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin; then
+ GRAILS_HOME=`cygpath --path --mixed "$GRAILS_HOME"`
+ JAVA_HOME=`cygpath --path --mixed "$JAVA_HOME"`
+ CP=`cygpath --path --mixed "$CP"`
+ TOOLS_JAR=`cygpath --path --mixed "$TOOLS_JAR"`
+ STARTER_CLASSPATH=`cygpath --path --mixed "$STARTER_CLASSPATH"`
+ # We build the pattern for arguments to be converted via cygpath
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+ SEP=""
+ for dir in $ROOTDIRSRAW; do
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
+ SEP="|"
+ done
+ OURCYGPATTERN="(^($ROOTDIRS))"
+ # Add a user-defined pattern to the cygpath arguments
+ if [ "$GROOVY_CYGPATTERN" != "" ] ; then
+ OURCYGPATTERN="$OURCYGPATTERN|($GROOVY_CYGPATTERN)"
+ fi
+ # Now convert the arguments
+ ARGUMENTS=""
+ for arg in "$@" ; do
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+ if [ $CHECK -ne 0 ] ; then
+ convArg=`cygpath --path --ignore --mixed "$arg"`
+ else
+ convArg=$arg
+ fi
+ ARGUMENTS="$ARGUMENTS $convArg"
+ done
+fi
+
+
+lateInit
+
+startGrails() {
+ CLASS=$1
+ shift
+ if [ -n "$GRAILS_OPTS" ]
+ then
+ GRAILS_OPTS="$GRAILS_OPTS"
+ else
+ GRAILS_OPTS="-server -Xmx768M -Xms768M -XX:PermSize=256m -XX:MaxPermSize=256m -Dfile.encoding=UTF-8"
+ fi
+ JAVA_OPTS="$GRAILS_OPTS $JAVA_OPTS $AGENT"
+ # Start the Profiler or the JVM
+ if $useprofiler; then
+ runProfiler
+ else
+ if [ $# -eq 0 ] ; then # no argument given
+ exec "$JAVACMD" $JAVA_OPTS \
+ -classpath "$STARTER_CLASSPATH" \
+ -Dgrails.home="$GRAILS_HOME" \
+ -Dtools.jar="$TOOLS_JAR" \
+ $CLASS \
+ --classpath "$CP"
+ else
+ exec "$JAVACMD" $JAVA_OPTS \
+ -classpath "$STARTER_CLASSPATH" \
+ -Dgrails.home="$GRAILS_HOME" \
+ -Dtools.jar="$TOOLS_JAR" \
+ $CLASS \
+ --classpath "$CP" \
+ "${ARGUMENTS}"
+ fi
+ fi
+}
+STARTER_MAIN_CLASS=org.grails.wrapper.GrailsWrapper
+startGrails $STARTER_MAIN_CLASS "$@"
View
80 scripts/Wrapper.groovy
@@ -0,0 +1,80 @@
+includeTargets << grailsScript("_GrailsInit")
+
+USAGE = """
+ wrapper [--wrapperVersion=version] [--wrapperDir=dir] [--distributionUrl=url]
+
+where
+ --wrapperVersion = The version of Grails that the wrapper should use
+ --wrapperDir = Directory where wrapper support files are installed relative to project root
+ --distributationUrl = URL to the directory where the release may be downloaded from if necessary
+
+examples
+ grails wrapper --wrapperVersion=2.0.3
+ grails wrapper --wrapperDir=grailsWrapper --wrapperVersion=2.0.3
+ grails wrapper --wrapperVersion=2.0.0.RC1 --distributionUrl=http://dist.springframework.org.s3.amazonaws.com/milestone/GRAILS/
+
+optional argument default values
+ wrapperVersion = the version of Grails that the wrapper is being generated with ($grailsVersion)
+ wrapperDir = 'wrapper'
+ distributionUrl = 'http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/'
+
+"""
+
+target ('default': "Installs the Grails wrapper") {
+ depends(checkVersion, parseArguments)
+ event 'InstallWrapperStart', [ 'Installing Wrapper...' ]
+
+ grailsWrapperVersion = argsMap.wrapperVersion ?: grailsVersion
+ grailsDistUrl = argsMap.distributionUrl ?: 'http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/'
+ grailsWrapperDir = argsMap.wrapperDir ?: 'wrapper'
+
+ targetDir = "${basedir}/${grailsWrapperDir}"
+
+ supportFiles = []
+ new File("${grailsHome}/dist/").eachFileMatch( groovy.io.FileType.FILES, { it ==~ /grails-wrapper-support.*/ }) {
+ supportFiles << it
+ }
+ if(supportFiles.size() != 1) {
+ if(supportFiles.size() == 0) {
+ event("StatusError", ["An error occurred locating the grails-wrapper-support jar file"])
+ } else {
+ event("StatusError", ["Multiple grails-wrapper-support jar files were found ${supportFiles.absolutePath}"])
+ }
+ exit 1
+ }
+ supportFile = supportFiles[0]
+ ant.unjar(dest: targetDir, src: supportFile.absolutePath, overwrite: true) {
+ patternset {
+ exclude(name: "META-INF/**")
+ }
+ }
+ ant.move(todir: basedir) {
+ fileset(dir: targetDir) {
+ include(name: 'grailsw*')
+ }
+ }
+ ant.replace(dir: targetDir, includes: '*.properties', token: '@wrapperVersion@', value: grailsWrapperVersion)
+ ant.replace(dir: targetDir, includes: '*.properties', token: '@distributationUrl@', value: grailsDistUrl)
+ ant.replace(dir: basedir, includes: 'grailsw*', token: '@wrapperDir@', value: grailsWrapperDir)
+ ant.chmod(file: 'grailsw', perm: 'u+x')
+
+ springloadedFiles = []
+ new File("${grailsHome}/lib/com.springsource.springloaded/springloaded-core/jars/").eachFileMatch( groovy.io.FileType.FILES, { it ==~ /springloaded-core-.*/ }) {
+ springloadedFiles << it
+ }
+ if(springloadedFiles.size() != 1) {
+ if(springloadedFiles.size() == 0) {
+ event("StatusError", ["An error occurred locating the springloaded-core jar file"])
+ } else {
+ event("StatusError", ["Multiple springloaded-core jar files were found ${springloadedFiles.absolutePath}"])
+ }
+ exit 1
+ }
+
+ springloadedFile = springloadedFiles[0]
+
+ ant.copy(todir: targetDir, file: springloadedFile.absolutePath, overwrite: true)
+
+ event("StatusUpdate", [ "Wrapper installed successfully"])
+ event 'InstallWrapperEnd', [ 'Finished Installing Wrapper.' ]
+}
View
1 settings.gradle
@@ -10,6 +10,7 @@ include 'grails-bootstrap',
'grails-resources',
'grails-scripts',
'grails-logging',
+ 'grails-wrapper',
// Plugins
'grails-plugin-domain-class',

0 comments on commit b7c3792

Please sign in to comment.
Something went wrong with that request. Please try again.