|
14 | 14 | package org.eclipse.m2e.core.internal.embedder; |
15 | 15 |
|
16 | 16 | import org.eclipse.aether.DefaultRepositorySystemSession; |
| 17 | +import org.eclipse.aether.RepositorySystemSession; |
17 | 18 | import org.eclipse.aether.SessionData; |
18 | 19 | import org.eclipse.aether.transfer.TransferListener; |
19 | 20 |
|
20 | 21 |
|
21 | 22 | /** |
22 | | - * FilterRepositorySystemSession implementation that allows setting of some/relevant session attributes. |
23 | | - * |
24 | | - * @since 1.4 |
| 23 | + * A forwarding {@link RepositorySystemSession} implementation that allows overriding specific session attributes. |
| 24 | + * <p> |
| 25 | + * This class extends {@link org.eclipse.aether.AbstractForwardingRepositorySystemSession} to provide a filtered view |
| 26 | + * of a repository system session, allowing selective modification of session properties such as update policy, |
| 27 | + * transfer listener, and session data while delegating all other operations to the wrapped session. |
| 28 | + * </p> |
| 29 | + * <p> |
| 30 | + * For mutable properties like {@link TransferListener} and {@link SessionData}, this class attempts to forward |
| 31 | + * modifications to the underlying session if it is a {@link DefaultRepositorySystemSession}. If forwarding fails or |
| 32 | + * the underlying session is immutable, the values are stored locally and returned by the corresponding getter methods. |
| 33 | + * </p> |
25 | 34 | */ |
26 | 35 | class FilterRepositorySystemSession extends org.eclipse.aether.AbstractForwardingRepositorySystemSession { |
27 | 36 |
|
| 37 | + /** |
| 38 | + * The custom update policy to override the session's default update policy, or {@code null} to use the session's |
| 39 | + * policy. |
| 40 | + */ |
28 | 41 | private final String updatePolicy; |
29 | 42 |
|
30 | | - private final DefaultRepositorySystemSession session; |
| 43 | + /** |
| 44 | + * The underlying repository system session that this instance wraps and delegates to. |
| 45 | + */ |
| 46 | + private final RepositorySystemSession session; |
| 47 | + |
| 48 | + /** |
| 49 | + * The custom transfer listener, or {@code null} if the session's transfer listener should be used. |
| 50 | + */ |
| 51 | + private TransferListener transferListener; |
| 52 | + |
| 53 | + /** |
| 54 | + * The custom session data, or {@code null} if the session's data should be used. |
| 55 | + */ |
| 56 | + private SessionData data; |
31 | 57 |
|
32 | | - public FilterRepositorySystemSession(DefaultRepositorySystemSession session, String updatePolicy) { |
| 58 | + /** |
| 59 | + * Creates a new filtering repository system session. |
| 60 | + * |
| 61 | + * @param session the underlying repository system session to wrap |
| 62 | + * @param updatePolicy the custom update policy to use, or {@code null} to use the session's default policy |
| 63 | + */ |
| 64 | + FilterRepositorySystemSession(RepositorySystemSession session, String updatePolicy) { |
33 | 65 | this.session = session; |
34 | 66 | this.updatePolicy = updatePolicy; |
35 | 67 | } |
36 | 68 |
|
| 69 | + /** |
| 70 | + * Returns the update policy for this session. |
| 71 | + * |
| 72 | + * @return the custom update policy if set, otherwise the policy from the underlying session |
| 73 | + */ |
37 | 74 | @Override |
38 | 75 | public String getUpdatePolicy() { |
39 | 76 | return updatePolicy != null ? updatePolicy : super.getUpdatePolicy(); |
40 | 77 | } |
41 | 78 |
|
42 | | - public TransferListener setTransferListener(TransferListener transferListener) { |
43 | | - DefaultRepositorySystemSession session = getSession(); |
44 | | - TransferListener origTransferListener = session.getTransferListener(); |
45 | | - session.setTransferListener(transferListener); |
| 79 | + /** |
| 80 | + * Returns the transfer listener for this session. |
| 81 | + * |
| 82 | + * @return the custom transfer listener if set, otherwise the listener from the underlying session |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public TransferListener getTransferListener() { |
| 86 | + if(this.transferListener == null) { |
| 87 | + // No custom value stored, use the listener from the underlying session |
| 88 | + return session.getTransferListener(); |
| 89 | + } |
| 90 | + // Return the custom listener that could not be forwarded to the underlying session |
| 91 | + return transferListener; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns the session data for this session. |
| 96 | + * |
| 97 | + * @return the custom session data if set, otherwise the data from the underlying session |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public SessionData getData() { |
| 101 | + if(this.data == null) { |
| 102 | + // No custom value stored, use the data from the underlying session |
| 103 | + return session.getData(); |
| 104 | + } |
| 105 | + // Return the custom data that could not be forwarded to the underlying session |
| 106 | + return this.data; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets the transfer listener for this session. |
| 111 | + * <p> |
| 112 | + * This method attempts to forward the transfer listener to the underlying session if it is a |
| 113 | + * {@link DefaultRepositorySystemSession}. If the underlying session is immutable or the forwarding fails, the |
| 114 | + * listener is stored locally and will be returned by {@link #getTransferListener()}. |
| 115 | + * </p> |
| 116 | + * |
| 117 | + * @param transferListener the transfer listener to set |
| 118 | + * @return the previous transfer listener |
| 119 | + */ |
| 120 | + TransferListener setTransferListener(TransferListener transferListener) { |
| 121 | + TransferListener origTransferListener = getTransferListener(); |
| 122 | + if(session instanceof DefaultRepositorySystemSession def) { |
| 123 | + // Attempt to forward to the underlying session if mutable |
| 124 | + def.setTransferListener(transferListener); |
| 125 | + } |
| 126 | + if(transferListener == session.getTransferListener()) { |
| 127 | + // Forwarding was successful or the same value already exists in the session, clear local override |
| 128 | + this.transferListener = null; |
| 129 | + } else { |
| 130 | + // Forwarding failed or session is immutable, store locally to return from getter |
| 131 | + this.transferListener = transferListener; |
| 132 | + } |
46 | 133 | return origTransferListener; |
47 | 134 | } |
48 | 135 |
|
49 | | - public SessionData setData(SessionData data) { |
50 | | - DefaultRepositorySystemSession session = getSession(); |
51 | | - SessionData origSessionData = session.getData(); |
52 | | - session.setData(data); |
| 136 | + /** |
| 137 | + * Sets the session data for this session. |
| 138 | + * <p> |
| 139 | + * This method attempts to forward the session data to the underlying session if it is a |
| 140 | + * {@link DefaultRepositorySystemSession}. If the underlying session is immutable or the forwarding fails, the data |
| 141 | + * is stored locally and will be returned by {@link #getData()}. |
| 142 | + * </p> |
| 143 | + * |
| 144 | + * @param data the session data to set |
| 145 | + * @return the previous session data |
| 146 | + */ |
| 147 | + SessionData setData(SessionData data) { |
| 148 | + SessionData origSessionData = getData(); |
| 149 | + if(session instanceof DefaultRepositorySystemSession def) { |
| 150 | + // Attempt to forward to the underlying session if mutable |
| 151 | + def.setData(data); |
| 152 | + } |
| 153 | + if(data == session.getData()) { |
| 154 | + // Forwarding was successful or the same value already exists in the session, clear local override |
| 155 | + this.data = null; |
| 156 | + } else { |
| 157 | + // Forwarding failed or session is immutable, store locally to return from getter |
| 158 | + this.data = data; |
| 159 | + } |
53 | 160 | return origSessionData; |
54 | 161 | } |
55 | 162 |
|
| 163 | + /** |
| 164 | + * Returns the underlying repository system session that this instance wraps. |
| 165 | + * |
| 166 | + * @return the wrapped repository system session |
| 167 | + */ |
56 | 168 | @Override |
57 | | - protected DefaultRepositorySystemSession getSession() { |
| 169 | + protected RepositorySystemSession getSession() { |
58 | 170 | return this.session; |
59 | 171 | } |
60 | 172 | } |
0 commit comments