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 7f19431 commit 2fa1190
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 35 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 @@ -22,6 +22,7 @@
import com.blazebit.notify.NotificationMessageResolver;
import com.blazebit.notify.NotificationMessageResolverModelCustomizer;
import com.blazebit.notify.NotificationRecipient;
import com.blazebit.notify.template.api.ServiceProvider;
import com.blazebit.notify.template.api.TemplateContext;
import com.blazebit.notify.template.api.TemplateProcessor;
import com.blazebit.notify.template.api.TemplateProcessorFactory;
Expand Down Expand Up @@ -135,19 +136,27 @@ 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));
com.blazebit.notify.template.api.ServiceProvider serviceProvider = new com.blazebit.notify.template.api.ServiceProvider() {
@Override
public <T> Optional<T> getService(Class<T> serviceClass) {
return Optional.ofNullable(jobContext.getService(serviceClass));
}
};
Function<String, Function<String, TemplateProcessor<String>>> templateProcessorFunction = templateProcessorFactoryNameProperty -> templateName ->
templateProcessorByKey(
String.class,
templateContext,
configurationSource.getPropertyOrDefault(templateProcessorFactoryNameProperty, String.class, null, value -> null),
configurationSource,
templateName);
templateName,
serviceProvider);
Function<String, TemplateProcessor<Attachment>> attachmentTemplateProcessorFunction = templateProcessorKey -> templateProcessorByKey(
Attachment.class,
templateContext,
templateProcessorKey,
configurationSource,
null);
null,
serviceProvider);
String literalSubject = (String) configurationSource.getProperty(EMAIL_MESSAGE_SUBJECT_PROPERTY);
this.subjectTemplateProcessor = literalSubject == null ? templateProcessorFunction.apply(
EMAIL_MESSAGE_SUBJECT_TEMPLATE_PROCESSOR_KEY_PROPERTY).apply((String) configurationSource.getProperty(EMAIL_MESSAGE_SUBJECT_TEMPLATE_PROPERTY)) : TemplateProcessor.of(literalSubject);
Expand Down Expand Up @@ -219,7 +228,7 @@ private static Function<Locale, ResourceBundle> resourceBundleByName(String name
return locale -> ResourceBundle.getBundle(name, locale);
}

private static <T> TemplateProcessor<T> templateProcessorByKey(Class<T> clazz, TemplateContext templateContext, String templateProcessorFactoryName, ConfigurationSource configurationSource, String templateName) {
private static <T> TemplateProcessor<T> templateProcessorByKey(Class<T> clazz, TemplateContext templateContext, String templateProcessorFactoryName, ConfigurationSource configurationSource, String templateName, ServiceProvider serviceProvider) {
if (templateProcessorFactoryName == null) {
return null;
}
Expand All @@ -233,6 +242,8 @@ private static <T> TemplateProcessor<T> templateProcessorByKey(Class<T> clazz, T
return templateProcessorFactory.createTemplateProcessor(templateContext, key -> {
if (TemplateProcessor.TEMPLATE_NAME_PROPERTY.equals(key)) {
return templateName;
} else if (TemplateProcessor.SERVICE_PROVIDER_PROPERTY.equals(key)) {
return serviceProvider;
}
return configurationSource.getProperty(key);
});
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blazebit.notify.email.model.memory;
package com.blazebit.notify.template.api;

import com.blazebit.notify.ConfigurationSourceProvider;
import java.util.Optional;

/**
* A simple E-Mail notification.
* A service provider.
*
* @author Christian Beikov
* @author Moritz Becker
* @since 1.0.0
*/
public class EmailNotification extends AbstractEmailNotification<Long> implements ConfigurationSourceProvider {

private static final long serialVersionUID = 1L;
public interface ServiceProvider {

/**
* Creates a E-Mail notification with the given id.
* Returns a registered service that is an instance of the given service class.
*
* @param id The notification id
* @param serviceClass The service class
* @param <T> The service type
* @return An {@link Optional}
*/
public EmailNotification(Long id) {
super(id);
}

@Override
public Long getPartitionKey() {
return getId();
}
<T> Optional<T> getService(Class<T> serviceClass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface TemplateProcessor<R> extends Serializable {

String TEMPLATE_NAME_PROPERTY = "template";

String SERVICE_PROVIDER_PROPERTY = "serviceProvider";

/**
* Processes this template based on the given map model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
package com.blazebit.template.thymeleaf;

import com.blazebit.notify.template.api.ConfigurationSource;
import com.blazebit.notify.template.api.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.Optional;
import java.util.function.Function;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
Expand All @@ -38,10 +40,15 @@ 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 static final ServiceProvider EMPTY_SERVICE_PROVIDER = new ServiceProvider() {
@Override
public <T> Optional<T> getService(Class<T> serviceClass) {
return Optional.empty();
}
};

private final ITemplateEngine templateEngine;
private final String templateName;

Expand All @@ -51,8 +58,9 @@ public class ThymeleafTemplateProcessor implements TemplateProcessor<String>, Se
* @param configurationSource The configuration source
*/
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());
ServiceProvider serviceProvider = configurationSource.getPropertyOrDefault(SERVICE_PROVIDER_PROPERTY, ServiceProvider.class, val -> null, o -> EMPTY_SERVICE_PROVIDER);
this.templateEngine = serviceProvider.getService(ITemplateEngine.class).orElse(new TemplateEngine());
this.templateName = configurationSource.getPropertyOrFail(TEMPLATE_NAME_PROPERTY, String.class, Function.identity());
}

@Override
Expand Down

0 comments on commit 2fa1190

Please sign in to comment.