001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl.version; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import java.util.Collections; 026import java.util.Map; 027 028import org.eclipse.aether.ConfigurationProperties; 029import org.eclipse.aether.RepositorySystemSession; 030import org.eclipse.aether.spi.version.VersionSchemeSelector; 031import org.eclipse.aether.util.ConfigUtils; 032import org.eclipse.aether.version.VersionScheme; 033 034import static java.util.Objects.requireNonNull; 035 036/** 037 * Default implementation. 038 * 039 * @since 2.0.0 040 */ 041@Singleton 042@Named 043public class DefaultVersionSchemeSelector implements VersionSchemeSelector { 044 static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_AETHER + "versionScheme."; 045 046 /** 047 * The name of the version scheme to be used in session. 048 * 049 * @configurationSource {@link RepositorySystemSession#getConfigProperties()} 050 * @configurationType {@link java.lang.String} 051 * @configurationDefaultValue {@link #DEFAULT_VERSION_SCHEME_NAME} 052 */ 053 public static final String CONFIG_PROP_VERSION_SCHEME_NAME = CONFIG_PROPS_PREFIX + "name"; 054 055 public static final String DEFAULT_VERSION_SCHEME_NAME = GenericVersionSchemeProvider.NAME; 056 057 private final Map<String, VersionScheme> versionSchemes; 058 059 @Inject 060 public DefaultVersionSchemeSelector(Map<String, VersionScheme> versionSchemes) { 061 this.versionSchemes = requireNonNull(versionSchemes); 062 } 063 064 @Override 065 public VersionScheme selectVersionScheme(String schemeName) { 066 requireNonNull(schemeName, "null schemeName"); 067 VersionScheme versionScheme = versionSchemes.get(schemeName); 068 if (versionScheme == null) { 069 throw new IllegalArgumentException("scheme not supported"); 070 } 071 return versionScheme; 072 } 073 074 @Override 075 public VersionScheme selectVersionScheme(RepositorySystemSession session) { 076 return selectVersionScheme( 077 ConfigUtils.getString(session, DEFAULT_VERSION_SCHEME_NAME, CONFIG_PROP_VERSION_SCHEME_NAME)); 078 } 079 080 @Override 081 public Map<String, VersionScheme> getVersionSchemes() { 082 return Collections.unmodifiableMap(versionSchemes); 083 } 084}