Skip to content

Commit

Permalink
refactor ScriptRouter: (#3404)
Browse files Browse the repository at this point in the history
1, remove priority field, the same as super class
2, remove getUrl method, the same as super class
3, refactor constructor, extract method: getRule, getEngine
4, refactor route, extract method: createBindings, getRoutedInvokers
  • Loading branch information
wanghbxxxx authored and beiwei30 committed Jan 31, 2019
1 parent 5d7b25a commit 713a3ea
Showing 1 changed file with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* ScriptRouter
Expand All @@ -51,68 +52,82 @@ public class ScriptRouter extends AbstractRouter {

private final ScriptEngine engine;

private final int priority;

private final String rule;

public ScriptRouter(URL url) {
this.url = url;
String type = url.getParameter(Constants.TYPE_KEY);
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
if (StringUtils.isEmpty(type)) {
type = Constants.DEFAULT_SCRIPT_TYPE_KEY;
}
if (StringUtils.isEmpty(rule)) {
throw new IllegalStateException("route rule can not be empty. rule:" + rule);
}
ScriptEngine engine = engines.get(type);
if (engine == null) {
engine = new ScriptEngineManager().getEngineByName(type);
if (engine == null) {
throw new IllegalStateException("unsupported route rule type: " + type + ", rule: " + rule);
}
engines.put(type, engine);
}
this.engine = engine;
this.rule = rule;

engine = getEngine(url);
rule = getRule(url);
}

@Override
public URL getUrl() {
return url;
/**
* get rule from url parameters.
*/
private String getRule(URL url) {
String vRule = url.getParameterAndDecoded(Constants.RULE_KEY);
if (StringUtils.isEmpty(vRule)) {
throw new IllegalStateException("route rule can not be empty.");
}
return vRule;
}

/**
* create ScriptEngine instance by type from url parameters, then cache it
*/
private ScriptEngine getEngine(URL url) {
String type = url.getParameter(Constants.TYPE_KEY, Constants.DEFAULT_SCRIPT_TYPE_KEY);

return engines.computeIfAbsent(type, t -> {
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(type);
if (scriptEngine == null) {
throw new IllegalStateException("unsupported route engine type: " + type);
}
return scriptEngine;
});
}

@Override
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
try {
List<Invoker<T>> invokersCopy = new ArrayList<>(invokers);
Bindings bindings = createBindings(invokers, invocation);
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings();
bindings.put("invokers", invokersCopy);
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
CompiledScript function = compilable.compile(rule);
Object obj = function.eval(bindings);
if (obj instanceof Invoker[]) {
invokersCopy = Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
invokersCopy = new ArrayList<Invoker<T>>();
for (Object inv : (Object[]) obj) {
invokersCopy.add((Invoker<T>) inv);
}
} else {
invokersCopy = (List<Invoker<T>>) obj;
}
return invokersCopy;
return getRoutedInvokers(function.eval(bindings));
} catch (ScriptException e) {
logger.error("route error, rule has been ignored. rule: " + rule + ", method:" +
invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e);
return invokers;
}
}

/**
* get routed invokers from result of script rule evaluation
*/
@SuppressWarnings("unchecked")
protected <T> List<Invoker<T>> getRoutedInvokers(Object obj) {
if (obj instanceof Invoker[]) {
return Arrays.asList((Invoker<T>[]) obj);
} else if (obj instanceof Object[]) {
return Arrays.stream((Object[]) obj).map(item -> (Invoker<T>) item).collect(Collectors.toList());
} else {
return (List<Invoker<T>>) obj;
}
}

/**
* create bindings for script engine
*/
private <T> Bindings createBindings(List<Invoker<T>> invokers, Invocation invocation) {
Bindings bindings = engine.createBindings();
// create a new List of invokers
bindings.put("invokers", new ArrayList<>(invokers));
bindings.put("invocation", invocation);
bindings.put("context", RpcContext.getContext());
return bindings;
}

@Override
public boolean isRuntime() {
return this.url.getParameter(Constants.RUNTIME_KEY, false);
Expand Down

0 comments on commit 713a3ea

Please sign in to comment.