Turned on checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-01 15:48:32 +01:00
parent 94e9b8f2f8
commit e4b08a083c
268 changed files with 5114 additions and 3993 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2012-2019 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.
@@ -45,13 +45,23 @@ import org.springframework.util.StringUtils;
public class ApplicationBootstrap {
private static Log logger = LogFactory.getLog(ApplicationBootstrap.class);
private ApplicationRunner runner;
private URLClassLoader classLoader;
private static boolean isolated(String[] args) {
for (String arg : args) {
if (arg.equals("--function.runner.isolated=false")) {
return false;
}
}
return true;
}
/**
* Run the provided main class as a Spring Boot application with the provided command
* line arguments.
*
* @param mainClass the main class
* @param args the command line arguments
*/
@@ -104,15 +114,6 @@ public class ApplicationBootstrap {
return this.runner;
}
private static boolean isolated(String[] args) {
for (String arg : args) {
if (arg.equals("--function.runner.isolated=false")) {
return false;
}
}
return true;
}
private URLClassLoader createClassLoader(Class<?> mainClass) {
URL[] urls = findClassPath(mainClass);
if (urls.length == 1) {
@@ -141,7 +142,6 @@ public class ApplicationBootstrap {
return new URLClassLoader(child.toArray(new URL[0]), base);
}
private URL[] findClassPath(Class<?> mainClass) {
ClassLoader base = mainClass.getClassLoader();
if (!(base instanceof URLClassLoader)) {
@@ -200,4 +200,5 @@ public class ApplicationBootstrap {
}
return null;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2012-2019 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.
@@ -52,12 +52,12 @@ public class ApplicationRunner {
private final String source;
private StandardEvaluationContext app;
private final SpelParserConfiguration config;
private final StandardTypeLocator typeLocator;
private StandardEvaluationContext app;
public ApplicationRunner(ClassLoader classLoader, String source) {
this.classLoader = classLoader;
this.source = source;
@@ -70,7 +70,8 @@ public class ApplicationRunner {
try {
ClassUtils.overrideThreadContextClassLoader(this.classLoader);
Class<?> cls = this.classLoader.loadClass(ContextRunner.class.getName());
this.app = new StandardEvaluationContext(cls.getDeclaredConstructor().newInstance());
this.app = new StandardEvaluationContext(
cls.getDeclaredConstructor().newInstance());
this.app.setTypeLocator(new StandardTypeLocator(this.classLoader));
runContext(this.source, defaultProperties(UUID.randomUUID().toString()),
args);
@@ -148,8 +149,8 @@ public class ApplicationRunner {
}
/**
* List the bean names in the application context for a given type (by its fully qualified name).
*
* List the bean names in the application context for a given type (by its fully
* qualified name).
* @param type the name of the type (Class)
* @return the bean names of that type
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -32,17 +32,31 @@ import org.springframework.util.ReflectionUtils;
/**
* Utility class for starting a Spring Boot application in a separate thread. Best used
* from an isolated class loader, e.g. through {@link ApplicationRunner}.
*
*
* @author Dave Syer
*/
public class ContextRunner {
private ConfigurableApplicationContext context;
private Thread runThread;
private volatile boolean running = false;
private Throwable error;
private long timeout = 120000;
private static boolean isFunctional(StandardEnvironment environment) {
if (!ClassUtils.isPresent(
"org.springframework.cloud.function.context.FunctionalSpringApplication",
null)) {
return false;
}
return environment.resolvePlaceholders("${spring.functional.enabled:true}")
.equals("true");
}
public void run(final String source, final Map<String, Object> properties,
final String... args) {
// Run in new thread to ensure that the context classloader is setup
@@ -59,21 +73,21 @@ public class ContextRunner {
environment.getPropertySources().addFirst(
new SimpleCommandLinePropertySource("args", args));
}
running = true;
ContextRunner.this.running = true;
Class<?> sourceClass = ClassUtils.resolveClassName(source, null);
SpringApplication builder = builder(sourceClass, environment);
context = builder.run(args);
ContextRunner.this.context = builder.run(args);
}
catch (Throwable ex) {
error = ex;
ContextRunner.this.error = ex;
}
}
});
this.runThread.start();
try {
this.runThread.join(timeout);
this.running = context != null && context.isRunning();
this.runThread.join(this.timeout);
this.running = this.context != null && this.context.isRunning();
}
catch (InterruptedException e) {
this.running = false;
@@ -114,7 +128,7 @@ public class ContextRunner {
}
public boolean isRunning() {
return running;
return this.running;
}
public Throwable getError() {
@@ -134,21 +148,12 @@ public class ContextRunner {
return application;
}
private static boolean isFunctional(StandardEnvironment environment) {
if (!ClassUtils.isPresent(
"org.springframework.cloud.function.context.FunctionalSpringApplication",
null)) {
return false;
}
return environment.resolvePlaceholders("${spring.functional.enabled:true}")
.equals("true");
}
private static class FunctionalSpringApplicationCreator {
public static SpringApplication create(Class<?> type) {
return new FunctionalSpringApplication(type);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.lang.annotation.Documented;
@@ -26,7 +27,6 @@ import org.springframework.context.annotation.Import;
/**
* Annotation to be used on a Spring Boot application if it wants to deploy a jar file
* containing a function definition.
*
* @author Dave Syer
*
*/
@@ -36,4 +36,4 @@ import org.springframework.context.annotation.Import;
@Import(FunctionDeployerConfiguration.class)
public @interface EnableFunctionDeployer {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -20,6 +20,7 @@ import java.io.IOException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @checkstyle:off
/**
* @author Mark Fisher
* @author Dave Syer
@@ -33,3 +34,4 @@ public class FunctionApplication {
}
}
// @checkstyle:on

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2012-2019 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.
@@ -42,6 +42,7 @@ import javax.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -112,24 +113,25 @@ class FunctionCreatorConfiguration {
*/
@PostConstruct
public void init() {
URL[] urls = Arrays.stream(properties.getLocation())
.flatMap(toResourceURL(delegatingResourceLoader)).toArray(URL[]::new);
URL[] roots = Arrays.stream(properties.getLocation()).map(this::toUrl)
URL[] urls = Arrays.stream(this.properties.getLocation())
.flatMap(toResourceURL(this.delegatingResourceLoader))
.toArray(URL[]::new);
URL[] roots = Arrays.stream(this.properties.getLocation()).map(this::toUrl)
.toArray(URL[]::new);
try {
logger.info(
"Locating function from " + Arrays.asList(properties.getLocation()));
logger.info("Locating function from "
+ Arrays.asList(this.properties.getLocation()));
this.creator = new BeanCreator(roots, urls);
this.creator.run(properties.getMain());
this.creator.run(this.properties.getMain());
Arrays.stream(functionNames()).map(this.creator::create).sequential()
.forEach(this.creator::register);
if (properties.getName().contains("|")) {
if (this.properties.getName().contains("|")) {
// A composite function has to be explicitly registered before it is
// looked up because we are using the SingleEntryFunctionRegistry
this.registry.lookup(Consumer.class, properties.getName());
this.registry.lookup(Function.class, properties.getName());
this.registry.lookup(Supplier.class, properties.getName());
this.registry.lookup(Consumer.class, this.properties.getName());
this.registry.lookup(Function.class, this.properties.getName());
this.registry.lookup(Supplier.class, this.properties.getName());
}
}
catch (Exception e) {
@@ -150,8 +152,8 @@ class FunctionCreatorConfiguration {
}
private String[] functionNames() {
if (properties.getBean() != null && properties.getBean().length > 0) {
return properties.getBean();
if (this.properties.getBean() != null && this.properties.getBean().length > 0) {
return this.properties.getBean();
}
return this.creator.getFunctionNames();
}
@@ -207,9 +209,78 @@ class FunctionCreatorConfiguration {
return urls.toArray(new URL[urls.size()]);
}
private static final class BeanCreatorClassLoader extends URLClassLoader {
private BeanCreatorClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
@Override
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
try {
if (name.startsWith("javax.") && JavaVersion.getJavaVersion()
.isEqualOrNewerThan(JavaVersion.NINE)) {
return getClass().getClassLoader().loadClass(name);
}
return super.loadClass(name, resolve);
}
catch (ClassNotFoundException e) {
if (name.contains(ContextRunner.class.getName())
|| name.contains(PostConstruct.class.getName())) {
// Special case for the ContextRunner. We can re-use the bytes for it,
// and the function jar doesn't have to include them since it is only
// used here.
byte[] bytes;
try {
bytes = StreamUtils.copyToByteArray(
getClass().getClassLoader().getResourceAsStream(
ClassUtils.convertClassNameToResourcePath(name)
+ ".class"));
return defineClass(name, bytes, 0, bytes.length);
}
catch (IOException ex) {
throw new ClassNotFoundException(
"Cannot find runner class: " + name, ex);
}
}
throw e;
}
}
}
@Configuration
protected static class SingleEntryConfiguration implements BeanPostProcessor {
@Autowired
private Environment env;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof FunctionRegistry) {
String name = FunctionProperties
.functionName(this.env.getProperty("function.bean", ""));
if (name.contains("|")) {
// A single composite function with an empty name
bean = new SingleEntryFunctionRegistry((FunctionRegistry) bean, name);
}
}
return bean;
}
}
private class ComputeLauncher extends JarLauncher {
public ComputeLauncher(Archive archive) {
ComputeLauncher(Archive archive) {
super(archive);
}
@@ -220,9 +291,10 @@ class FunctionCreatorConfiguration {
if (manifest != null) {
String functionClass = manifest.getMainAttributes()
.getValue("Function-Class");
if (StringUtils.hasText(functionClass)
&& ObjectUtils.isEmpty(properties.getBean())) {
properties.setBean(new String[] { functionClass });
if (StringUtils.hasText(functionClass) && ObjectUtils.isEmpty(
FunctionCreatorConfiguration.this.properties.getBean())) {
FunctionCreatorConfiguration.this.properties
.setBean(new String[] { functionClass });
}
mainClass = manifest.getMainAttributes().getValue("Start-Class");
if (mainClass == null
@@ -287,6 +359,7 @@ class FunctionCreatorConfiguration {
}
return null;
}
}
/**
@@ -303,8 +376,9 @@ class FunctionCreatorConfiguration {
private String defaultMain;
public BeanCreator(URL[] roots, URL[] urls) {
functionClassLoader = new BeanCreatorClassLoader(expand(urls), getParent());
BeanCreator(URL[] roots, URL[] urls) {
FunctionCreatorConfiguration.this.functionClassLoader = new BeanCreatorClassLoader(
expand(urls), getParent());
this.defaultMain = findMain(roots);
}
@@ -329,8 +403,7 @@ class FunctionCreatorConfiguration {
File file = ResourceUtils.getFile(url);
if (file.exists()) {
Archive archive = file.getName().endsWith(".jar")
? new JarFileArchive(file)
: new ExplodedArchive(file);
? new JarFileArchive(file) : new ExplodedArchive(file);
String main = new ComputeLauncher(archive).getMainClass();
if (main != null) {
return main;
@@ -388,13 +461,14 @@ class FunctionCreatorConfiguration {
return;
}
if (ClassUtils.isPresent(SpringApplication.class.getName(),
functionClassLoader)) {
FunctionCreatorConfiguration.this.functionClassLoader)) {
logger.info("SpringApplication available. Bootstrapping: " + main);
ClassLoader contextClassLoader = ClassUtils
.overrideThreadContextClassLoader(functionClassLoader);
.overrideThreadContextClassLoader(
FunctionCreatorConfiguration.this.functionClassLoader);
try {
ApplicationRunner runner = new ApplicationRunner(functionClassLoader,
main);
ApplicationRunner runner = new ApplicationRunner(
FunctionCreatorConfiguration.this.functionClassLoader, main);
// TODO: make the runtime properties configurable
runner.run("--spring.main.webEnvironment=false",
"--spring.cloud.stream.enabled=false",
@@ -416,8 +490,8 @@ class FunctionCreatorConfiguration {
public String[] getFunctionNames() {
Set<String> list = new LinkedHashSet<>();
ClassLoader contextClassLoader = ClassUtils
.overrideThreadContextClassLoader(functionClassLoader);
ClassLoader contextClassLoader = ClassUtils.overrideThreadContextClassLoader(
FunctionCreatorConfiguration.this.functionClassLoader);
try {
if (this.runner.containsBean(FunctionCatalog.class.getName())) {
Object catalog = this.runner.getBean(FunctionCatalog.class.getName());
@@ -447,9 +521,10 @@ class FunctionCreatorConfiguration {
}
public Object create(String type) {
ClassLoader contextClassLoader = ClassUtils
.overrideThreadContextClassLoader(functionClassLoader);
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
ClassLoader contextClassLoader = ClassUtils.overrideThreadContextClassLoader(
FunctionCreatorConfiguration.this.functionClassLoader);
AutowireCapableBeanFactory factory = FunctionCreatorConfiguration.this.context
.getAutowireCapableBeanFactory();
try {
Object result = null;
if (this.runner != null) {
@@ -483,9 +558,10 @@ class FunctionCreatorConfiguration {
}
if (result == null) {
logger.info("No bean found. Instantiating: " + type);
if (ClassUtils.isPresent(type, functionClassLoader)) {
result = factory.createBean(
ClassUtils.resolveClassName(type, functionClassLoader));
if (ClassUtils.isPresent(type,
FunctionCreatorConfiguration.this.functionClassLoader)) {
result = factory.createBean(ClassUtils.resolveClassName(type,
FunctionCreatorConfiguration.this.functionClassLoader));
}
}
if (result != null) {
@@ -503,7 +579,8 @@ class FunctionCreatorConfiguration {
return;
}
FunctionRegistration<Object> registration = new FunctionRegistration<Object>(
bean, FunctionProperties.functionName(counter.getAndIncrement()));
bean,
FunctionProperties.functionName(this.counter.getAndIncrement()));
if (this.runner != null) {
if (this.runner.containsBean(FunctionInspector.class.getName())) {
Object inspector = this.runner
@@ -533,7 +610,7 @@ class FunctionCreatorConfiguration {
registration.type(FunctionType.of(bean.getClass()).getType());
}
registration.target(bean);
registry.register(registration);
FunctionCreatorConfiguration.this.registry.register(registration);
}
private Class<?> findType(String method, Object inspector, Object bean) {
@@ -549,69 +626,4 @@ class FunctionCreatorConfiguration {
}
private static final class BeanCreatorClassLoader extends URLClassLoader {
private BeanCreatorClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
@Override
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
try {
if (name.startsWith("javax.") && JavaVersion.getJavaVersion().isEqualOrNewerThan(JavaVersion.NINE)) {
return getClass().getClassLoader().loadClass(name);
}
return super.loadClass(name, resolve);
}
catch (ClassNotFoundException e) {
if (name.contains(ContextRunner.class.getName())
|| name.contains(PostConstruct.class.getName())) {
// Special case for the ContextRunner. We can re-use the bytes for it,
// and the function jar doesn't have to include them since it is only
// used here.
byte[] bytes;
try {
bytes = StreamUtils.copyToByteArray(
getClass().getClassLoader().getResourceAsStream(
ClassUtils.convertClassNameToResourcePath(name)
+ ".class"));
return defineClass(name, bytes, 0, bytes.length);
}
catch (IOException ex) {
throw new ClassNotFoundException(
"Cannot find runner class: " + name, ex);
}
}
throw e;
}
}
}
@Configuration
protected static class SingleEntryConfiguration implements BeanPostProcessor {
@Autowired
private Environment env;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof FunctionRegistry) {
String name = FunctionProperties
.functionName(env.getProperty("function.bean", ""));
if (name.contains("|")) {
// A single composite function with an empty name
bean = new SingleEntryFunctionRegistry((FunctionRegistry) bean, name);
}
}
return bean;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.util.HashMap;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -42,38 +42,10 @@ public class FunctionProperties {
private String[] bean = new String[0];
/**
* Optional main class from which to build a Spring application context
* Optional main class from which to build a Spring application context.
*/
private String main;
public String getName() {
return functionName(StringUtils.arrayToDelimitedString(bean, ","));
}
public String[] getBean() {
return bean;
}
public void setBean(String[] bean) {
this.bean = bean;
}
public String[] getLocation() {
return location;
}
public void setLocation(String[] location) {
this.location = location;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public static String functionName(String name) {
if (!name.contains(",")) {
return "function0";
@@ -89,11 +61,40 @@ public class FunctionProperties {
return "function" + value;
}
public String getName() {
return functionName(StringUtils.arrayToDelimitedString(this.bean, ","));
}
public String[] getBean() {
return this.bean;
}
public void setBean(String[] bean) {
this.bean = bean;
}
public String[] getLocation() {
return this.location;
}
public void setLocation(String[] location) {
this.location = location;
}
public String getMain() {
return this.main;
}
public void setMain(String main) {
this.main = main;
}
@PostConstruct
public void init() {
if (location.length == 0) {
if (this.location.length == 0) {
throw new IllegalStateException(
"No archive location provided, please configure function.location as a jar or directory.");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.deployer;
import java.util.Collections;
@@ -40,12 +41,13 @@ public class SingleEntryFunctionRegistry implements FunctionRegistry {
@Override
public <T> T lookup(Class<?> type, String name) {
if (StringUtils.isEmpty(name)) {
if (delegate.getNames(type).size() == 1) {
return delegate.lookup(type, delegate.getNames(type).iterator().next());
if (this.delegate.getNames(type).size() == 1) {
return this.delegate.lookup(type,
this.delegate.getNames(type).iterator().next());
}
name = this.name;
}
return name.equals(this.name) ? delegate.lookup(type, name) : null;
return name.equals(this.name) ? this.delegate.lookup(type, name) : null;
}
@Override