Skip to content

Commit

Permalink
update dependencies and introduce SPI for querygateway
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx committed Aug 18, 2024
1 parent b0ab644 commit 08f427e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cms-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.thmarx.cms.api.annotations.Experimental;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
*
Expand All @@ -39,6 +40,11 @@ public <T, Q extends Query<T>> void register (Class<Q> query, QueryHandler<Q, T
handlers.put(query, handler);
}

public void init () {
ServiceLoader<QueryProvider> loader = ServiceLoader.load(QueryProvider.class);
loader.forEach(provider -> register(provider.queryClass(), provider.handler()));
}

public <T> T execute (Query<T> query) {
if (!handlers.containsKey(query.getClass())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.thmarx.cms.api.query;

/*-
* #%L
* cms-api
* %%
* 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%
*/

/**
*
* @author thmar
*/
public interface QueryProvider<Q extends Query<T>, T> {

Class<? extends Query<T>> queryClass ();

QueryHandler<Q, T> handler();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
* #L%
*/

import com.google.auto.service.AutoService;
import com.google.j2objc.annotations.AutoreleasePool;
import lombok.Data;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -38,13 +41,20 @@ public class QueryGatewayTest {
@BeforeAll
public static void setup () {
gateway.register(CustomQuery.class, new CustomQueryHandler());
gateway.init();
}

@Test
public void testSomeMethod() {
String message = gateway.execute(new CustomQuery("cms"));
System.out.println(message);
}

@Test
public void testSecondQuery() {
String message = gateway.execute(new SecondQueryProvider.SecondQuery("coder"));
Assertions.assertThat(message).isEqualTo("hello coder");
}

public static record CustomQuery(String name) implements Query<String> {

Expand All @@ -57,4 +67,24 @@ public String handle(CustomQuery query) {
}

}

@AutoService(QueryProvider.class)
public static class SecondQueryProvider implements QueryProvider<SecondQueryProvider.SecondQuery, String> {

@Override
public Class<? extends Query<String>> queryClass() {
return SecondQuery.class;
}

@Override
public QueryHandler<SecondQuery, String> handler() {
return (query) -> {
return "hello " + query.name();
};
}

public static record SecondQuery(String name) implements Query<String> {

}
}
}
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>24.0.1</version>
<version>24.0.2</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-language</artifactId>
<version>24.0.1</version>
<version>24.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down Expand Up @@ -233,6 +233,11 @@
<artifactId>semver4j</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 08f427e

Please sign in to comment.