Search in sources :

Example 1 with ScriptSource

use of org.springframework.scripting.ScriptSource in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method predictBeanType.

@Override
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }
    BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
    try {
        String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
        String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
        prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
        ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
        ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
        Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
        Class<?> scriptedType = scriptFactory.getScriptedObjectType(scriptSource);
        if (scriptedType != null) {
            return scriptedType;
        } else if (!ObjectUtils.isEmpty(interfaces)) {
            return (interfaces.length == 1 ? interfaces[0] : createCompositeInterface(interfaces));
        } else {
            if (bd.isSingleton()) {
                Object bean = this.scriptBeanFactory.getBean(scriptedObjectBeanName);
                if (bean != null) {
                    return bean.getClass();
                }
            }
        }
    } catch (Exception ex) {
        if (ex instanceof BeanCreationException && ((BeanCreationException) ex).getMostSpecificCause() instanceof BeanCurrentlyInCreationException) {
            if (logger.isTraceEnabled()) {
                logger.trace("Could not determine scripted object type for bean '" + beanName + "': " + ex.getMessage());
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not determine scripted object type for bean '" + beanName + "'", ex);
            }
        }
    }
    return null;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ScriptSource(org.springframework.scripting.ScriptSource) ScriptFactory(org.springframework.scripting.ScriptFactory) BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 2 with ScriptSource

use of org.springframework.scripting.ScriptSource in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method postProcessBeforeInstantiation.

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }
    BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
    String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
    String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
    prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
    ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
    ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
    boolean isFactoryBean = false;
    try {
        Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
        // Returned type may be null if the factory is unable to determine the type.
        if (scriptedObjectType != null) {
            isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
        }
    } catch (Exception ex) {
        throw new BeanCreationException(beanName, "Could not determine scripted object type for " + scriptFactory, ex);
    }
    long refreshCheckDelay = resolveRefreshCheckDelay(bd);
    if (refreshCheckDelay >= 0) {
        Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
        RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory, scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
        boolean proxyTargetClass = resolveProxyTargetClass(bd);
        String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
        if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
            throw new BeanDefinitionValidationException("Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" + language + "'");
        }
        ts.setRefreshCheckDelay(refreshCheckDelay);
        return createRefreshableProxy(ts, interfaces, proxyTargetClass);
    }
    if (isFactoryBean) {
        scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
    }
    return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinitionValidationException(org.springframework.beans.factory.support.BeanDefinitionValidationException) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) FactoryBean(org.springframework.beans.factory.FactoryBean) ScriptSource(org.springframework.scripting.ScriptSource) ScriptFactory(org.springframework.scripting.ScriptFactory)

Example 3 with ScriptSource

use of org.springframework.scripting.ScriptSource in project spring-framework by spring-projects.

the class BshScriptFactoryTests method scriptThatCompilesButIsJustPlainBad.

@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) {
    }
}
Also used : TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) ScriptSource(org.springframework.scripting.ScriptSource) Test(org.junit.Test)

Example 4 with ScriptSource

use of org.springframework.scripting.ScriptSource in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method prepareScriptBeans.

/**
	 * 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);
        }
    }
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ScriptSource(org.springframework.scripting.ScriptSource) ScriptFactory(org.springframework.scripting.ScriptFactory)

Example 5 with ScriptSource

use of org.springframework.scripting.ScriptSource in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessor method getScriptSource.

/**
	 * 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;
    }
}
Also used : ScriptSource(org.springframework.scripting.ScriptSource)

Aggregations

ScriptSource (org.springframework.scripting.ScriptSource)8 Test (org.junit.Test)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 ScriptFactory (org.springframework.scripting.ScriptFactory)3 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanDefinitionValidationException (org.springframework.beans.factory.support.BeanDefinitionValidationException)2 GroovyObject (groovy.lang.GroovyObject)1 FactoryBean (org.springframework.beans.factory.FactoryBean)1 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)1 Messenger (org.springframework.scripting.Messenger)1 ScriptCompilationException (org.springframework.scripting.ScriptCompilationException)1 TestBeanAwareMessenger (org.springframework.scripting.TestBeanAwareMessenger)1