Commit 0e2300cf authored by Andy Wilkinson's avatar Andy Wilkinson

Disable check of Groovy template location by default for CLI apps

In 1.1, the Groovy template support did not check that its configured
template location exists. A check was added in 1.2, however this
breaks CLI web applications that don't have the expected templates
location.

Rather than reintroducing 1.1's behaviour by removing the check, this
commit updates the CLI to set
spring.groovy.template.check-template-location to false by default.
This allows flow-blown applications to benefit from the check, while
allowing CLI apps to behave as they did in 1.1.

Closes gh-1959
parent 11894a54
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.cli.app;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* A launcher for {@code SpringApplication}. Uses reflection to allow the launching code
* to exist in a separate ClassLoader from the application code.
*
* @author Andy Wilkinson
* @since 1.2.0
*/
public class SpringApplicationLauncher {
private static final String SPRING_APPLICATION_CLASS = "org.springframework.boot.SpringApplication";
private final ClassLoader classLoader;
/**
* Creates a new launcher that will use the given {@code classLoader} to load
* {@code SpringApplication}.
*
* @param classLoader the {@code ClassLoader} to use
*/
public SpringApplicationLauncher(ClassLoader classLoader) {
this.classLoader = classLoader;
}
/**
* Launches the application created using the given {@code sources}. The application
* is launched with the given {@code args}.
*
* @param sources The sources for the application
* @param args The args for the application
* @return The application's {@code ApplicationContext}
* @throws Exception if the launch fails
*/
public Object launch(Object[] sources, String[] args) throws Exception {
Map<String, Object> defaultProperties = new HashMap<String, Object>();
defaultProperties.put("spring.groovy.template.check-template-location", "false");
Class<?> applicationClass = this.classLoader.loadClass(SPRING_APPLICATION_CLASS);
Object application = applicationClass.getConstructor(Object[].class).newInstance(
(Object) sources);
applicationClass.getMethod("setDefaultProperties", Map.class).invoke(application,
defaultProperties);
Method method = applicationClass.getMethod("run", String[].class);
return method.invoke(application, (Object) args);
}
}
......@@ -42,6 +42,7 @@ import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.transform.ASTTransformation;
import org.springframework.boot.cli.app.SpringApplicationLauncher;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.OptionParsingCommand;
import org.springframework.boot.cli.command.jar.ResourceMatcher.MatchedResource;
......@@ -207,6 +208,7 @@ public class JarCommand extends OptionParsingCommand {
private void addCliClasses(JarWriter writer) throws IOException {
addClass(writer, PackagedSpringApplicationLauncher.class);
addClass(writer, SpringApplicationLauncher.class);
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("org/springframework/boot/groovy/**");
for (Resource resource : resources) {
......
......@@ -25,6 +25,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.springframework.boot.cli.app.SpringApplicationLauncher;
import org.springframework.boot.cli.compiler.GroovyCompiler;
import org.springframework.boot.cli.util.ResourceUtils;
......@@ -144,12 +145,8 @@ public class SpringApplicationRunner {
@Override
public void run() {
try {
// User reflection to load and call Spring
Class<?> application = getContextClassLoader().loadClass(
"org.springframework.boot.SpringApplication");
Method method = application.getMethod("run", Object[].class,
String[].class);
this.applicationContext = method.invoke(null, this.compiledSources,
this.applicationContext = new SpringApplicationLauncher(
getContextClassLoader()).launch(this.compiledSources,
SpringApplicationRunner.this.args);
}
catch (Exception ex) {
......
......@@ -16,13 +16,14 @@
package org.springframework.boot.cli.jar;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.springframework.boot.cli.app.SpringApplicationLauncher;
/**
* A launcher for a CLI application that has been compiled and packaged as a jar file.
*
......@@ -35,14 +36,10 @@ public class PackagedSpringApplicationLauncher {
public static final String START_CLASS_ENTRY = "Start-Class";
private static final String SPRING_APPLICATION_CLASS = "org.springframework.boot.SpringApplication";
private void run(String[] args) throws Exception {
URLClassLoader classLoader = (URLClassLoader) Thread.currentThread()
.getContextClassLoader();
Class<?> application = classLoader.loadClass(SPRING_APPLICATION_CLASS);
Method method = application.getMethod("run", Object[].class, String[].class);
method.invoke(null, getSources(classLoader), args);
new SpringApplicationLauncher(classLoader).launch(getSources(classLoader), args);
}
private Object[] getSources(URLClassLoader classLoader) throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment