org.springframework.scripting.ScriptSource Java Examples

The following examples show how to use org.springframework.scripting.ScriptSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardScriptFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
protected ScriptEngine retrieveScriptEngine(ScriptSource scriptSource) {
	ScriptEngineManager scriptEngineManager = new ScriptEngineManager(this.beanClassLoader);

	if (this.scriptEngineName != null) {
		return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.scriptEngineName);
	}

	if (scriptSource instanceof ResourceScriptSource) {
		String filename = ((ResourceScriptSource) scriptSource).getResource().getFilename();
		if (filename != null) {
			String extension = StringUtils.getFilenameExtension(filename);
			if (extension != null) {
				ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
				if (engine != null) {
					return engine;
				}
			}
		}
	}

	return null;
}
 
Example #2
Source File: BshScriptFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
		}
	}
}
 
Example #3
Source File: BshScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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) {
	}
}
 
Example #4
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
	}
}
 
Example #5
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
	}
}
 
Example #6
Source File: BshScriptFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
		}
	}
}
 
Example #7
Source File: BshScriptFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	try {
		synchronized (this.scriptClassMonitor) {
			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) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #8
Source File: BshScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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) {
	}
}
 
Example #9
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptThatCompilesButIsJustPlainBad() throws Exception {
	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) {
	}
}
 
Example #10
Source File: BshScriptFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
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);
		}
	}
}
 
Example #11
Source File: StandardScriptFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected ScriptEngine retrieveScriptEngine(ScriptSource scriptSource) {
	ScriptEngineManager scriptEngineManager = new ScriptEngineManager(this.beanClassLoader);

	if (this.scriptEngineName != null) {
		return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.scriptEngineName);
	}

	if (scriptSource instanceof ResourceScriptSource) {
		String filename = ((ResourceScriptSource) scriptSource).getResource().getFilename();
		if (filename != null) {
			String extension = StringUtils.getFilenameExtension(filename);
			if (extension != null) {
				ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
				if (engine != null) {
					return engine;
				}
			}
		}
	}

	return null;
}
 
Example #12
Source File: StandardScriptFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
protected ScriptEngine retrieveScriptEngine(ScriptSource scriptSource) {
	ScriptEngineManager scriptEngineManager = new ScriptEngineManager(this.beanClassLoader);

	if (this.scriptEngineName != null) {
		return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.scriptEngineName);
	}

	if (scriptSource instanceof ResourceScriptSource) {
		String filename = ((ResourceScriptSource) scriptSource).getResource().getFilename();
		if (filename != null) {
			String extension = StringUtils.getFilenameExtension(filename);
			if (extension != null) {
				ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
				if (engine != null) {
					return engine;
				}
			}
		}
	}

	return null;
}
 
Example #13
Source File: StandardScriptFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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);
	}
}
 
Example #14
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	final String badScript = "class Foo { protected 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 oublic no-arg ctor in scripted class).");
	}
	catch (ScriptCompilationException expected) {
		assertTrue(expected.contains(IllegalAccessException.class));
	}
}
 
Example #15
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	final 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(InstantiationException.class));
	}
}
 
Example #16
Source File: StandardScriptFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected ScriptEngine retrieveScriptEngine(ScriptSource scriptSource) {
	ScriptEngineManager scriptEngineManager = new ScriptEngineManager(this.beanClassLoader);

	if (this.scriptEngineName != null) {
		return StandardScriptUtils.retrieveEngineByName(scriptEngineManager, this.scriptEngineName);
	}

	if (scriptSource instanceof ResourceScriptSource) {
		String filename = ((ResourceScriptSource) scriptSource).getResource().getFilename();
		if (filename != null) {
			String extension = StringUtils.getFilenameExtension(filename);
			if (extension != null) {
				ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
				if (engine != null) {
					return engine;
				}
			}
		}
	}

	return null;
}
 
