diff --git a/src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java b/src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java index 797079e..2b34e71 100644 --- a/src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java +++ b/src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.apache.maven.plugin.LegacySupport; import org.codehaus.plexus.build.connect.BuildConnection; import org.codehaus.plexus.build.connect.messages.RefreshMessage; import org.codehaus.plexus.logging.AbstractLogEnabled; @@ -59,17 +60,23 @@ public class DefaultBuildContext implements BuildContext { private final Map contextMap = new ConcurrentHashMap<>(); private org.sonatype.plexus.build.incremental.BuildContext legacy; private BuildConnection connection; + private LegacySupport legacySupport; /** - * @param legacy the legacy API we delegate to by default, this allow us to - * support "older" plugins and implementors of the API while - * still having a way to move forward! - * @param connection the connection we use to forward refresh events + * @param legacy the legacy API we delegate to by default, this allow us + * to support "older" plugins and implementors of the API + * while still having a way to move forward! + * @param connection the connection we use to forward refresh events + * @param legacySupport legacy support to get the current session */ @Inject - public DefaultBuildContext(org.sonatype.plexus.build.incremental.BuildContext legacy, BuildConnection connection) { + public DefaultBuildContext( + org.sonatype.plexus.build.incremental.BuildContext legacy, + BuildConnection connection, + LegacySupport legacySupport) { this.legacy = legacy; this.connection = connection; + this.legacySupport = legacySupport; } /** {@inheritDoc} */ @@ -122,7 +129,7 @@ public Scanner newScanner(File basedir) { /** {@inheritDoc} */ public void refresh(File file) { legacy.refresh(file); - connection.send(new RefreshMessage(file.toPath())); + connection.send(new RefreshMessage(file.toPath()), legacySupport.getSession()); } /** {@inheritDoc} */ diff --git a/src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java b/src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java index f2b83df..921115e 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java +++ b/src/main/java/org/codehaus/plexus/build/connect/BuildConnection.java @@ -12,6 +12,7 @@ */ package org.codehaus.plexus.build.connect; +import org.apache.maven.execution.MavenSession; import org.codehaus.plexus.build.connect.messages.Message; /** @@ -25,11 +26,12 @@ public interface BuildConnection { * Send a message and returns the reply from the other endpoint, should only be * called from a maven thread! * - * @param message the message to send + * @param message the message to send + * @param mavenSession the maven session to reference * @return the reply message or null if this connection is not * enabled and the message was discarded. */ - Message send(Message message); + Message send(Message message, MavenSession mavenSession); /** * This method allows code to perform an eager check if a buildconnection is @@ -40,11 +42,4 @@ public interface BuildConnection { * if they will be discarded */ boolean isEnabled(); - - /** - * Obtains the current configuration, can only be called from a maven thread - * - * @return the active configuration - */ - Configuration getConfiguration(); } diff --git a/src/main/java/org/codehaus/plexus/build/connect/Configuration.java b/src/main/java/org/codehaus/plexus/build/connect/Configuration.java index c467b36..2444145 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/Configuration.java +++ b/src/main/java/org/codehaus/plexus/build/connect/Configuration.java @@ -13,7 +13,6 @@ package org.codehaus.plexus.build.connect; import org.codehaus.plexus.build.connect.messages.Message; -import org.codehaus.plexus.build.connect.messages.ProjectsReadMessage; /** * Provides access to the configuration provided by the server @@ -21,14 +20,12 @@ public interface Configuration { /** - * If this property is set to true in reply to a session start, a - * {@link ProjectsReadMessage} will be send to the endpoint containing all - * projects with their effective model + * If this property is set to true in reply to a InitMessage */ - public static final String CONFIG_SEND_AFTER_PROJECTS_READ = "afterProjectsRead"; + public static final String CONFIG_SEND_PROJECTS = "sendProjectInfos"; /** - * @return true if {@link #CONFIG_SEND_AFTER_PROJECTS_READ} is + * @return true if {@link #CONFIG_SEND_PROJECTS} is * provided */ public boolean isSendProjects(); @@ -44,7 +41,7 @@ public static Configuration of(Message message) { @Override public boolean isSendProjects() { - return message.getBooleanProperty(CONFIG_SEND_AFTER_PROJECTS_READ, false); + return message.getBooleanProperty(CONFIG_SEND_PROJECTS, false); } }; } diff --git a/src/main/java/org/codehaus/plexus/build/connect/EventListener.java b/src/main/java/org/codehaus/plexus/build/connect/EventListener.java new file mode 100644 index 0000000..5806c08 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/connect/EventListener.java @@ -0,0 +1,100 @@ +/* +Copyright (c) 2025 Christoph Läubrich All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.connect; + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.eventspy.EventSpy; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.ExecutionEvent.Type; +import org.apache.maven.execution.MavenSession; +import org.codehaus.plexus.build.connect.messages.InitMessage; +import org.codehaus.plexus.build.connect.messages.Message; +import org.codehaus.plexus.build.connect.messages.ProjectMessage; +import org.codehaus.plexus.build.connect.messages.ProjectsMessage; +import org.codehaus.plexus.build.connect.messages.SessionMessage; + +/** + * Listen to all maven events and forward them to the endpoint + */ +@Named +@Singleton +public class EventListener implements EventSpy { + + private BuildConnection connection; + private Configuration configuration; + + /** + * Creates endpoint for the given connection + * + * @param connection injected + */ + @Inject + public EventListener(BuildConnection connection) { + this.connection = connection; + } + + @Override + public void init(Context context) throws Exception { + Map data = new LinkedHashMap<>(); + context.getData().forEach((k, v) -> { + data.put(k, String.valueOf(v)); + }); + Message message = connection.send(new InitMessage(data), null); + if (message != null) { + configuration = Configuration.of(message); + } + } + + @Override + public void onEvent(Object event) throws Exception { + if (configuration == null) { + return; + } + if (event instanceof ExecutionEvent) { + handleExecutionEvent((ExecutionEvent) event); + } + } + + private void handleExecutionEvent(ExecutionEvent executionEvent) { + MavenSession session = executionEvent.getSession(); + Type type = executionEvent.getType(); + switch (type) { + case SessionStarted: + connection.send(new SessionMessage(session, true), session); + if (configuration.isSendProjects()) { + connection.send(new ProjectsMessage(session.getProjects()), session); + } + break; + case SessionEnded: + connection.send(new SessionMessage(session, false), session); + break; + case ProjectStarted: + case ProjectFailed: + case ProjectSkipped: + case ProjectSucceeded: + connection.send(new ProjectMessage(executionEvent.getProject(), type), session); + break; + default: + break; + } + } + + @Override + public void close() throws Exception {} +} diff --git a/src/main/java/org/codehaus/plexus/build/connect/SessionListener.java b/src/main/java/org/codehaus/plexus/build/connect/SessionListener.java deleted file mode 100644 index c8cea4c..0000000 --- a/src/main/java/org/codehaus/plexus/build/connect/SessionListener.java +++ /dev/null @@ -1,65 +0,0 @@ -/* -Copyright (c) 2025 Christoph Läubrich All rights reserved. - -This program is licensed to you under the Apache License Version 2.0, -and you may not use this file except in compliance with the Apache License Version 2.0. -You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. - -Unless required by applicable law or agreed to in writing, -software distributed under the Apache License Version 2.0 is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. -*/ -package org.codehaus.plexus.build.connect; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.apache.maven.AbstractMavenLifecycleParticipant; -import org.apache.maven.MavenExecutionException; -import org.apache.maven.execution.MavenSession; -import org.codehaus.plexus.build.connect.messages.Message; -import org.codehaus.plexus.build.connect.messages.ProjectsReadMessage; -import org.codehaus.plexus.build.connect.messages.SessionMessage; - -/** - * Listen to session events and send them to the connection - */ -@Named -@Singleton -public class SessionListener extends AbstractMavenLifecycleParticipant { - - @Inject - private BuildConnection connection; - - private boolean sendProjects; - private boolean started; - - @Override - public void afterSessionStart(MavenSession session) throws MavenExecutionException { - started = true; - Message reply = connection.send(new SessionMessage(session, true)); - if (reply != null) { - sendProjects = Configuration.of(reply).isSendProjects(); - } - } - - @Override - public void afterProjectsRead(MavenSession session) throws MavenExecutionException { - if (connection.isEnabled()) { - if (!started) { - afterSessionStart(session); - } - if (sendProjects) { - connection.send(new ProjectsReadMessage(session.getAllProjects())); - } - } - } - - @Override - public void afterSessionEnd(MavenSession session) throws MavenExecutionException { - connection.send(new SessionMessage(session, false)); - started = false; - } -} diff --git a/src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java b/src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java index b9b1a95..339487e 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java +++ b/src/main/java/org/codehaus/plexus/build/connect/TcpBuildConnection.java @@ -12,7 +12,6 @@ */ package org.codehaus.plexus.build.connect; -import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -25,16 +24,16 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.UUID; +import java.util.WeakHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; import java.util.function.Function; -import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.execution.MavenSession; import org.codehaus.plexus.build.connect.messages.Message; -import org.codehaus.plexus.build.connect.messages.SessionMessage; /** * Default implementation using the system property @@ -48,10 +47,7 @@ public class TcpBuildConnection implements BuildConnection { private static final int PORT = Integer.getInteger(PLEXUS_BUILD_IPC_PORT, 0); - @Inject - private LegacySupport support; - - private Map configMap = new ConcurrentHashMap<>(); + private final Map sessionMap = new WeakHashMap<>(); private final ThreadLocal connections = ThreadLocal.withInitial(() -> new TcpClientConnection()); @@ -62,47 +58,23 @@ public boolean isEnabled() { } @Override - public Message send(Message message) { + public Message send(Message message, MavenSession mavenSession) { if (isEnabled()) { - String sessionId; - boolean sessionStart; - if (message instanceof SessionMessage) { - sessionId = message.getSessionId(); - sessionStart = ((SessionMessage) message).isSessionStart(); - } else { - sessionId = getThreadSessionId(); - sessionStart = false; - } + String sessionId = getId(mavenSession); byte[] messageBytes = message.serialize(sessionId); byte[] replyBytes = connections.get().send(messageBytes); if (replyBytes.length > 0) { - Message reply = Message.decode(replyBytes); - if (reply != null && sessionStart) { - configMap.put(sessionId, Configuration.of(reply)); - } - return reply; + return Message.decode(replyBytes); } } return null; } - private String getThreadSessionId() { - // We must use LegacySupport here to get the currents threads session (what - // might be cloned) - return SessionMessage.getId(support.getSession()); - } - - @Override - public Configuration getConfiguration() { - String id = getThreadSessionId(); - if (id == null) { - throw new IllegalStateException("No session attached to current thread!"); - } - Configuration configuration = configMap.get(id); - if (configuration == null) { - throw new IllegalStateException("No configuration active for session " + id + "!"); + private synchronized String getId(MavenSession session) { + if (session == null) { + return Thread.currentThread().getName(); } - return configuration; + return sessionMap.computeIfAbsent(session, x -> UUID.randomUUID().toString()); } /** diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java new file mode 100644 index 0000000..7239b92 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java @@ -0,0 +1,34 @@ +/* +Copyright (c) 2025 Christoph Läubrich All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.connect.messages; + +import java.util.Map; + +/** + * Message send to init the inital communication with the endpoints + */ +public class InitMessage extends Message { + + /** + * Creates a message with inital information about the running maven system + * + * @param settings the context settings to send to the endpoint + */ + public InitMessage(Map settings) { + super(settings); + } + + InitMessage(String sessionId, long threadId, Map payload) { + super(sessionId, threadId, payload); + } +} diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java b/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java index af47e5d..4400f22 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java @@ -25,6 +25,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Stream; /** * A message exchanged between two endpoints, usually an IDE and a maven build @@ -52,6 +53,13 @@ protected Long initialValue() { this.threadId = threadId; } + /** + * @return the keys stored in this message + */ + public Stream keys() { + return properties.keySet().stream(); + } + /** * Get a String property from the payload * @@ -187,12 +195,18 @@ public static Message decode(byte[] bytes) { if ("SessionMessage".equals(messageType)) { return new SessionMessage(sessionId, threadId, payload); } - if ("ProjectsReadMessage".equals(messageType)) { - return new ProjectsReadMessage(sessionId, threadId, payload); + if ("ProjectsMessage".equals(messageType)) { + return new ProjectsMessage(sessionId, threadId, payload); } if ("RefreshMessage".equals(messageType)) { return new RefreshMessage(sessionId, threadId, payload); } + if ("InitMessage".equals(messageType)) { + return new InitMessage(sessionId, threadId, payload); + } + if ("ProjectMessage".equals(messageType)) { + return new ProjectMessage(sessionId, threadId, payload); + } return new Message(sessionId, threadId, payload); } catch (IOException e) { // should never happen, but if it happens something is wrong! diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java new file mode 100644 index 0000000..68a30fd --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java @@ -0,0 +1,122 @@ +/* +Copyright (c) 2025 Christoph Läubrich All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.connect.messages; + +import java.io.File; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +import org.apache.maven.execution.ExecutionEvent.Type; +import org.apache.maven.project.MavenProject; + +/** + * Send to inform about project changes + */ +public class ProjectMessage extends Message { + + private static final String BASE_DIR = "baseDir"; + private static final String VERSION = "version"; + private static final String ARTIFACT_ID = "artifactId"; + private static final String GROUP_ID = "groupId"; + private static final String EVENT_TYPE = "eventType"; + + ProjectMessage(String sessionId, long threadId, Map payload) { + super(sessionId, threadId, payload); + } + + /** + * Constructs a new event based on project and type + * + * @param mavenProject + * @param eventtype + */ + public ProjectMessage(MavenProject mavenProject, Type eventtype) { + super(toMap(mavenProject, eventtype)); + } + + /** + * @return the group id of the project + */ + public String getGroupId() { + return getProperty(GROUP_ID); + } + + /** + * @return the artifact id of the project + */ + public String getArtifactId() { + return getProperty(ARTIFACT_ID); + } + + /** + * @return the version of the project + */ + public String getVersion() { + return getProperty(ARTIFACT_ID); + } + + /** + * @return the basedir of the project + */ + public Path getBaseDir() { + return new File(getProperty(BASE_DIR)).toPath(); + } + + /** + * @return the type of the event + */ + public EventType getType() { + try { + return EventType.valueOf(getProperty(EVENT_TYPE)); + } catch (RuntimeException e) { + return EventType.Unknown; + } + } + + private static Map toMap(MavenProject mavenProject, Type eventtype) { + Map map = new HashMap<>(); + map.put(EVENT_TYPE, eventtype.name()); + map.put(GROUP_ID, mavenProject.getGroupId()); + map.put(ARTIFACT_ID, mavenProject.getArtifactId()); + map.put(VERSION, mavenProject.getVersion()); + map.put(BASE_DIR, mavenProject.getBasedir().getAbsolutePath()); + return map; + } + + /** + * Describe the type of the event + */ + public enum EventType { + /** + * The project was started + */ + ProjectStarted, + /** + * The project failed + */ + ProjectFailed, + /** + * The project was skipped + */ + ProjectSkipped, + /** + * the project succeed + */ + ProjectSucceeded, + /** + * the type of event is unknown + */ + Unknown; + } +} diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsMessage.java new file mode 100644 index 0000000..93bfa5a --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsMessage.java @@ -0,0 +1,143 @@ +/* +Copyright (c) 2025 Christoph Läubrich All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.connect.messages; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.file.Path; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.DefaultModelWriter; +import org.apache.maven.project.MavenProject; + +/** + * Message send to inform about reactor project in the build and their effective + * model + */ +public class ProjectsMessage extends Message { + + private static final DefaultModelWriter MODEL_WRITER = new DefaultModelWriter(); + + ProjectsMessage(String sessionId, long threadId, Map payload) { + super(sessionId, threadId, payload); + } + + /** + * @param projects the projects to send + */ + public ProjectsMessage(Collection projects) { + super(buildMap(projects)); + } + + /** + * @return a stream of project infos + */ + public Stream projects() { + return keys().map(key -> { + String[] gav = key.split("\t"); + if (gav.length == 5 && "p".equals(gav[0])) { + ProjectInfo pi = new ProjectInfo() { + + @Override + public String getGroupId() { + return gav[1]; + } + + @Override + public String getArtifactId() { + return gav[2]; + } + + @Override + public String getVersion() { + return gav[3]; + } + + @Override + public String getModel() { + return getProperty(key); + } + + @Override + public Path getBaseDir() { + return new File(gav[4]).toPath(); + } + }; + return pi; + } + return null; + }) + .filter(Objects::nonNull); + } + + private static Map buildMap(Collection projects) { + Map map = new HashMap<>(); + for (MavenProject project : projects) { + String key = String.format( + "p\t%s\t%s\t%s\t%s", + project.getGroupId(), + project.getArtifactId(), + project.getVersion(), + project.getBasedir().getAbsolutePath()); + map.put(key, getEffectiveModel(project)); + } + return map; + } + + private static String getEffectiveModel(MavenProject project) { + Model model = project.getModel(); + StringWriter writer = new StringWriter(); + try { + MODEL_WRITER.write(writer, null, model); + } catch (IOException e) { + } + String string = writer.toString(); + return string; + } + + /** + * Holds basic project info + */ + public static interface ProjectInfo { + /** + * @return the group id of the reactor project + */ + String getGroupId(); + + /** + * @return the artifact id of the reactor project + */ + String getArtifactId(); + + /** + * @return the version of the reactor project + */ + String getVersion(); + + /** + * @return the effective model of the project + */ + String getModel(); + + /** + * @return the basedir of the project + */ + Path getBaseDir(); + } +} diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsReadMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsReadMessage.java deleted file mode 100644 index 91825ef..0000000 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectsReadMessage.java +++ /dev/null @@ -1,63 +0,0 @@ -/* -Copyright (c) 2025 Christoph Läubrich All rights reserved. - -This program is licensed to you under the Apache License Version 2.0, -and you may not use this file except in compliance with the Apache License Version 2.0. -You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. - -Unless required by applicable law or agreed to in writing, -software distributed under the Apache License Version 2.0 is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. -*/ -package org.codehaus.plexus.build.connect.messages; - -import java.io.IOException; -import java.io.StringWriter; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.maven.model.Model; -import org.apache.maven.model.io.DefaultModelWriter; -import org.apache.maven.project.MavenProject; - -/** - * Message send to inform about reactor project in the build and their effective - * model - */ -public class ProjectsReadMessage extends Message { - - private static final DefaultModelWriter MODEL_WRITER = new DefaultModelWriter(); - - ProjectsReadMessage(String sessionId, long threadId, Map payload) { - super(sessionId, threadId, payload); - } - - /** - * @param projects the projects to send - */ - public ProjectsReadMessage(Collection projects) { - super(buildMap(projects)); - } - - private static Map buildMap(Collection projects) { - Map map = new HashMap<>(); - for (MavenProject project : projects) { - String key = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion(); - map.put(key, getEffectiveModel(project)); - } - return map; - } - - private static String getEffectiveModel(MavenProject project) { - Model model = project.getModel(); - StringWriter writer = new StringWriter(); - try { - MODEL_WRITER.write(writer, null, model); - } catch (IOException e) { - } - String string = writer.toString(); - return string; - } -} diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/SessionMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/SessionMessage.java index f88d906..2f9e0a6 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/SessionMessage.java +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/SessionMessage.java @@ -14,10 +14,7 @@ import java.util.HashMap; import java.util.Map; -import java.util.UUID; -import java.util.WeakHashMap; -import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.execution.MavenSession; /** @@ -27,8 +24,6 @@ public class SessionMessage extends Message { private static final String SESSION_EXECUTION_ROOT_DIRECTORY = "sessionExecutionRootDirectory"; private static final String SESSION_START = "sessionStart"; - private static final String SESSION_ID = "sessionId"; - private static final Map ID_MAP = new WeakHashMap<>(); /** * Creates a new session message @@ -45,10 +40,6 @@ public SessionMessage(MavenSession session, boolean start) { super(sessionId, threadId, payload); } - public String getSessionId() { - return getProperty(SESSION_ID); - } - /** * @return true if this is a session start event */ @@ -63,26 +54,8 @@ public String getExecutionRootDirectory() { return getProperty(SESSION_EXECUTION_ROOT_DIRECTORY); } - /** - * Returns the unique ID for a session - * - * @param session the session to get an Id for - * @return the id of the session or the name of the current thread if the - * session is null - */ - public static synchronized String getId(MavenSession session) { - if (session == null) { - return Thread.currentThread().getName(); - } - // we can't use the session itself as a key, because sessions might be cloned, - // but the execution request should (hopefully) stay constant... - return ID_MAP.computeIfAbsent( - session.getRequest(), x -> UUID.randomUUID().toString()); - } - private static Map buildMap(MavenSession session, boolean start) { Map map = new HashMap<>(2); - map.put(SESSION_ID, getId(session)); map.put(SESSION_START, Boolean.toString(start)); map.put(SESSION_EXECUTION_ROOT_DIRECTORY, session.getExecutionRootDirectory()); return map;