|
Spring Framework example source code file (JRubyScriptUtils.java)
The Spring Framework JRubyScriptUtils.java source code
/*
* Copyright 2002-2007 the original author or 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 org.springframework.scripting.jruby;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.List;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyException;
import org.jruby.RubyNil;
import org.jruby.ast.ClassNode;
import org.jruby.ast.Colon2Node;
import org.jruby.ast.NewlineNode;
import org.jruby.ast.Node;
import org.jruby.exceptions.JumpException;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.builtin.IRubyObject;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.NestedRuntimeException;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* Utility methods for handling JRuby-scripted objects.
*
* <p>Note: As of Spring 2.0.4, this class requires JRuby 0.9.8 or 0.9.9.
* As of Spring 2.0.6 / 2.1, it supports JRuby 1.0 as well.
*
* @author Rob Harrop
* @author Juergen Hoeller
* @author Rick Evans
* @since 2.0
*/
public abstract class JRubyScriptUtils {
// Determine whether the old JRuby 0.9 parse method is available (incompatible with 1.0)
private final static Method oldParseMethod = ClassUtils.getMethodIfAvailable(
Ruby.class, "parse", new Class[] {String.class, String.class, DynamicScope.class});
/**
* Create a new JRuby-scripted object from the given script source,
* using the default {@link ClassLoader}.
* @param scriptSource the script source text
* @param interfaces the interfaces that the scripted Java object is to implement
* @return the scripted Java object
* @throws JumpException in case of JRuby parsing failure
* @see ClassUtils#getDefaultClassLoader()
*/
public static Object createJRubyObject(String scriptSource, Class[] interfaces) throws JumpException {
return createJRubyObject(scriptSource, interfaces, ClassUtils.getDefaultClassLoader());
}
/**
* Create a new JRuby-scripted object from the given script source.
* @param scriptSource the script source text
* @param interfaces the interfaces that the scripted Java object is to implement
* @param classLoader the {@link ClassLoader} to create the script proxy with
* @return the scripted Java object
* @throws JumpException in case of JRuby parsing failure
*/
public static Object createJRubyObject(String scriptSource, Class[] interfaces, ClassLoader classLoader) {
Ruby ruby = initializeRuntime();
Node scriptRootNode = (oldParseMethod != null ?
(Node) ReflectionUtils.invokeMethod(oldParseMethod, ruby, new Object[] {scriptSource, "", null}) :
ruby.parse(scriptSource, "", null, 0));
IRubyObject rubyObject = ruby.eval(scriptRootNode);
if (rubyObject instanceof RubyNil) {
String className = findClassName(scriptRootNode);
rubyObject = ruby.evalScript("\n" + className + ".new");
}
// still null?
if (rubyObject instanceof RubyNil) {
throw new IllegalStateException("Compilation of JRuby script returned RubyNil: " + rubyObject);
}
return Proxy.newProxyInstance(classLoader, interfaces, new RubyObjectInvocationHandler(rubyObject, ruby));
}
/**
* Initializes an instance of the {@link org.jruby.Ruby} runtime.
*/
private static Ruby initializeRuntime() {
return JavaEmbedUtils.initialize(Collections.EMPTY_LIST);
}
/**
* Given the root {@link Node} in a JRuby AST will locate the name of the
* class defined by that AST.
* @throws IllegalArgumentException if no class is defined by the supplied AST
*/
private static String findClassName(Node rootNode) {
ClassNode classNode = findClassNode(rootNode);
if (classNode == null) {
throw new IllegalArgumentException("Unable to determine class name for root node '" + rootNode + "'");
}
Colon2Node node = (Colon2Node) classNode.getCPath();
return node.getName();
}
/**
* Find the first {@link ClassNode} under the supplied {@link Node}.
* @return the found <code>ClassNode, or
Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework JRubyScriptUtils.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.