/** * Determine whether a refresh is required through calling * ScriptFactory's {@code requiresScriptedObjectRefresh} method. * @see ScriptFactory#requiresScriptedObjectRefresh(ScriptSource) */ @Override protected boolean requiresRefresh() { return this.scriptFactory.requiresScriptedObjectRefresh(this.scriptSource); }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
@Override @Nullable public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException { synchronized (this.scriptClassMonitor) { try { if (scriptSource.isModified()) { // New script content: Let's check whether it evaluates to a Class. this.wasModifiedForTypeCheck = true; this.scriptClass = BshScriptUtils.determineBshObjectType( scriptSource.getScriptAsString(), this.beanClassLoader); } return this.scriptClass; } catch (EvalError ex) { this.scriptClass = null; throw new ScriptCompilationException(scriptSource, ex); } } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> argumentBindings) { ScriptEngine engine = getScriptEngine(script); try { if (CollectionUtils.isEmpty(argumentBindings)) { return engine.eval(script.getScriptAsString()); } else { Bindings bindings = StandardScriptUtils.getBindings(argumentBindings); return engine.eval(script.getScriptAsString(), bindings); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, new StandardScriptEvalException(ex)); } }
@Test public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception { ScriptSource script = mock(ScriptSource.class); String badScript = "class Foo { public Foo(String foo) {}}"; given(script.getScriptAsString()).willReturn(badScript); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript); try { factory.getScriptedObject(script); fail("Must have thrown a ScriptCompilationException (no public no-arg ctor in scripted class)."); } catch (ScriptCompilationException expected) { assertTrue(expected.contains(NoSuchMethodException.class)); } }
protected Object evaluateScript(ScriptSource scriptSource) { try { ScriptEngine scriptEngine = this.scriptEngine; if (scriptEngine == null) { scriptEngine = retrieveScriptEngine(scriptSource); if (scriptEngine == null) { throw new IllegalStateException("Could not determine script engine for " + scriptSource); } this.scriptEngine = scriptEngine; } return scriptEngine.eval(scriptSource.getScriptAsString()); } catch (Exception ex) { throw new ScriptCompilationException(scriptSource, ex); } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { String filename = (script instanceof ResourceScriptSource ? ((ResourceScriptSource) script).getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } else { return groovyShell.evaluate(script.getScriptAsString()); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access Groovy script", ex); } catch (GroovyRuntimeException ex) { throw new ScriptCompilationException(script, ex); } }
@Test public void scriptThatCompilesButIsJustPlainBad() throws IOException { ScriptSource script = mock(ScriptSource.class); final String badScript = "String getMessage() { throw new IllegalArgumentException(); }"; given(script.getScriptAsString()).willReturn(badScript); given(script.isModified()).willReturn(true); BshScriptFactory factory = new BshScriptFactory( ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class); try { Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class); messenger.getMessage(); fail("Must have thrown a BshScriptUtils.BshExecutionException."); } catch (BshScriptUtils.BshExecutionException expected) { } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { try { Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(this.classLoader); if (arguments != null) { for (Map.Entry<String, Object> entry : arguments.entrySet()) { interpreter.set(entry.getKey(), entry.getValue()); } } return interpreter.eval(new StringReader(script.getScriptAsString())); } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex); } catch (EvalError ex) { throw new ScriptCompilationException(script, ex); } }
@Test public void testGroovyScriptFromString() { ScriptEvaluator evaluator = new GroovyScriptEvaluator(); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2")); assertEquals(6, result); }
@Test public void testBshScriptFromString() { ScriptEvaluator evaluator = new BshScriptEvaluator(); Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2;")); assertEquals(6, result); }
@Test public void testResourceScriptFromTag() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass()); Messenger messenger = (Messenger) ctx.getBean("messenger"); CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect"); assertTrue(AopUtils.isAopProxy(messenger)); assertFalse(messenger instanceof Refreshable); assertEquals(0, countingAspect.getCalls()); assertEquals("Hello World!", messenger.getMessage()); assertEquals(1, countingAspect.getCalls()); ctx.close(); assertEquals(-200, countingAspect.getCalls()); }
public String getMessage() { return this.messenger.getMessage(); } }
private void testMetaClass(String xmlFile) { // expect the exception we threw in the custom metaclass to show it got invoked try { ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlFile); Calculator calc = (Calculator) ctx.getBean("delegatingCalculator"); calc.add(1, 2); fail("expected IllegalStateException"); } catch (IllegalStateException ex) { assertEquals("Gotcha", ex.getMessage()); } }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
/** * Determine whether a refresh is required through calling * ScriptFactory's {@code requiresScriptedObjectRefresh} method. * @see ScriptFactory#requiresScriptedObjectRefresh(ScriptSource) */ @Override protected boolean requiresRefresh() { return this.scriptFactory.requiresScriptedObjectRefresh(this.scriptSource); }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { return scriptSource.isModified(); }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { return scriptSource.isModified(); }