Skip to content

Commit

Permalink
[MRESOLVER-266] Simplify named lock adapter creation and align config…
Browse files Browse the repository at this point in the history
…uration

Simplified adapter creation and now as everything else in Resolver, session
config properties are used as configuration source, not Java system
properties.

Important: This change also drops internal/impl things. Therefore, this is a
breakage, IF someone uses internal/private/impl details from Resolver
(mvnd does, but is fine, is our kiddo), and for the rest of the worlds:
hands off, please.

This closes #196
  • Loading branch information
cstamas authored and michael-o committed Sep 24, 2022
1 parent 9ca3751 commit 510dcd3
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 257 deletions.
6 changes: 3 additions & 3 deletions maven-resolver-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelector;
import org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector;
import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
Expand Down Expand Up @@ -227,7 +225,6 @@ public DefaultServiceLocator()
addService( LocalRepositoryManagerFactory.class, EnhancedLocalRepositoryManagerFactory.class );
addService( LoggerFactory.class, Slf4jLoggerFactory.class );
addService( TrackingFileManager.class, DefaultTrackingFileManager.class );
addService( NamedLockFactorySelector.class, SimpleNamedLockFactorySelector.class );
addService( ChecksumAlgorithmFactorySelector.class, DefaultChecksumAlgorithmFactorySelector.class );
addService( LocalPathComposer.class, DefaultLocalPathComposer.class );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
import org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector;
import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector;
import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelector;
import org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector;
import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
Expand Down Expand Up @@ -203,7 +201,6 @@ protected void configure()
bind( ChecksumAlgorithmFactorySelector.class )
.to( DefaultChecksumAlgorithmFactorySelector.class ).in ( Singleton.class );

