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

Support custom ClassLoader for JavascriptCommand '-l' #297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -6,12 +6,15 @@
import com.github.ompc.greys.core.command.annotation.Cmd;
import com.github.ompc.greys.core.command.annotation.IndexArg;
import com.github.ompc.greys.core.command.annotation.NamedArg;
import com.github.ompc.greys.core.manager.ReflectManager;
import com.github.ompc.greys.core.server.Session;
import com.github.ompc.greys.core.util.GaMethod;
import com.github.ompc.greys.core.util.GaStringUtils;
import com.github.ompc.greys.core.util.LogUtil;
import com.github.ompc.greys.core.util.PointCut;
import com.github.ompc.greys.core.util.matcher.*;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jline.internal.Nullable;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -21,6 +24,7 @@
import java.lang.instrument.Instrumentation;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -61,6 +65,9 @@ public class JavaScriptCommand implements ScriptSupportCommand, Command {
@NamedArg(name = "E", summary = "Enable regular expression to match (wildcard matching by default)")
private boolean isRegEx = false;

@NamedArg(name = "l", hasValue = true, summary = "The class pattern of the class whose classloader will be used for the ScriptEngine")
private String jsEngineClassLoader;

/*
* 获取指定字符集
*/
Expand Down Expand Up @@ -133,6 +140,15 @@ else if (StringUtils.isNotBlank(argument1)

}

private ClassLoader getJSEngineClassLoader() {
final ClassMatcher classMatcher = new ClassMatcher(new PatternMatcher(isRegEx, jsEngineClassLoader));
final Collection<Class<?>> matchedClassSet = ReflectManager.Factory.getInstance().searchClassWithSubClass(classMatcher);
if (matchedClassSet.isEmpty()) {
throw new IllegalArgumentException("Can't find class for classLoader: " + jsEngineClassLoader);
}
return matchedClassSet.iterator().next().getClassLoader();
}

@Override
public Action getAction() {

Expand All @@ -159,7 +175,21 @@ public Action getAction() {
*
*/
final ScriptEngineManager mgr = new ScriptEngineManager();
final ScriptEngine jsEngine = mgr.getEngineByMimeType("application/javascript");
ScriptEngine jsEngine = mgr.getEngineByMimeType("application/javascript");

if (!StringUtils.isEmpty(jsEngineClassLoader)) {
try {
// Only NashornScriptEngineFactory can support custom class loader, we have to hardcode it here
jsEngine = new NashornScriptEngineFactory().getScriptEngine(getJSEngineClassLoader());
}catch (final IllegalArgumentException e) {
return new SilentAction() {
@Override
public void action(Session session, Instrumentation inst, Printer printer) {
printer.println("Error create ScriptEngine with class loader: " + getCauseMessage(e)).finish();
}
};
}
}

final Compilable compilable = (Compilable) jsEngine;
final Invocable invocable = (Invocable) jsEngine;
Expand Down