mirror of https://github.com/halo-dev/halo
Refactor freemarker configuration with event
parent
bd7d627d76
commit
bd9dd69674
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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<String, String> options = optionService.listOptions();
|
||||
log.debug("Set shared variable options: [{}]", options);
|
||||
configuration.setSharedVariable("options", options);
|
||||
} catch (TemplateModelException e) {
|
||||
log.warn("Failed to configure freemarker", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -61,7 +61,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
|
|||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
// save halo version to database
|
||||
this.cacheOwo();
|
||||
this.cacheActiveTheme();
|
||||
// this.cacheActiveTheme();
|
||||
this.printStartInfo();
|
||||
this.initThemes();
|
||||
|
||||
|
@ -97,7 +97,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
|
|||
*/
|
||||
private void cacheActiveTheme() {
|
||||
try {
|
||||
configuration.setSharedVariable("themeId", themeService.getActivatedThemeId());
|
||||
configuration.setSharedVariable("theme", themeService.getActivatedTheme());
|
||||
} catch (TemplateModelException e) {
|
||||
log.error("", e);
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ import cn.hutool.core.io.file.FileWriter;
|
|||
import cn.hutool.core.text.StrBuilder;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
@ -21,6 +21,7 @@ import org.springframework.web.client.RestTemplate;
|
|||
import org.springframework.web.multipart.MultipartFile;
|
||||
import run.halo.app.cache.StringCacheStore;
|
||||
import run.halo.app.config.properties.HaloProperties;
|
||||
import run.halo.app.event.theme.ThemeActivatedEvent;
|
||||
import run.halo.app.exception.*;
|
||||
import run.halo.app.handler.theme.config.ThemeConfigResolver;
|
||||
import run.halo.app.handler.theme.config.ThemePropertyResolver;
|
||||
|
@ -74,6 +75,8 @@ public class ThemeServiceImpl implements ThemeService {
|
|||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
/**
|
||||
* Activated theme id.
|
||||
*/
|
||||
|
@ -90,7 +93,8 @@ public class ThemeServiceImpl implements ThemeService {
|
|||
Configuration configuration,
|
||||
ThemeConfigResolver themeConfigResolver,
|
||||
ThemePropertyResolver themePropertyResolver,
|
||||
RestTemplate restTemplate) {
|
||||
RestTemplate restTemplate,
|
||||
ApplicationEventPublisher eventPublisher) {
|
||||
this.optionService = optionService;
|
||||
this.cacheStore = cacheStore;
|
||||
this.configuration = configuration;
|
||||
|
@ -99,6 +103,7 @@ public class ThemeServiceImpl implements ThemeService {
|
|||
this.restTemplate = restTemplate;
|
||||
|
||||
workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER);
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -112,9 +117,6 @@ public class ThemeServiceImpl implements ThemeService {
|
|||
|
||||
Set<ThemeProperty> 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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue