001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.scripting.jruby;
018:
019: import java.lang.reflect.Array;
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Proxy;
023: import java.util.Collections;
024: import java.util.List;
025:
026: import org.jruby.Ruby;
027: import org.jruby.RubyArray;
028: import org.jruby.RubyException;
029: import org.jruby.RubyNil;
030: import org.jruby.ast.ClassNode;
031: import org.jruby.ast.Colon2Node;
032: import org.jruby.ast.NewlineNode;
033: import org.jruby.ast.Node;
034: import org.jruby.exceptions.JumpException;
035: import org.jruby.exceptions.RaiseException;
036: import org.jruby.javasupport.JavaEmbedUtils;
037: import org.jruby.runtime.DynamicScope;
038: import org.jruby.runtime.builtin.IRubyObject;
039:
040: import org.springframework.aop.support.AopUtils;
041: import org.springframework.core.NestedRuntimeException;
042: import org.springframework.util.ClassUtils;
043: import org.springframework.util.ObjectUtils;
044: import org.springframework.util.ReflectionUtils;
045: import org.springframework.util.StringUtils;
046:
047: /**
048: * Utility methods for handling JRuby-scripted objects.
049: *
050: * <p>Note: As of Spring 2.0.4, this class requires JRuby 0.9.8 or 0.9.9.
051: * As of Spring 2.0.6, it supports JRuby 1.0 as well.
052: *
053: * @author Rob Harrop
054: * @author Juergen Hoeller
055: * @author Rick Evans
056: * @since 2.0
057: */
058: public abstract class JRubyScriptUtils {
059:
060: // Determine whether the new JRuby 1.0 parse method is available (incompatible with 0.9)
061: private final static Method newParseMethod = ClassUtils
062: .getMethodIfAvailable(Ruby.class, "parse", new Class[] {
063: String.class, String.class, DynamicScope.class,
064: int.class });
065:
066: /**
067: * Create a new JRuby-scripted object from the given script source,
068: * using the default {@link ClassLoader}.
069: * @param scriptSource the script source text
070: * @param interfaces the interfaces that the scripted Java object is to implement
071: * @return the scripted Java object
072: * @throws JumpException in case of JRuby parsing failure
073: * @see ClassUtils#getDefaultClassLoader()
074: */
075: public static Object createJRubyObject(String scriptSource,
076: Class[] interfaces) throws JumpException {
077: return createJRubyObject(scriptSource, interfaces, ClassUtils
078: .getDefaultClassLoader());
079: }
080:
081: /**
082: * Create a new JRuby-scripted object from the given script source.
083: * @param scriptSource the script source text
084: * @param interfaces the interfaces that the scripted Java object is to implement
085: * @param classLoader the {@link ClassLoader} to create the script proxy with
086: * @return the scripted Java object
087: * @throws JumpException in case of JRuby parsing failure
088: */
089: public static Object createJRubyObject(String scriptSource,
090: Class[] interfaces, ClassLoader classLoader) {
091: Ruby ruby = initializeRuntime();
092:
093: Node scriptRootNode = (newParseMethod != null ? (Node) ReflectionUtils
094: .invokeMethod(newParseMethod, ruby, new Object[] {
095: scriptSource, "", null, new Integer(0) })
096: : ruby.parse(scriptSource, "", null));
097: IRubyObject rubyObject = ruby.eval(scriptRootNode);
098:
099: if (rubyObject instanceof RubyNil) {
100: String className = findClassName(scriptRootNode);
101: rubyObject = ruby.evalScript("\n" + className + ".new");
102: }
103: // still null?
104: if (rubyObject instanceof RubyNil) {
105: throw new IllegalStateException(
106: "Compilation of JRuby script returned RubyNil: "
107: + rubyObject);
108: }
109:
110: return Proxy.newProxyInstance(classLoader, interfaces,
111: new RubyObjectInvocationHandler(rubyObject, ruby));
112: }
113:
114: /**
115: * Initializes an instance of the {@link org.jruby.Ruby} runtime.
116: */
117: private static Ruby initializeRuntime() {
118: return JavaEmbedUtils.initialize(Collections.EMPTY_LIST);
119: }
120:
121: /**
122: * Given the root {@link Node} in a JRuby AST will locate the name of the
123: * class defined by that AST.
124: * @throws IllegalArgumentException if no class is defined by the supplied AST
125: */
126: private static String findClassName(Node rootNode) {
127: ClassNode classNode = findClassNode(rootNode);
128: if (classNode == null) {
129: throw new IllegalArgumentException(
130: "Unable to determine class name for root node '"
131: + rootNode + "'");
132: }
133: Colon2Node node = (Colon2Node) classNode.getCPath();
134: return node.getName();
135: }
136:
137: /**
138: * Find the first {@link ClassNode} under the supplied {@link Node}.
139: * @return the found <code>ClassNode</code>, or <code>null</code>
140: * if no {@link ClassNode} is found
141: */
142: private static ClassNode findClassNode(Node node) {
143: if (node instanceof ClassNode) {
144: return (ClassNode) node;
145: }
146: List children = node.childNodes();
147: for (int i = 0; i < children.size(); i++) {
148: Node child = (Node) children.get(i);
149: if (child instanceof ClassNode) {
150: return (ClassNode) child;
151: } else if (child instanceof NewlineNode) {
152: NewlineNode nn = (NewlineNode) child;
153: Node found = findClassNode(nn.getNextNode());
154: if (found instanceof ClassNode) {
155: return (ClassNode) found;
156: }
157: }
158: }
159: for (int i = 0; i < children.size(); i++) {
160: Node child = (Node) children.get(i);
161: Node found = findClassNode(child);
162: if (found instanceof ClassNode) {
163: return (ClassNode) found;
164: }
165: }
166: return null;
167: }
168:
169: /**
170: * InvocationHandler that invokes a JRuby script method.
171: */
172: private static class RubyObjectInvocationHandler implements
173: InvocationHandler {
174:
175: private final IRubyObject rubyObject;
176:
177: private final Ruby ruby;
178:
179: public RubyObjectInvocationHandler(IRubyObject rubyObject,
180: Ruby ruby) {
181: this .rubyObject = rubyObject;
182: this .ruby = ruby;
183: }
184:
185: public Object invoke(Object proxy, Method method, Object[] args)
186: throws Throwable {
187: if (AopUtils.isEqualsMethod(method)) {
188: return (isProxyForSameRubyObject(args[0]) ? Boolean.TRUE
189: : Boolean.FALSE);
190: }
191: if (AopUtils.isHashCodeMethod(method)) {
192: return new Integer(this .rubyObject.hashCode());
193: }
194: if (AopUtils.isToStringMethod(method)) {
195: String toStringResult = this .rubyObject.toString();
196: if (!StringUtils.hasText(toStringResult)) {
197: toStringResult = ObjectUtils
198: .identityToString(this .rubyObject);
199: }
200: return "JRuby object [" + toStringResult + "]";
201: }
202: try {
203: IRubyObject[] rubyArgs = convertToRuby(args);
204: IRubyObject rubyResult = this .rubyObject.callMethod(
205: this .ruby.getCurrentContext(),
206: method.getName(), rubyArgs);
207: return convertFromRuby(rubyResult, method
208: .getReturnType());
209: } catch (RaiseException ex) {
210: throw new JRubyExecutionException(ex);
211: }
212: }
213:
214: private boolean isProxyForSameRubyObject(Object other) {
215: if (!Proxy.isProxyClass(other.getClass())) {
216: return false;
217: }
218: InvocationHandler ih = Proxy.getInvocationHandler(other);
219: return (ih instanceof RubyObjectInvocationHandler && this .rubyObject
220: .equals(((RubyObjectInvocationHandler) ih).rubyObject));
221: }
222:
223: private IRubyObject[] convertToRuby(Object[] javaArgs) {
224: if (javaArgs == null || javaArgs.length == 0) {
225: return new IRubyObject[0];
226: }
227: IRubyObject[] rubyArgs = new IRubyObject[javaArgs.length];
228: for (int i = 0; i < javaArgs.length; ++i) {
229: rubyArgs[i] = JavaEmbedUtils.javaToRuby(this .ruby,
230: javaArgs[i]);
231: }
232: return rubyArgs;
233: }
234:
235: private Object convertFromRuby(IRubyObject rubyResult,
236: Class returnType) {
237: Object result = JavaEmbedUtils.rubyToJava(this .ruby,
238: rubyResult, returnType);
239: if (result instanceof RubyArray && returnType.isArray()) {
240: result = convertFromRubyArray(((RubyArray) result)
241: .toJavaArray(), returnType);
242: }
243: return result;
244: }
245:
246: private Object convertFromRubyArray(IRubyObject[] rubyArray,
247: Class returnType) {
248: Class targetType = returnType.getComponentType();
249: Object javaArray = Array.newInstance(targetType,
250: rubyArray.length);
251: for (int i = 0; i < rubyArray.length; i++) {
252: IRubyObject rubyObject = rubyArray[i];
253: Array.set(javaArray, i, convertFromRuby(rubyObject,
254: targetType));
255: }
256: return javaArray;
257: }
258: }
259:
260: /**
261: * Exception thrown in response to a JRuby {@link RaiseException}
262: * being thrown from a JRuby method invocation.
263: * <p>Introduced because the <code>RaiseException</code> class does not
264: * have useful {@link Object#toString()}, {@link Throwable#getMessage()},
265: * and {@link Throwable#printStackTrace} implementations.
266: */
267: public static class JRubyExecutionException extends
268: NestedRuntimeException {
269:
270: /**
271: * Create a new <code>JRubyException</code>,
272: * wrapping the given JRuby <code>RaiseException</code>.
273: * @param ex the cause (must not be <code>null</code>)
274: */
275: public JRubyExecutionException(RaiseException ex) {
276: super (buildMessage(ex), ex);
277: }
278:
279: private static String buildMessage(RaiseException ex) {
280: RubyException rubyEx = ex.getException();
281: return (rubyEx != null && rubyEx.message != null) ? rubyEx.message
282: .toString()
283: : "Unexpected JRuby error";
284: }
285: }
286:
287: }
|