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 java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024import java.util.Map;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.deployment.DeployRequest;
029import org.eclipse.aether.impl.MetadataGenerator;
030import org.eclipse.aether.impl.MetadataGeneratorFactory;
031import org.eclipse.aether.impl.OfflineController;
032import org.eclipse.aether.installation.InstallRequest;
033import org.eclipse.aether.metadata.Metadata;
034import org.eclipse.aether.repository.RemoteRepository;
035import org.eclipse.aether.resolution.ResolutionErrorPolicy;
036import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
037import org.eclipse.aether.spi.artifact.decorator.ArtifactDecorator;
038import org.eclipse.aether.spi.artifact.decorator.ArtifactDecoratorFactory;
039import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator;
040import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory;
041import org.eclipse.aether.transfer.RepositoryOfflineException;
042
043/**
044 * Internal utility methods.
045 */
046public final class Utils {
047    private Utils() {}
048
049    private static PrioritizedComponents<ArtifactDecoratorFactory> sortArtifactDecoratorFactories(
050            RepositorySystemSession session, Map<String, ArtifactDecoratorFactory> factories) {
051        return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactDecoratorFactory::getPriority);
052    }
053
054    public static List<? extends ArtifactDecorator> getArtifactDecorators(
055            RepositorySystemSession session, Map<String, ArtifactDecoratorFactory> artifactDecoratorFactories) {
056        PrioritizedComponents<ArtifactDecoratorFactory> factories =
057                sortArtifactDecoratorFactories(session, artifactDecoratorFactories);
058        List<ArtifactDecorator> decorators = new ArrayList<>();
059        for (PrioritizedComponent<ArtifactDecoratorFactory> factory : factories.getEnabled()) {
060            ArtifactDecorator decorator = factory.getComponent().newInstance(session);
061            if (decorator != null) {
062                decorators.add(decorator);
063            }
064        }
065        return decorators;
066    }
067
068    private static PrioritizedComponents<ArtifactGeneratorFactory> sortArtifactGeneratorFactories(
069            RepositorySystemSession session, Map<String, ArtifactGeneratorFactory> factories) {
070        return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactGeneratorFactory::getPriority);
071    }
072
073    private static List<? extends ArtifactGenerator> doGetArtifactGenerators(
074            RepositorySystemSession session, Map<String, ArtifactGeneratorFactory> artifactFactories, Object request) {
075        PrioritizedComponents<ArtifactGeneratorFactory> factories =
076                sortArtifactGeneratorFactories(session, artifactFactories);
077        List<ArtifactGenerator> generators = new ArrayList<>();
078        for (PrioritizedComponent<ArtifactGeneratorFactory> factory : factories.getEnabled()) {
079            ArtifactGenerator generator;
080            if (request instanceof InstallRequest) {
081                generator = factory.getComponent().newInstance(session, (InstallRequest) request);
082            } else if (request instanceof DeployRequest) {
083                generator = factory.getComponent().newInstance(session, (DeployRequest) request);
084            } else {
085                throw new IllegalArgumentException("Unknown request");
086            }
087            if (generator != null) {
088                generators.add(generator);
089            }
090        }
091        return generators;
092    }
093
094    public static List<? extends ArtifactGenerator> getArtifactGenerators(
095            RepositorySystemSession session,
096            Map<String, ArtifactGeneratorFactory> artifactFactories,
097            InstallRequest request) {
098        return doGetArtifactGenerators(session, artifactFactories, request);
099    }
100
101    public static List<? extends ArtifactGenerator> getArtifactGenerators(
102            RepositorySystemSession session,
103            Map<String, ArtifactGeneratorFactory> artifactFactories,
104            DeployRequest request) {
105        return doGetArtifactGenerators(session, artifactFactories, request);
106    }
107
108    private static PrioritizedComponents<MetadataGeneratorFactory> sortMetadataGeneratorFactories(
109            RepositorySystemSession session, Map<String, MetadataGeneratorFactory> factories) {
110        return PrioritizedComponents.reuseOrCreate(session, factories, MetadataGeneratorFactory::getPriority);
111    }
112
113    private static List<? extends MetadataGenerator> doGetMetadataGenerators(
114            RepositorySystemSession session, Map<String, MetadataGeneratorFactory> metadataFactories, Object request) {
115        PrioritizedComponents<MetadataGeneratorFactory> factories =
116                sortMetadataGeneratorFactories(session, metadataFactories);
117        List<MetadataGenerator> generators = new ArrayList<>();
118        for (PrioritizedComponent<MetadataGeneratorFactory> factory : factories.getEnabled()) {
119            MetadataGenerator generator;
120            if (request instanceof InstallRequest) {
121                generator = factory.getComponent().newInstance(session, (InstallRequest) request);
122            } else if (request instanceof DeployRequest) {
123                generator = factory.getComponent().newInstance(session, (DeployRequest) request);
124            } else {
125                throw new IllegalArgumentException("Unknown request");
126            }
127            if (generator != null) {
128                generators.add(generator);
129            }
130        }
131        return generators;
132    }
133
134    public static List<? extends MetadataGenerator> getMetadataGenerators(
135            RepositorySystemSession session,
136            Map<String, MetadataGeneratorFactory> metadataFactories,
137            InstallRequest request) {
138        return doGetMetadataGenerators(session, metadataFactories, request);
139    }
140
141    public static List<? extends MetadataGenerator> getMetadataGenerators(
142            RepositorySystemSession session,
143            Map<String, MetadataGeneratorFactory> metadataFactories,
144            DeployRequest request) {
145        return doGetMetadataGenerators(session, metadataFactories, request);
146    }
147
148    public static List<Metadata> prepareMetadata(
149            List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
150        List<Metadata> metadatas = new ArrayList<>();
151
152        for (MetadataGenerator generator : generators) {
153            metadatas.addAll(generator.prepare(artifacts));
154        }
155
156        return metadatas;
157    }
158
159    public static List<Metadata> finishMetadata(
160            List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
161        List<Metadata> metadatas = new ArrayList<>();
162
163        for (MetadataGenerator generator : generators) {
164            metadatas.addAll(generator.finish(artifacts));
165        }
166
167        return metadatas;
168    }
169
170    public static <T> List<T> combine(Collection<? extends T> first, Collection<? extends T> second) {
171        List<T> result = new ArrayList<>(first.size() + second.size());
172        result.addAll(first);
173        result.addAll(second);
174        return result;
175    }
176
177    public static int getPolicy(RepositorySystemSession session, Artifact artifact, RemoteRepository repository) {
178        ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
179        if (rep == null) {
180            return ResolutionErrorPolicy.CACHE_DISABLED;
181        }
182        return rep.getArtifactPolicy(session, new ResolutionErrorPolicyRequest<>(artifact, repository));
183    }
184
185    public static int getPolicy(RepositorySystemSession session, Metadata metadata, RemoteRepository repository) {
186        ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
187        if (rep == null) {
188            return ResolutionErrorPolicy.CACHE_DISABLED;
189        }
190        return rep.getMetadataPolicy(session, new ResolutionErrorPolicyRequest<>(metadata, repository));
191    }
192
193    public static void appendClassLoader(StringBuilder buffer, Object component) {
194        ClassLoader loader = component.getClass().getClassLoader();
195        if (loader != null && !loader.equals(Utils.class.getClassLoader())) {
196            buffer.append(" from ").append(loader);
197        }
198    }
199
200    public static void checkOffline(
201            RepositorySystemSession session, OfflineController offlineController, RemoteRepository repository)
202            throws RepositoryOfflineException {
203        if (session.isOffline()) {
204            offlineController.checkOffline(session, repository);
205        }
206    }
207}