[Red5commits] [1653] removed custom versions in lieu of Spring 2.0.2 version
pgregoire
luke at codegent.com
Wed Jan 24 11:14:54 EST 2007
removed custom versions in lieu of Spring 2.0.2 version
Timestamp: 01/23/07 23:45:08 EST (11 hours ago)
Change: 1653
Author: pgregoire
Files (see diff or trac for details):
java/server/trunk/src/org/red5/server/script/jruby/JRubyScriptFactory.java
java/server/trunk/src/org/red5/server/script/jruby/JRubyScriptUtils.java
Trac: http://mirror1.cvsdude.com/trac/osflash/red5/changeset/1653
Index: /va/server/trunk/src/org/red5/server/script/jruby/JRubyScriptUtils.java
===================================================================
--- /java/server/trunk/src/org/red5/server/script/jruby/JRubyScriptUtils.java (revision 1590)
+++ (revision )
@@ -1,158 +1,0 @@
-/*
- * Copyright 2002-2006 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.red5.server.script.jruby;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.List;
-
-import org.jruby.IRuby;
-import org.jruby.Ruby;
-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.javasupport.JavaUtil;
-import org.jruby.runtime.builtin.IRubyObject;
-import org.springframework.scripting.ScriptCompilationException;
-import org.springframework.util.ClassUtils;
-
-/**
- * Utility methods for handling JRuby-scripted objects.
- *
- * @author Rob Harrop
- * @author Juergen Hoeller
- * @since 2.0
- */
-public abstract class JRubyScriptUtils {
-
- /**
- * 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 supposed to implement
- * @return the scripted Java object
- * @throws JumpException in case of JRuby parsing failure
- */
- public static Object createJRubyObject(String scriptSource,
- Class[] interfaces) throws JumpException {
- IRuby ruby = Ruby.getDefaultInstance();
- //node is a compiled script
- Node scriptRootNode = ruby.parse(scriptSource, "");
- //rubyObj is the result from an evaluated compiled script
- IRubyObject rubyObject = ruby.eval(scriptRootNode);
- //if the script does not return an instance then call the constructor
- if (rubyObject instanceof RubyNil) {
- String className = findClassName(scriptRootNode);
- rubyObject = ruby.evalScript("\n" + className + ".new");
- }
- // still null?
- if (rubyObject instanceof RubyNil) {
- throw new ScriptCompilationException(
- "Compilation of JRuby script returned '" + rubyObject + "' ");
- }
- return Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(),
- interfaces, new RubyObjectInvocationHandler(rubyObject, ruby));
- }
-
- /**
- * Given the root {@link Node} in a JRuby AST will locate the name of the class defined
- * by that AST.
- * @param rootNode
- * @return
- * @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();
- }
-
- /**
- * Finds the first {@link ClassNode} under the supplied {@link Node}. Returns
- * '<code>null</code>' if no {@link ClassNode} is found.
- * @param node
- * @return
- */
- private static ClassNode findClassNode(Node node) {
- if (node instanceof ClassNode) {
- return (ClassNode) node;
- }
- List children = node.childNodes();
- for (int i = 0; i < children.size(); i++) {
- Node child = (Node) children.get(i);
- if (child instanceof ClassNode) {
- return (ClassNode) child;
- } else if (child instanceof NewlineNode) {
- NewlineNode nn = (NewlineNode) child;
- Node found = findClassNode(nn.getNextNode());
- if (found instanceof ClassNode) {
- return (ClassNode) found;
- }
- }
- }
-
- for (int i = 0; i < children.size(); i++) {
- Node child = (Node) children.get(i);
- Node found = findClassNode(child);
- if (found instanceof ClassNode) {
- return (ClassNode) child;
- }
- }
- return null;
- }
-
- /**
- * InvocationHandler that invokes a JRuby script method.
- */
- private static class RubyObjectInvocationHandler implements
- InvocationHandler {
- //scripted instance
- private final IRubyObject rubyObject;
-
- //script engine
- private final IRuby ruby;
-
- public RubyObjectInvocationHandler(IRubyObject rubyObject, IRuby ruby) {
- this.rubyObject = rubyObject;
- this.ruby = ruby;
- }
-
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- IRubyObject[] rubyArgs = convertToRuby(args);
- IRubyObject result = this.rubyObject.callMethod(method.getName(),
- rubyArgs);
- return JavaUtil.convertRubyToJava(result);
- }
-
- private IRubyObject[] convertToRuby(Object[] javaArgs) {
- if (javaArgs == null || javaArgs.length == 0) {
- return new IRubyObject[0];
- }
- return JavaUtil.convertJavaArrayToRuby(this.ruby, javaArgs);
- }
- }
-
-}
Index: /va/server/trunk/src/org/red5/server/script/jruby/JRubyScriptFactory.java
===================================================================
--- /java/server/trunk/src/org/red5/server/script/jruby/JRubyScriptFactory.java (revision 1606)
+++ (revision )
@@ -1,101 +1,0 @@
-/*
- * Copyright 2002-2006 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.red5.server.script.jruby;
-
-import java.io.IOException;
-
-import org.jruby.exceptions.JumpException;
-import org.springframework.scripting.ScriptCompilationException;
-import org.springframework.scripting.ScriptFactory;
-import org.springframework.scripting.ScriptSource;
-import org.springframework.util.Assert;
-
-/**
- * {@link org.springframework.scripting.ScriptFactory} implementation
- * for a JRuby script.
- *
- * <p>Typically used in combination with a
- * {@link org.springframework.scripting.support.ScriptFactoryPostProcessor};
- * see the latter's
- * {@link org.springframework.scripting.support.ScriptFactoryPostProcessor Javadoc}
- * for a configuration example.
- *
- * @author Juergen Hoeller
- * @author Rob Harrop
- * @since 2.0
- * @see org.springframework.scripting.support.ScriptFactoryPostProcessor
- * @see JRubyScriptUtils
- */
-public class JRubyScriptFactory implements ScriptFactory {
-
- private final String scriptSourceLocator;
-
- private final Class[] scriptInterfaces;
-
- /**
- * Create a new JRubyScriptFactory for the given script source.
- * @param scriptSourceLocator a locator that points to the source of the script.
- * Interpreted by the post-processor that actually creates the script.
- * @param scriptInterfaces the Java interfaces that the scripted object
- * is supposed to implement
- * @throws IllegalArgumentException if either of the supplied arguments is <code>null</code>;
- * or the supplied <code>scriptSourceLocator</code> argument is composed wholly of whitespace;
- * or if the supplied <code>scriptInterfaces</code> argument array has no elements
- */
- public JRubyScriptFactory(String scriptSourceLocator,
- Class[] scriptInterfaces) {
- Assert.hasText(scriptSourceLocator);
- Assert.notEmpty(scriptInterfaces);
- this.scriptSourceLocator = scriptSourceLocator;
- this.scriptInterfaces = scriptInterfaces;
- }
-
- /** {@inheritDoc} */
- public String getScriptSourceLocator() {
- return this.scriptSourceLocator;
- }
-
- /** {@inheritDoc} */
- public Class[] getScriptInterfaces() {
- return this.scriptInterfaces;
- }
-
- /**
- * JRuby scripts do require a config interface.
- * @return <code>true</code> always
- */
- public boolean requiresConfigInterface() {
- return true;
- }
-
- /**
- * Load and parse the JRuby script via JRubyScriptUtils.
- * @see JRubyScriptUtils#createJRubyObject(String, Class[])
- */
- public Object getScriptedObject(ScriptSource actualScriptSource,
- Class[] actualInterfaces) throws IOException,
- ScriptCompilationException {
- try {
- return JRubyScriptUtils.createJRubyObject(actualScriptSource
- .getScriptAsString(), actualInterfaces);
- } catch (JumpException ex) {
- throw new ScriptCompilationException(
- "Could not compile JRuby script: " + actualScriptSource, ex);
- }
- }
-
-}
Note:
Diffs are chopped if more than 25k.
This is to get past the limit on the mailing list.
More information about the Red5commits
mailing list