Example #17
Source File: ScriptFactoryPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Prepare the script beans in the internal BeanFactory that this
 * post-processor uses. Each original bean definition will be split
 * into a ScriptFactory definition and a scripted object definition.
 * @param bd the original bean definition in the main BeanFactory
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptedObjectBeanName the name of the internal scripted object bean
 */
protected void prepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName, String scriptedObjectBeanName) {
	// Avoid recreation of the script bean definition in case of a prototype.
	synchronized (this.scriptBeanFactory) {
		if (!this.scriptBeanFactory.containsBeanDefinition(scriptedObjectBeanName)) {

			this.scriptBeanFactory.registerBeanDefinition(
					scriptFactoryBeanName, createScriptFactoryBeanDefinition(bd));
			ScriptFactory scriptFactory =
					this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
			ScriptSource scriptSource =
					getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
			Class<?>[] interfaces = scriptFactory.getScriptInterfaces();

			Class<?>[] scriptedInterfaces = interfaces;
			if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
				Class<?> configInterface = createConfigInterface(bd, interfaces);
				scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
			}

			BeanDefinition objectBd = createScriptedObjectBeanDefinition(
					bd, scriptFactoryBeanName, scriptSource, scriptedInterfaces);
			long refreshCheckDelay = resolveRefreshCheckDelay(bd);
			if (refreshCheckDelay >= 0) {
				objectBd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
			}

			this.scriptBeanFactory.registerBeanDefinition(scriptedObjectBeanName, objectBd);
		}
	}
}
 
Example #18
Source File: RefreshableScriptTargetSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new RefreshableScriptTargetSource.
 * @param beanFactory the BeanFactory to fetch the scripted bean from
 * @param beanName the name of the target bean
 * @param scriptFactory the ScriptFactory to delegate to for determining
 * whether a refresh is required
 * @param scriptSource the ScriptSource for the script definition
 * @param isFactoryBean whether the target script defines a FactoryBean
 */
public RefreshableScriptTargetSource(BeanFactory beanFactory, String beanName,
		ScriptFactory scriptFactory, ScriptSource scriptSource, boolean isFactoryBean) {

	super(beanFactory, beanName);
	Assert.notNull(scriptFactory, "ScriptFactory must not be null");
	Assert.notNull(scriptSource, "ScriptSource must not be null");
	this.scriptFactory = scriptFactory;
	this.scriptSource = scriptSource;
	this.isFactoryBean = isFactoryBean;
}
 
Example #19
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert the given script source locator to a ScriptSource instance.
 * <p>By default, supported locators are Spring resource locations
 * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh")
 * and inline scripts ("inline:myScriptText...").
 * @param beanName the name of the scripted bean
 * @param scriptSourceLocator the script source locator
 * @param resourceLoader the ResourceLoader to use (if necessary)
 * @return the ScriptSource instance
 */
protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator,
		ResourceLoader resourceLoader) {

	if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) {
		return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName);
	}
	else {
		return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator));
	}
}
 
Example #20
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	String badScript = "class Foo { protected Foo() {} \n String toString() { 'X' }}";
	given(script.getScriptAsString()).willReturn(badScript);
	given(script.suggestedClassName()).willReturn("someName");
	GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript);
	assertEquals("X", factory.getScriptedObject(script).toString());
}
 
Example #21
Source File: StandardScriptEvaluator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the JSR-223 ScriptEngine to use for the given script.
 * @param script the script to evaluate
 * @return the ScriptEngine (never {@code null})
 */
