summaryrefslogtreecommitdiffstats
path: root/sandbox/container.ruby/src/main/java/org/apache/tuscany/container/ruby/invoker/RubyScript.java
blob: ac66a13806a66912158f1adb54eecf89ca54174a (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
	}
}