Skip to content

Commit

Permalink
#201 add system modules to extensions subproject, add request context…
Browse files Browse the repository at this point in the history
… to script execution
  • Loading branch information
Thorsten Marx committed May 29, 2024
1 parent 5fddd66 commit 17d22bc
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
import com.github.thmarx.cms.api.ServerProperties;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.hooks.HookSystem;
import com.github.thmarx.cms.api.feature.features.HookSystemFeature;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.extensions.request.RequestExtensions;
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.extensions.ExtensionFileSystem;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -129,7 +129,7 @@ protected void loadExtensions(final Path extPath, final Consumer<Source> loader)
.forEach(loader);
}

public RequestExtensions newContext(Theme theme, final HookSystem hookSystem) throws IOException {
public RequestExtensions newContext(Theme theme, RequestContext requestContext) throws IOException {
var context = Context.newBuilder()
.allowAllAccess(true)
.allowHostClassLookup(className -> true)
Expand All @@ -155,15 +155,10 @@ public RequestExtensions newContext(Theme theme, final HookSystem hookSystem) th
.engine(engine).build();
}

RequestExtensions holder = new RequestExtensions(context, themeContext);
RequestExtensions requestExtensions = new RequestExtensions(context, themeContext);

final Value bindings = context.getBindings("js");
bindings.putMember("extensions", holder);
bindings.putMember("fileSystem", db.getFileSystem());
bindings.putMember("db", db);
bindings.putMember("theme", theme);
bindings.putMember("hooks", hookSystem);
bindings.putMember("ENV", serverProperties.env());
setUpBinding(bindings, requestExtensions, theme, requestContext);

var extPath = db.getFileSystem().resolve("extensions/");
if (Files.exists(extPath)) {
Expand All @@ -173,12 +168,7 @@ public RequestExtensions newContext(Theme theme, final HookSystem hookSystem) th

if (!theme.empty()) {
final Value themeBindings = themeContext.getBindings("js");
themeBindings.putMember("extensions", holder);
themeBindings.putMember("fileSystem", db.getFileSystem());
themeBindings.putMember("db", db);
themeBindings.putMember("theme", theme);
themeBindings.putMember("hooks", hookSystem);
themeBindings.putMember("ENV", serverProperties.env());
setUpBinding(themeBindings, requestExtensions, theme, requestContext);

// theme_sources.forEach(themeContext::eval);
var themeExtPath = parentTheme.extensionsPath();
Expand All @@ -188,7 +178,19 @@ public RequestExtensions newContext(Theme theme, final HookSystem hookSystem) th
}
}

return holder;
return requestExtensions;
}