protected ScriptEngine getScriptEngine(ScriptSource script) {
	if (this.scriptEngineManager == null) {
		this.scriptEngineManager = new ScriptEngineManager();
	}

	if (StringUtils.hasText(this.engineName)) {
		return StandardScriptUtils.retrieveEngineByName(this.scriptEngineManager, this.engineName);
	}
	else if (script instanceof ResourceScriptSource) {
		Resource resource = ((ResourceScriptSource) script).getResource();
		String extension = StringUtils.getFilenameExtension(resource.getFilename());
		if (extension == null) {
			throw new IllegalStateException(
					"No script language defined, and no file extension defined for resource: " + resource);
		}
		ScriptEngine engine = this.scriptEngineManager.getEngineByExtension(extension);
		if (engine == null) {
			throw new IllegalStateException("No matching engine found for file extension '" + extension + "'");
		}
		return engine;
	}
	else {
		throw new IllegalStateException(
				"No script language defined, and no resource associated with script: " + script);
	}
}
 
Example #22
Source File: RefreshableScriptTargetSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new RefreshableScriptTargetSource.
 * @param beanFactory the BeanFactory to fetch the scripted bean from
 * @param beanName the name of the target bean
 * @param scriptFactory the ScriptFactory to delegate to for determining
 * whether a refresh is required
 * @param scriptSource the ScriptSource for the script definition
 * @param isFactoryBean whether the target script defines a FactoryBean
 */
public RefreshableScriptTargetSource(BeanFactory beanFactory, String beanName,
		ScriptFactory scriptFactory, ScriptSource scriptSource, boolean isFactoryBean) {

	super(beanFactory, beanName);
	Assert.notNull(scriptFactory, "ScriptFactory must not be null");
	Assert.notNull(scriptSource, "ScriptSource must not be null");
	this.scriptFactory = scriptFactory;
	this.scriptSource = scriptSource;
	this.isFactoryBean = isFactoryBean;
}
 
Example #23
Source File: GroovyScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (this.scriptClass == null || scriptSource.isModified()) {
				// New script content...
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = getGroovyClassLoader().parseClass(
						scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

				if (Script.class.isAssignableFrom(this.scriptClass)) {
					// A Groovy script, probably creating an instance: let's execute it.
					Object result = executeScript(scriptSource, this.scriptClass);
					this.scriptResultClass = (result != null ? result.getClass() : null);
					this.cachedResult = new CachedResultHolder(result);
				}
				else {
					this.scriptResultClass = this.scriptClass;
				}
			}
			return this.scriptResultClass;
		}
		catch (CompilationFailedException ex) {
			this.scriptClass = null;
			this.scriptResultClass = null;
			this.cachedResult = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #24
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Prepare the script beans in the internal BeanFactory that this
 * post-processor uses. Each original bean definition will be split
 * into a ScriptFactory definition and a scripted object definition.
 * @param bd the original bean definition in the main BeanFactory
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptedObjectBeanName the name of the internal scripted object bean
 */
protected void prepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName, String scriptedObjectBeanName) {
	// Avoid recreation of the script bean definition in case of a prototype.
	synchronized (this.scriptBeanFactory) {
		if (!this.scriptBeanFactory.containsBeanDefinition(scriptedObjectBeanName)) {

			this.scriptBeanFactory.registerBeanDefinition(
					scriptFactoryBeanName, createScriptFactoryBeanDefinition(bd));
			ScriptFactory scriptFactory =
					this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
			ScriptSource scriptSource =
					getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
			Class<?>[] interfaces = scriptFactory.getScriptInterfaces();

			Class<?>[] scriptedInterfaces = interfaces;
			if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
				Class<?> configInterface = createConfigInterface(bd, interfaces);
				scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
			}

			BeanDefinition objectBd = createScriptedObjectBeanDefinition(
					bd, scriptFactoryBeanName, scriptSource, scriptedInterfaces);
			long refreshCheckDelay = resolveRefreshCheckDelay(bd);
			if (refreshCheckDelay >= 0) {
				objectBd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
			}

			this.scriptBeanFactory.registerBeanDefinition(scriptedObjectBeanName, objectBd);
		}
	}
}
 
Example #25
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a ScriptSource for the given bean, lazily creating it
 * if not cached already.
 * @param beanName the name of the scripted bean
 * @param scriptSourceLocator the script source locator associated with the bean
 * @return the corresponding ScriptSource instance
 * @see #convertToScriptSource
 */
