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.transport.wagon;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.Objects;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.repository.RemoteRepository;
028import org.eclipse.aether.spi.connector.transport.Transporter;
029import org.eclipse.aether.spi.connector.transport.TransporterFactory;
030import org.eclipse.aether.spi.locator.Service;
031import org.eclipse.aether.spi.locator.ServiceLocator;
032import org.eclipse.aether.transfer.NoTransporterException;
033
034/**
035 * A transporter factory using <a href="http://maven.apache.org/wagon/" target="_blank">Apache Maven Wagon</a>. Note
036 * that this factory merely serves as an adapter to the Wagon API and by itself does not provide any transport services
037 * unless one or more wagon implementations are registered with the {@link WagonProvider}.
038 */
039@Named("wagon")
040public final class WagonTransporterFactory implements TransporterFactory, Service {
041
042    private WagonProvider wagonProvider;
043
044    private WagonConfigurator wagonConfigurator;
045
046    private float priority = -1.0f;
047
048    /**
049     * Creates an (uninitialized) instance of this transporter factory. <em>Note:</em> In case of manual instantiation
050     * by clients, the new factory needs to be configured via its various mutators before first use or runtime errors
051     * will occur.
052     */
053    public WagonTransporterFactory() {
054        // enables default constructor
055    }
056
057    @Inject
058    WagonTransporterFactory(WagonProvider wagonProvider, WagonConfigurator wagonConfigurator) {
059        setWagonProvider(wagonProvider);
060        setWagonConfigurator(wagonConfigurator);
061    }
062
063    @Override
064    public void initService(ServiceLocator locator) {
065        setWagonProvider(locator.getService(WagonProvider.class));
066        setWagonConfigurator(locator.getService(WagonConfigurator.class));
067    }
068
069    /**
070     * Sets the wagon provider to use to acquire and release wagon instances.
071     *
072     * @param wagonProvider The wagon provider to use, may be {@code null}.
073     * @return This factory for chaining, never {@code null}.
074     */
075    public WagonTransporterFactory setWagonProvider(WagonProvider wagonProvider) {
076        this.wagonProvider = wagonProvider;
077        return this;
078    }
079
080    /**
081     * Sets the wagon configurator to use to apply provider-specific configuration to wagon instances.
082     *
083     * @param wagonConfigurator The wagon configurator to use, may be {@code null}.
084     * @return This factory for chaining, never {@code null}.
085     */
086    public WagonTransporterFactory setWagonConfigurator(WagonConfigurator wagonConfigurator) {
087        this.wagonConfigurator = wagonConfigurator;
088        return this;
089    }
090
091    @Override
092    public float getPriority() {
093        return priority;
094    }
095
096    /**
097     * Sets the priority of this component.
098     *
099     * @param priority The priority.
100     * @return This component for chaining, never {@code null}.
101     */
102    public WagonTransporterFactory setPriority(float priority) {
103        this.priority = priority;
104        return this;
105    }
106
107    @Override
108    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
109            throws NoTransporterException {
110        Objects.requireNonNull(session, "session cannot be null");
111        Objects.requireNonNull(repository, "repository cannot be null");
112
113        return new WagonTransporter(wagonProvider, wagonConfigurator, repository, session);
114    }
115}