Return to Snippet

Revision: 63004
at March 29, 2013 13:16 by laurenceosx


Updated Code
// applicationContext.groovy

import org.springframework.scripting.groovy.GroovyScriptFactory
import org.springframework.scripting.support.ScriptFactoryPostProcessor
import org.codehaus.groovy.grails.commons.spring.DefaultBeanConfiguration

////////////////////////////////////////////////////////////
String findScriptResourcePath( String aScriptResourcePath) {
    // Created this to generate script path for Groovy Refreshable Spring Beans
    // for use in applicationContext.groovy because refresh feature wouldn't work
    // when Groovy script is looked up on classpath.
    // e.g. sample arg 'src/main/resources/USA.groovy' when running gradle runJetty task in IDEA.
    File f1 = new File( aScriptResourcePath ).canonicalFile;
    if (f1.exists()) {
        return f1.toURI().toString(); // most likely running in IDE
    }
    // try again
    // special case
    String token1 = 'src/main/resources';
    if ( aScriptResourcePath.startsWith( token1 ) ) {
        return findScriptResourcePath( aScriptResourcePath - token1 );
    } else {
        // note: refresh feature will probably not work in this case
        def result = this.getClass().getResource( aScriptResourcePath );
        return (result != null) ? result.toString() : null; // e.g. returns file:/C:/blah/blah/blah.groovy
    }
}
////////////////////////////////////////////////////////////

beans {
    importBeans('classpath:applicationContext.xml')
    
    String USAscriptUriPath = findScriptResourcePath('src/main/resources/USA.groovy')

    // setup refreshable usa bean
    usa(GroovyScriptFactory, USAscriptUriPath) { DefaultBeanConfiguration bean ->
        capital    = 'Washington, D.C.'
        population = 617996

        bean.beanDefinition.setAttribute( ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 5000 )
    }

    scriptFactoryPostProcessor(ScriptFactoryPostProcessor) {
        defaultRefreshCheckDelay = 5000
    }
}

Revision: 63003
at March 29, 2013 13:15 by laurenceosx


Updated Code
// applicationContext.groovy

import org.springframework.scripting.groovy.GroovyScriptFactory
import org.springframework.scripting.support.ScriptFactoryPostProcessor
import org.codehaus.groovy.grails.commons.spring.DefaultBeanConfiguration

////////////////////////////////////////////////////////////
String findScriptResourcePath( String aScriptResourcePath) {
    // Created this to generate script path for Groovy Refreshable Spring Beans
    // for use in applicationContext.groovy because refresh feature wouldn't work
    // when Groovy script is looked up on classpath.
    // e.g. sample arg 'src/main/resources/USA.groovy' when running gradle runJetty task in IDEA.
    File f1 = new File( aScriptResourcePath ).canonicalFile;
    if (f1.exists()) {
        return f1.toURI().toString(); // most likely running in IDE
    }
    // try again
    // special case
    String token1 = 'src/main/resources';
    if ( aScriptResourcePath.startsWith( token1 ) ) {
        return findResourcePath( aScriptResourcePath - token1 );
    } else {
        // note: refresh feature will probably not work in this case
        def result = this.getClass().getResource( aScriptResourcePath );
        return (result != null) ? result.toString() : null; // e.g. returns file:/C:/blah/blah/blah.groovy
    }
}
////////////////////////////////////////////////////////////

beans {
    importBeans('classpath:applicationContext.xml')
    
    String USAscriptUriPath = findScriptResourcePath('src/main/resources/USA.groovy')

    // setup refreshable usa bean
    usa(GroovyScriptFactory, USAscriptUriPath) { DefaultBeanConfiguration bean ->
        capital    = 'Washington, D.C.'
        population = 617996

        bean.beanDefinition.setAttribute( ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 5000 )
    }

    scriptFactoryPostProcessor(ScriptFactoryPostProcessor) {
        defaultRefreshCheckDelay = 5000
    }
}

Revision: 63002
at March 29, 2013 13:10 by laurenceosx


Updated Code
// applicationContext.groovy

