// Path: arquillian-chameleon-runner/runner/src/main/java/org/arquillian/container/chameleon/runner/extension/ChameleonRunnerAppender.java
// public class ChameleonRunnerAppender implements AuxiliaryArchiveAppender {
// 
//     public static final String CHAMELEON_RUNNER_INCONTAINER_FILE = "chameleonrunner.txt";
// 
//     @Override
//     public Archive<?> createAuxiliaryArchive() {
//         return ShrinkWrap.create(JavaArchive.class, "arquillian-chameleon-runner.jar")
//             .addPackage(ArquillianChameleon.class.getPackage())
//             .addAsResource(new StringAsset("test"), CHAMELEON_RUNNER_INCONTAINER_FILE);
//     }
// }


import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import org.arquillian.container.chameleon.runner.extension.ChameleonRunnerAppender;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
                        new ArquillianChameleonConfigurator().setup(testClass, classLoader);

                    log.info(String.format("Arquillian Configuration created by Chameleon runner is placed at %s.",
                        arquillianChameleonConfiguration.toFile().getAbsolutePath()));

                    createChameleonMarkerFile(arquillianChameleonConfiguration.getParent());
                    addsArquillianFile(arquillianChameleonConfiguration.getParent(),
                        classLoader);
                } catch (Exception e) {
                    throw new IllegalArgumentException(e);
                }
            }
        }
        super.run(notifier);
    }


     // We need to create an special file and put it inside classloader. This is because Chameleon runner adds configuration files dynamically inside the classpath.
     // When you run your tests from your build tool, some or all of them shares the same classloader. So we need to avoid calling the same logic all the time to not get multiple files placed at classloader
     // representing different versions, because then you have uncertainty on which configuration file is really used
    private void createChameleonMarkerFile(Path parent) throws IOException {
        final Path chameleon = parent.resolve("chameleonrunner");
        Files.write(chameleon, "Chameleon Runner was there".getBytes());
    }

    private boolean isSpecialChameleonFile(ClassLoader parent) {
        return parent.getResource("chameleonrunner") != null;
    }

    private boolean isInClientSide(ClassLoader parent) {
// Path: arquillian-chameleon-runner/runner/src/main/java/org/arquillian/container/chameleon/runner/extension/ChameleonRunnerAppender.java
// public class ChameleonRunnerAppender implements AuxiliaryArchiveAppender {
// 
//     public static final String CHAMELEON_RUNNER_INCONTAINER_FILE = "chameleonrunner.txt";
// 
//     @Override
//     public Archive<?> createAuxiliaryArchive() {
//         return ShrinkWrap.create(JavaArchive.class, "arquillian-chameleon-runner.jar")
//             .addPackage(ArquillianChameleon.class.getPackage())
//             .addAsResource(new StringAsset("test"), CHAMELEON_RUNNER_INCONTAINER_FILE);
//     }
// }


// Path: arquillian-chameleon-runner/runner/src/main/java/org/arquillian/container/chameleon/runner/ArquillianChameleon.java
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import org.arquillian.container.chameleon.runner.extension.ChameleonRunnerAppender;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

                        new ArquillianChameleonConfigurator().setup(testClass, classLoader);

                    log.info(String.format("Arquillian Configuration created by Chameleon runner is placed at %s.",
                        arquillianChameleonConfiguration.toFile().getAbsolutePath()));

                    createChameleonMarkerFile(arquillianChameleonConfiguration.getParent());
                    addsArquillianFile(arquillianChameleonConfiguration.getParent(),
                        classLoader);
                } catch (Exception e) {
                    throw new IllegalArgumentException(e);
                }
            }
        }
        super.run(notifier);
    }


     // We need to create an special file and put it inside classloader. This is because Chameleon runner adds configuration files dynamically inside the classpath.
     // When you run your tests from your build tool, some or all of them shares the same classloader. So we need to avoid calling the same logic all the time to not get multiple files placed at classloader
     // representing different versions, because then you have uncertainty on which configuration file is really used
    private void createChameleonMarkerFile(Path parent) throws IOException {
        final Path chameleon = parent.resolve("chameleonrunner");
        Files.write(chameleon, "Chameleon Runner was there".getBytes());
    }

    private boolean isSpecialChameleonFile(ClassLoader parent) {
        return parent.getResource("chameleonrunner") != null;
    }

    private boolean isInClientSide(ClassLoader parent) {
        return parent.getResourceAsStream(ChameleonRunnerAppender.CHAMELEON_RUNNER_INCONTAINER_FILE) == null;