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 org.eclipse.aether.named.NamedLockFactory;
023
024import java.util.Map;
025
026/**
027 * Selector implementation support class: by extending this class one may override defaults, or provide completely
028 * alternative way of configuration. This implementation uses Java System properties to select factory and name mapper.
029 *
030 * @since 1.7.3
031 */
032public abstract class NamedLockFactorySelectorSupport
033    implements NamedLockFactorySelector
034{
035    public static final String FACTORY_KEY = "aether.syncContext.named.factory";
036
037    public static final String NAME_MAPPER_KEY = "aether.syncContext.named.nameMapper";
038
039    private final NamedLockFactory namedLockFactory;
040
041    private final NameMapper nameMapper;
042
043    public NamedLockFactorySelectorSupport( final Map<String, NamedLockFactory> factories,
044                                            final String defaultFactoryName,
045                                            final Map<String, NameMapper> nameMappers,
046                                            final String defaultNameMapperName )
047    {
048        this.namedLockFactory = selectNamedLockFactory( factories, getFactoryName( defaultFactoryName ) );
049        this.nameMapper = selectNameMapper( nameMappers, getNameMapperName( defaultNameMapperName ) );
050    }
051
052    /**
053     * Returns the selected {@link NamedLockFactory}, never null.
054     */
055    @Override
056    public NamedLockFactory getSelectedNamedLockFactory()
057    {
058        return namedLockFactory;
059    }
060
061    /**
062     * Returns the selected {@link NameMapper}, never null.
063     */
064    @Override
065    public NameMapper getSelectedNameMapper()
066    {
067        return nameMapper;
068    }
069
070    /**
071     * Returns selected factory name (or passed in default) using System property value of {@link #FACTORY_KEY}.
072     */
073    protected String getFactoryName( final String defaultFactoryName )
074    {
075        return System.getProperty( FACTORY_KEY, defaultFactoryName );
076    }
077
078    /**
079     * Returns selected name mapper name (or passed in default) using System property value of {@link #NAME_MAPPER_KEY}.
080     */
081    protected String getNameMapperName( final String defaultNameMapperName )
082    {
083        return System.getProperty( NAME_MAPPER_KEY, defaultNameMapperName );
084    }
085
086    private NamedLockFactory selectNamedLockFactory( final Map<String, NamedLockFactory> factories,
087                                                     final String factoryName )
088    {
089        NamedLockFactory factory = factories.get( factoryName );
090        if ( factory == null )
091        {
092            throw new IllegalArgumentException( "Unknown NamedLockFactory name: " + factoryName
093                + ", known ones: " + factories.keySet() );
094        }
095        return factory;
096    }
097
098    private NameMapper selectNameMapper( final Map<String, NameMapper> nameMappers,
099                                         final String mapperName )
100    {
101        NameMapper nameMapper = nameMappers.get( mapperName );
102        if ( nameMapper == null )
103        {
104            throw new IllegalArgumentException( "Unknown NameMapper name: " + mapperName
105                + ", known ones: " + nameMappers.keySet() );
106        }
107        return nameMapper;
108    }
109}