bind( NamedLockFactorySelector.class ).to( SimpleNamedLockFactorySelector.class ).in( Singleton.class );
bind( SyncContextFactory.class ).to( DefaultSyncContextFactory.class ).in( Singleton.class );
bind( org.eclipse.aether.impl.SyncContextFactory.class )
.to( org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,34 @@
* under the License.
*/

import java.util.Objects;

import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.SyncContext;
import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactoryAdapter;
import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelector;
import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
import org.eclipse.aether.named.NamedLockFactory;
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
import org.eclipse.aether.named.providers.NoopNamedLockFactory;
import org.eclipse.aether.spi.locator.Service;
import org.eclipse.aether.spi.locator.ServiceLocator;
import org.eclipse.aether.spi.synccontext.SyncContextFactory;
import org.eclipse.aether.util.ConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Objects.requireNonNull;

Expand All @@ -44,20 +58,41 @@
public final class DefaultSyncContextFactory
implements SyncContextFactory, Service
{
private NamedLockFactoryAdapter namedLockFactoryAdapter;
private static final Logger LOGGER = LoggerFactory.getLogger( DefaultSyncContextFactory.class );

private static final String ADAPTER_KEY = DefaultSyncContextFactory.class.getName() + ".adapter";

private static final String NAME_MAPPER_KEY = "aether.syncContext.named.nameMapper";

private static final String DEFAULT_NAME_MAPPER_NAME = GAVNameMapper.NAME;

private static final String FACTORY_KEY = "aether.syncContext.named.factory";

private static final String DEFAULT_FACTORY_NAME = LocalReadWriteLockNamedLockFactory.NAME;

private Map<String, NameMapper> nameMappers;

private Map<String, NamedLockFactory> namedLockFactories;

private final CopyOnWriteArrayList<NamedLockFactoryAdapter> createdAdapters = new CopyOnWriteArrayList<>();

/**
* Constructor used with DI, where factories are injected and selected based on key.
*/
@Inject
public DefaultSyncContextFactory( final NamedLockFactorySelector selector )
public DefaultSyncContextFactory( final Map<String, NameMapper> nameMappers,
final Map<String, NamedLockFactory> namedLockFactories )
{
this.namedLockFactoryAdapter = new NamedLockFactoryAdapter(
selector.getSelectedNameMapper(),
selector.getSelectedNamedLockFactory()
);
this.nameMappers = requireNonNull( nameMappers );
this.namedLockFactories = requireNonNull( namedLockFactories );
}

/**
* ServiceLocator default ctor.
*
* @deprecated Will be removed once ServiceLocator removed.
*/
@Deprecated
public DefaultSyncContextFactory()
{
// ctor for ServiceLoader
Expand All @@ -66,24 +101,69 @@ public DefaultSyncContextFactory()
@Override
public void initService( final ServiceLocator locator )
{
NamedLockFactorySelector selector = Objects.requireNonNull(
locator.getService( NamedLockFactorySelector.class ) );
this.namedLockFactoryAdapter = new NamedLockFactoryAdapter(
selector.getSelectedNameMapper(),
selector.getSelectedNamedLockFactory()
);
HashMap<String, NameMapper> mappers = new HashMap<>();
mappers.put( StaticNameMapper.NAME, new StaticNameMapper() );
mappers.put( GAVNameMapper.NAME, new GAVNameMapper() );
mappers.put( DiscriminatingNameMapper.NAME, new DiscriminatingNameMapper( new GAVNameMapper() ) );
mappers.put( FileGAVNameMapper.NAME, new FileGAVNameMapper() );
this.nameMappers = mappers;

HashMap<String, NamedLockFactory> factories = new HashMap<>();
factories.put( NoopNamedLockFactory.NAME, new NoopNamedLockFactory() );
factories.put( LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory() );
factories.put( LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory() );
factories.put( FileLockNamedLockFactory.NAME, new FileLockNamedLockFactory() );
this.namedLockFactories = factories;
}

@Override
public SyncContext newInstance( final RepositorySystemSession session, final boolean shared )
{
requireNonNull( session, "session cannot be null" );
return namedLockFactoryAdapter.newInstance( session, shared );
NamedLockFactoryAdapter adapter =
(NamedLockFactoryAdapter) session.getData().computeIfAbsent(
ADAPTER_KEY,
() -> createAdapter( session )
);
return adapter.newInstance( session, shared );
}

private NamedLockFactoryAdapter createAdapter( final RepositorySystemSession session )
{
String nameMapperName = ConfigUtils.getString( session, DEFAULT_NAME_MAPPER_NAME, NAME_MAPPER_KEY );
String namedLockFactoryName = ConfigUtils.getString( session, DEFAULT_FACTORY_NAME, FACTORY_KEY );
NameMapper nameMapper = nameMappers.get( nameMapperName );
if ( nameMapper == null )
{
throw new IllegalArgumentException( "Unknown NameMapper name: " + nameMapperName
+ ", known ones: " + nameMappers.keySet() );
}
NamedLockFactory namedLockFactory = namedLockFactories.get( namedLockFactoryName );
if ( namedLockFactory == null )
{
throw new IllegalArgumentException( "Unknown NamedLockFactory name: " + namedLockFactoryName
+ ", known ones: " + namedLockFactories.keySet() );
}
NamedLockFactoryAdapter adapter = new NamedLockFactoryAdapter( nameMapper, namedLockFactory );
createdAdapters.add( adapter );
return adapter;
}

@PreDestroy
public void shutdown()
{
namedLockFactoryAdapter.shutdown();
LOGGER.debug( "Shutting down created adapters..." );
createdAdapters.forEach( adapter ->
{
try
{
adapter.shutdown();
}
catch ( Exception e )
{
LOGGER.warn( "Could not shutdown: {}", adapter, e );
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void shutdown()
namedLockFactory.shutdown();
}

public String toString()
{
return getClass().getSimpleName()
+ "(nameMapper=" + nameMapper
+ ", namedLockFactory=" + namedLockFactory
+ ")";
}

private static class AdaptedLockSyncContext implements SyncContext
{
private static final Logger LOGGER = LoggerFactory.getLogger( AdaptedLockSyncContext.class );
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 510dcd3

Please sign in to comment.