/**
 * Copyright 2011-2013 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gatling.mojo;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import scala_maven_executions.JavaMainCallerInProcess;

/**
 * This class will call a java main method via reflection. Modified to suit
 * Gatling's needs.
 * 
 * @author J. Suereth
 *         <p/>
 *         Note: a -classpath argument *must* be passed into the jvmargs.
 */
public class GatlingJavaMainCallerInProcess extends JavaMainCallerInProcess {

	public GatlingJavaMainCallerInProcess(AbstractMojo requester, String mainClassName, String classpath, String[] args) throws Exception {
		super(requester, mainClassName, classpath, null, args);

		// Pull out classpath and create class loader
		ArrayList<URL> urls = new ArrayList<URL>();
		for (String path : classpath.split(File.pathSeparator)) {
			try {
				urls.add(new File(path).toURI().toURL());
			} catch (MalformedURLException e) {
				requester.getLog().error(e);
			}
		}

		// FIXME why not child of current classloader?
		// FIXME what about old classloader?
		Thread.currentThread().setContextClassLoader(new URLClassLoader(urls.toArray(new URL[urls.size()])));
	}

	@Override
	// In process, ignore jvm args
	public void addJvmArgs(String... args) {
	}

	@Override
	// Not used, @see #run()
	public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
		throw new UnsupportedOperationException("boolean run(boolean displayCmd, boolean throwFailure) is not supported, call int run() instead");
	}

	public int run() throws Exception {
		return runGatling(mainClassName, args);
	}

	private int runGatling(String mainClassName, List<String> args) throws Exception {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		Class<?> mainClass = cl.loadClass(mainClassName);
		Method runGatlingMethod = mainClass.getMethod("runGatling", String[].class);
		String[] argArray = args.toArray(new String[args.size()]);
		return (Integer) runGatlingMethod.invoke(null, new Object[] { argArray });
	}

}

@@ -1272,16 +1272,47 @@
 cess %7B%0A%0A
+%09private String oldClassPath;%0A%0A
 %09public 
@@ -1790,16 +1790,163 @@
 %09%7D%0A%09%09%7D%0A%0A
+%09%09// substitute classpath system prop%0A%09%09oldClassPath = System.getProperty(%22java.class.path%22);%0A%09%09System.setProperty(%22java.class.path%22, classpath);%0A%0A
 %09%09// FIX
@@ -2913,22 +2913,29 @@
 ()%5D);%0A%09%09
-return
+Integer ret =
  (Intege
@@ -2994,15 +2994,153 @@
 rray %7D);
+%0A%0A%09%09// restore previous classpath system prop%0A%09%09System.setProperty(%22java.class.path%22, oldClassPath);%0A%09%09oldClassPath = null;%0A%0A%09%09return ret;
 %0A%09%7D%0A%0A%7D%0A

Fix in-process mode w/ new ZincLauncher