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