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( RepositorySystemSession session,
44                                                                                                    Collection<? extends MetadataGeneratorFactory> factories )
45      {
46          PrioritizedComponents<MetadataGeneratorFactory> result =
47              new PrioritizedComponents<MetadataGeneratorFactory>( session );
48          for ( MetadataGeneratorFactory factory : factories )
49          {
50              result.add( factory, factory.getPriority() );
51          }
52          return result;
53      }
54  
55      public static List<Metadata> prepareMetadata( List<? extends MetadataGenerator> generators,
56                                                    List<? extends Artifact> artifacts )
57      {
58          List<Metadata> metadatas = new ArrayList<Metadata>();
59  
60          for ( MetadataGenerator generator : generators )
61          {
62              metadatas.addAll( generator.prepare( artifacts ) );
63          }
64  
65          return metadatas;
66      }
67  
68      public static List<Metadata> finishMetadata( List<? extends MetadataGenerator> generators,
69                                                   List<? extends Artifact> artifacts )
70      {
71          List<Metadata> metadatas = new ArrayList<Metadata>();
72  
73          for ( MetadataGenerator generator : generators )
74          {
75              metadatas.addAll( generator.finish( artifacts ) );
76          }
77  
78          return metadatas;
79      }
80  
81      public static <T> List<T> combine( Collection<? extends T> first, Collection<? extends T> second )
82      {
83          List<T> result = new ArrayList<T>( first.size() + second.size() );
84          result.addAll( first );
85          result.addAll( second );
86          return result;
87      }
88  
89      public static int getPolicy( RepositorySystemSession session, Artifact artifact, RemoteRepository repository )
90      {
91          ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
92          if ( rep == null )
93          {
94              return ResolutionErrorPolicy.CACHE_DISABLED;
95          }
96          return rep.getArtifactPolicy( session, new ResolutionErrorPolicyRequest<Artifact>( artifact, repository ) );
97      }
98  
99      public static int getPolicy( RepositorySystemSession session, Metadata metadata, RemoteRepository repository )
100     {
101         ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
102         if ( rep == null )
103         {
104             return ResolutionErrorPolicy.CACHE_DISABLED;
105         }
106         return rep.getMetadataPolicy( session, new ResolutionErrorPolicyRequest<Metadata>( metadata, repository ) );
107     }
108 
109     public static void appendClassLoader( StringBuilder buffer, Object component )
110     {
111         ClassLoader loader = component.getClass().getClassLoader();
112         if ( loader != null && !loader.equals( Utils.class.getClassLoader() ) )
113         {
114             buffer.append( " from " ).append( loader );
115         }
116     }
117 
118     public static void checkOffline( RepositorySystemSession session, OfflineController offlineController,
119                                      RemoteRepository repository )
120         throws RepositoryOfflineException
121     {
122         if ( session.isOffline() )
123         {
124             offlineController.checkOffline( session, repository );
125         }
126     }
127 
128 }