package org.jbehave.mojo;

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

import org.jbehave.scenario.Scenario;

/**
 * Extends URLClassLoader to instantiate Scenarios.
 * 
 * @author Mauro Talevi
 */
public class ScenarioClassLoader extends URLClassLoader {

    public ScenarioClassLoader(List<String> classpathElements) throws MalformedURLException {
        super(classpathURLs(classpathElements), Scenario.class.getClassLoader());
    }

    public ScenarioClassLoader(List<String> classpathElements, ClassLoader parent) throws MalformedURLException {
        super(classpathURLs(classpathElements), parent);
    }

    /**
     * Loads and instantiates a Scenario class
     * 
     * @param scenarioClassName the name of the Scenario class
     * @return A Scenario instance
     */
    public Scenario newScenario(String scenarioClassName) {
        try {
            Scenario scenario = (Scenario) loadClass(scenarioClassName).getConstructor(ClassLoader.class).newInstance(
                    this);
            Thread.currentThread().setContextClassLoader(this);
            return scenario;
        } catch (ClassCastException e) {
            String message = "The scenario " + scenarioClassName + " must be of type " + Scenario.class.getName();
            throw new RuntimeException(message, e);
        } catch (Exception e) {
            String message = "The Scenario " + scenarioClassName
                    + " could not be instantiated with classpath elements " + asShortPaths(getURLs());
            throw new RuntimeException(message, e);
        }
    }

    private List<String> asShortPaths(URL[] urls) {
        List<String> names = new ArrayList<String>();
        for (URL url : urls) {
            String path = url.getPath();
            if (path.endsWith(".jar")) {
                names.add(path.substring(path.lastIndexOf("/") + 1));
            } else {
                names.add(path);
            }
        }
        return names;
    }

    private static URL[] classpathURLs(List<String> elements) throws MalformedURLException {
        List<URL> urls = new ArrayList<URL>();
        if (elements != null) {
            for (String element : elements) {
                urls.add(new File(element).toURL());
            }
        }
        return urls.toArray(new URL[urls.size()]);
    }

}

@@ -1290,32 +1290,33 @@
 = %22The scenario 
+'
 %22 + scenarioClas
@@ -1324,16 +1324,17 @@
 Name + %22
+'
  must be
@@ -1342,16 +1342,17 @@
 of type 
+'
 %22 + Scen
@@ -1371,16 +1371,21 @@
 etName()
+ +%22'%22
 ;%0A      
@@ -1505,16 +1505,17 @@
 cenario 
+'
 %22 + scen
@@ -1551,16 +1551,17 @@
      + %22
+'
  could n
@@ -1601,16 +1601,17 @@
  element
+:
 s %22 + as
@@ -1900,28 +1900,18 @@
 if (
-path.endsWith(%22.jar%22
+isJar(path
 )) %7B
@@ -1941,48 +1941,22 @@
 add(
-path.substring(path.lastIndexOf(%22/%22) + 1
+shortPath(path
 ));%0A
@@ -2054,32 +2054,241 @@
 n names;%0A    %7D%0A%0A
+    private static String shortPath(String path) %7B%0A        return path.substring(path.lastIndexOf(%22/%22) + 1);%0A    %7D%0A%0A    private static boolean isJar(String path) %7B%0A        return path.endsWith(%22.jar%22);%0A    %7D%0A%0A
     private stat

Use quotes in messages and private methods for better readability.