Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ Institute of Molecular Cell Biology and Genetics.</license.copyrightOwners>
<groupId>org.scijava</groupId>
<artifactId>scijava-common</artifactId>
</dependency>

<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.172</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
133 changes: 35 additions & 98 deletions src/main/java/org/scijava/plugins/scripting/java/JavaEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@

package org.scijava.plugins.scripting.java;

import io.github.classgraph.ClassGraph;
import org.scijava.command.CommandService;
import org.scijava.minimaven.BuildEnvironment;
import org.scijava.minimaven.Coordinate;
import org.scijava.minimaven.MavenProject;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.PluginService;
import org.scijava.run.RunService;
import org.scijava.script.AbstractScriptEngine;
import org.scijava.util.FileUtils;
import org.scijava.util.LineOutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -43,44 +70,13 @@
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.scijava.command.CommandService;
import org.scijava.minimaven.BuildEnvironment;
import org.scijava.minimaven.Coordinate;
import org.scijava.minimaven.MavenProject;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.PluginService;
import org.scijava.run.RunService;
import org.scijava.script.AbstractScriptEngine;
import org.scijava.util.FileUtils;
import org.scijava.util.LineOutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
* A pseudo-{@link ScriptEngine} compiling and executing Java classes.
* <p>
Expand Down Expand Up @@ -787,23 +783,14 @@ private static Element append(final Document document, final Element parent,
getAllDependencies(final BuildEnvironment env)
{
final List<Coordinate> result = new ArrayList<Coordinate>();
for (ClassLoader loader = Thread.currentThread().getContextClassLoader(); loader != null; loader =
loader.getParent())
{
if (loader instanceof URLClassLoader) {
for (final URL url : ((URLClassLoader) loader).getURLs()) {
if (url.getProtocol().equals("file")) {
final File file = new File(url.getPath());
if (url.toString().matches(
".*/target/surefire/surefirebooter[0-9]*\\.jar"))
{
getSurefireBooterURLs(file, url, env, result);
continue;
}
result.add(fakeDependency(env, file));
}
}
}
ClassGraph cg = new ClassGraph();
String cp = cg.getClasspath();
String[] candidates = cp.split(File.pathSeparator);

for( String candidate : candidates ){
File file = new File(candidate);
Coordinate c = fakeDependency(env, file);
result.add(c);
}
return result;
}
Expand All @@ -830,56 +817,6 @@ private static Coordinate fakeDependency(final BuildEnvironment env,
return dependency;
}

/**
* Figures out the class path given a {@code .jar} file generated by the
* {@code maven-surefire-plugin}.
* <p>
* A little-known feature of JAR files is that their manifest can specify
* additional class path elements in a {@code Class-Path} entry. The
* {@code maven-surefire-plugin} makes extensive use of that: the URLs of the
* of the active {@link URLClassLoader} will consist of only a single
* {@code .jar} file that is empty except for a manifest whose sole purpose is
* to specify the dependencies.
* </p>
* <p>
* This method can be used to discover those additional class path elements.
* </p>
*
* @param file the {@code .jar} file generated by the
* {@code maven-surefire-plugin}
* @param baseURL the {@link URL} of the {@code .jar} file, needed for class
* path elements specified as relative paths
* @param env the {@link BuildEnvironment}, to store the Maven POMs faked for
* the class path elements
* @param result the list of dependencies to which the discovered dependencies
* are added
*/
private static void getSurefireBooterURLs(final File file, final URL baseURL,
final BuildEnvironment env, final List<Coordinate> result)
{
try {
final JarFile jar = new JarFile(file);
Manifest manifest = jar.getManifest();
if (manifest != null) {
final String classPath =
manifest.getMainAttributes().getValue(Name.CLASS_PATH);
if (classPath != null) {
for (final String element : classPath.split(" +"))
try {
final File dependency =
new File(new URL(baseURL, element).getPath());
result.add(fakeDependency(env, dependency));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
}
catch (final IOException e) {
e.printStackTrace();
}
}

/**
* Read complete contents of a Reader and return as String.
Expand Down