diff --git a/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/ReplEngineConnExecutor.java b/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/ReplEngineConnExecutor.java index bf7201cb61..81f014e030 100644 --- a/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/ReplEngineConnExecutor.java +++ b/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/ReplEngineConnExecutor.java @@ -17,6 +17,9 @@ package org.apache.linkis.engineplugin.repl.executor; +import org.apache.linkis.common.io.MetaData; +import org.apache.linkis.common.io.Record; +import org.apache.linkis.common.io.resultset.ResultSetWriter; import org.apache.linkis.common.log.LogUtils; import org.apache.linkis.common.utils.OverloadUtils; import org.apache.linkis.engineconn.computation.executor.entity.EngineConnTask; @@ -36,15 +39,23 @@ import org.apache.linkis.manager.label.entity.engine.UserCreatorLabel; import org.apache.linkis.protocol.engine.JobProgressInfo; import org.apache.linkis.rpc.Sender; +import org.apache.linkis.scheduler.executer.ErrorExecuteResponse; import org.apache.linkis.scheduler.executer.ExecuteResponse; import org.apache.linkis.scheduler.executer.SuccessExecuteResponse; +import org.apache.linkis.storage.LineMetaData; +import org.apache.linkis.storage.LineRecord; +import org.apache.linkis.storage.resultset.ResultSetFactory; import org.apache.commons.collections4.MapUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.util.CollectionUtils; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -132,14 +143,33 @@ public ExecuteResponse executeLine(EngineExecutionContext engineExecutorContext, threadCache.put(taskId, Thread.currentThread()); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024); + PrintStream cacheStream = new PrintStream(outputStream); + PrintStream oldStream = System.out; + System.setOut(cacheStream); + try { replAdapter.executorCode(realCode, classpathDir, methodName); } catch (Exception e) { String errorMessage = ExceptionUtils.getStackTrace(e); logger.error("Repl engine execute failed : {}", errorMessage); engineExecutorContext.appendStdout(LogUtils.generateERROR(errorMessage)); + return new ErrorExecuteResponse(errorMessage, null); } + String message = outputStream.toString(); + System.setOut(oldStream); + engineExecutorContext.appendStdout(message); + ResultSetWriter resultSetWriter = + engineExecutorContext.createResultSetWriter(ResultSetFactory.TEXT_TYPE); + try { + resultSetWriter.addMetaData(new LineMetaData()); + resultSetWriter.addRecord(new LineRecord(message)); + } catch (IOException e) { + logger.error("Failed to get the task result"); + } finally { + IOUtils.closeQuietly(resultSetWriter); + } return new SuccessExecuteResponse(); } diff --git a/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/javarepl/JavaReplCompiler.java b/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/javarepl/JavaReplCompiler.java index 7be4369735..9878ed5025 100644 --- a/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/javarepl/JavaReplCompiler.java +++ b/linkis-engineconn-plugins/repl/src/main/java/org/apache/linkis/engineplugin/repl/executor/javarepl/JavaReplCompiler.java @@ -54,7 +54,7 @@ public class JavaReplCompiler { Pattern.compile("\\s+implements\\s+([\\w\\.]+)\\s*\\{\n"); private static final Pattern METHODS_PATTERN = - Pattern.compile("\n(private|public|protected)\\s+"); + Pattern.compile("(?<=\\})\\s+(private|public|protected)\\s+"); private static final Pattern FIELD_PATTERN = Pattern.compile("[^\n]+=[^\n]+;"); diff --git a/linkis-engineconn-plugins/repl/src/main/resources/repl-ec.md b/linkis-engineconn-plugins/repl/src/main/resources/repl-ec.md index 5eb8c6c1b2..e04aec5d29 100644 --- a/linkis-engineconn-plugins/repl/src/main/resources/repl-ec.md +++ b/linkis-engineconn-plugins/repl/src/main/resources/repl-ec.md @@ -71,6 +71,43 @@ import org.apache.commons.lang3.StringUtils; } ``` +#### 1.3. Multiple methods + +```text +import org.apache.commons.lang3.StringUtils; + + public void sayHello() { + System.out.println("hello"); + System.out.println(StringUtils.isEmpty("hello")); + } + public void sayHi() { + System.out.println("hi"); + System.out.println(StringUtils.isEmpty("hi")); + } +``` + +```json +{ + "executionContent":{ + "code":"import org.apache.commons.lang3.StringUtils;\n\n public void sayHello() {\n System.out.println(\"hello\");\n System.out.println(StringUtils.isEmpty(\"hello\"));\n }\n public void sayHi() {\n System.out.println(\"hi\");\n System.out.println(StringUtils.isEmpty(\"hi\"));\n }", + "runType":"repl" + }, + "params":{ + "configuration":{ + "runtime":{ + "linkis.repl.type":"java", + "linkis.repl.method.name":"sayHi" + } + } + }, + "labels":{ + "engineType":"repl-1", + "userCreator":"linkis-IDE" + } +} +``` + + ### 2. Submitting scala tasks with Restful API ```text