View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.impl.MetadataGenerator;
29  import org.eclipse.aether.impl.MetadataGeneratorFactory;
30  import org.eclipse.aether.impl.OfflineController;
31  import org.eclipse.aether.metadata.Metadata;
32  import org.eclipse.aether.repository.RemoteRepository;
33  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
34  import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
35  import org.eclipse.aether.transfer.RepositoryOfflineException;
36  
37  /**
38   * Internal utility methods.
39   */
40  final class Utils
41  {
42  
43      public static PrioritizedComponents<MetadataGeneratorFactory> sortMetadataGeneratorFactories(
44              RepositorySystemSession session, Collection<? extends MetadataGeneratorFactory> factories )
45      {
46          PrioritizedComponents<MetadataGeneratorFactory> result = new PrioritizedComponents<>( session );
47          for ( MetadataGeneratorFactory factory : factories )
48          {
49              result.add( factory, factory.getPriority() );
50          }
51          return result;
52      }
53  
54      public static List<Metadata> prepareMetadata( List<? extends MetadataGenerator> generators,
55                                                    List<? extends Artifact> artifacts )
56      {
57          List<Metadata> metadatas = new ArrayList<>();
58  
59          for ( MetadataGenerator generator : generators )
60          {
61              metadatas.addAll( generator.prepare( artifacts ) );
62          }
63  
64          return metadatas;
65      }
66  
67      public static List<Metadata> finishMetadata( List<? extends MetadataGenerator> generators,
68                                                   List<? extends Artifact> artifacts )
69      {
70          List<Metadata> metadatas = new ArrayList<>();
71  
72          for ( MetadataGenerator generator : generators )
73          {
74              metadatas.addAll( generator.finish( artifacts ) );
75          }
76  
77          return metadatas;
78      }
79  
80      public static <T> List<T> combine( Collection<? extends T> first, Collection<? extends T> second )
81      {
82          List<T> result = new ArrayList<>( first.size() + second.size() );
83          result.addAll( first );
84          result.addAll( second );
85          return result;
86      }
87  
88      public static int getPolicy( RepositorySystemSession session, Artifact artifact, RemoteRepository repository )
89      {
90          ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
91          if ( rep == null )
92          {
93              return ResolutionErrorPolicy.CACHE_DISABLED;
94          }
95          return rep.getArtifactPolicy( session, new ResolutionErrorPolicyRequest<>( artifact, repository ) );
96      }
97  
98      public static int getPolicy( RepositorySystemSession session, Metadata metadata, RemoteRepository repository )
99      {
100         ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
101         if ( rep == null )
102         {
103             return ResolutionErrorPolicy.CACHE_DISABLED;
104         }
105         return rep.getMetadataPolicy( session, new ResolutionErrorPolicyRequest<>( metadata, repository ) );
106     }
107 
108     public static void appendClassLoader( StringBuilder buffer, Object component )
109     {
110         ClassLoader loader = component.getClass().getClassLoader();
111         if ( loader != null && !loader.equals( Utils.class.getClassLoader() ) )
112         {
113             buffer.append( " from " ).append( loader );
114         }
115     }
116 
117     public static void checkOffline( RepositorySystemSession session, OfflineController offlineController,
118                                      RemoteRepository repository )
119         throws RepositoryOfflineException
120     {
121         if ( session.isOffline() )
122         {
123             offlineController.checkOffline( session, repository );
124         }
125     }
126 
127 }