diff --git a/DEPENDENCIES b/DEPENDENCIES index 62aad4a97..d85031105 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -273,6 +273,7 @@ maven/mavencentral/org.eclipse.edc/dsp-transfer-process/0.2.0, Apache-2.0, appro maven/mavencentral/org.eclipse.edc/dsp/0.2.0, Apache-2.0, approved, technology.edc maven/mavencentral/org.eclipse.edc/http-spi/0.2.0, Apache-2.0, approved, technology.edc maven/mavencentral/org.eclipse.edc/http/0.2.0, Apache-2.0, approved, technology.edc +maven/mavencentral/org.eclipse.edc/iam-mock/0.2.0, Apache-2.0, approved, technology.edc maven/mavencentral/org.eclipse.edc/jersey-core/0.2.0, Apache-2.0, approved, technology.edc maven/mavencentral/org.eclipse.edc/jersey-micrometer/0.2.0, Apache-2.0, approved, technology.edc maven/mavencentral/org.eclipse.edc/jersey-providers/0.2.0, Apache-2.0, approved, technology.edc diff --git a/samples/multi-tenancy/README.md b/samples/multi-tenancy/README.md index ecc0bf60c..4a854a8c5 100644 --- a/samples/multi-tenancy/README.md +++ b/samples/multi-tenancy/README.md @@ -3,12 +3,15 @@ This sample show how to create a custom runtime to run multiple EDC tenants in a single java process. ## How it works + In a Java Runtime, multiple "sub-runtimes" with dedicated classloader can be launched in parallel, giving object-level separation (an object instantiated by a sub-runtime cannot be accessed by another sub-runtime). ## How to use + The module provides an extension of the `BaseRuntime` class called `MultiTenantRuntime`. This class can be set in the `build.gradle.kts` as the main class: + ```kotlin application { mainClass.set("org.eclipse.tractusx.edc.samples.multitenancy.MultiTenantRuntime") @@ -18,6 +21,7 @@ application { This runtime looks for a properties file which path can be specified with the `edc.tenants.path` property. In this file the tenants are defined through settings, e.g.: + ```properties edc.tenants.tenant1.edc.fs.config=/config/path edc.tenants.tenant2.edc.fs.config=/config/path @@ -28,6 +32,7 @@ Using this file the EDC will run with 3 tenants: `tenant1`, `tenant2` and `tenan configuration file. Everything that stays after the tenant name in the setting key will be loaded in the tenant runtime, so *theoretically* (but not recommended) you could define all the tenants configuration in the tenants properties file: + ```properties edc.tenants.tenant1.web.http.port=18181 edc.tenants.tenant1.any.other.setting=value @@ -38,16 +43,19 @@ edc.tenants.tenant3.web.http.port=38181 ## Sample Build: + ```shell ./gradlew :samples:multi-tenancy:build ``` Run: + ```shell java -jar -Dedc.tenants.path=samples/multi-tenancy/tenants.properties samples/multi-tenancy/build/libs/multitenant.jar ``` Create a PolicyDefinition on `first` tenant: + ```shell curl -X POST http://localhost:18183/management/v2/policydefinitions \ --header 'Content-Type: application/json' \ @@ -65,10 +73,13 @@ curl -X POST http://localhost:18183/management/v2/policydefinitions \ ``` Get `first` tenant assets: + ```shell curl -X POST http://localhost:18183/management/v2/policydefinitions/request ``` + Will get a list containing the PolicyDefinition we created: + ```json [ { @@ -95,15 +106,19 @@ Will get a list containing the PolicyDefinition we created: ``` `second` and `third` tenants will have no assets: + ```shell curl -X POST http://localhost:28183/management/v2/policydefinitions/request ``` + and + ```shell curl -X POST http://localhost:38183/management/v2/policydefinitions/request ``` will return + ```json [] ``` diff --git a/samples/multi-tenancy/src/main/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntime.java b/samples/multi-tenancy/src/main/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntime.java index 6008b401c..b9ddbec27 100644 --- a/samples/multi-tenancy/src/main/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntime.java +++ b/samples/multi-tenancy/src/main/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntime.java @@ -53,8 +53,9 @@ private void bootTenant(Config tenantConfig) { .map(this::toUrl) .toArray(URL[]::new); + Thread runtimeThread = null; try (var classLoader = URLClassLoader.newInstance(classPathEntries, getSystemClassLoader())) { - var runtimeThread = new Thread(() -> { + runtimeThread = new Thread(() -> { try { Thread.currentThread().setContextClassLoader(classLoader); super.boot(); @@ -68,7 +69,10 @@ private void bootTenant(Config tenantConfig) { runtimeThread.join(20_000); - } catch (InterruptedException | IOException e) { + } catch (InterruptedException e) { + runtimeThread.interrupt(); + throw new EdcException(e); + } catch (IOException e) { throw new EdcException(e); } finally { System.setProperties(baseProperties); @@ -94,7 +98,7 @@ private URL toUrl(String entry) { try { return new File(entry).toURI().toURL(); } catch (MalformedURLException e) { - throw new RuntimeException(e); + throw new EdcException(e); } } } diff --git a/samples/multi-tenancy/src/test/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntimeTest.java b/samples/multi-tenancy/src/test/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntimeTest.java index 991446ebc..5cd49157e 100644 --- a/samples/multi-tenancy/src/test/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntimeTest.java +++ b/samples/multi-tenancy/src/test/java/org/eclipse/tractusx/edc/samples/multitenancy/MultiTenantRuntimeTest.java @@ -1,3 +1,17 @@ +/* + * Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * 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: + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation + * + */ + package org.eclipse.tractusx.edc.samples.multitenancy; import org.eclipse.edc.spi.EdcException;