Skip to content

Commit

Permalink
Implement the new agent properties to be backed by maven
Browse files Browse the repository at this point in the history
IProvisioningAgent now supports new methods to gather so called "agent
properties", this implements the new methods in terms of maven so they
can be supplied by user or system properties as well as profiles.
  • Loading branch information
laeubi committed Apr 22, 2024
1 parent 53f7fd5 commit 9cbac6f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 37 deletions.
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.eclipse.sisu.equinox.EquinoxServiceFactory;
import org.eclipse.tycho.helper.MavenPropertyHelper;

@Component(role = IProvisioningAgent.class)
public class DefaultProvisioningAgent implements IProvisioningAgent {
Expand All @@ -40,6 +41,9 @@ public class DefaultProvisioningAgent implements IProvisioningAgent {
@Requirement
Map<String, IAgentServiceFactory> agentFactories;

@Requirement
MavenPropertyHelper propertyHelper;

private Map<String, Supplier<Object>> agentServices = new ConcurrentHashMap<>();

@Override
Expand Down Expand Up @@ -107,6 +111,16 @@ public void unregisterService(String serviceName, Object service) {
}
}

@Override
public String getProperty(String key, String defaultValue) {
return propertyHelper.getGlobalProperty(key, defaultValue);
}

@Override
public String getProperty(String key) {
return propertyHelper.getGlobalProperty(key);
}

private static final class LazyAgentServiceFactory implements Supplier<Object> {

private IAgentServiceFactory factory;
Expand Down
Expand Up @@ -12,12 +12,6 @@
*******************************************************************************/
package org.eclipse.tycho.p2maven.transport;

import java.util.List;
import java.util.Properties;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
Expand All @@ -26,6 +20,7 @@
import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.eclipse.tycho.IRepositoryIdManager;
import org.eclipse.tycho.helper.MavenPropertyHelper;
import org.eclipse.tycho.version.TychoVersion;

@Component(role = IAgentServiceFactory.class, hint = "org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager")
Expand All @@ -41,7 +36,7 @@ public class RemoteArtifactRepositoryManagerAgentFactory implements IAgentServic
MavenAuthenticator authenticator;

@Requirement
MavenSession mavenSession;
MavenPropertyHelper propertyHelper;

@Override
public Object createService(IProvisioningAgent agent) {
Expand All @@ -55,7 +50,7 @@ public Object createService(IProvisioningAgent agent) {

private boolean getDisableP2MirrorsConfiguration() {
String deprecatedKey = "tycho.disableP2Mirrors";
String deprecatedValue = getMirrorProperty(deprecatedKey);
String deprecatedValue = propertyHelper.getGlobalProperty(deprecatedKey);

if (deprecatedValue != null) {
logger.info("Using " + deprecatedKey
Expand All @@ -64,7 +59,7 @@ private boolean getDisableP2MirrorsConfiguration() {
return getBooleanValue(deprecatedValue);
}

String value = getMirrorProperty("eclipse.p2.mirrors");
String value = propertyHelper.getGlobalProperty("eclipse.p2.mirrors");

if (value != null) {
// eclipse.p2.mirrors false -> disable mirrors
Expand All @@ -85,32 +80,4 @@ private boolean getBooleanValue(String value) {
return Boolean.parseBoolean(value);
}

private String getMirrorProperty(String key) {
// Check user properties first ...
Properties userProperties = mavenSession.getUserProperties();
String userProperty = userProperties.getProperty(key);
if (userProperty != null) {
return userProperty;
}
// check if there are any active profile properties ...
Settings settings = mavenSession.getSettings();
List<Profile> profiles = settings.getProfiles();
List<String> activeProfiles = settings.getActiveProfiles();
for (Profile profile : profiles) {
if (activeProfiles.contains(profile.getId())) {
String profileProperty = profile.getProperties().getProperty(key);
if (profileProperty != null) {
return profileProperty;
}
}
}
// now maven system properties
Properties systemProperties = mavenSession.getSystemProperties();
String systemProperty = systemProperties.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
// java sysem properties last
return System.getProperty(key);
}
}
@@ -0,0 +1,96 @@
/*******************************************************************************
* Copyright (c) 2024 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.helper;

import java.util.List;
import java.util.Properties;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;

@Component(role = MavenPropertyHelper.class)
public class MavenPropertyHelper {

@Requirement
LegacySupport legacySupport;

/**
* Returns a global (user) property of the given key, the search order is
* <ol>
* <li>Maven session user properties</li>
* <li>Active profile(s) property</li>
* <li>Maven session system properties</li>
* <li>Java System Properties</li>
* </ol>
*
* @param key
* the key to search
* @return the value according to the described search order or <code>null</code> if nothing can
* be found.
*/
public String getGlobalProperty(String key) {
return getGlobalProperty(key, null);
}

/**
* Returns a global (user) property of the given key, the search order is
* <ol>
* <li>Maven session user properties</li>
* <li>Active profile(s) property</li>
* <li>Maven session system properties</li>
* <li>Java System Properties</li>
* <li>default value</li>
* </ol>
*
* @param key
* the key to search
* @param defaultValue
* the default value to use as a last resort
* @return the value according to the described search order
*/
public String getGlobalProperty(String key, String defaultValue) {
MavenSession mavenSession = legacySupport.getSession();
if (mavenSession != null) {
// Check user properties first ...
Properties userProperties = mavenSession.getUserProperties();
String userProperty = userProperties.getProperty(key);
if (userProperty != null) {
return userProperty;
}
// check if there are any active profile properties ...
Settings settings = mavenSession.getSettings();
List<Profile> profiles = settings.getProfiles();
List<String> activeProfiles = settings.getActiveProfiles();
for (Profile profile : profiles) {
if (activeProfiles.contains(profile.getId())) {
String profileProperty = profile.getProperties().getProperty(key);
if (profileProperty != null) {
return profileProperty;
}
}
}
// now maven system properties
Properties systemProperties = mavenSession.getSystemProperties();
String systemProperty = systemProperties.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
}
// java sysem properties last
return System.getProperty(key, defaultValue);
}
}

0 comments on commit 9cbac6f

Please sign in to comment.