Skip to content

Commit

Permalink
Expose ServiceProvider to Channel and TemplateProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobe91 committed Dec 19, 2022
1 parent 196b7fa commit cc204e5
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 85 deletions.
2 changes: 2 additions & 0 deletions core/api/src/main/java/com/blazebit/notify/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
public interface Channel<R extends NotificationRecipient<?>, M extends NotificationMessage> extends AutoCloseable {

String SERVICE_PROVIDER_PROPERTY = "serviceProvider";

/**
* Returns the notification message type class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,12 @@ public <T extends NotificationMessage> NotificationMessageResolver<T> getNotific
if (channelFactory == null) {
throw new NotificationException("No channel factory for channel key available: " + channelKey);
}
return channelFactory.createChannel(this, configurationSource);
return channelFactory.createChannel(this, (key) -> {
if (Channel.SERVICE_PROVIDER_PROPERTY.equals(key)) {
return this;
}
return configurationSource.getProperty(key);
});
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.blazebit.notify.email.message;

import com.blazebit.job.ConfigurationSource;
import com.blazebit.job.ServiceProvider;
import com.blazebit.notify.Notification;
import com.blazebit.notify.NotificationException;
import com.blazebit.notify.NotificationJobContext;
Expand All @@ -33,7 +34,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.function.Function;

Expand Down Expand Up @@ -134,17 +134,32 @@ public EmailNotificationMessageResolver(NotificationJobContext jobContext, Confi
this.envelopeFrom = configurationSource.getPropertyOrDefault(EMAIL_MESSAGE_ENVELOP_FROM_PROPERTY, String.class, Function.identity(), o -> null);
this.resourceBundleAccessor = configurationSource.getPropertyOrDefault(EMAIL_MESSAGE_RESOURCE_BUNDLE_PROPERTY, Function.class, EmailNotificationMessageResolver::resourceBundleByName, o -> null);
TemplateContext templateContext = configurationSource.getPropertyOrDefault(EMAIL_TEMPLATE_CONTEXT_PROPERTY, TemplateContext.class, null, o -> jobContext.getService(TemplateContext.class));
Function<String, Function<String, TemplateProcessor<String>>> templateProcessorFunction = templateProcessorFactoryNameProperty -> templateName ->
TemplateProcessorFactory<String> templateProcessorFactory = configurationSource.getPropertyOrDefault(
EMAIL_TEMPLATE_PROCESSOR_TYPE_PROPERTY, TemplateProcessorFactory.class, s -> {
if (templateContext == null) {
throw new NotificationException("No template context given!");
}
return templateContext.getTemplateProcessorFactory(s, String.class);
}, o -> null);
Function<String, TemplateProcessorFactory<Attachment>> attachmentTemplateProcessorFactory = templateProcessorFactoryKey -> {
if (templateContext == null) {
throw new NotificationException("No template context given!");
}
return templateContext.getTemplateProcessorFactory(templateProcessorFactoryKey, Attachment.class);
};
Function<String, TemplateProcessor<String>> templateProcessorFunction = templateName ->
templateProcessorByType(
templateContext,
templateProcessorFactory,
configurationSource,
templateName);
Function<String, TemplateProcessor<Attachment>> attachmentTemplateProcessorFunction = templateProcessorKey -> templateProcessorByKey(
templateName,
jobContext);
Function<String, TemplateProcessor<Attachment>> attachmentTemplateProcessorFunction = templateProcessorType -> templateProcessorByType(
templateContext,
attachmentTemplateProcessorFactory.apply(templateProcessorType),
configurationSource,
null);
null,
jobContext);
String literalSubject = (String) configurationSource.getProperty(EMAIL_MESSAGE_SUBJECT_PROPERTY);
this.subjectTemplateProcessor = literalSubject == null ? templateProcessorFunction.apply((String) configurationSource.getProperty(EMAIL_MESSAGE_SUBJECT_TEMPLATE_PROPERTY)) : TemplateProcessor.of(literalSubject);
String literalBodyText = (String) configurationSource.getProperty(EMAIL_MESSAGE_TEXT_PROPERTY);
Expand Down Expand Up @@ -213,7 +228,7 @@ private static Function<Locale, ResourceBundle> resourceBundleByName(String name
return locale -> ResourceBundle.getBundle(name, locale);
}

private static <T> TemplateProcessor<T> templateProcessorByType(TemplateContext templateContext, TemplateProcessorFactory<T> templateProcessorFactory, ConfigurationSource configurationSource, String templateName) {
private static <T> TemplateProcessor<T> templateProcessorByType(TemplateContext templateContext, TemplateProcessorFactory<T> templateProcessorFactory, ConfigurationSource configurationSource, String templateName, ServiceProvider serviceProvider) {
if (templateName == null) {
return null;
}
Expand All @@ -223,12 +238,7 @@ private static <T> TemplateProcessor<T> templateProcessorByType(TemplateContext
if (templateProcessorFactory == null) {
throw new NotificationException("No template processor factory given!");
}
return templateProcessorFactory.createTemplateProcessor(templateContext, key -> {
if (TemplateProcessor.TEMPLATE_NAME_PROPERTY.equals(key)) {
return templateName;
}
return configurationSource.getProperty(key);
});
return templateProcessorFactory.createTemplateProcessor(templateContext, templateName, configurationSource::getProperty, serviceProvider);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.blazebit.job.jpa.model.BaseEntity;
import com.blazebit.notify.NotificationRecipient;

import java.util.TimeZone;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.Locale;

Expand All @@ -36,6 +34,7 @@
public abstract class AbstractNotificationRecipient extends BaseEntity<Long> implements NotificationRecipient<Long> {

private Locale locale;
private TimeZone timeZone;

/**
* Creates an empty notification recipient.
Expand All @@ -52,12 +51,6 @@ public AbstractNotificationRecipient(Long id) {
super(id);
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator")
public Long getId() {
return id();
}

@Override
@Column(nullable = false)
public Locale getLocale() {
Expand All @@ -72,4 +65,14 @@ public Locale getLocale() {
public void setLocale(Locale locale) {
this.locale = locale;
}

@Override
@Column(nullable = false)
public TimeZone getTimeZone() {
return timeZone;
}

public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
import com.blazebit.notify.NotificationRecipient;

import java.util.Locale;
import java.util.TimeZone;

/**
* An abstract base class implementing the {@link NotificationRecipient} interface.
*
* @author Christian Beikov
* @since 1.0.0
*/
public class AbstractNotificationRecipient extends BaseEntity<Long> implements NotificationRecipient<Long> {
public abstract class AbstractNotificationRecipient extends BaseEntity<Long> implements NotificationRecipient<Long> {

private Locale locale;
private TimeZone timeZone;

/**
* Creates an empty notification recipient.
Expand Down Expand Up @@ -59,4 +61,13 @@ public Locale getLocale() {
public void setLocale(Locale locale) {
this.locale = locale;
}

@Override
public TimeZone getTimeZone() {
return timeZone;
}

public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
}
7 changes: 7 additions & 0 deletions template/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@

<artifactId>blaze-notify-template-api</artifactId>

<dependencies>
<dependency>
<groupId>com.blazebit</groupId>
<artifactId>blaze-job-core-api</artifactId>
<version>${version.blaze-job}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
*/
public interface TemplateProcessor<R> extends Serializable {

String TEMPLATE_NAME_PROPERTY = "template";

/**
* Processes this template based on the given map model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.blazebit.notify.template.api;

import com.blazebit.job.ServiceProvider;

/**
* A factory for template processors of a specific type.
*
Expand All @@ -34,9 +36,11 @@ public interface TemplateProcessorFactory<R> {
/**
* Creates a new template processor for the given template context and configuration source.
*
* @param templateContext The template context
* @param templateContext The template context
* @param templateName The name of the template to retrieve a template processor for
* @param configurationSource The configuration source
* @param serviceProvider The service provider of the job context
* @return the template processor
*/
TemplateProcessor<R> createTemplateProcessor(TemplateContext templateContext, ConfigurationSource configurationSource);
TemplateProcessor<R> createTemplateProcessor(TemplateContext templateContext, String templateName, ConfigurationSource configurationSource, ServiceProvider serviceProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class FreemarkerTemplateProcessor implements TemplateProcessor<String>, S
/**
* The configuration property for the Freemarker {@link Template}.
*/
public static final String FREEMARKER_TEMPLATE_PROPERTY = TEMPLATE_NAME_PROPERTY;
public static final String FREEMARKER_TEMPLATE_PROPERTY = "template";

/**
* The configuration property for the {@link ResourceBundle}.
Expand All @@ -73,9 +73,10 @@ public class FreemarkerTemplateProcessor implements TemplateProcessor<String>, S
/**
* Creates a new Freemarker template processor from the given configuration source.
*
* @param templateName The template name
* @param configurationSource The configuration source
*/
public FreemarkerTemplateProcessor(ConfigurationSource configurationSource) {
public FreemarkerTemplateProcessor(String templateName, ConfigurationSource configurationSource) {
Function<String, FreemarkerTemplateLookup> templateAccessor = name -> (Locale locale) -> {
Configuration configuration = configurationSource.getPropertyOrDefault(FREEMARKER_CONFIGURATION_PROPERTY, Configuration.class, null, o -> {
Configuration c = new Configuration(Configuration.VERSION_2_3_28);
Expand All @@ -89,7 +90,7 @@ public FreemarkerTemplateProcessor(ConfigurationSource configurationSource) {
throw new TemplateException("", e);
}
};
this.freemarkerTemplateLookup = configurationSource.getPropertyOrFail(FREEMARKER_TEMPLATE_PROPERTY, FreemarkerTemplateLookup.class, templateAccessor);
this.freemarkerTemplateLookup = configurationSource.getPropertyOrDefault(FREEMARKER_TEMPLATE_PROPERTY, FreemarkerTemplateLookup.class, templateAccessor, o -> templateAccessor.apply(templateName));
Function<String, TemplateResourceBundleLookup> resourceBundleAccessor = name -> (Locale locale) -> ResourceBundle.getBundle(name, locale);
this.resourceBundleLookup = configurationSource.getPropertyOrDefault(RESOURCE_BUNDLE_KEY, TemplateResourceBundleLookup.class, resourceBundleAccessor, o -> locale -> null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TemplateProcessorKey<String> getTemplateProcessorKey() {
}

@Override
public TemplateProcessor<String> createTemplateProcessor(TemplateContext templateContext, ConfigurationSource configurationSource) {
return new FreemarkerTemplateProcessor(configurationSource);
public TemplateProcessor<String> createTemplateProcessor(TemplateContext templateContext, String templateName, ConfigurationSource configurationSource, com.blazebit.job.ServiceProvider serviceProvider) {
return new FreemarkerTemplateProcessor(templateName, configurationSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package com.blazebit.template.thymeleaf;

import com.blazebit.notify.template.api.ConfigurationSource;
import com.blazebit.job.ServiceProvider;
import com.blazebit.notify.template.api.TemplateException;
import com.blazebit.notify.template.api.TemplateProcessor;
import com.blazebit.notify.template.api.TemplateProcessorKey;
import java.io.Serializable;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
Expand All @@ -38,8 +37,6 @@ public class ThymeleafTemplateProcessor implements TemplateProcessor<String>, Se

public static final TemplateProcessorKey<String> KEY = TemplateProcessorKey.of("thymeleaf", String.class);

public static final String THYMELEAF_TEMPLATE_ENGINE_PROPERTY = "templateEngine";
public static final String THYMELEAF_TEMPLATE_NAME_PROPERTY = TEMPLATE_NAME_PROPERTY;
public static final String LOCALE_MODEL_KEY = "locale";

private final ITemplateEngine templateEngine;
Expand All @@ -48,11 +45,14 @@ public class ThymeleafTemplateProcessor implements TemplateProcessor<String>, Se
/**
* Creates a new Thymeleaf template processor from the given configuration source.
*
* @param configurationSource The configuration source
* @param templateName The template name
* @param serviceProvider The service provider of the job context
*
*/
public ThymeleafTemplateProcessor(ConfigurationSource configurationSource) {
this.templateEngine = configurationSource.getPropertyOrDefault(THYMELEAF_TEMPLATE_ENGINE_PROPERTY, ITemplateEngine.class, val -> null, val -> new TemplateEngine());
this.templateName = configurationSource.getPropertyOrFail(THYMELEAF_TEMPLATE_NAME_PROPERTY, String.class, Function.identity());
public ThymeleafTemplateProcessor(String templateName, ServiceProvider serviceProvider) {
ITemplateEngine templateEngine = serviceProvider.getService(ITemplateEngine.class);
this.templateEngine = templateEngine == null ? new TemplateEngine() : templateEngine;
this.templateName = templateName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public TemplateProcessorKey<String> getTemplateProcessorKey() {
}

@Override
public TemplateProcessor<String> createTemplateProcessor(TemplateContext templateContext, ConfigurationSource configurationSource) {
return new ThymeleafTemplateProcessor(configurationSource);
public TemplateProcessor<String> createTemplateProcessor(TemplateContext templateContext, String templateName, ConfigurationSource configurationSource, com.blazebit.job.ServiceProvider serviceProvider) {
return new ThymeleafTemplateProcessor(templateName, serviceProvider);
}
}

0 comments on commit cc204e5

Please sign in to comment.