-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAWSLambdaJRuby.java
More file actions
40 lines (29 loc) · 1.1 KB
/
AWSLambdaJRuby.java
File metadata and controls
40 lines (29 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import org.jruby.Ruby;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
import com.amazonaws.services.lambda.runtime.Context;
import com.google.gson.Gson;
public class AWSLambdaJRuby {
private static final String rubyFileName = "main.rb";
static {
Ruby.newInstance();
}
@SuppressWarnings("rawtypes")
public String handler(Map data, Context context) throws Exception {
ScriptingContainer container = new ScriptingContainer();
String arg = new Gson().toJson(data);
context.getLogger().log(arg);
//add std lib path
// URL stdLibPath = getClass().getResource("/stdlib/").toURI().toURL();
// container.addLoadPath(new URLClassLoader(new URL[]{stdLibPath}));
//set argument of lambda function to ruby global variable as JSON format
container.put("$lambda_arg", arg);
// uploaded zip is extracted to /var/task directory
container.setCurrentDirectory("/var/task");
Object result = container.runScriptlet(PathType.CLASSPATH,rubyFileName);
return result == null ? null : result.toString();
}
}