From e61c307d9bed9ae33892c57cde4da5a06c26ffbb Mon Sep 17 00:00:00 2001 From: Alex Collins Date: Thu, 10 Jun 2021 09:24:35 -0700 Subject: [PATCH] feat: add context to Java runtime --- Dockerfile | 2 ++ dsls/python/__init__.py | 12 ++++++------ examples/104-java16-pipeline.py | 5 +++-- examples/104-java16-pipeline.yaml | 5 +++-- runtimes/java16/Handler.java | 4 +++- runtimes/java16/Main.java | 4 ++-- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1a3081e7..c095c4ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,6 +49,7 @@ ADD runtimes/go1-16 /workspace RUN chown -R 9653 /.cache /workspace WORKDIR /workspace USER 9653:9653 +RUN go build ./... ENTRYPOINT ./entrypoint.sh FROM openjdk:16 AS java16 @@ -56,6 +57,7 @@ ADD runtimes/java16 /workspace RUN chown -R 9653 /workspace WORKDIR /workspace USER 9653:9653 +RUN javac *.java ENTRYPOINT ./entrypoint.sh FROM python:3.9-alpine AS python3-9 diff --git a/dsls/python/__init__.py b/dsls/python/__init__.py index fc647259..ec01f49b 100644 --- a/dsls/python/__init__.py +++ b/dsls/python/__init__.py @@ -8,7 +8,7 @@ def str_presenter(dumper, data): - if len(data.splitlines()) > 1 or '"' in data or "'" in data: + if '\n' in data or '"' in data or "'" in data: return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') return dumper.represent_scalar('tag:yaml.org,2002:str', data) @@ -433,7 +433,7 @@ def dump(self): class KafkaSource(Source): - def __init__(self, topic, name=None, retryPolicy=None): + def __init__(self, topic, name=None, retryPolicy=None): super().__init__(name=name, retryPolicy=retryPolicy) self._topic = topic @@ -444,7 +444,7 @@ def dump(self): class STANSource(Source): - def __init__(self, subject, name=None, retryPolicy=None): + def __init__(self, subject, name=None, retryPolicy=None): super().__init__(name=name, retryPolicy=retryPolicy) self._subject = subject @@ -463,9 +463,9 @@ def http(name=None, retryPolicy=None): return HTTPSource(name=name, retryPolicy=retryPolicy) -def kafka(topic, name=None, retryPolicy=None): - return KafkaSource(topic, name=name, retryPolicy=retryPolicy) +def kafka(topic, name=None, retryPolicy=None): + return KafkaSource(topic, name=name, retryPolicy=retryPolicy) -def stan(subject, name=None, retryPolicy=None): +def stan(subject, name=None, retryPolicy=None): return STANSource(subject, name=name, retryPolicy=retryPolicy) diff --git a/examples/104-java16-pipeline.py b/examples/104-java16-pipeline.py index cd3386be..6966902b 100644 --- a/examples/104-java16-pipeline.py +++ b/examples/104-java16-pipeline.py @@ -13,8 +13,9 @@ def handler(msg): [Learn about handlers](../docs/HANDLERS.md)""") .step( (kafka('input-topic') - .handler('main', code="""public class Handler { - public static byte[] Handle(byte[] msg) throws Exception { + .handler('main', code="""import java.util.Map; + +public static byte[] Handle(byte[] msg, Map context) throws Exception { return msg; } }""", runtime='java16') diff --git a/examples/104-java16-pipeline.yaml b/examples/104-java16-pipeline.yaml index 795fa3de..8ee23632 100644 --- a/examples/104-java16-pipeline.yaml +++ b/examples/104-java16-pipeline.yaml @@ -12,8 +12,9 @@ spec: steps: - handler: code: |- - public class Handler { - public static byte[] Handle(byte[] msg) throws Exception { + import java.util.Map; + + public static byte[] Handle(byte[] msg, Map context) throws Exception { return msg; } } diff --git a/runtimes/java16/Handler.java b/runtimes/java16/Handler.java index 1b129bd6..80b6fbad 100644 --- a/runtimes/java16/Handler.java +++ b/runtimes/java16/Handler.java @@ -1,5 +1,7 @@ +import java.util.Map; + public class Handler { - public static byte[] Handle(byte[] msg) throws Exception { + public static byte[] Handle(byte[] msg, Map context) throws Exception { var x = "hi " + new String(msg); return x.getBytes(); } diff --git a/runtimes/java16/Main.java b/runtimes/java16/Main.java index 9499baa5..f876bc0f 100644 --- a/runtimes/java16/Main.java +++ b/runtimes/java16/Main.java @@ -1,5 +1,4 @@ import com.sun.net.httpserver.HttpServer; - import java.io.ByteArrayOutputStream; import java.io.InputStreamReader; import java.net.InetSocketAddress; @@ -9,6 +8,7 @@ import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.util.concurrent.Executors; +import java.util.Collections; public class Main { public static void main(String[] args) throws Exception { @@ -25,7 +25,7 @@ public static void main(String[] args) throws Exception { } isr.close(); try { - var out = Handler.Handle(in.toByteArray()); + var out = Handler.Handle(in.toByteArray(), Collections.emptyMap()); if (out != null) { he.sendResponseHeaders(201, 0); try (var os = he.getResponseBody()) {