|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Christoph Läubrich and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.m2e.internal.maven.compat; |
| 15 | + |
| 16 | +import java.lang.reflect.Field; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import org.eclipse.aether.DefaultRepositorySystemSession; |
| 20 | +import org.eclipse.aether.RepositorySystemSession; |
| 21 | +import org.eclipse.aether.SessionData; |
| 22 | +import org.eclipse.aether.collection.DependencyGraphTransformer; |
| 23 | +import org.eclipse.aether.internal.impl.session.DefaultCloseableSession; |
| 24 | +import org.eclipse.aether.transfer.TransferListener; |
| 25 | + |
| 26 | +/** |
| 27 | + * Compatibility layer for RepositorySystemSession to handle Maven 3 and Maven 4 differences. |
| 28 | + * This class provides methods to work with session configuration without direct dependencies |
| 29 | + * on DefaultRepositorySystemSession. |
| 30 | + * <p> |
| 31 | + * Maven 4 uses org.eclipse.aether.internal.impl.session.DefaultCloseableSession which is final |
| 32 | + * and uses private final fields without setter methods, so field reflection is used for Maven 4 compatibility. |
| 33 | + */ |
| 34 | +public class RepositorySessionUtil { |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new mutable session based on the given session. |
| 38 | + * |
| 39 | + * @param session the base session to copy from |
| 40 | + * @return a new mutable session |
| 41 | + */ |
| 42 | + public static RepositorySystemSession newSession(RepositorySystemSession session) { |
| 43 | + return new DefaultRepositorySystemSession(session); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the dependency graph transformer on the session. |
| 49 | + * |
| 50 | + * @param session the session to modify |
| 51 | + * @param transformer the transformer to set |
| 52 | + */ |
| 53 | + public static void setDependencyGraphTransformer(RepositorySystemSession session, |
| 54 | + DependencyGraphTransformer transformer) { |
| 55 | + if (session instanceof DefaultRepositorySystemSession) { |
| 56 | + ((DefaultRepositorySystemSession) session).setDependencyGraphTransformer(transformer); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (session instanceof DefaultCloseableSession) { |
| 60 | + setFieldValue(session, "dependencyGraphTransformer", transformer); |
| 61 | + return; |
| 62 | + } |
| 63 | + throw new IllegalArgumentException("Unsupported session type: " + session.getClass().getName()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Sets a configuration property on the session. |
| 68 | + * |
| 69 | + * @param session the session to modify |
| 70 | + * @param key the configuration key |
| 71 | + * @param value the configuration value |
| 72 | + */ |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + public static void setConfigProperty(RepositorySystemSession session, String key, Object value) { |
| 75 | + if (session instanceof DefaultRepositorySystemSession) { |
| 76 | + ((DefaultRepositorySystemSession) session).setConfigProperty(key, value); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (session instanceof DefaultCloseableSession) { |
| 80 | + Map<String, Object> configProperties = (Map<String, Object>) getFieldValue(session, "configProperties"); |
| 81 | + configProperties.put(key, value); |
| 82 | + return; |
| 83 | + } |
| 84 | + throw new IllegalArgumentException("Unsupported session type: " + session.getClass().getName()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Sets the transfer listener on the session. |
| 89 | + * |
| 90 | + * @param session the session to modify |
| 91 | + * @param transferListener the transfer listener to set |
| 92 | + * @return the previous transfer listener |
| 93 | + */ |
| 94 | + public static TransferListener setTransferListener(RepositorySystemSession session, |
| 95 | + TransferListener transferListener) { |
| 96 | + if (session instanceof DefaultRepositorySystemSession) { |
| 97 | + DefaultRepositorySystemSession defaultSession = (DefaultRepositorySystemSession) session; |
| 98 | + TransferListener previous = defaultSession.getTransferListener(); |
| 99 | + defaultSession.setTransferListener(transferListener); |
| 100 | + return previous; |
| 101 | + } |
| 102 | + if (session instanceof DefaultCloseableSession) { |
| 103 | + TransferListener previous = session.getTransferListener(); |
| 104 | + setFieldValue(session, "transferListener", transferListener); |
| 105 | + return previous; |
| 106 | + } |
| 107 | + throw new IllegalArgumentException("Unsupported session type: " + session.getClass().getName()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the session data on the session. |
| 112 | + * |
| 113 | + * @param session the session to modify |
| 114 | + * @param data the session data to set |
| 115 | + * @return the previous session data |
| 116 | + */ |
| 117 | + public static SessionData setData(RepositorySystemSession session, SessionData data) { |
| 118 | + if (session instanceof DefaultRepositorySystemSession) { |
| 119 | + DefaultRepositorySystemSession defaultSession = (DefaultRepositorySystemSession) session; |
| 120 | + SessionData previous = defaultSession.getData(); |
| 121 | + defaultSession.setData(data); |
| 122 | + return previous; |
| 123 | + } |
| 124 | + if (session instanceof DefaultCloseableSession) { |
| 125 | + SessionData previous = session.getData(); |
| 126 | + setFieldValue(session, "data", data); |
| 127 | + return previous; |
| 128 | + } |
| 129 | + throw new IllegalArgumentException("Unsupported session type: " + session.getClass().getName()); |
| 130 | + } |
| 131 | + |
| 132 | + private static void setFieldValue(RepositorySystemSession session, String fieldName, Object value) { |
| 133 | + try { |
| 134 | + Field field = session.getClass().getDeclaredField(fieldName); |
| 135 | + field.setAccessible(true); |
| 136 | + field.set(session, value); |
| 137 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 138 | + throw new IllegalStateException( |
| 139 | + "Failed to set field " + fieldName + " on session type: " + session.getClass().getName(), e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static Object getFieldValue(RepositorySystemSession session, String fieldName) { |
| 144 | + try { |
| 145 | + Field field = session.getClass().getDeclaredField(fieldName); |
| 146 | + field.setAccessible(true); |
| 147 | + return field.get(session); |
| 148 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 149 | + throw new IllegalStateException( |
| 150 | + "Failed to get field " + fieldName + " on session type: " + session.getClass().getName(), e); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments