001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import javax.inject.Inject;
023import javax.inject.Named;
024import javax.inject.Singleton;
025
026import java.util.Objects;
027
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.repository.LocalRepository;
030import org.eclipse.aether.repository.LocalRepositoryManager;
031import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
032import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
033import org.eclipse.aether.spi.locator.Service;
034import org.eclipse.aether.spi.locator.ServiceLocator;
035
036import static java.util.Objects.requireNonNull;
037
038/**
039 * Creates local repository managers for repository type {@code "simple"}.
040 */
041@Singleton
042@Named( "simple" )
043public class SimpleLocalRepositoryManagerFactory
044    implements LocalRepositoryManagerFactory, Service
045{
046    private float priority;
047
048    private LocalPathComposer localPathComposer;
049
050    public SimpleLocalRepositoryManagerFactory()
051    {
052        // enable no-arg constructor
053        this.localPathComposer = new DefaultLocalPathComposer(); // maven UTs needs this
054    }
055
056    @Inject
057    public SimpleLocalRepositoryManagerFactory( final LocalPathComposer localPathComposer )
058    {
059        this.localPathComposer = requireNonNull( localPathComposer );
060    }
061
062    @Override
063    public void initService( final ServiceLocator locator )
064    {
065        this.localPathComposer = Objects.requireNonNull( locator.getService( LocalPathComposer.class ) );
066    }
067
068    @Override
069    public LocalRepositoryManager newInstance( RepositorySystemSession session, LocalRepository repository )
070        throws NoLocalRepositoryManagerException
071    {
072        requireNonNull( session, "session cannot be null" );
073        requireNonNull( repository, "repository cannot be null" );
074
075        if ( "".equals( repository.getContentType() ) || "simple".equals( repository.getContentType() ) )
076        {
077            return new SimpleLocalRepositoryManager( repository.getBasedir(), "simple", localPathComposer );
078        }
079        else
080        {
081            throw new NoLocalRepositoryManagerException( repository );
082        }
083    }
084
085    @Override
086    public float getPriority()
087    {
088        return priority;
089    }
090
091    /**
092     * Sets the priority of this component.
093     *
094     * @param priority The priority.
095     * @return This component for chaining, never {@code null}.
096     */
097    public SimpleLocalRepositoryManagerFactory setPriority( float priority )
098    {
099        this.priority = priority;
100        return this;
101    }
102
103}