Permalink
Browse files

initial support for grailsw

  • Loading branch information...
1 parent dccba63 commit bb2481df6eabaffea65f11c409c156181f50c2f2 @jeffbrown jeffbrown committed May 2, 2012
View
@@ -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/
@@ -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
+}
@@ -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();
+ }
+}
@@ -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();
+ }
+ }
+ }
+}
@@ -0,0 +1,2 @@
+wrapper.version=@wrapperVersion@
+wrapper.dist.url=@distributationUrl@
Oops, something went wrong.

0 comments on commit bb2481d

Please sign in to comment.