[bs-29] Support for XML configuration in groovy apps

Mixed .groovy and .xml now supported bia spring CLI, e.g.

$ spring samples/runner.groovy samples/runner.xml

[Fixes #48059037]
This commit is contained in:
Dave Syer 2013-04-29 16:12:29 +01:00
parent bb62ca835e
commit e3d5bf2e21
5 changed files with 52 additions and 10 deletions

View File

@ -0,0 +1,10 @@
package org.test
class Runner implements CommandLineRunner {
void run(String... args) {
print "Hello World!"
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="runner" class="org.test.Runner"/>
</beans>

View File

@ -22,6 +22,7 @@ import groovy.lang.GroovyClassLoader.ClassCollector;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.ClassNode;
@ -81,6 +82,21 @@ public class GroovyCompiler {
.addCompilationCustomizers(new CompilerAutoConfigureCustomizer()); .addCompilationCustomizers(new CompilerAutoConfigureCustomizer());
} }
public Object[] sources(File[] files) throws CompilationFailedException, IOException {
List<File> compilables = new ArrayList<File>();
List<Object> others = new ArrayList<Object>();
for (File file : files) {
if (file.getName().endsWith(".groovy") || file.getName().endsWith(".java")) {
compilables.add(file);
} else {
others.add(file);
}
}
Class<?>[] compiled = compile(compilables.toArray(new File[compilables.size()]));
others.addAll(0, Arrays.asList(compiled));
return others.toArray(new Object[others.size()]);
}
/** /**
* Compile the specified Groovy source files, applying any * Compile the specified Groovy source files, applying any
* {@link CompilerAutoConfiguration}s. All classes defined in the files will be * {@link CompilerAutoConfiguration}s. All classes defined in the files will be

View File

@ -75,13 +75,13 @@ public class BootstrapRunner {
stop(); stop();
// Compile // Compile
Class<?>[] classes = this.compiler.compile(this.files); Object[] sources = this.compiler.sources(this.files);
if (classes.length == 0) { if (sources.length == 0) {
throw new RuntimeException("No classes found in '" + this.files + "'"); throw new RuntimeException("No classes found in '" + this.files + "'");
} }
// Run in new thread to ensure that the context classloader is setup // Run in new thread to ensure that the context classloader is setup
this.runThread = new RunThread(classes); this.runThread = new RunThread(sources);
this.runThread.start(); this.runThread.start();
this.runThread.join(); this.runThread.join();
@ -106,18 +106,18 @@ public class BootstrapRunner {
*/ */
private class RunThread extends Thread { private class RunThread extends Thread {
private final Class<?>[] classes; private final Object[] sources;
private Object applicationContext; private Object applicationContext;
/** /**
* Create a new {@link RunThread} instance. * Create a new {@link RunThread} instance.
* @param classes the classes to launch * @param sources the sources to launch
*/ */
public RunThread(Class<?>... classes) { public RunThread(Object... sources) {
this.classes = classes; this.sources = sources;
if (classes.length != 0) { if (sources.length != 0 && sources[0] instanceof Class) {
setContextClassLoader(classes[0].getClassLoader()); setContextClassLoader(((Class<?>) sources[0]).getClassLoader());
} }
setDaemon(true); setDaemon(true);
} }
@ -130,7 +130,7 @@ public class BootstrapRunner {
"org.springframework.bootstrap.SpringApplication"); "org.springframework.bootstrap.SpringApplication");
Method method = application.getMethod("run", Object[].class, Method method = application.getMethod("run", Object[].class,
String[].class); String[].class);
this.applicationContext = method.invoke(null, this.classes, this.applicationContext = method.invoke(null, this.sources,
BootstrapRunner.this.args); BootstrapRunner.this.args);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();

View File

@ -134,4 +134,11 @@ public class SampleIntegrationTests {
assertTrue("Wrong output: " + output, output.contains("Hello, World")); assertTrue("Wrong output: " + output, output.contains("Hello, World"));
} }
@Test
public void xmlSample() throws Exception {
start("samples/app.xml", "samples/runner.groovy");
String output = getOutput();
assertTrue("Wrong output: " + output, output.contains("Hello World"));
}
} }