Example usage for org.springframework.scripting ScriptFactory getScriptedObjectType

List of usage examples for org.springframework.scripting ScriptFactory getScriptedObjectType

Introduction

In this page you can find the example usage for org.springframework.scripting ScriptFactory getScriptedObjectType.

Prototype

@Nullable
Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException;

Source Link

Document

Determine the type of the scripted Java object.

Usage

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }/*from   w w  w.  j a  v  a  2s .c o  m*/

    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);
    String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);//add by tylerchen
    if (refreshCheckDelay >= 0 || "groovy".equals(language)) {
        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);//remove by tylerchen
        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);//remove by tylerchen
        ts.setRefreshCheckDelay(-1);//add by tylerchen
        return createRefreshableProxy(ts, interfaces, proxyTargetClass);
    }

    if (isFactoryBean) {
        scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
    }
    return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

@Override
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }//from   w ww  .j av  a2s  .  c o m

    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;
}

From source file:org.springframework.scripting.support.ScriptFactoryPostProcessor.java

@Override
@Nullable// ww w . j a  v a  2s. co m
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }

    Assert.state(this.beanFactory != null, "No BeanFactory set");
    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()) {
                return this.scriptBeanFactory.getBean(scriptedObjectBeanName).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;
}

From source file:org.springframework.scripting.support.ScriptFactoryPostProcessor.java

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    // We only apply special treatment to ScriptFactory implementations here.
    if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
        return null;
    }// w w w .j  av a 2  s . co m

    Assert.state(this.beanFactory != null, "No BeanFactory set");
    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);
}