View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.aether;
20  
21  import java.io.File;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.apache.maven.model.Model;
26  import org.apache.maven.repository.internal.MavenWorkspaceReader;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.repository.WorkspaceReader;
29  import org.eclipse.aether.repository.WorkspaceRepository;
30  import org.eclipse.aether.util.repository.ChainedWorkspaceReader;
31  
32  /**
33   * A maven workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
34   */
35  public final class MavenChainedWorkspaceReader implements MavenWorkspaceReader {
36  
37      private ChainedWorkspaceReader delegate;
38  
39      private WorkspaceReader[] readers;
40  
41      /**
42       * Creates a new workspace reader by chaining the specified readers.
43       *
44       * @param readers The readers to chain must not be {@code null}.
45       */
46      private MavenChainedWorkspaceReader(WorkspaceReader... readers) {
47          this.delegate = new ChainedWorkspaceReader(readers);
48          this.readers = readers;
49      }
50  
51      @Override
52      public Model findModel(Artifact artifact) {
53          for (WorkspaceReader workspaceReader : readers) {
54              if (workspaceReader instanceof MavenWorkspaceReader) {
55                  Model model = ((MavenWorkspaceReader) workspaceReader).findModel(artifact);
56                  if (model != null) {
57                      return model;
58                  }
59              }
60          }
61          return null;
62      }
63  
64      @Override
65      public WorkspaceRepository getRepository() {
66          return delegate.getRepository();
67      }
68  
69      @Override
70      public File findArtifact(Artifact artifact) {
71          return delegate.findArtifact(artifact);
72      }
73  
74      @Override
75      public List<String> findVersions(Artifact artifact) {
76          return delegate.findVersions(artifact);
77      }
78  
79      /**
80       * chains a collection of {@link WorkspaceReader}s
81       * @param workspaceReaderCollection the collection of readers, might be empty but never <code>null</code>
82       * @return if the collection contains only one item returns the single item, otherwise creates a new
83       *         {@link MavenChainedWorkspaceReader} chaining all readers in the order of the given collection.
84       */
85      public static WorkspaceReader of(Collection<WorkspaceReader> workspaceReaderCollection) {
86          WorkspaceReader[] readers = workspaceReaderCollection.toArray(new WorkspaceReader[0]);
87          if (readers.length == 1) {
88              return readers[0];
89          }
90          return new MavenChainedWorkspaceReader(readers);
91      }
92  }