createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient);
Injector injector = Guice.createInjector(Arrays.asList(
+ ObjectBinding.forClass(String.class).named("smtpHostname").is(options.smtpHostname),
+ ObjectBinding.forClass(String.class).named("emailSender").is(options.emailSender),
+ ObjectBinding.forClass(String.class).named("emailRecipient").is(options.emailRecipient),
ObjectBinding.forClass(StateDirectory.class).is(StateDirectory.of(options.stateDirectory)),
ObjectBinding.forClass(ChainDirectory.class).is(ChainDirectory.of(options.chainDirectory)),
ObjectBinding.forClass(EmailAction.class).is(errorEmailAction)
import java.util.List;
import java.util.concurrent.TimeUnit;
+import jakarta.inject.Inject;
+import jakarta.inject.Named;
import net.pterodactylus.rhynodge.Action;
import net.pterodactylus.rhynodge.Filter;
import net.pterodactylus.rhynodge.Query;
*/
public class ReactionLoader {
+ @Inject
+ public ReactionLoader(@Named("smtpHostname") String smtpHostname, @Named("emailSender") String emailSender, @Named("emailRecipient") String emailRecipient) {
+ this.smtpHostname = smtpHostname;
+ this.emailSender = emailSender;
+ this.emailRecipient = emailRecipient;
+ }
+
/**
* Creates a {@link Reaction} from the given {@link Chain}.
*
* @throws LoaderException
* if a class can not be loaded
*/
- @SuppressWarnings("static-method")
public Reaction loadReaction(Chain chain) throws LoaderException {
/* check if chain is enabled. */
/* create filters. */
List<Filter> filters = new ArrayList<Filter>();
for (Part filterPart : chain.filters()) {
- filters.add(ReactionLoader.<Filter> createObject(filterPart.name(), "net.pterodactylus.rhynodge.filters", extractParameters(filterPart.parameters())));
+ filters.add(createObject(filterPart.name(), "net.pterodactylus.rhynodge.filters", extractParameters(filterPart.parameters())));
}
/* create merger. */
* The parameters to extract the values from
* @return The extracted values
*/
- private static List<String> extractParameters(List<Parameter> parameters) {
+ private List<String> extractParameters(List<Parameter> parameters) {
List<String> parameterValues = new ArrayList<String>();
for (Parameter parameter : parameters) {
* if the object can not be created
*/
@SuppressWarnings("unchecked")
- private static <T> T createObject(String className, String packageName, List<String> parameters) throws LoaderException {
+ private <T> T createObject(String className, String packageName, List<String> parameters) throws LoaderException {
/* try to load class without package name. */
Class<?> objectClass = null;
}
}
+ var effectiveParameters = overrideParameters(className, parameters);
+
/* locate an eligible constructor. */
Constructor<?> wantedConstructor = null;
for (Constructor<?> constructor : objectClass.getConstructors()) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
- if (parameterTypes.length != parameters.size()) {
+ if (parameterTypes.length != effectiveParameters.size()) {
continue;
}
boolean compatibleTypes = true;
}
try {
- return (T) wantedConstructor.newInstance(parameters.toArray());
+ return (T) wantedConstructor.newInstance(effectiveParameters.toArray());
} catch (IllegalArgumentException iae1) {
throw new LoaderException("Could not invoke constructor.", iae1);
} catch (InstantiationException ie1) {
}
+ private List<String> overrideParameters(String className, List<String> parameters) {
+ if (className.equals("EmailAction")) {
+ return List.of(smtpHostname, emailSender, emailRecipient);
+ }
+ return parameters;
+ }
+
+ private final String smtpHostname;
+ private final String emailSender;
+ private final String emailRecipient;
+
}