Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Plugin lookup in execution contexts #251

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;
import org.sonatype.maven.polyglot.PolyglotModelManager;
import org.sonatype.maven.polyglot.execute.ExecuteContext;
import org.sonatype.maven.polyglot.execute.ExecuteManager;
Expand Down Expand Up @@ -59,6 +60,9 @@ public class ExecuteMojo extends AbstractMojo {
@Component(role = PolyglotModelManager.class)
private PolyglotModelManager modelManager;

@javax.inject.Inject
private PlexusContainer container;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Log log = getLog();
Expand Down Expand Up @@ -97,6 +101,13 @@ public File getBasedir() {
public Log getLog() {
return ExecuteMojo.this.getLog();
}

public <T> T lookup(Class<T> clazz) {
return container.lookup(clazz);
}
public Object lookup(String className) {
return container.lookup(Thread.currentThread().getContextClassLoader().loadClass(className));
}
};

for (ExecuteTask task : tasks) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class DualClassLoader extends ClassLoader {

// not our real parents, but we try them too
List<ClassLoader> fosterParents = new ArrayList<>();

public DualClassLoader(ClassLoader parent) {
super("DualClassLoader",parent);
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
for (ClassLoader cl : fosterParents) {
try {
return cl.loadClass(name);
} catch (ClassNotFoundException e) {
// ignore
}
}
return getParent().loadClass(name);
}

public void append(ClassLoader additional){
fosterParents.add(additional);
}

//?
//protected String findLibrary(String libname) {}

@Override
protected URL findResource(String name) {

URL result = null;
for (ClassLoader cl : fosterParents) {
result = cl.getResource(name);
if (result != null)
return result;
}
return getParent().getResource(name);
}

@Override
protected Enumeration<URL> findResources(String name) throws IOException {
List<URL> results = new ArrayList<>();

for (ClassLoader cl : fosterParents) {
results.addAll(Collections.list(cl.getResources(name)));
}
return Collections.enumeration(results);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public RubyParser( ExecuteManager executeManager ) throws IOException
{
this.executeManager = executeManager;
this.jruby = new IsolatedScriptingContainer();
DualClassLoader loader = new DualClassLoader(jruby.getClassLoader())
jruby.setClassLoader(loader);
this.parser = jruby.runScriptlet( PathType.CLASSPATH, "parser.rb" );
this.factory = new RubyExecuteTaskFactory( jruby );
this.factory = new RubyExecuteTaskFactory( jruby, loader );
}

// synchronize it since it is not clear how threadsafe everything is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RubyExecuteTask extends ExecuteTaskSupport {
private ScriptingContainer jruby;
private Object script;

public RubyExecuteTask(ScriptingContainer jruby) {
public RubyExecuteTask(ScriptingContainer jruby, DualClassLoader loader) {
this.jruby = jruby;
}

Expand All @@ -37,6 +37,7 @@ public void setScript(Object script) {
}

public void execute(final ExecuteContext context) throws Exception {
loader.append(jruby.getProvider().getRubyInstanceConfig().getCurrentThreadClassLoader())
jruby.callMethod(script, "call", context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class RubyExecuteTaskFactory {
private ScriptingContainer jruby;
private List<ExecuteTask> tasks = new LinkedList<ExecuteTask>();

public RubyExecuteTaskFactory( ScriptingContainer jruby ) {
public RubyExecuteTaskFactory( ScriptingContainer jruby, DualClassLoader loader ) {
this.jruby = jruby;
}

public void addExecuteTask( String id, String phase, String profileId, Object script ){
RubyExecuteTask task = new RubyExecuteTask( jruby );
RubyExecuteTask task = new RubyExecuteTask( jruby, loader );
task.setId( id );
task.setPhase( phase );
task.setProfileId( profileId );
Expand Down