fix: 修复透传解析脚本无法使用onDownstream,onUpstream函数注册回调. (#409)
This commit is contained in:
parent
f84621178b
commit
3420f36110
|
|
@ -1,5 +1,7 @@
|
|||
package org.jetlinks.community.script;
|
||||
|
||||
import org.jetlinks.community.script.context.ExecutionContext;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -87,6 +89,12 @@ public interface ScriptFactory {
|
|||
* @return 接口代理实现
|
||||
*/
|
||||
<T> T bind(Script script,
|
||||
Class<T> interfaceType);
|
||||
Class<T> interfaceType,
|
||||
ExecutionContext context);
|
||||
|
||||
default <T> T bind(Script script,
|
||||
Class<T> interfaceType) {
|
||||
return bind(script, interfaceType, ExecutionContext.create());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -96,12 +96,12 @@ public abstract class Jsr223ScriptFactory extends AbstractScriptFactory {
|
|||
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public final <T> T bind(Script script, Class<T> interfaceType) {
|
||||
public final <T> T bind(Script script, Class<T> interfaceType,ExecutionContext context) {
|
||||
String returns = createFunctionMapping(interfaceType.getDeclaredMethods());
|
||||
String content = script.getContent() + "\n return " + returns + ";";
|
||||
|
||||
CompiledScript compiledScript = compile(script.content(content), false);
|
||||
Object source = compiledScript.call(Collections.emptyMap());
|
||||
Object source = compiledScript.call(context);
|
||||
Set<Method> ignoreMethods = new HashSet<>();
|
||||
|
||||
return (T) Proxy.newProxyInstance(
|
||||
|
|
|
|||
|
|
@ -1,15 +1,19 @@
|
|||
package org.jetlinks.community.device.message.transparent.script;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.hswebframework.web.exception.ValidationException;
|
||||
import org.jetlinks.community.device.message.transparent.SimpleTransparentMessageCodec;
|
||||
import org.jetlinks.community.device.message.transparent.TransparentMessageCodec;
|
||||
import org.jetlinks.community.device.message.transparent.TransparentMessageCodecProvider;
|
||||
import org.jetlinks.community.script.Script;
|
||||
import org.jetlinks.community.script.ScriptFactory;
|
||||
import org.jetlinks.community.script.Scripts;
|
||||
import org.jetlinks.community.script.context.ExecutionContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
|
@ -28,12 +32,15 @@ public class Jsr223TransparentMessageCodecProvider implements TransparentMessage
|
|||
Assert.hasText(lang, "lang can not be null");
|
||||
Assert.hasText(script, "script can not be null");
|
||||
|
||||
CodecContext context = new CodecContext();
|
||||
ScriptFactory factory = Scripts.getFactory(lang);
|
||||
|
||||
CodecContext context = new CodecContext(factory);
|
||||
|
||||
SimpleTransparentMessageCodec.Codec codec = factory.bind(
|
||||
Script.of("jsr223-transparent", script),
|
||||
SimpleTransparentMessageCodec.Codec.class,
|
||||
ExecutionContext.create(Collections.singletonMap("codec", context)));
|
||||
|
||||
SimpleTransparentMessageCodec.Codec codec = Scripts
|
||||
.getFactory(lang)
|
||||
.bind(Script.of("jsr223-transparent", script),
|
||||
SimpleTransparentMessageCodec.Codec.class);
|
||||
|
||||
if (context.encoder == null && codec != null) {
|
||||
context.onDownstream(codec::encode);
|
||||
|
|
@ -52,8 +59,9 @@ public class Jsr223TransparentMessageCodecProvider implements TransparentMessage
|
|||
));
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public static class CodecContext implements SimpleTransparentMessageCodec.Codec {
|
||||
|
||||
private final ScriptFactory factory;
|
||||
private Function<SimpleTransparentMessageCodec.EncodeContext, Object> encoder;
|
||||
private Function<SimpleTransparentMessageCodec.DecodeContext, Object> decoder;
|
||||
|
||||
|
|
@ -70,7 +78,7 @@ public class Jsr223TransparentMessageCodecProvider implements TransparentMessage
|
|||
if (decoder == null) {
|
||||
return null;
|
||||
}
|
||||
return decoder.apply(context);
|
||||
return factory.convertToJavaType(decoder.apply(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -78,7 +86,7 @@ public class Jsr223TransparentMessageCodecProvider implements TransparentMessage
|
|||
if (encoder == null) {
|
||||
return null;
|
||||
}
|
||||
return encoder.apply(context);
|
||||
return factory.convertToJavaType(encoder.apply(context));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue