diff --git a/src/main/java/run/halo/app/Application.java b/src/main/java/run/halo/app/Application.java index cac3c1b18..b32c4c1c7 100755 --- a/src/main/java/run/halo/app/Application.java +++ b/src/main/java/run/halo/app/Application.java @@ -1,5 +1,6 @@ package run.halo.app; +import org.springframework.scheduling.annotation.EnableAsync; import run.halo.app.repository.base.BaseRepositoryImpl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -18,6 +19,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableJpaAuditing @EnableScheduling +@EnableAsync @EnableJpaRepositories(basePackages = "run.halo.app.repository", repositoryBaseClass = BaseRepositoryImpl.class) public class Application { diff --git a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java new file mode 100644 index 000000000..b5cbc20a3 --- /dev/null +++ b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java @@ -0,0 +1,66 @@ +package run.halo.app.event.theme; + +import freemarker.template.Configuration; +import freemarker.template.TemplateModelException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import run.halo.app.handler.theme.config.support.ThemeProperty; +import run.halo.app.service.OptionService; +import run.halo.app.service.ThemeService; + +import java.util.Map; + +/** + * Theme activated listener. + * + * @author johnniang + * @date 19-4-20 + */ +@Component +@Slf4j +public class FreemarkerConfigAwareListener { + + private final OptionService optionService; + + private final Configuration configuration; + + private final ThemeService themeService; + + public FreemarkerConfigAwareListener(OptionService optionService, + Configuration configuration, + ThemeService themeService) { + this.optionService = optionService; + this.configuration = configuration; + this.themeService = themeService; + } + + @Async + @EventListener + public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) { + try { + ThemeProperty activatedTheme = themeService.getActivatedTheme(); + log.debug("Set shared variable theme: [{}]", activatedTheme); + configuration.setSharedVariable("theme", activatedTheme); + } catch (TemplateModelException e) { + log.warn("Failed to configure freemarker", e); + } + } + + @Async + @EventListener + public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) { + try { + ThemeProperty activatedTheme = themeActivatedEvent.getThemeProperty(); + log.debug("Set shared variable theme: [{}]", activatedTheme); + configuration.setSharedVariable("theme", activatedTheme); + Map options = optionService.listOptions(); + log.debug("Set shared variable options: [{}]", options); + configuration.setSharedVariable("options", options); + } catch (TemplateModelException e) { + log.warn("Failed to configure freemarker", e); + } + } +} diff --git a/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java b/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java new file mode 100644 index 000000000..c140d4f33 --- /dev/null +++ b/src/main/java/run/halo/app/event/theme/ThemeActivatedEvent.java @@ -0,0 +1,33 @@ +package run.halo.app.event.theme; + +import org.springframework.context.ApplicationEvent; +import org.springframework.lang.NonNull; +import org.springframework.util.Assert; +import run.halo.app.handler.theme.config.support.ThemeProperty; + +/** + * Theme activated event. + * + * @author johnniang + * @date 19-4-20 + */ +public class ThemeActivatedEvent extends ApplicationEvent { + + private final ThemeProperty themeProperty; + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param themeProperty theme property must not be null + */ + public ThemeActivatedEvent(Object source, @NonNull ThemeProperty themeProperty) { + super(source); + Assert.notNull(themeProperty, "Activated theme property must not be null"); + this.themeProperty = themeProperty; + } + + public ThemeProperty getThemeProperty() { + return themeProperty; + } +} diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index f4fa5c035..ea9be126f 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -61,7 +61,7 @@ public class StartedListener implements ApplicationListener themes = getThemes(); - log.debug("Themes type: [{}]", themes.getClass()); - log.debug("Themes: [{}]", themes); - return themes.stream().filter(themeProperty -> StringUtils.equals(themeProperty.getId(), themeId)).findFirst(); } @@ -327,13 +329,8 @@ public class ThemeServiceImpl implements ThemeService { // Clear the cache clearThemeCache(); - try { - // TODO Refactor here in the future - configuration.setSharedVariable("themeId", themeId); - configuration.setSharedVariable("options", optionService.listOptions()); - } catch (TemplateModelException e) { - throw new ServiceException("Failed to set shared variable", e).setErrorData(themeId); - } + // Publish a theme activated event + eventPublisher.publishEvent(new ThemeActivatedEvent(this, themeProperty)); return themeProperty; }