diff --git a/README.md b/README.md index 2ea97c6..0b64d2d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# e364a76504dd4c99b7bc1eb507873f53 - +# java hello-world diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8e316e9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + dev.openfunction.samples + samples + 1.0-SNAPSHOT + + + 11 + 11 + + + + + snapshots + Maven snapshots + https://s01.oss.sonatype.org/content/repositories/snapshots/ + + false + + + true + + + + + + + dev.openfunction.functions + functions-framework-api + 1.0.0 + + + + diff --git a/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java b/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java new file mode 100644 index 0000000..ce876d8 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java @@ -0,0 +1,34 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples; + +import dev.openfunction.functions.CloudEventFunction; +import dev.openfunction.functions.Context; +import io.cloudevents.CloudEvent; + +public class CloudEventFunctionImpl implements CloudEventFunction { + @Override + public Error accept(Context ctx, CloudEvent event) throws Exception { + if (event.getData() == null) { + System.out.println("receive event without data"); + } else { + System.out.println("receive event: " + new String(event.getData().toBytes())); + } + + return null; + } +} diff --git a/src/main/java/dev/openfunction/samples/Main.java b/src/main/java/dev/openfunction/samples/Main.java new file mode 100644 index 0000000..635eb10 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/Main.java @@ -0,0 +1,25 @@ +import java.util.logging.Logger; +import java.util.logging.Level; + +public class AddNumbers { + // Create a logger instance + private static final Logger logger = Logger.getLogger(AddNumbers.class.getName()); + + public static void main(String[] args) { + // Example inputs + int num1 = 5; + int num2 = 8; + + // Log inputs + logger.info("Function called with inputs: num1=" + num1 + ", num2=" + num2); + + // Perform addition + int result = num1 + num2; + + // Log result + logger.info("The result of adding " + num1 + " and " + num2 + " is: " + result); + + // Print result to console + System.out.println("Sum: " + result); + } +} \ No newline at end of file diff --git a/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java b/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java new file mode 100644 index 0000000..d26e553 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java @@ -0,0 +1,41 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples; + +import dev.openfunction.functions.OpenFunction; +import dev.openfunction.functions.Context; +import dev.openfunction.functions.Out; + +public class OpenFunctionImpl implements OpenFunction { + + @Override + public Out accept(Context context, String payload) throws Exception { + System.out.printf("receive event: %s", payload).println(); + + if (context.getOutputs() != null) { + for (String key : context.getOutputs().keySet()) { + Error error = context.send(key, payload); + if (error != null) { + System.out.printf("send to output %s error, %s", key, error.getMessage()).println(); + } else { + System.out.printf("send to output %s", key).println(); + } + } + } + return new Out(); + } +} diff --git a/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java b/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java new file mode 100644 index 0000000..828d388 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java @@ -0,0 +1,74 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples.plugins; + +import dev.openfunction.functions.Context; +import dev.openfunction.functions.Plugin; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ExamplePlugin implements Plugin { + @Override + public String name() { + return "plugin-example"; + } + + @Override + public String version() { + return "v1.0.0"; + } + + @Override + public Plugin init() { + return this; + } + + @Override + public Error execPreHook(Context ctx) { + execHook(ctx, "pre"); + return null; + } + + @Override + public Error execPostHook(Context ctx) { + execHook(ctx, "post"); + return null; + } + + private void execHook(Context ctx, String type) { + String ts = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.XXX").format(new Date()); + if (ctx.getBindingEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for binding %s at %s", name(), version(), type, ctx.getBindingEvent().getName(), ts).println(); + } else if (ctx.getTopicEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for pubsub %s at %s", name(), version(), type, ctx.getTopicEvent().getName(), ts).println(); + } else if (ctx.getHttpRequest() != null) { + if (ctx.getCloudEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for cloudevent function at %s", name(), version(), type, ts).println(); + } else { + System.out.printf("plugin %s:%s exec %s hook for http function at %s", name(), version(), type, ts).println(); + } + } else { + System.out.println("unknown function type"); + } + } + + @Override + public Object getField(String fieldName) { + return null; + } +}