4040import java .util .concurrent .atomic .AtomicInteger ;
4141import java .util .stream .Collectors ;
4242import org .apache .maven .model .Model ;
43+ import org .apache .maven .rtinfo .RuntimeInformation ;
4344import org .eclipse .aether .DefaultRepositorySystemSession ;
4445import org .eclipse .aether .artifact .Artifact ;
4546import org .eclipse .aether .repository .RemoteRepository ;
4647
4748public class DefaultSession extends CloseableConfigSupport <SessionConfig > implements Session {
48- private final String sessionBoundStoreKey ;
49+ private final RuntimeInformation mavenRuntimeInformation ;
50+ private final String sessionBoundStoresKey ;
4951 private final InternalArtifactStoreManager internalArtifactStoreManager ;
5052 private final ArtifactStoreWriter artifactStoreWriter ;
5153 private final ArtifactStoreMerger artifactStoreMerger ;
@@ -56,6 +58,7 @@ public class DefaultSession extends CloseableConfigSupport<SessionConfig> implem
5658
5759 public DefaultSession (
5860 SessionConfig sessionConfig ,
61+ RuntimeInformation mavenRuntimeInformation ,
5962 InternalArtifactStoreManagerFactory internalArtifactStoreManagerFactory ,
6063 ArtifactStoreWriterFactory artifactStoreWriterFactory ,
6164 ArtifactStoreMergerFactory artifactStoreMergerFactory ,
@@ -64,7 +67,8 @@ public DefaultSession(
6467 Map <String , ArtifactStoreComparatorFactory > artifactStoreComparatorFactories ,
6568 MavenModelReaderImpl mavenModelReader ) {
6669 super (sessionConfig );
67- this .sessionBoundStoreKey = Session .class .getName () + "." + ArtifactStore .class + "." + UUID .randomUUID ();
70+ this .mavenRuntimeInformation = requireNonNull (mavenRuntimeInformation );
71+ this .sessionBoundStoresKey = Session .class .getName () + "." + ArtifactStore .class + "." + UUID .randomUUID ();
6872 this .internalArtifactStoreManager = internalArtifactStoreManagerFactory .create (sessionConfig );
6973 this .artifactStoreWriter = requireNonNull (artifactStoreWriterFactory ).create (sessionConfig );
7074 this .artifactStoreMerger = requireNonNull (artifactStoreMergerFactory ).create (sessionConfig );
@@ -181,12 +185,34 @@ public ArtifactStoreTemplate selectSessionArtifactStoreTemplate(String uri) {
181185 }
182186 }
183187
188+ @ Override
189+ public boolean handleRemoteRepository (RemoteRepository repository ) {
190+ requireNonNull (repository );
191+ checkClosed ();
192+ if (mavenRuntimeInformation .isMavenVersion ("[4.0.0-rc-5,4.0.0-rc-5]" )) {
193+ // BEGIN: workaround for Maven 4.0.0-rc-5 (only)
194+ // IF: repoId is in form '[some string]-hex' (hex part is 40 char long; sha1)
195+ // THEN: chop it off, and check if "some string" ID repo is already present
196+ if (repository .getId ().length () > 41
197+ && repository
198+ .getId ()
199+ .substring (repository .getId ().length () - 41 )
200+ .matches ("-[0-9a-f]+" )) {
201+ String id = repository .getId ().substring (0 , repository .getId ().length () - 41 );
202+ return getSessionBoundStores ().keySet ().stream ().noneMatch (r -> id .equals (r .getId ()));
203+ }
204+ // END: workaround for Maven 4.0.0-rc-5 (only)
205+ }
206+ return true ;
207+ }
208+
184209 @ Override
185210 public ArtifactStore getOrCreateSessionArtifactStore (RemoteRepository repository , String uri ) {
211+ requireNonNull (repository );
186212 requireNonNull (uri );
187213 checkClosed ();
188- ConcurrentMap <RemoteRepository , String > sessionBoundStore = getSessionBoundStore ();
189- String storeName = sessionBoundStore .computeIfAbsent (repository , k -> {
214+ ConcurrentMap <RemoteRepository , String > sessionBoundStores = getSessionBoundStores ();
215+ String storeName = sessionBoundStores .computeIfAbsent (repository , r -> {
190216 try {
191217 String artifactStoreName ;
192218 boolean isNewArtifactStore = true ;
@@ -223,7 +249,9 @@ public ArtifactStore getOrCreateSessionArtifactStore(RemoteRepository repository
223249 } else {
224250 throw new IllegalArgumentException ("Invalid repository URI: " + uri );
225251 }
226- logger .info ("Created Njord artifact store {} for remote repository {}" , artifactStoreName , repository );
252+ if (isNewArtifactStore ) {
253+ logger .info ("Created Njord artifact store {} for remote repository {}" , artifactStoreName , r );
254+ }
227255 return artifactStoreName ;
228256 } catch (IOException e ) {
229257 throw new UncheckedIOException (e );
@@ -264,8 +292,8 @@ private String createUsingTemplate(String templateName) throws IOException {
264292 @ Override
265293 public int publishSessionArtifactStores () throws IOException {
266294 checkClosed ();
267- ConcurrentMap <RemoteRepository , String > sessionBoundStore = getSessionBoundStore ();
268- if (sessionBoundStore .isEmpty ()) {
295+ ConcurrentMap <RemoteRepository , String > sessionBoundStores = getSessionBoundStores ();
296+ if (sessionBoundStores .isEmpty ()) {
269297 return 0 ;
270298 }
271299 AtomicInteger result = new AtomicInteger (0 );
@@ -275,7 +303,7 @@ public int publishSessionArtifactStores() throws IOException {
275303 Optional <ArtifactStorePublisher > po = selectArtifactStorePublisher (publisherName );
276304 if (po .isPresent ()) {
277305 ArtifactStorePublisher p = po .orElseThrow (J8Utils .OET );
278- for (String storeName : sessionBoundStore .values ()) {
306+ for (String storeName : sessionBoundStores .values ()) {
279307 logger .info ("Publishing {} with {}" , storeName , publisherName );
280308 try (ArtifactStore as = internalArtifactStoreManager
281309 .selectArtifactStore (storeName )
@@ -300,12 +328,12 @@ public int publishSessionArtifactStores() throws IOException {
300328 @ Override
301329 public int dropSessionArtifactStores () {
302330 checkClosed ();
303- ConcurrentMap <RemoteRepository , String > sessionBoundStore = getSessionBoundStore ();
304- if (sessionBoundStore .isEmpty ()) {
331+ ConcurrentMap <RemoteRepository , String > sessionBoundStores = getSessionBoundStores ();
332+ if (sessionBoundStores .isEmpty ()) {
305333 return 0 ;
306334 }
307335 AtomicInteger result = new AtomicInteger (0 );
308- for (String storeName : sessionBoundStore .values ()) {
336+ for (String storeName : sessionBoundStores .values ()) {
309337 try {
310338 if (internalArtifactStoreManager .dropArtifactStore (storeName )) {
311339 result .addAndGet (1 );
@@ -326,8 +354,8 @@ protected void doClose() throws IOException {
326354 * Returns map of "Njord URI" to "storeName" that were created in current session.
327355 */
328356 @ SuppressWarnings ("unchecked" )
329- private ConcurrentMap <RemoteRepository , String > getSessionBoundStore () {
357+ private ConcurrentMap <RemoteRepository , String > getSessionBoundStores () {
330358 return (ConcurrentHashMap <RemoteRepository , String >)
331- config .session ().getData ().computeIfAbsent (sessionBoundStoreKey , ConcurrentHashMap ::new );
359+ config .session ().getData ().computeIfAbsent (sessionBoundStoresKey , ConcurrentHashMap ::new );
332360 }
333361}
0 commit comments