|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.tractusx.edc.boot.runtime; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | +import java.net.URLClassLoader; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Properties; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.eclipse.dataspaceconnector.boot.system.runtime.BaseRuntime; |
| 29 | +import org.eclipse.dataspaceconnector.spi.EdcException; |
| 30 | +import org.eclipse.dataspaceconnector.spi.monitor.Monitor; |
| 31 | +import org.eclipse.dataspaceconnector.spi.system.configuration.Config; |
| 32 | +import org.eclipse.dataspaceconnector.spi.system.configuration.ConfigFactory; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | + |
| 35 | +public class MultiTenantRuntime extends BaseRuntime { |
| 36 | + |
| 37 | + private final @NotNull Monitor monitor = createMonitor(); |
| 38 | + |
| 39 | + public static void main(String[] args) { |
| 40 | + var runtime = new MultiTenantRuntime(); |
| 41 | + runtime.boot(); |
| 42 | + } |
| 43 | + |
| 44 | + protected void boot() { |
| 45 | + loadTenantsConfig().getConfig("edc.tenants").partition().forEach(this::bootTenant); |
| 46 | + } |
| 47 | + |
| 48 | + private void bootTenant(Config tenantConfig) { |
| 49 | + var baseProperties = System.getProperties(); |
| 50 | + tenantConfig.getRelativeEntries().forEach(System::setProperty); |
| 51 | + var entries = |
| 52 | + Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)) |
| 53 | + .map(this::toUrl) |
| 54 | + .toArray(URL[]::new); |
| 55 | + var classLoader = URLClassLoader.newInstance(entries, ClassLoader.getSystemClassLoader()); |
| 56 | + |
| 57 | + var latch = new CountDownLatch(1); |
| 58 | + var runtimeThread = |
| 59 | + new Thread( |
| 60 | + () -> { |
| 61 | + try { |
| 62 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 63 | + |
| 64 | + super.boot(); |
| 65 | + latch.countDown(); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new EdcException(e); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + monitor.info("Starting tenant " + tenantConfig.currentNode()); |
| 72 | + runtimeThread.start(); |
| 73 | + try { |
| 74 | + latch.await(20, TimeUnit.SECONDS); |
| 75 | + System.setProperties(baseProperties); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @NotNull |
| 82 | + private Config loadTenantsConfig() { |
| 83 | + var tenantsPath = System.getProperty("edc.tenants.path"); |
| 84 | + if (tenantsPath == null) { |
| 85 | + throw new EdcException("No edc.tenants.path mandatory property provided"); |
| 86 | + } |
| 87 | + try (var is = Files.newInputStream(Path.of(tenantsPath))) { |
| 88 | + var properties = new Properties(); |
| 89 | + properties.load(is); |
| 90 | + return ConfigFactory.fromProperties(properties); |
| 91 | + } catch (IOException e) { |
| 92 | + throw new EdcException(e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private URL toUrl(String entry) { |
| 97 | + try { |
| 98 | + return new File(entry).toURI().toURL(); |
| 99 | + } catch (MalformedURLException e) { |
| 100 | + throw new RuntimeException(e); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments