Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.talend.sdk.component.runtime.manager.service;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class MavenRepositoryDefaultResolver implements MavenRepositoryResolver {

private PathHandler handler = new PathHandlerImpl();

private boolean hasFallback = Boolean.getBoolean("talend.component.manager.m2.fallback");

@Override
public Path discover() {
return Stream
Expand All @@ -63,8 +66,18 @@ public Path discover() {

@Override
public Path fallback() {
log.debug("[fallback] default to user's default repository.");
return Paths.get(USER_HOME).resolve(M2_REPOSITORY);
log.debug("[fallback::{}] default to user's default repository.", hasFallback);
if (hasFallback) {
return Paths.get(USER_HOME).resolve(M2_REPOSITORY);
} else {
try {
// we create an empty temporary folder as our default m2.
return Files.createTempDirectory("empty_m2_");
} catch (IOException e) {
// if any permission issue or other, we go to user's dir.
return Paths.get(USER_HOME);
}
}
}

public Path fromSystemProperties() {
Expand Down Expand Up @@ -119,30 +132,38 @@ private static String parseSettings(final Path settings) {
}

public Path fromMavenSettings() {
return Stream.of(
Optional.ofNullable(System.getProperty(TALEND_COMPONENT_MANAGER_M2_SETTINGS))
.map(Paths::get)
.orElse(null), //
Optional.ofNullable(System.getProperty("user.home")).map(it -> Paths.get(it, M2_SETTINGS)).orElse(null),
Optional.ofNullable(System.getenv(MAVEN_HOME)).map(it -> Paths.get(it, CONF_SETTINGS)).orElse(null),
Optional.ofNullable(System.getenv(M2_HOME)).map(it -> Paths.get(it, CONF_SETTINGS)).orElse(null))
.filter(Objects::nonNull)
.filter(Files::exists)
.map(MavenRepositoryDefaultResolver::parseSettings)
.filter(Objects::nonNull)
.map(handler::get)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
if (hasFallback) {
return Stream.of(
Optional.ofNullable(System.getProperty(TALEND_COMPONENT_MANAGER_M2_SETTINGS))
.map(Paths::get)
.orElse(null), //
Optional.ofNullable(System.getProperty("user.home"))
.map(it -> Paths.get(it, M2_SETTINGS))
.orElse(null),
Optional.ofNullable(System.getenv(MAVEN_HOME)).map(it -> Paths.get(it, CONF_SETTINGS)).orElse(null),
Optional.ofNullable(System.getenv(M2_HOME)).map(it -> Paths.get(it, CONF_SETTINGS)).orElse(null))
.filter(Objects::nonNull)
.filter(Files::exists)
.map(MavenRepositoryDefaultResolver::parseSettings)
.filter(Objects::nonNull)
.map(handler::get)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
} else {
return null;
}
}

public Path fromEnvironmentVariables() {
final String vm2 = System.getenv(M2_HOME);
log.debug("[fromEnvironmentVariables] M2_HOME={}", vm2);
if (vm2 != null) {
return handler.get(Paths.get(vm2, "repository").toString());
if (hasFallback) {
final String vm2 = System.getenv(M2_HOME);
log.debug("[fromEnvironmentVariables] M2_HOME={}", vm2);
if (vm2 != null) {
return handler.get(Paths.get(vm2, "repository").toString());
}
log.debug("[fromEnvironmentVariables] Could not get m2 from environment.");
}
log.debug("[fromEnvironmentVariables] Could not get m2 from environment.");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
import javax.management.ReflectionException;

import org.apache.xbean.finder.util.Files;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -98,6 +100,16 @@ private ComponentManager newManager() {
return newManager(new File("target/test-dependencies"));
}

@BeforeAll
static void setup() {
System.setProperty("talend.component.manager.m2.fallback", "true");
}

@AfterAll
static void teardown() {
System.clearProperty("talend.component.manager.m2.fallback");
}

@Test
void doubleClose() {
final ComponentManager instance = ComponentManager.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Path get(final String path) {

@BeforeAll
static void setup() throws IOException {
System.setProperty("talend.component.manager.m2.fallback", "true");
final Path repository = Paths.get(new File("target/test-classes").getAbsolutePath());
Files.createDirectories(repository.resolve(M2_REPOSITORY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -111,18 +110,8 @@ public ContainerManager(final DependenciesResolutionConfiguration dependenciesRe
this.logInfoLevelMapping = logInfoLevelMapping;
this.containerInitializer = containerInitializer;
this.resolver = dependenciesResolutionConfiguration.getResolver();
this.rootRepositoryLocation = dependenciesResolutionConfiguration.getRootRepositoryLocation();

Path rootRepo = ofNullable(dependenciesResolutionConfiguration.getRootRepositoryLocation())
.filter(Files::exists)
.orElseGet(() -> PathFactory.get(System.getProperty("user.home")).resolve(".m2/repository"));
// if we've defaulted to user home m2 (fallback), we want to check if we're in running in a fatjar
if (PathFactory.get(System.getProperty("user.home")).resolve(".m2/repository").equals(rootRepo)) {
final URL nested = classLoaderConfiguration.getParent().getResource("MAVEN-INF/repository");
if (nested != null) {
rootRepo = PathFactory.get(nested.getFile().replace("file:", ""));
}
}
this.rootRepositoryLocation = rootRepo;
info("Using root repository: " + this.rootRepositoryLocation.toAbsolutePath());
final String pluginsLocation = System.getProperty("talend.component.manager.plugins.location",
"TALEND-INF/plugins.properties");
Expand Down
Loading
Loading