/*
 * 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.io.IOException;

import org.jruby.exceptions.JumpException;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.scripting.ScriptCompilationException;
import org.springframework.scripting.ScriptFactory;
import org.springframework.scripting.ScriptSource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
 * {@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 JRubyScriptUtils
 * @see org.springframework.scripting.support.ScriptFactoryPostProcessor
 */
public class JRubyScriptFactory implements ScriptFactory, BeanClassLoaderAware {

	private final String scriptSourceLocator;

	private final Class[] scriptInterfaces;

	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();


	/**
	 * 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
	 */
	public JRubyScriptFactory(String scriptSourceLocator, Class[] scriptInterfaces) {
		Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
		Assert.notEmpty(scriptInterfaces, "'scriptInterfaces' must not be empty");
		this.scriptSourceLocator = scriptSourceLocator;
		this.scriptInterfaces = scriptInterfaces;
	}


	public void setBeanClassLoader(ClassLoader classLoader) {
		this.beanClassLoader = classLoader;
	}


	public String getScriptSourceLocator() {
		return this.scriptSourceLocator;
	}

	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[], ClassLoader)
	 */
	public Object getScriptedObject(ScriptSource actualScriptSource, Class[] actualInterfaces)
			throws IOException, ScriptCompilationException {
		try {
			return JRubyScriptUtils.createJRubyObject(
					actualScriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
		}
		catch (JumpException ex) {
			throw new ScriptCompilationException("Could not compile JRuby script: " + actualScriptSource, ex);
		}
	}

}

@@ -711,23 +711,99 @@
 uby.
-exceptions.Jump
+RubyException;%0Aimport org.jruby.exceptions.JumpException;%0Aimport org.jruby.exceptions.Raise
 Exce
@@ -1369,91 +1369,16 @@
 er's
-%0A * %7B@link org.springframework.scripting.support.ScriptFactoryPostProcessor
  javadoc
 %7D%0A *
@@ -1377,12 +1377,8 @@
 adoc
-%7D%0A *
  for
@@ -2805,45 +2805,8 @@
 ce.%0A
-%09 * @return %3Ccode%3Etrue%3C/code%3E always%0A
 %09 */
@@ -3299,16 +3299,337 @@
 r);%0A%09%09%7D%0A
+%09%09catch (RaiseException ex) %7B%0A%09%09%09RubyException rubyEx = ex.getException();%0A%09%09%09String msg = (rubyEx != null && rubyEx.message != null) ?%0A%09%09%09%09%09rubyEx.message.toString() : %22Unexpected JRuby error%22;%0A%09%09%09throw new ScriptCompilationException(%0A%09%09%09%09%09%22Could not compile JRuby script %5B%22 + actualScriptSource + %22%5D: %22 + msg, ex);%0A%09%09%7D%0A
 %09%09catch 
@@ -3689,16 +3689,22 @@
 ception(
+%0A%09%09%09%09%09
 %22Could n
@@ -3726,18 +3726,18 @@
 y script
-:
  
+%5B
 %22 + actu
@@ -3750,16 +3750,22 @@
 ptSource
+ + %22%5D%22
 , ex);%0A%09

handle RaiseException specifically, extracting a proper message