Skip to content

Commit

Permalink
feat: initialize broker on startup (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmberthold authored May 11, 2023
1 parent 303c9f2 commit d944e50
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.edc.connector.api.management.configuration.ManagementApiConfiguration;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.web.spi.WebService;
Expand All @@ -24,6 +25,9 @@ public class BrokerServerExtension implements ServiceExtension {

public static final String EXTENSION_NAME = "BrokerServerExtension";

@Setting
public static final String KNOWN_CONNECTORS = "edc.brokerserver.known.connectors";

@Inject
private ManagementApiConfiguration managementApiConfiguration;

Expand All @@ -37,10 +41,11 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var brokerServerExtensionContext = BrokerServerExtensionContextBuilder.buildContext();
var services = BrokerServerExtensionContextBuilder.buildContext(context.getConfig());
services.brokerServerInitializer().initializeConnectorList();

String managementApiGroup = managementApiConfiguration.getContextAlias();
brokerServerExtensionContext.jaxRsResources().forEach(resource ->
services.jaxRsResources().forEach(resource ->
webService.registerResource(managementApiGroup, resource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package de.sovity.edc.ext.brokerserver;

import de.sovity.edc.ext.brokerserver.services.BrokerServerInitializer;

import java.util.List;


Expand All @@ -22,5 +24,7 @@
*
* @param jaxRsResources Jax RS Resource implementations to register.
*/
public record BrokerServerExtensionContext(List<Object> jaxRsResources) {
public record BrokerServerExtensionContext(
List<Object> jaxRsResources,
BrokerServerInitializer brokerServerInitializer) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

package de.sovity.edc.ext.brokerserver;

import de.sovity.edc.ext.brokerserver.dao.stores.ConnectorStore;
import de.sovity.edc.ext.brokerserver.services.BrokerServerInitializer;
import lombok.NoArgsConstructor;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.configuration.Config;

import java.util.List;

Expand All @@ -30,7 +33,9 @@
*/
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public class BrokerServerExtensionContextBuilder {
public static BrokerServerExtensionContext buildContext() {
return new BrokerServerExtensionContext(List.of());
public static BrokerServerExtensionContext buildContext(Config config) {
var connectorStore = new ConnectorStore();
var brokerServerInitializer = new BrokerServerInitializer(connectorStore, config);
return new BrokerServerExtensionContext(List.of(), brokerServerInitializer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;
import java.util.stream.Stream;

public class InMemoryConnectorStore {
public class ConnectorStore {
private final Map<String, ConnectorRecord> connectorsById = new HashMap<>();

public Stream<ConnectorRecord> findAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;
import java.util.stream.Stream;

public class InMemoryContractOfferStore {
public class ContractOfferStore {
private final Map<String, ContractOfferRecord> contractOffersById = new HashMap<>();

public Stream<ContractOfferRecord> findAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 sovity GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.ext.brokerserver.services;

import de.sovity.edc.ext.brokerserver.BrokerServerExtension;
import de.sovity.edc.ext.brokerserver.dao.models.ConnectorRecord;
import de.sovity.edc.ext.brokerserver.dao.stores.ConnectorStore;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.spi.system.configuration.Config;

import java.time.OffsetDateTime;
import java.util.Arrays;

@RequiredArgsConstructor
public class BrokerServerInitializer {
private final ConnectorStore connectorStore;
private final Config config;

public void initializeConnectorList() {
var knownConnectors = config.getString(BrokerServerExtension.KNOWN_CONNECTORS).split(",");

Arrays.stream(knownConnectors).forEach(connectorId -> {
connectorId = connectorId.trim();

var connectorRecord = ConnectorRecord.builder()
.id(connectorId)
.idsId(connectorId)
.endpoint(connectorId)
.createdAt(OffsetDateTime.now())
.build();

connectorStore.save(connectorRecord);
});
}
}

0 comments on commit d944e50

Please sign in to comment.