1 16 17 package org.springframework.scripting.bsh; 18 19 import java.io.IOException ; 20 21 import bsh.EvalError; 22 23 import org.springframework.beans.factory.BeanClassLoaderAware; 24 import org.springframework.scripting.ScriptCompilationException; 25 import org.springframework.scripting.ScriptFactory; 26 import org.springframework.scripting.ScriptSource; 27 import org.springframework.util.Assert; 28 import org.springframework.util.ClassUtils; 29 30 44 public class BshScriptFactory implements ScriptFactory, BeanClassLoaderAware { 45 46 private final String scriptSourceLocator; 47 48 private final Class [] scriptInterfaces; 49 50 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 51 52 private Class scriptClass; 53 54 private final Object scriptClassMonitor = new Object (); 55 56 57 64 public BshScriptFactory(String scriptSourceLocator) { 65 this(scriptSourceLocator, null); 66 } 67 68 79 public BshScriptFactory(String scriptSourceLocator, Class [] scriptInterfaces) { 80 Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty"); 81 this.scriptSourceLocator = scriptSourceLocator; 82 this.scriptInterfaces = scriptInterfaces; 83 } 84 85 public void setBeanClassLoader(ClassLoader classLoader) { 86 this.beanClassLoader = classLoader; 87 } 88 89 90 public String getScriptSourceLocator() { 91 return this.scriptSourceLocator; 92 } 93 94 public Class [] getScriptInterfaces() { 95 return this.scriptInterfaces; 96 } 97 98 101 public boolean requiresConfigInterface() { 102 return true; 103 } 104 105 109 public Object getScriptedObject(ScriptSource actualScriptSource, Class [] actualInterfaces) 110 throws IOException , ScriptCompilationException { 111 112 try { 113 Class clazz = null; 114 synchronized (this.scriptClassMonitor) { 115 if (actualScriptSource.isModified()) { 116 Object result = BshScriptUtils.evaluateBshScript( 118 actualScriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); 119 if (result instanceof Class ) { 120 this.scriptClass = (Class ) result; 123 } 124 else { 125 return result; 130 } 131 } 132 clazz = this.scriptClass; 133 } 134 if (clazz != null) { 135 try { 137 return clazz.newInstance(); 138 } 139 catch (Throwable ex) { 140 throw new ScriptCompilationException("Could not instantiate script class: " + clazz, ex); 141 } 142 } 143 else { 144 return BshScriptUtils.createBshObject( 146 actualScriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); 147 } 148 } 149 catch (EvalError ex) { 150 throw new ScriptCompilationException("Could not compile BeanShell script: " + actualScriptSource, ex); 151 } 152 } 153 154 public Class getScriptedObjectType(ScriptSource scriptSource) 155 throws IOException , ScriptCompilationException { 156 157 try { 158 synchronized (this.scriptClassMonitor) { 159 if (scriptSource.isModified()) { 160 this.scriptClass = BshScriptUtils.determineBshObjectType(scriptSource.getScriptAsString()); 162 } 163 return this.scriptClass; 164 } 165 } 166 catch (EvalError ex) { 167 throw new ScriptCompilationException("Could not compile BeanShell script: " + scriptSource, ex); 168 } 169 } 170 171 172 public String toString() { 173 return "BshScriptFactory: script source locator [" + this.scriptSourceLocator + "]"; 174 } 175 176 } 177 | Popular Tags |