de.codecentric.spring.boot.chaos.monkey.configuration.AssaultException Maven / Gradle / Ivy
package de.codecentric.spring.boot.chaos.monkey.configuration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "chaos.monkey.assaults.exception")
public class AssaultException {
private static final Logger Logger = LoggerFactory.getLogger(AssaultException.class);
@Value("${type : java.lang.RuntimeException}")
private String type;
@Value("${arguments : #{null}}")
private List arguments;
public List getArguments() {
return arguments;
}
public void setArguments(List arguments) {
this.arguments = arguments;
}
@JsonIgnore
@SneakyThrows
public void throwExceptionInstance() {
Exception instance;
try {
Class extends Exception> exceptionClass = getExceptionClass();
if (arguments == null) {
Constructor extends Exception> constructor = exceptionClass.getConstructor();
instance = constructor.newInstance();
} else {
Constructor extends Exception> constructor =
exceptionClass.getConstructor(this.getExceptionArgumentTypes().toArray(new Class[0]));
instance =
constructor.newInstance(this.getExceptionArgumentValues().toArray(new Object[0]));
}
} catch (ReflectiveOperationException e) {
Logger.warn(
"Cannot instantiate the class for provided type: {}. Fallback: Throw RuntimeException",
type);
instance = new RuntimeException("Chaos Monkey - RuntimeException");
}
throw instance;
}
@JsonIgnore
public Class extends Exception> getExceptionClass() throws ClassNotFoundException {
if (type == null) {
// use Chaos Monkey default Runtime Exception
type = "java.lang.RuntimeException";
ExceptionArgument exceptionArgument = new ExceptionArgument();
exceptionArgument.setClassName("java.lang.String");
exceptionArgument.setValue("Chaos Monkey - RuntimeException");
arguments = Collections.singletonList(exceptionArgument);
}
return Class.forName(type).asSubclass(Exception.class);
}
@JsonIgnore
public List getExceptionArgumentTypes() {
return arguments.stream().map(ExceptionArgument::getClassType).collect(Collectors.toList());
}
@JsonIgnore
private List