Skip to content

Commit

Permalink
#158 first events
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Feb 5, 2024
1 parent 37dfc12 commit 08973f1
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.thmarx.cms.api.eventbus.events;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.eventbus.Event;
import java.nio.file.Path;

/**
*
* @author t.marx
*/
public record InvalidateContentCacheEvent () implements Event {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.thmarx.cms.api.eventbus.events;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.eventbus.Event;
import java.nio.file.Path;

/**
*
* @author t.marx
*/
public record InvalidateTemplateCacheEvent () implements Event {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.thmarx.cms.api.eventbus.events;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.eventbus.Event;

/**
*
* @author t.marx
*/
public record ReIndexContentMetaDataEvent () implements Event {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.github.thmarx.cms.api.db.DBFileSystem;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
import com.github.thmarx.cms.api.eventbus.events.InvalidateContentCacheEvent;
import com.github.thmarx.cms.api.eventbus.events.InvalidateTemplateCacheEvent;
import com.github.thmarx.cms.api.eventbus.events.ReIndexContentMetaDataEvent;
import com.github.thmarx.cms.api.eventbus.events.TemplateChangedEvent;
import com.github.thmarx.cms.api.utils.PathUtil;
import com.github.thmarx.cms.filesystem.query.Query;
Expand All @@ -46,6 +49,8 @@
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -286,19 +291,29 @@ public void onNext(FileEvent item) {
@Override
public void onNext(FileEvent item) {
eventBus.publish(new TemplateChangedEvent(item.file().toPath()));
eventBus.publish(new InvalidateTemplateCacheEvent());
}
});

reInitFolder(contentBase);

fileWatcher.start();

eventBus.register(ReIndexContentMetaDataEvent.class, (event) -> {
try {
swapMetaData();
} catch (IOException ex) {
log.error("error while reindex meta data", ex);
}
});
}

private void swapMetaData() throws IOException {
log.debug("rebuild metadata");
metaData.clear();
reInitFolder(contentBase);
eventBus.publish(new ContentChangedEvent(contentBase));
eventBus.publish(new InvalidateContentCacheEvent());
}

private void reInitFolder(final Path folder) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.eventbus.EventBus;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.yaml.snakeyaml.Yaml;

/**
Expand All @@ -37,10 +38,15 @@
public class FileSystemTest {

static FileSystem fileSystem;



@BeforeAll
static void setup() throws IOException {
fileSystem = new FileSystem(Path.of("src/test/resources"), null, (file) -> {

var eventBus = Mockito.mock(EventBus.class);

fileSystem = new FileSystem(Path.of("src/test/resources"), eventBus, (file) -> {
try {
return new Yaml().load(Files.readString(file));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.github.thmarx.cms.api.PropertiesLoader;
import com.github.thmarx.cms.api.configuration.Configuration;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -32,6 +33,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.yaml.snakeyaml.Yaml;

/**
Expand All @@ -50,7 +52,9 @@ public static void setup () throws IOException {

var config = new Configuration(Path.of("src/test/resources/"));

fileSystem = new FileSystem(Path.of("src/test/resources"), null, (file) -> {
var eventBus = Mockito.mock(EventBus.class);

fileSystem = new FileSystem(Path.of("src/test/resources"), eventBus, (file) -> {
try {
return new Yaml().load(Files.readString(file));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.github.thmarx.cms.api.eventbus.EventListener;
import com.github.thmarx.cms.api.eventbus.events.ConfigurationFileChanged;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
import com.github.thmarx.cms.api.eventbus.events.InvalidateContentCacheEvent;
import com.github.thmarx.cms.api.eventbus.events.InvalidateTemplateCacheEvent;
import com.github.thmarx.cms.api.eventbus.events.SitePropertiesChanged;
import com.github.thmarx.cms.api.eventbus.events.TemplateChangedEvent;
import com.github.thmarx.cms.extensions.ExtensionManager;
Expand Down Expand Up @@ -137,11 +139,11 @@ public void init(Path modulesPath) throws IOException {
}
});

injector.getInstance(EventBus.class).register(ContentChangedEvent.class, (EventListener<ContentChangedEvent>) (ContentChangedEvent event) -> {
injector.getInstance(EventBus.class).register(InvalidateContentCacheEvent.class, (EventListener<InvalidateContentCacheEvent>) (InvalidateContentCacheEvent event) -> {
log.debug("invalidate content cache");
injector.getInstance(ContentParser.class).clearCache();
});
injector.getInstance(EventBus.class).register(TemplateChangedEvent.class, (EventListener<TemplateChangedEvent>) (TemplateChangedEvent event) -> {
injector.getInstance(EventBus.class).register(InvalidateTemplateCacheEvent.class, (EventListener<InvalidateTemplateCacheEvent>) (InvalidateTemplateCacheEvent event) -> {
log.debug("invalidate template cache");
injector.getInstance(TemplateEngine.class).invalidateCache();
});
Expand Down

0 comments on commit 08973f1

Please sign in to comment.