001package org.eclipse.aether.internal.impl.synccontext.named;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import javax.inject.Inject;
023import javax.inject.Named;
024import javax.inject.Singleton;
025
026import java.util.HashMap;
027import java.util.Map;
028
029import org.eclipse.aether.internal.impl.synccontext.named.providers.DiscriminatingNameMapperProvider;
030import org.eclipse.aether.internal.impl.synccontext.named.providers.FileGAVNameMapperProvider;
031import org.eclipse.aether.internal.impl.synccontext.named.providers.FileHashingGAVNameMapperProvider;
032import org.eclipse.aether.internal.impl.synccontext.named.providers.GAVNameMapperProvider;
033import org.eclipse.aether.internal.impl.synccontext.named.providers.StaticNameMapperProvider;
034import org.eclipse.aether.named.NamedLockFactory;
035import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
036import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
037import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
038import org.eclipse.aether.named.providers.NoopNamedLockFactory;
039
040/**
041 * Parameterized selector implementation that selects based on injected parameters.
042 *
043 * @since 1.9.0
044 */
045@Singleton
046@Named
047public final class ParameterizedNamedLockFactorySelector
048        implements NamedLockFactorySelector
049{
050    private static final String FACTORY_KEY = "aether.syncContext.named.factory";
051
052    private static final String NAME_MAPPER_KEY = "aether.syncContext.named.nameMapper";
053
054    private static final Map<String, NamedLockFactory> FACTORIES;
055
056    private static final String DEFAULT_FACTORY = LocalReadWriteLockNamedLockFactory.NAME;
057
058    private static final Map<String, NameMapper> NAME_MAPPERS;
059
060    private static final String DEFAULT_NAME_MAPPER = GAVNameMapperProvider.NAME;
061
062    static
063    {
064        HashMap<String, NamedLockFactory> factories = new HashMap<>();
065        factories.put( NoopNamedLockFactory.NAME, new NoopNamedLockFactory() );
066        factories.put( LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory() );
067        factories.put( LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory() );
068        factories.put( FileLockNamedLockFactory.NAME, new FileLockNamedLockFactory() );
069        FACTORIES = factories;
070
071        HashMap<String, NameMapper> mappers = new HashMap<>();
072        mappers.put( StaticNameMapperProvider.NAME, new StaticNameMapperProvider().get() );
073        mappers.put( GAVNameMapperProvider.NAME, new GAVNameMapperProvider().get() );
074        mappers.put( DiscriminatingNameMapperProvider.NAME, new DiscriminatingNameMapperProvider().get() );
075        mappers.put( FileGAVNameMapperProvider.NAME, new FileGAVNameMapperProvider().get() );
076        mappers.put( FileHashingGAVNameMapperProvider.NAME, new FileHashingGAVNameMapperProvider().get() );
077        NAME_MAPPERS = mappers;
078    }
079
080    private final NamedLockFactory namedLockFactory;
081
082    private final NameMapper nameMapper;
083
084    /**
085     * Default constructor for non Eclipse Sisu uses.
086     */
087    public ParameterizedNamedLockFactorySelector()
088    {
089        this( FACTORIES, DEFAULT_FACTORY, NAME_MAPPERS, DEFAULT_NAME_MAPPER );
090    }
091
092    /**
093     * Constructor that uses Eclipse Sisu parameter injection.
094     */
095    @SuppressWarnings( "checkstyle:LineLength" )
096    @Inject
097    public ParameterizedNamedLockFactorySelector( final Map<String, NamedLockFactory> factories,
098                                                  @Named( "${" + FACTORY_KEY + ":-" + DEFAULT_FACTORY + "}" ) final String selectedFactoryName,
099                                                  final Map<String, NameMapper> nameMappers,
100                                                  @Named( "${" + NAME_MAPPER_KEY + ":-" + DEFAULT_NAME_MAPPER + "}" ) final String selectedMapperName )
101    {
102        this.namedLockFactory = selectNamedLockFactory( factories, selectedFactoryName );
103        this.nameMapper = selectNameMapper( nameMappers, selectedMapperName );
104    }
105
106    /**
107     * Returns the selected {@link NamedLockFactory}, never null.
108     */
109    @Override
110    public NamedLockFactory getSelectedNamedLockFactory()
111    {
112        return namedLockFactory;
113    }
114
115    /**
116     * Returns the selected {@link NameMapper}, never null.
117     */
118    @Override
119    public NameMapper getSelectedNameMapper()
120    {
121        return nameMapper;
122    }
123
124    private static NamedLockFactory selectNamedLockFactory( final Map<String, NamedLockFactory> factories,
125                                                     final String factoryName )
126    {
127        NamedLockFactory factory = factories.get( factoryName );
128        if ( factory == null )
129        {
130            throw new IllegalArgumentException( "Unknown NamedLockFactory name: " + factoryName
131                    + ", known ones: " + factories.keySet() );
132        }
133        return factory;
134    }
135
136    private static NameMapper selectNameMapper( final Map<String, NameMapper> nameMappers,
137                                         final String nameMapperName )
138    {
139        NameMapper nameMapper = nameMappers.get( nameMapperName );
140        if ( nameMapper == null )
141        {
142            throw new IllegalArgumentException( "Unknown NameMapper name: " + nameMapperName
143                    + ", known ones: " + nameMappers.keySet() );
144        }
145        return nameMapper;
146    }
147}