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.locator.Service;
031import org.eclipse.aether.spi.locator.ServiceLocator;
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, Service {
041    public static final String NAME = "simple";
042    private float priority;
043
044    private LocalPathComposer localPathComposer;
045
046    @Deprecated
047    public SimpleLocalRepositoryManagerFactory() {
048        // enable no-arg constructor
049        this.localPathComposer = new DefaultLocalPathComposer(); // maven UTs needs this
050    }
051
052    @Inject
053    public SimpleLocalRepositoryManagerFactory(final LocalPathComposer localPathComposer) {
054        this.localPathComposer = requireNonNull(localPathComposer);
055    }
056
057    @Override
058    public void initService(final ServiceLocator locator) {
059        this.localPathComposer = requireNonNull(locator.getService(LocalPathComposer.class));
060    }
061
062    @Override
063    public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
064            throws NoLocalRepositoryManagerException {
065        requireNonNull(session, "session cannot be null");
066        requireNonNull(repository, "repository cannot be null");
067
068        if ("".equals(repository.getContentType()) || "simple".equals(repository.getContentType())) {
069            return new SimpleLocalRepositoryManager(repository.getBasedir(), "simple", localPathComposer);
070        } else {
071            throw new NoLocalRepositoryManagerException(repository);
072        }
073    }
074
075    @Override
076    public float getPriority() {
077        return priority;
078    }
079
080    /**
081     * Sets the priority of this component.
082     *
083     * @param priority The priority.
084     * @return This component for chaining, never {@code null}.
085     */
086    public SimpleLocalRepositoryManagerFactory setPriority(float priority) {
087        this.priority = priority;
088        return this;
089    }
090}