package aQute.launcher.embedded;

import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;

public class EmbeddedLauncher {

	public static final String	EMBEDDED_RUNPATH	= "Embedded-Runpath";
	static byte[]				buffer				= new byte[30000];

	public static void main(String... args) throws Exception {
		ClassLoader cl = EmbeddedLauncher.class.getClassLoader();
		Enumeration<URL> manifests = cl.getResources("META-INF/MANIFEST.MF");
		while (manifests.hasMoreElements()) {

			Manifest m = new Manifest(manifests.nextElement().openStream());
			m.write(System.out);
			String runpath = m.getMainAttributes().getValue(EMBEDDED_RUNPATH);
			if (runpath != null) {
				List<URL> classpath = new ArrayList<URL>();

				for (String path : runpath.split("\\s*,\\s*")) {
					URL url = toFileURL(cl.getResource(path));
					classpath.add(url);
				}

				URLClassLoader urlc = new URLClassLoader(classpath.toArray(new URL[classpath.size()]));
				System.out.println("URLS: " + Arrays.toString(urlc.getURLs()));
				Class< ? > embeddedLauncher = urlc.loadClass("aQute.launcher.Launcher");
				Method method = embeddedLauncher.getMethod("main", new Class< ? >[] {
					String[].class
				});
				method.invoke(null, new Object[] {
					args
				});
				return;
			}
		}
	}

	private static URL toFileURL(URL resource) throws IOException {
		File f = File.createTempFile("resource", "jar");
		f.getParentFile().mkdirs();
		InputStream in = resource.openStream();
		try {
			OutputStream out = new FileOutputStream(f);
			try {
				int size = in.read(buffer);
				while ( size > 0 ) {
					out.write( buffer, 0, size);
					size = in.read(buffer);
				}
			}
			finally {
				out.close();
			}
		}
		finally {
			in.close();
		}
		return f.toURL();
	}

}

@@ -345,16 +345,17 @@
 ption %7B%0A
+%0A
 %09%09ClassL
@@ -1609,17 +1609,16 @@
 le (
- 
 size %3E 0
  ) %7B
@@ -1613,17 +1613,16 @@
 size %3E 0
- 
 ) %7B%0A%09%09%09%09
@@ -1632,17 +1632,16 @@
 t.write(
- 
 buffer, 
@@ -1760,16 +1760,36 @@
 ();%0A%09%09%7D%0A
+%09%09f.deleteOnExit();%0A
 %09%09return

Delete files on exit