private void setUpBinding (Value bindings,
RequestExtensions requestExtensions, Theme theme, RequestContext requestContext) {
bindings.putMember("extensions", requestExtensions);
bindings.putMember("fileSystem", db.getFileSystem());
bindings.putMember("db", db);
bindings.putMember("theme", theme);
// for backword compability
// bindings.putMember("hooks", requestContext.get(HookSystemFeature.class).hookSystem());
bindings.putMember("requestContext", requestContext);
bindings.putMember("ENV", serverProperties.env());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const AuthFeature = Java.type("com.github.thmarx.cms.api.feature.features.AuthFeature").class
export const ConfigurationFeature = Java.type("com.github.thmarx.cms.api.feature.features.ConfigurationFeature").class
export const ContentNodeMapperFeature = Java.type("com.github.thmarx.cms.api.feature.features.ContentNodeMapperFeature").class
export const ContentParserFeature = Java.type("com.github.thmarx.cms.api.feature.features.ContentParserFeature").class
export const ContentRenderFeature = Java.type("com.github.thmarx.cms.api.feature.features.ContentRenderFeature").class
export const CurrentNodeFeature = Java.type("com.github.thmarx.cms.api.feature.features.CurrentNodeFeature").class
export const CurrentTaxonomyFeature = Java.type("com.github.thmarx.cms.api.feature.features.CurrentTaxonomyFeature").class
export const DBFeature = Java.type("com.github.thmarx.cms.api.feature.features.DBFeature").class
export const EventBusFeature = Java.type("com.github.thmarx.cms.api.feature.features.EventBusFeature").class
export const HookSystemFeature = Java.type("com.github.thmarx.cms.api.feature.features.HookSystemFeature").class
export const InjectorFeature = Java.type("com.github.thmarx.cms.api.feature.features.InjectorFeature").class
export const IsDevModeFeature = Java.type("com.github.thmarx.cms.api.feature.features.IsDevModeFeature").class
export const IsPreviewFeature = Java.type("com.github.thmarx.cms.api.feature.features.IsPreviewFeature").class
export const MarkdownRendererFeature = Java.type("com.github.thmarx.cms.api.feature.features.MarkdownRendererFeature").class
export const ModuleManagerFeature = Java.type("com.github.thmarx.cms.api.feature.features.ModuleManagerFeature").class
export const RequestFeature = Java.type("com.github.thmarx.cms.api.feature.features.RequestFeature").class
export const ServerPropertiesFeature = Java.type("com.github.thmarx.cms.api.feature.features.ServerPropertiesFeature").class
export const SiteMediaServiceFeature = Java.type("com.github.thmarx.cms.api.feature.features.SiteMediaServiceFeature").class
export const SitePropertiesFeature = Java.type("com.github.thmarx.cms.api.feature.features.SitePropertiesFeature").class
export const ThemeFeature = Java.type("com.github.thmarx.cms.api.feature.features.ThemeFeature").class
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { HookSystemFeature } from 'system/features.mjs';

const hooks = requestContext.get(HookSystemFeature).hookSystem()

export const $hooks = {
register : (name, fun, priority) => {
if (priority) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.github.thmarx.cms.extensions;

/*-
* #%L
* cms-extensions
* %%
* Copyright (C) 2023 - 2024 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.ServerProperties;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.feature.features.HookSystemFeature;
import com.github.thmarx.cms.api.hooks.HookSystem;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.io.IOException;
import java.nio.file.Path;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

/**
*
* @author t.marx
*/
@ExtendWith(MockitoExtension.class)
public class ExtensionManagerTest {

@Mock
DB db;
@Mock
Theme theme;
@Mock
ServerProperties properties;
@Mock
FileSystem fileSystem;


ExtensionManager extensionManager;

@BeforeEach
public void setup () throws Exception {

Mockito.when(fileSystem.resolve("libs/"))
.thenReturn(Path.of("src/test/resources/site/libs"));
Mockito.when(fileSystem.resolve("extensions/"))
.thenReturn(Path.of("src/test/resources/site/extensions"));
Mockito.when(db.getFileSystem()).thenReturn(fileSystem);
Mockito.when(theme.extensionsPath())
.thenReturn(Path.of("src/test/resources/theme/extensions"));

extensionManager = new ExtensionManager(db, theme, properties);
extensionManager.init();
}

@AfterEach
public void shutdown () throws Exception {
extensionManager.close();
}

@Test
public void testSomeMethod() throws IOException {

var requestContext = new RequestContext();
final HookSystem hookSystem = new HookSystem();
requestContext.add(HookSystemFeature.class, new HookSystemFeature(hookSystem));
extensionManager.newContext(theme, requestContext);

Assertions.assertThat(hookSystem.call("test").results())
.hasSize(1)
.containsExactly("hallo")
;
}

}
30 changes: 30 additions & 0 deletions cms-extensions/src/test/resources/site/extensions/hook-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*-
* #%L
* cms-extensions
* %%
* Copyright (C) 2023 - 2024 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 { $hooks } from 'system/hooks.mjs';

$hooks.register(
"test",
(context) => {
return 'hallo'
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void run() {
new IPCServerThread(serverSocket.accept(), eventConsumer, properties).start();
}
} catch (IOException e) {
System.exit(-1);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,42 @@ public RequestContext create(

var hookSystem = injector.getInstance(HookSystem.class);

RequestExtensions requestExtensions = extensionManager.newContext(theme, hookSystem);

RenderContext renderContext = new RenderContext(
markdownRenderer,
createShortCodes(requestExtensions),
theme);

var context = new RequestContext();
context.add(InjectorFeature.class, new InjectorFeature(injector));
context.add(HookSystemFeature.class, new HookSystemFeature(hookSystem));
context.add(RequestFeature.class, new RequestFeature(uri, queryParameters));
context.add(RequestExtensions.class, requestExtensions);
context.add(ThemeFeature.class, new ThemeFeature(theme));
context.add(RenderContext.class, renderContext);
context.add(MarkdownRendererFeature.class, new MarkdownRendererFeature(renderContext.markdownRenderer()));
context.add(ContentParserFeature.class, new ContentParserFeature(injector.getInstance(ContentParser.class)));
context.add(ContentNodeMapperFeature.class, new ContentNodeMapperFeature(injector.getInstance(ContentNodeMapper.class)));
var requestContext = new RequestContext();
requestContext.add(InjectorFeature.class, new InjectorFeature(injector));
requestContext.add(HookSystemFeature.class, new HookSystemFeature(hookSystem));
requestContext.add(RequestFeature.class, new RequestFeature(uri, queryParameters));
requestContext.add(ThemeFeature.class, new ThemeFeature(theme));
requestContext.add(ContentParserFeature.class, new ContentParserFeature(injector.getInstance(ContentParser.class)));
requestContext.add(ContentNodeMapperFeature.class, new ContentNodeMapperFeature(injector.getInstance(ContentNodeMapper.class)));
if (ServerContext.IS_DEV) {
context.add(IsDevModeFeature.class, new IsDevModeFeature());
requestContext.add(IsDevModeFeature.class, new IsDevModeFeature());

if (queryParameters.containsKey("preview")) {
context.add(IsPreviewFeature.class, new IsPreviewFeature());
requestContext.add(IsPreviewFeature.class, new IsPreviewFeature());
}
}
context.add(ConfigurationFeature.class, new ConfigurationFeature(injector.getInstance(Configuration.class)));
context.add(ServerPropertiesFeature.class, new ServerPropertiesFeature(
requestContext.add(ConfigurationFeature.class, new ConfigurationFeature(injector.getInstance(Configuration.class)));
requestContext.add(ServerPropertiesFeature.class, new ServerPropertiesFeature(
injector.getInstance(Configuration.class)
.get(ServerConfiguration.class).serverProperties()
));
context.add(SitePropertiesFeature.class, new SitePropertiesFeature(siteProperties));
context.add(SiteMediaServiceFeature.class, new SiteMediaServiceFeature(siteMediaService));
requestContext.add(SitePropertiesFeature.class, new SitePropertiesFeature(siteProperties));
requestContext.add(SiteMediaServiceFeature.class, new SiteMediaServiceFeature(siteMediaService));

RequestExtensions requestExtensions = extensionManager.newContext(theme, requestContext);

RenderContext renderContext = new RenderContext(
markdownRenderer,
createShortCodes(requestExtensions),
theme);
requestContext.add(RenderContext.class, renderContext);
requestContext.add(MarkdownRendererFeature.class, new MarkdownRendererFeature(renderContext.markdownRenderer()));


requestContext.add(RequestExtensions.class, requestExtensions);


return context;
return requestContext;
}

private ShortCodes createShortCodes (RequestExtensions requestExtensions) {
Expand Down
4 changes: 2 additions & 2 deletions cms-server/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
<!--Logger name="org.eclipse.jetty.server.RequestLog" level="info" additivity="false">
<AppenderRef ref="console"/>
</Logger-->
<!--

<Root level="error">
<AppenderRef ref="console"/>
</Root>
-->

</Loggers>
</Configuration>
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>24.0.0</version>
<version>24.0.1</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-language</artifactId>
<version>24.0.0</version>
<version>24.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down

0 comments on commit 17d22bc

Please sign in to comment.