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