protected ScriptSource getScriptSource(String beanName, String scriptSourceLocator) {
	synchronized (this.scriptSourceCache) {
		ScriptSource scriptSource = this.scriptSourceCache.get(beanName);
		if (scriptSource == null) {
			scriptSource = convertToScriptSource(beanName, scriptSourceLocator, this.resourceLoader);
			this.scriptSourceCache.put(beanName, scriptSource);
		}
		return scriptSource;
	}
}
 
Example #26
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a bean definition for the scripted object, based on the given script
 * definition, extracting the definition data that is relevant for the scripted
 * object (that is, everything but bean class and constructor arguments).
 * @param bd the full script bean definition
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptSource the ScriptSource for the scripted bean
 * @param interfaces the interfaces that the scripted bean is supposed to implement
 * @return the extracted ScriptFactory bean definition
 * @see org.springframework.scripting.ScriptFactory#getScriptedObject
 */
protected BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, String scriptFactoryBeanName,
		ScriptSource scriptSource, Class<?>[] interfaces) {

	GenericBeanDefinition objectBd = new GenericBeanDefinition(bd);
	objectBd.setFactoryBeanName(scriptFactoryBeanName);
	objectBd.setFactoryMethodName("getScriptedObject");
	objectBd.getConstructorArgumentValues().clear();
	objectBd.getConstructorArgumentValues().addIndexedArgumentValue(0, scriptSource);
	objectBd.getConstructorArgumentValues().addIndexedArgumentValue(1, interfaces);
	return objectBd;
}
 
Example #27
Source File: StandardScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object evaluateScript(ScriptSource scriptSource) {
	try {
		if (this.scriptEngine == null) {
			this.scriptEngine = retrieveScriptEngine(scriptSource);
			if (this.scriptEngine == null) {
				throw new IllegalStateException("Could not determine script engine for " + scriptSource);
			}
		}
		return this.scriptEngine.eval(scriptSource.getScriptAsString());
	}
	catch (Exception ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #28
Source File: GroovyScriptFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (this.scriptClass == null || scriptSource.isModified()) {
				// New script content...
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = getGroovyClassLoader().parseClass(
						scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

				if (Script.class.isAssignableFrom(this.scriptClass)) {
					// A Groovy script, probably creating an instance: let's execute it.
					Object result = executeScript(scriptSource, this.scriptClass);
					this.scriptResultClass = (result != null ? result.getClass() : null);
					this.cachedResult = new CachedResultHolder(result);
				}
				else {
					this.scriptResultClass = this.scriptClass;
				}
			}
			return this.scriptResultClass;
		}
		catch (CompilationFailedException ex) {
			this.scriptClass = null;
			this.scriptResultClass = null;
			this.cachedResult = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #29
Source File: StandardScriptFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	return null;
}
 
Example #30
Source File: StandardScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object adaptToInterfaces(Object script, ScriptSource scriptSource, Class<?>... actualInterfaces) {
	Class<?> adaptedIfc;
	if (actualInterfaces.length == 1) {
		adaptedIfc = actualInterfaces[0];
	}
	else {
		adaptedIfc = ClassUtils.createCompositeInterface(actualInterfaces, this.beanClassLoader);
	}

	if (adaptedIfc != null) {
		if (!(this.scriptEngine instanceof Invocable)) {
			throw new ScriptCompilationException(scriptSource,
					"ScriptEngine must implement Invocable in order to adapt it to an interface: " +
							this.scriptEngine);
		}
		Invocable invocable = (Invocable) this.scriptEngine;
		if (script != null) {
			script = invocable.getInterface(script, adaptedIfc);
		}
		if (script == null) {
			script = invocable.getInterface(adaptedIfc);
			if (script == null) {
				throw new ScriptCompilationException(scriptSource,
						"Could not adapt script to interface [" + adaptedIfc.getName() + "]");
			}
		}
	}

	return script;
}