forked from apache/maven
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick Fix for quarkusio/quarkus#24285
Signed-off-by: Christoph Läubrich <christoph@laeubi-soft.de>
- Loading branch information
Christoph Läubrich
committed
Mar 15, 2022
1 parent
007ef38
commit 5c3939b
Showing
2 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
...vider/src/main/java/org/apache/maven/repository/internal/MavenChainedWorkspaceReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package org.apache.maven.repository.internal; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
|
||
import org.apache.maven.model.Model; | ||
import org.eclipse.aether.artifact.Artifact; | ||
import org.eclipse.aether.repository.WorkspaceReader; | ||
import org.eclipse.aether.repository.WorkspaceRepository; | ||
|
||
/** | ||
* A workspace reader that delegates to a chain of other readers, effectively aggregating their contents. | ||
*/ | ||
public final class MavenChainedWorkspaceReader | ||
implements MavenWorkspaceReader | ||
{ | ||
|
||
private List<WorkspaceReader> readers = new ArrayList<>(); | ||
|
||
private WorkspaceRepository repository; | ||
|
||
/** | ||
* Creates a new workspace reader by chaining the specified readers. | ||
* | ||
* @param readers The readers to chain, may be {@code null}. | ||
* @see #newInstance(WorkspaceReader, WorkspaceReader) | ||
*/ | ||
public MavenChainedWorkspaceReader( WorkspaceReader... readers ) | ||
{ | ||
if ( readers != null ) | ||
{ | ||
Collections.addAll( this.readers, readers ); | ||
} | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
for ( WorkspaceReader reader : this.readers ) | ||
{ | ||
if ( buffer.length() > 0 ) | ||
{ | ||
buffer.append( '+' ); | ||
} | ||
buffer.append( reader.getRepository().getContentType() ); | ||
} | ||
|
||
repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) ); | ||
} | ||
|
||
/** | ||
* Creates a new workspace reader by chaining the specified readers. In contrast to the constructor, this factory | ||
* method will avoid creating an actual chained reader if one of the specified readers is actually {@code null}. | ||
* | ||
* @param reader1 The first workspace reader, may be {@code null}. | ||
* @param reader2 The second workspace reader, may be {@code null}. | ||
* @return The chained reader or {@code null} if no workspace reader was supplied. | ||
*/ | ||
public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 ) | ||
{ | ||
if ( reader1 == null ) | ||
{ | ||
return reader2; | ||
} | ||
else if ( reader2 == null ) | ||
{ | ||
return reader1; | ||
} | ||
return new MavenChainedWorkspaceReader( reader1, reader2 ); | ||
} | ||
|
||
@Override | ||
public File findArtifact( Artifact artifact ) | ||
{ | ||
File file = null; | ||
|
||
for ( WorkspaceReader reader : readers ) | ||
{ | ||
file = reader.findArtifact( artifact ); | ||
if ( file != null ) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return file; | ||
} | ||
|
||
@Override | ||
public List<String> findVersions( Artifact artifact ) | ||
{ | ||
Collection<String> versions = new LinkedHashSet<>(); | ||
|
||
for ( WorkspaceReader reader : readers ) | ||
{ | ||
versions.addAll( reader.findVersions( artifact ) ); | ||
} | ||
|
||
return Collections.unmodifiableList( new ArrayList<>( versions ) ); | ||
} | ||
|
||
@Override | ||
public WorkspaceRepository getRepository() | ||
{ | ||
Key key = new Key( readers ); | ||
if ( !key.equals( repository.getKey() ) ) | ||
{ | ||
repository = new WorkspaceRepository( repository.getContentType(), key ); | ||
} | ||
return repository; | ||
} | ||
|
||
private static class Key | ||
{ | ||
|
||
private final List<Object> keys = new ArrayList<>(); | ||
|
||
Key( List<WorkspaceReader> readers ) | ||
{ | ||
for ( WorkspaceReader reader : readers ) | ||
{ | ||
keys.add( reader.getRepository().getKey() ); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals( Object obj ) | ||
{ | ||
if ( this == obj ) | ||
{ | ||
return true; | ||
} | ||
if ( obj == null || !getClass().equals( obj.getClass() ) ) | ||
{ | ||
return false; | ||
} | ||
return keys.equals( ( (Key) obj ).keys ); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
return keys.hashCode(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Model findModel( Artifact artifact ) | ||
{ | ||
for ( WorkspaceReader workspaceReader : readers ) | ||
{ | ||
if ( workspaceReader instanceof MavenWorkspaceReader ) | ||
{ | ||
Model model = ( (MavenWorkspaceReader) workspaceReader ).findModel( artifact ); | ||
if ( model != null ) | ||
{ | ||
return model; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |