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 org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.repository.RemoteRepository;
026import org.eclipse.aether.spi.connector.transport.Transporter;
027import org.eclipse.aether.spi.connector.transport.TransporterFactory;
028import org.eclipse.aether.spi.locator.Service;
029import org.eclipse.aether.spi.locator.ServiceLocator;
030import org.eclipse.aether.transfer.NoTransporterException;
031
032import static java.util.Objects.requireNonNull;
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    @Deprecated
054    public WagonTransporterFactory() {
055        // enables default constructor
056    }
057
058    @Inject
059    public WagonTransporterFactory(WagonProvider wagonProvider, WagonConfigurator wagonConfigurator) {
060        setWagonProvider(wagonProvider);
061        setWagonConfigurator(wagonConfigurator);
062    }
063
064    @Override
065    public void initService(ServiceLocator locator) {
066        setWagonProvider(locator.getService(WagonProvider.class));
067        setWagonConfigurator(locator.getService(WagonConfigurator.class));
068    }
069
070    /**
071     * Sets the wagon provider to use to acquire and release wagon instances.
072     *
073     * @param wagonProvider The wagon provider to use, may be {@code null}.
074     * @return This factory for chaining, never {@code null}.
075     */
076    public WagonTransporterFactory setWagonProvider(WagonProvider wagonProvider) {
077        this.wagonProvider = wagonProvider;
078        return this;
079    }
080
081    /**
082     * Sets the wagon configurator to use to apply provider-specific configuration to wagon instances.
083     *
084     * @param wagonConfigurator The wagon configurator to use, may be {@code null}.
085     * @return This factory for chaining, never {@code null}.
086     */
087    public WagonTransporterFactory setWagonConfigurator(WagonConfigurator wagonConfigurator) {
088        this.wagonConfigurator = wagonConfigurator;
089        return this;
090    }
091
092    @Override
093    public float getPriority() {
094        return priority;
095    }
096
097    /**
098     * Sets the priority of this component.
099     *
100     * @param priority The priority.
101     * @return This component for chaining, never {@code null}.
102     */
103    public WagonTransporterFactory setPriority(float priority) {
104        this.priority = priority;
105        return this;
106    }
107
108    @Override
109    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
110            throws NoTransporterException {
111        requireNonNull(session, "session cannot be null");
112        requireNonNull(repository, "repository cannot be null");
113
114        return new WagonTransporter(wagonProvider, wagonConfigurator, repository, session);
115    }
116}