Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8394] Event bridge and properties fix #1937

Merged
merged 3 commits into from
Nov 29, 2024
Merged
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 @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -72,6 +73,8 @@ protected LookupContext(InvokerRequest invokerRequest) {

// this one "evolves" as process progresses (instance is immutable but instances are replaced)
public ProtoSession protoSession;
// here we track which user properties we pushed to Java System Properties (internal only)
public Set<String> pushedUserProperties;

public Logger logger;
public ILoggerFactory loggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public int invoke(InvokerRequest invokerRequest) throws InvokerException {

protected int doInvoke(C context) throws Exception {
pushCoreProperties(context);
pushUserProperties(context);
validate(context);
prepare(context);
configureLogging(context);
Expand All @@ -133,7 +134,7 @@ protected int doInvoke(C context) throws Exception {
preCommands(context);
container(context);
postContainer(context);
pushUserProperties(context);
pushUserProperties(context); // after PropertyContributor SPI
lookup(context);
init(context);
postCommands(context);
Expand Down Expand Up @@ -165,12 +166,27 @@ protected void pushCoreProperties(C context) throws Exception {
context.invokerRequest.installationDirectory().toString());
}

/**
* Note: this method is called twice from {@link #doInvoke(LookupContext)} and modifies context. First invocation
* when {@link LookupContext#pushedUserProperties} is null will push user properties IF key does not already
* exist among Java System Properties, and collects all they key it pushes. Second invocation happens AFTER
* {@link PropertyContributor} SPI invocation, and "refreshes" already pushed user properties by re-writing them
* as SPI may have modified them.
*/
protected void pushUserProperties(C context) throws Exception {
ProtoSession protoSession = context.protoSession;
HashSet<String> sys = new HashSet<>(protoSession.getSystemProperties().keySet());
protoSession.getUserProperties().entrySet().stream()
.filter(k -> !sys.contains(k.getKey()))
.forEach(k -> System.setProperty(k.getKey(), k.getValue()));
if (context.pushedUserProperties == null) {
context.pushedUserProperties = new HashSet<>();
protoSession.getUserProperties().entrySet().stream()
.filter(k -> !sys.contains(k.getKey()))
.peek(k -> context.pushedUserProperties.add(k.getKey()))
.forEach(k -> System.setProperty(k.getKey(), k.getValue()));
} else {
protoSession.getUserProperties().entrySet().stream()
.filter(k -> context.pushedUserProperties.contains(k.getKey()) || !sys.contains(k.getKey()))
.forEach(k -> System.setProperty(k.getKey(), k.getValue()));
}
}

protected void validate(C context) throws Exception {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public interface ExecutionEvent {

/**
* The possible types of execution events.
*
* Note: do not modify this enum, or, make sure that this enum and
* {@link org.apache.maven.api.EventType} have same elements in same order.
*/
enum Type {
ProjectDiscoveryStarted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@
public class DefaultEvent implements Event {
private final InternalMavenSession session;
private final ExecutionEvent delegate;
private final EventType eventType;

public DefaultEvent(InternalMavenSession session, ExecutionEvent delegate) {
public DefaultEvent(InternalMavenSession session, ExecutionEvent delegate, EventType eventType) {
this.session = session;
this.delegate = delegate;
this.eventType = eventType;
}

@Override
public EventType getType() {
return EventType.valueOf(delegate.getType().name());
return eventType;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,49 @@
*/
package org.apache.maven.internal.impl;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.Collection;

import org.apache.maven.api.Event;
import org.apache.maven.api.EventType;
import org.apache.maven.api.Listener;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;

/**
* Bridges between Maven3 events and Maven4 events.
*/
@Named
@Singleton
public class EventSpyImpl implements EventSpy {

private DefaultSessionFactory sessionFactory;

@Inject
EventSpyImpl(DefaultSessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

@Override
public void init(Context context) throws Exception {}

@Override
public void onEvent(Object arg) throws Exception {
if (arg instanceof ExecutionEvent) {
ExecutionEvent ee = (ExecutionEvent) arg;
if (arg instanceof ExecutionEvent ee) {
InternalMavenSession session =
InternalMavenSession.from(ee.getSession().getSession());
EventType eventType = convert(ee.getType());
Collection<Listener> listeners = session.getListeners();
if (!listeners.isEmpty()) {
Event event = new DefaultEvent(session, ee);
Event event = new DefaultEvent(session, ee, eventType);
for (Listener listener : listeners) {
listener.onEvent(event);
}
}
}
}

/**
* Simple "conversion" from Maven3 event type enum to Maven4 enum.
*/
protected EventType convert(ExecutionEvent.Type type) {
return EventType.values()[type.ordinal()];
}

@Override
public void close() throws Exception {}
}