View Javadoc
1   package org.apache.maven.internal.aether;
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.io.File;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.apache.maven.model.Model;
27  import org.apache.maven.repository.internal.MavenWorkspaceReader;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.repository.WorkspaceReader;
30  import org.eclipse.aether.repository.WorkspaceRepository;
31  import org.eclipse.aether.util.repository.ChainedWorkspaceReader;
32  
33  /**
34   * A maven workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
35   */
36  public final class MavenChainedWorkspaceReader
37      implements MavenWorkspaceReader
38  {
39  
40      private ChainedWorkspaceReader delegate;
41  
42      private WorkspaceReader[] readers;
43  
44      /**
45       * Creates a new workspace reader by chaining the specified readers.
46       * 
47       * @param readers The readers to chain must not be {@code null}.
48       */
49      private MavenChainedWorkspaceReader( WorkspaceReader... readers )
50      {
51          this.delegate = new ChainedWorkspaceReader( readers );
52          this.readers = readers;
53      }
54  
55      @Override
56      public Model findModel( Artifact artifact )
57      {
58          for ( WorkspaceReader workspaceReader : readers )
59          {
60              if ( workspaceReader instanceof MavenWorkspaceReader )
61              {
62                  Model model = ( (MavenWorkspaceReader) workspaceReader ).findModel( artifact );
63                  if ( model != null )
64                  {
65                      return model;
66                  }
67              }
68          }
69          return null;
70      }
71  
72      @Override
73      public WorkspaceRepository getRepository()
74      {
75          return delegate.getRepository();
76      }
77  
78      @Override
79      public File findArtifact( Artifact artifact )
80      {
81          return delegate.findArtifact( artifact );
82      }
83  
84      @Override
85      public List<String> findVersions( Artifact artifact )
86      {
87          return delegate.findVersions( artifact );
88      }
89  
90      /**
91       * chains a collection of {@link WorkspaceReader}s
92       * @param workspaceReaderCollection the collection of readers, might be empty but never <code>null</code>
93       * @return if the collection contains only one item returns the single item, otherwise creates a new
94       *         {@link MavenChainedWorkspaceReader} chaining all readers in the order of the given collection.
95       */
96      public static WorkspaceReader of( Collection<WorkspaceReader> workspaceReaderCollection )
97      {
98          WorkspaceReader[] readers = workspaceReaderCollection.toArray( new WorkspaceReader[0] );
99          if ( readers.length == 1 )
100         {
101             return readers[0];
102         }
103         return new MavenChainedWorkspaceReader( readers );
104     }
105 
106 }