Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 490f701

Browse filesBrowse files
Christoph Läubrichlaeubi
authored andcommitted
Use EventSpy to handle execution of events
Currently we use an AbstractMavenLifecycleParticipant but this only offers limited number of events, an EventSpy is much more flexible and offers much more insights. This now replaces the SessionListener with an EventSpy and also adds support for project execution events. Alongside with that the configuration is now local to the EventListener and no longer part of the connection implementation. Signed-off-by: Christoph Läubrich <christoph@laeubi-soft.de>
1 parent b4dbb22 commit 490f701
Copy full SHA for 490f701
Expand file treeCollapse file tree

12 files changed

+447
-218
lines changed

‎src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java
+13-6Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.concurrent.ConcurrentHashMap;
2626

27+
import org.apache.maven.plugin.LegacySupport;
2728
import org.codehaus.plexus.build.connect.BuildConnection;
2829
import org.codehaus.plexus.build.connect.messages.RefreshMessage;
2930
import org.codehaus.plexus.logging.AbstractLogEnabled;
@@ -59,17 +60,23 @@ public class DefaultBuildContext implements BuildContext {
5960
private final Map<String, Object> contextMap = new ConcurrentHashMap<>();
6061
private org.sonatype.plexus.build.incremental.BuildContext legacy;
6162
private BuildConnection connection;
63+
private LegacySupport legacySupport;
6264

6365
/**
64-
* @param legacy the legacy API we delegate to by default, this allow us to
65-
* support "older" plugins and implementors of the API while
66-
* still having a way to move forward!
67-
* @param connection the connection we use to forward refresh events
66+
* @param legacy the legacy API we delegate to by default, this allow us
67+
* to support "older" plugins and implementors of the API
68+
* while still having a way to move forward!
69+
* @param connection the connection we use to forward refresh events
70+
* @param legacySupport legacy support to get the current session
6871
*/
6972
@Inject
70-
public DefaultBuildContext(org.sonatype.plexus.build.incremental.BuildContext legacy, BuildConnection connection) {
73+
public DefaultBuildContext(
74+
org.sonatype.plexus.build.incremental.BuildContext legacy,
75+
BuildConnection connection,
76+
LegacySupport legacySupport) {
7177
this.legacy = legacy;
7278
this.connection = connection;
79+
this.legacySupport = legacySupport;
7380
}
7481

7582
/** {@inheritDoc} */
@@ -122,7 +129,7 @@ public Scanner newScanner(File basedir) {
122129
/** {@inheritDoc} */
123130
public void refresh(File file) {
124131
legacy.refresh(file);
125-
connection.send(new RefreshMessage(file.toPath()));
132+
connection.send(new RefreshMessage(file.toPath()), legacySupport.getSession());
126133
}
127134

128135
/** {@inheritDoc} */

‎src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java
+4-9Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.codehaus.plexus.build.connect;
1414

15+
import org.apache.maven.execution.MavenSession;
1516
import org.codehaus.plexus.build.connect.messages.Message;
1617

1718
/**
@@ -25,11 +26,12 @@ public interface BuildConnection {
2526
* Send a message and returns the reply from the other endpoint, should only be
2627
* called from a maven thread!
2728
*
28-
* @param message the message to send
29+
* @param message the message to send
30+
* @param mavenSession the maven session to reference
2931
* @return the reply message or <code>null</code> if this connection is not
3032
* enabled and the message was discarded.
3133
*/
32-
Message send(Message message);
34+
Message send(Message message, MavenSession mavenSession);
3335

3436
/**
3537
* This method allows code to perform an eager check if a buildconnection is
@@ -40,11 +42,4 @@ public interface BuildConnection {
4042
* if they will be discarded
4143
*/
4244
boolean isEnabled();
43-
44-
/**
45-
* Obtains the current configuration, can only be called from a maven thread
46-
*
47-
* @return the active configuration
48-
*/
49-
Configuration getConfiguration();
5045
}

‎src/main/java/org/codehaus/plexus/build/connect/Configuration.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/build/connect/Configuration.java
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@
1313
package org.codehaus.plexus.build.connect;
1414

1515
import org.codehaus.plexus.build.connect.messages.Message;
16-
import org.codehaus.plexus.build.connect.messages.ProjectsReadMessage;
1716

1817
/**
1918
* Provides access to the configuration provided by the server
2019
*/
2120
public interface Configuration {
2221

2322
/**
24-
* If this property is set to <code>true</code> in reply to a session start, a
25-
* {@link ProjectsReadMessage} will be send to the endpoint containing all
26-
* projects with their effective model
23+
* If this property is set to <code>true</code> in reply to a InitMessage
2724
*/
28-
public static final String CONFIG_SEND_AFTER_PROJECTS_READ = "afterProjectsRead";
25+
public static final String CONFIG_SEND_PROJECTS = "sendProjectInfos";
2926

3027
/**
31-
* @return <code>true</code> if {@link #CONFIG_SEND_AFTER_PROJECTS_READ} is
28+
* @return <code>true</code> if {@link #CONFIG_SEND_PROJECTS} is
3229
* provided
3330
*/
3431
public boolean isSendProjects();
@@ -44,7 +41,7 @@ public static Configuration of(Message message) {
4441

4542
@Override
4643
public boolean isSendProjects() {
47-
return message.getBooleanProperty(CONFIG_SEND_AFTER_PROJECTS_READ, false);
44+
return message.getBooleanProperty(CONFIG_SEND_PROJECTS, false);
4845
}
4946
};
5047
}
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright (c) 2025 Christoph Läubrich All rights reserved.
3+
4+
This program is licensed to you under the Apache License Version 2.0,
5+
and you may not use this file except in compliance with the Apache License Version 2.0.
6+
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
8+
Unless required by applicable law or agreed to in writing,
9+
software distributed under the Apache License Version 2.0 is distributed on an
10+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package org.codehaus.plexus.build.connect;
14+
15+
import java.util.LinkedHashMap;
16+
import java.util.Map;
17+
18+
import javax.inject.Inject;
19+
import javax.inject.Named;
20+
import javax.inject.Singleton;
21+
22+
import org.apache.maven.eventspy.EventSpy;
23+
import org.apache.maven.execution.ExecutionEvent;
24+
import org.apache.maven.execution.ExecutionEvent.Type;
25+
import org.apache.maven.execution.MavenSession;
26+
import org.codehaus.plexus.build.connect.messages.InitMessage;
27+
import org.codehaus.plexus.build.connect.messages.Message;
28+
import org.codehaus.plexus.build.connect.messages.ProjectMessage;
29+
import org.codehaus.plexus.build.connect.messages.ProjectsMessage;
30+
import org.codehaus.plexus.build.connect.messages.SessionMessage;
31+
32+
/**
33+
* Listen to all maven events and forward them to the endpoint
34+
*/
35+
@Named
36+
@Singleton
37+
public class EventListener implements EventSpy {
38+
39+
private BuildConnection connection;
40+
private Configuration configuration;
41+
42+
/**
43+
* Creates endpoint for the given connection
44+
*
45+
* @param connection injected
46+
*/
47+
@Inject
48+
public EventListener(BuildConnection connection) {
49+
this.connection = connection;
50+
}
51+
52+
@Override
53+
public void init(Context context) throws Exception {
54+
Map<String, String> data = new LinkedHashMap<>();
55+
context.getData().forEach((k, v) -> {
56+
data.put(k, String.valueOf(v));
57+
});
58+
Message message = connection.send(new InitMessage(data), null);
59+
if (message != null) {
60+
configuration = Configuration.of(message);
61+
}
62+
}
63+
64+
@Override
65+
public void onEvent(Object event) throws Exception {
66+
if (configuration == null) {
67+
return;
68+
}
69+
if (event instanceof ExecutionEvent) {
70+
handleExecutionEvent((ExecutionEvent) event);
71+
}
72+
}
73+
74+
private void handleExecutionEvent(ExecutionEvent executionEvent) {
75+
MavenSession session = executionEvent.getSession();
76+
Type type = executionEvent.getType();
77+
switch (type) {
78+
case SessionStarted:
79+
connection.send(new SessionMessage(session, true), session);
80+
if (configuration.isSendProjects()) {
81+
connection.send(new ProjectsMessage(session.getProjects()), session);
82+
}
83+
break;
84+
case SessionEnded:
85+
connection.send(new SessionMessage(session, false), session);
86+
break;
87+
case ProjectStarted:
88+
case ProjectFailed:
89+
case ProjectSkipped:
90+
case ProjectSucceeded:
91+
connection.send(new ProjectMessage(executionEvent.getProject(), type), session);
92+
break;
93+
default:
94+
break;
95+
}
96+
}
97+
98+
@Override
99+
public void close() throws Exception {}
100+
}

‎src/main/java/org/codehaus/plexus/build/connect/SessionListener.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/build/connect/SessionListener.java
-65Lines changed: 0 additions & 65 deletions
This file was deleted.

‎src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java

Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java
+11-39Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.codehaus.plexus.build.connect;
1414

15-
import javax.inject.Inject;
1615
import javax.inject.Named;
1716
import javax.inject.Singleton;
1817

@@ -25,16 +24,16 @@
2524
import java.util.ArrayList;
2625
import java.util.List;
2726
import java.util.Map;
28-
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.UUID;
28+
import java.util.WeakHashMap;
2929
import java.util.concurrent.ExecutorService;
3030
import java.util.concurrent.Executors;
3131
import java.util.concurrent.atomic.AtomicBoolean;
3232
import java.util.function.BiConsumer;
3333
import java.util.function.Function;
3434

35-
import org.apache.maven.plugin.LegacySupport;
35+
import org.apache.maven.execution.MavenSession;
3636
import org.codehaus.plexus.build.connect.messages.Message;
37-
import org.codehaus.plexus.build.connect.messages.SessionMessage;
3837

3938
/**
4039
* Default implementation using the system property
@@ -48,10 +47,7 @@ public class TcpBuildConnection implements BuildConnection {
4847

4948
private static final int PORT = Integer.getInteger(PLEXUS_BUILD_IPC_PORT, 0);
5049

51-
@Inject
52-
private LegacySupport support;
53-
54-
private Map<String, Configuration> configMap = new ConcurrentHashMap<>();
50+
private final Map<MavenSession, String> sessionMap = new WeakHashMap<>();
5551

5652
private final ThreadLocal<TcpClientConnection> connections =
5753
ThreadLocal.withInitial(() -> new TcpClientConnection());
@@ -62,47 +58,23 @@ public boolean isEnabled() {
6258
}
6359

6460
@Override
65-
public Message send(Message message) {
61+
public Message send(Message message, MavenSession mavenSession) {
6662
if (isEnabled()) {
67-
String sessionId;
68-
boolean sessionStart;
69-
if (message instanceof SessionMessage) {
70-
sessionId = message.getSessionId();
71-
sessionStart = ((SessionMessage) message).isSessionStart();
72-
} else {
73-
sessionId = getThreadSessionId();
74-
sessionStart = false;
75-
}
63+
String sessionId = getId(mavenSession);
7664
byte[] messageBytes = message.serialize(sessionId);
7765
byte[] replyBytes = connections.get().send(messageBytes);
7866
if (replyBytes.length > 0) {
79-
Message reply = Message.decode(replyBytes);
80-
if (reply != null && sessionStart) {
81-
configMap.put(sessionId, Configuration.of(reply));
82-
}
83-
return reply;
67+
return Message.decode(replyBytes);
8468
}
8569
}
8670
return null;
8771
}
8872

89-
private String getThreadSessionId() {
90-
// We must use LegacySupport here to get the currents threads session (what
91-
// might be cloned)
92-
return SessionMessage.getId(support.getSession());
93-
}
94-
95-
@Override
96-
public Configuration getConfiguration() {
97-
String id = getThreadSessionId();
98-
if (id == null) {
99-
throw new IllegalStateException("No session attached to current thread!");
100-
}
101-
Configuration configuration = configMap.get(id);
102-
if (configuration == null) {
103-
throw new IllegalStateException("No configuration active for session " + id + "!");
73+
private synchronized String getId(MavenSession session) {
74+
if (session == null) {
75+
return Thread.currentThread().getName();
10476
}
105-
return configuration;
77+
return sessionMap.computeIfAbsent(session, x -> UUID.randomUUID().toString());
10678
}
10779

10880
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.