Skip to content

Commit

Permalink
Quick Fix for quarkusio/quarkus#24285
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 2 deletions.
11 changes: 9 additions & 2 deletions maven-core/src/main/java/org/apache/maven/DefaultMaven.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.repository.LocalRepositoryNotAccessibleException;
import org.apache.maven.repository.internal.MavenChainedWorkspaceReader;
import org.apache.maven.session.scope.internal.SessionScope;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.annotations.Component;
Expand All @@ -58,7 +59,6 @@
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.util.repository.ChainedWorkspaceReader;

/**
* @author Jason van Zyl
Expand Down Expand Up @@ -339,7 +339,14 @@ private void setupWorkspaceReader( MavenSession session, DefaultRepositorySystem
workspaceReaders.add( workspaceReader );
}
WorkspaceReader[] readers = workspaceReaders.toArray( new WorkspaceReader[0] );
repoSession.setWorkspaceReader( new ChainedWorkspaceReader( readers ) );
if ( readers.length == 1 )
{
repoSession.setWorkspaceReader( readers[0] );
}
else
{
repoSession.setWorkspaceReader( new MavenChainedWorkspaceReader( readers ) );
}

}

Expand Down
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;
}

}

0 comments on commit 5c3939b

Please sign in to comment.