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