Groovy

Groovy namísto shell skriptů

real 0m1.370s user 0m1.684s sys 0m0.168s #spuštění přes GroovyServ novoj@jnonb:~/Prototypes/groovyScripts/src$ time groovyclient cz/novoj/infrastructure/groovy/InfrastructureScriptInvoker.groovy PWD Current project set to: [/www/p_prj/p_fg/bm2010/] real 0m0.148s user 0m0.000s sys 0m0.004s -server option for JVM (for performance) (experimental) export JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9080 -server" #příkaz zazálohuje konfigurační soubor aktuální domény novoj@jnonb:~$ gscript swd Using Tomcat: /opt/Apache/Tomcat_6.0 Saved file: /opt/Apache/Tomcat_6.0/conf/backup/server-cps.xml #příkaz obnoví konkrétní soubor ze zálohy, #skript inteligentně vyhledává konfiguraci podle shody názvu s argumentem volání novoj@jnonb:~$ gscript rd cps Using Tomcat: /opt/Apache/Tomcat_6.

Groovy - making existing objects refreshable

@SuppressWarnings({"RawUseOfParameterizedType"}) public class DevelopmentGroovyFactory implements ScriptingFactory { private static final Log log = LogFactory.getLog(DevelopmentGroovyFactory.class); private final GroovyScriptEngine engine; private final long refreshInterval; private ResourceConnector resourceConnector; private boolean closed; public DevelopmentGroovyFactory(ClassLoader parentClassLoader, String[] urls, long refreshInterval) { try { this.engine = new GroovyScriptEngine(urls, parentClassLoader); this.engine.getGroovyClassLoader().setShouldRecompile(true); this.refreshInterval = refreshInterval * 1000L; GroovyGarbageCollectorMonitor.addGroovyScriptEngineToMonitoring(engine); } catch(IOException ex) { String msg = "Invalid source urls: " + ex.getLocalizedMessage(); log.error(msg); throw new RuntimeException(msg, ex); } } public DevelopmentGroovyFactory(ClassLoader parentClassLoader, URL[] urls, long refreshInterval) { this.

The secret of Groovy script refresh

public void testReloadGroovyClass() throws Exception { //loads class, creates instance of it and calls nonparametrized method helloWorld, all references are //then stored in custom class GroovyContext GroovyContext ctx = createInstanceAndCallMethod(engine, "com.fg.mock.MainGroovyClass", "helloWorld"); //initial result of helloWorld method is following assertEquals("Hello Universe.", ctx.getGroovyResult()); //we'll change source file contents so as method returns different value modifyGroovyFile( "com/fg/mock/MainGroovyClass.groovy", "return HELLO_UNIVERSE;", "return \"Bye bye\";" ); //existing instances will remain unchanged assertEquals("Hello Universe.