summaryrefslogtreecommitdiffstats
path: root/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java')
-rw-r--r--sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
new file mode 100644
index 0000000000..ac66a13806
--- /dev/null
+++ b/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
@@ -0,0 +1,101 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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 org.apache.tuscany.container.ruby.invoker;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import org.eclipse.emf.common.util.URI;
+
+import org.apache.bsf.BSFManager;
+import org.apache.bsf.util.IOUtils;
+import org.jruby.RubyObject;
+
+
+/**
+ * Models a Ruby script.
+ */
+public class RubyScript {
+
+ // Script to run.
+ private String script;
+ private BSFManager bsfManager;
+
+ /**
+ * Initializes the script name.
+ * @param script Full path to the script.
+ */
+ public RubyScript(String script) {
+ this.script = script;
+
+ // Register the BSF Engine
+ BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine",
+ new String[] { "rb" });
+ bsfManager = new BSFManager();
+ }
+
+ /**
+ * Executes the script.
+ * @param methodName Name of the method to be executed.
+ * @param args Arguments to the method.
+ * @return Result of invocation.
+ * @throws CompilationFailedException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public Object runScript(String methodName, Object[] args) throws InstantiationException, IllegalAccessException {
+
+ ClassLoader classLoader = getClass().getClassLoader();
+
+ try {
+ // Get the Ruby Class
+ Object rubyClass = getRubyClass(classLoader, script);
+ Method rubyMethod = findFunction(rubyClass, methodName);
+ Object result = null;
+ if(rubyMethod != null) {
+ result = rubyMethod.invoke(rubyClass, args);
+ }
+ return result;
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ }
+
+ return null;
+
+ }
+
+ // Gives the Ruby class for the corresponding script
+ // Can optimize in future by caching the instance
+ private Object getRubyClass(ClassLoader classLoader, String rubyScriptName) throws Exception {
+ // Load the ruby script
+ InputStream inputStream = classLoader.getResourceAsStream(rubyScriptName);
+ String script = IOUtils.getStringFromReader(new InputStreamReader(inputStream));
+ bsfManager.exec("ruby", "(java)", 1, 1, script);
+
+ // Get the Ruby class
+ String rubyClassName=URI.createURI(rubyScriptName).trimFileExtension().lastSegment();
+ Object rubyClass = bsfManager.eval("ruby", "(java)", 1, 1, rubyClassName+".new");
+ return rubyClass;
+ }
+
+ // Find whether a particular method exists
+ private Method findFunction(Object rubyClass, String functionName) throws Exception {
+ Class[] parameterTypes = new Class[]{String.class};
+ Method method = rubyClass.getClass().getMethod(functionName, parameterTypes);
+ return method;
+ }
+}