1 16 17 package org.springframework.scripting.jruby; 18 19 import java.io.IOException ; 20 21 import org.jruby.RubyException; 22 import org.jruby.exceptions.JumpException; 23 import org.jruby.exceptions.RaiseException; 24 25 import org.springframework.beans.factory.BeanClassLoaderAware; 26 import org.springframework.scripting.ScriptCompilationException; 27 import org.springframework.scripting.ScriptFactory; 28 import org.springframework.scripting.ScriptSource; 29 import org.springframework.util.Assert; 30 import org.springframework.util.ClassUtils; 31 32 46 public class JRubyScriptFactory implements ScriptFactory, BeanClassLoaderAware { 47 48 private final String scriptSourceLocator; 49 50 private final Class [] scriptInterfaces; 51 52 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 53 54 55 62 public JRubyScriptFactory(String scriptSourceLocator, Class [] scriptInterfaces) { 63 Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty"); 64 Assert.notEmpty(scriptInterfaces, "'scriptInterfaces' must not be empty"); 65 this.scriptSourceLocator = scriptSourceLocator; 66 this.scriptInterfaces = scriptInterfaces; 67 } 68 69 70 public void setBeanClassLoader(ClassLoader classLoader) { 71 this.beanClassLoader = classLoader; 72 } 73 74 75 public String getScriptSourceLocator() { 76 return this.scriptSourceLocator; 77 } 78 79 public Class [] getScriptInterfaces() { 80 return this.scriptInterfaces; 81 } 82 83 86 public boolean requiresConfigInterface() { 87 return true; 88 } 89 90 94 public Object getScriptedObject(ScriptSource scriptSource, Class [] actualInterfaces) 95 throws IOException , ScriptCompilationException { 96 try { 97 return JRubyScriptUtils.createJRubyObject( 98 scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); 99 } 100 catch (RaiseException ex) { 101 RubyException rubyEx = ex.getException(); 102 String msg = (rubyEx != null && rubyEx.message != null) ? 103 rubyEx.message.toString() : "Unexpected JRuby error"; 104 throw new ScriptCompilationException( 105 "Could not compile JRuby script [" + scriptSource + "]: " + msg, ex); 106 } 107 catch (JumpException ex) { 108 throw new ScriptCompilationException( 109 "Could not compile JRuby script [" + scriptSource + "]", ex); 110 } 111 } 112 113 public Class getScriptedObjectType(ScriptSource scriptSource) 114 throws IOException , ScriptCompilationException { 115 116 return null; 117 } 118 119 120 public String toString() { 121 return "JRubyScriptFactory: script source locator [" + this.scriptSourceLocator + "]"; 122 } 123 124 } 125 | Popular Tags |