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;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.repository.LocalRepository;
027import org.eclipse.aether.repository.LocalRepositoryManager;
028import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
029import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
030import org.eclipse.aether.util.repository.RepositoryIdHelper;
031
032import static java.util.Objects.requireNonNull;
033
034/**
035 * Creates local repository managers for repository type {@code "simple"}.
036 */
037@Singleton
038@Named(SimpleLocalRepositoryManagerFactory.NAME)
039public class SimpleLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory {
040    public static final String NAME = "simple";
041    private float priority;
042
043    private final LocalPathComposer localPathComposer;
044
045    /**
046     * No-arg constructor, as "simple" local repository is meant mainly for use in tests.
047     */
048    public SimpleLocalRepositoryManagerFactory() {
049        this.localPathComposer = new DefaultLocalPathComposer();
050    }
051
052    @Inject
053    public SimpleLocalRepositoryManagerFactory(final LocalPathComposer localPathComposer) {
054        this.localPathComposer = requireNonNull(localPathComposer);
055    }
056
057    @Override
058    public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
059            throws NoLocalRepositoryManagerException {
060        requireNonNull(session, "session cannot be null");
061        requireNonNull(repository, "repository cannot be null");
062
063        if ("".equals(repository.getContentType()) || "simple".equals(repository.getContentType())) {
064            return new SimpleLocalRepositoryManager(
065                    repository.getBasePath(),
066                    "simple",
067                    localPathComposer,
068                    RepositoryIdHelper.cachedIdToPathSegment(session));
069        } else {
070            throw new NoLocalRepositoryManagerException(repository);
071        }
072    }
073
074    @Override
075    public float getPriority() {
076        return priority;
077    }
078
079    /**
080     * Sets the priority of this component.
081     *
082     * @param priority The priority.
083     * @return This component for chaining, never {@code null}.
084     */
085    public SimpleLocalRepositoryManagerFactory setPriority(float priority) {
086        this.priority = priority;
087        return this;
088    }
089}