import org.springframework.scripting.groovy.GroovyScriptFactory
import org.springframework.scripting.support.ScriptFactoryPostProcessor
import org.codehaus.groovy.grails.commons.spring.DefaultBeanConfiguration

////////////////////////////////////////////////////////////
String findScriptResourcePath( String aScriptResourcePath) {
    // Created this to generate script path for Groovy Refreshable Spring Beans
    // for use in applicationContext.groovy because refresh feature wouldn't work
    // when Groovy script is looked up on classpath.
    // e.g. sample arg 'src/main/resources/USA.groovy' when running gradle runJetty task in IDEA.
    File f1 = new File( aScriptResourcePath ).canonicalFile;
    if (f1.exists()) {
        return f1.toURI().toString(); // most likely running in IDE
    }
    // try again
    // special case
    String token1 = 'src/main/resources';
    if ( aScriptResourcePath.startsWith( token1 ) ) {
        return findResourcePath( aScriptResourcePath - token1 );
    } else {
        // note: refresh feature will probably not work in this case
        def result = this.getClass().getResource( aScriptResourcePath );
        return (result != null) ? result.toString() : null;
    }
}
////////////////////////////////////////////////////////////

beans {
    importBeans('classpath:applicationContext.xml')
    
    String USAscriptUriPath = findScriptResourcePath('src/main/resources/USA.groovy')

    // setup refreshable usa bean
    usa(GroovyScriptFactory, USAscriptUriPath) { DefaultBeanConfiguration bean ->
        capital    = 'Washington, D.C.'
        population = 617996

        bean.beanDefinition.setAttribute( ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 5000 )
    }

    scriptFactoryPostProcessor(ScriptFactoryPostProcessor) {
        defaultRefreshCheckDelay = 5000
    }
}

Revision: 63001
at March 29, 2013 13:03 by laurenceosx


Initial Code
// applicationContext.groovy

import org.springframework.scripting.groovy.GroovyScriptFactory
import org.springframework.scripting.support.ScriptFactoryPostProcessor
import org.codehaus.groovy.grails.commons.spring.DefaultBeanConfiguration

////////////////////////////////////////////////////////////
String findScriptResourcePath( String aScriptResourcePath) {
    // Created this to generate script path for Groovy Refreshable Spring Beans
    // for use in applicationContext.groovy because refresh feature wouldn't work
    // when Groovy script is looked up on classpath.
    // e.g. sample arg 'src/main/resources/USA.groovy' when running gradle runJetty task in IDEA.
    File f1 = new File( aScriptResourcePath ).canonicalFile;
    if (f1.exists()) {
        return f1.toURI().toString(); // most likely running in IDE
    }
    // try again
    // special case
    String token1 = 'src/main/resources';
    if ( aScriptResourcePath.startsWith( token1 ) ) {
        return findResourcePath( aScriptResourcePath - token1 );
    } else {
        // note: refresh feature will probably not work in this case
        def result = this.getClass().getResource( aScriptResourcePath );
        return (result != null) ? result.toString() : null;
    }
}
////////////////////////////////////////////////////////////

beans {
    importBeans('classpath:applicationContext.xml')
    
    String USAscriptPath = findScriptResourcePath('src/main/resources/USA.groovy')

    // setup refreshable usa bean
    usa(GroovyScriptFactory, USAscriptPath) { DefaultBeanConfiguration bean ->
        capital    = 'Washington, D.C.'
        population = 617996

        bean.beanDefinition.setAttribute( ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 5000 )
    }

    scriptFactoryPostProcessor(ScriptFactoryPostProcessor) {
        defaultRefreshCheckDelay = 5000
    }
}

Initial URL
http://my.safaribooksonline.com/book/programming/java/9781430224990/scripting-in-spring/refreshing_beans_from_scripts

Initial Description
Created this to generate file:/ script path for Groovy Refreshable Spring Beans
for use in applicationContext.groovy because refresh feature wouldn't work
when Groovy script is looked up on classpath.

Initial Title
Groovy Spring Refreshable Script Bean - findScriptResourcePath

Initial Tags
groovy

Initial Language
Groovy