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.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.stream.Collectors;
29  
30  import org.apache.maven.model.Model;
31  import org.apache.maven.repository.internal.MavenWorkspaceReader;
32  import org.eclipse.aether.artifact.Artifact;
33  import org.eclipse.aether.repository.WorkspaceReader;
34  import org.eclipse.aether.repository.WorkspaceRepository;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   * A maven workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
40   */
41  public class MavenChainedWorkspaceReader implements MavenWorkspaceReader {
42  
43      protected List<WorkspaceReader> readers;
44      protected WorkspaceRepository repository;
45  
46      /**
47       * Creates a new workspace reader by chaining the specified readers.
48       *
49       * @param readers The readers to chain must not be {@code null}.
50       */
51      public MavenChainedWorkspaceReader(WorkspaceReader... readers) {
52          setReaders(Arrays.asList(readers));
53      }
54  
55      @Override
56      public WorkspaceRepository getRepository() {
57          return this.repository;
58      }
59  
60      @Override
61      public Model findModel(Artifact artifact) {
62          requireNonNull(artifact, "artifact cannot be null");
63          Model model = null;
64  
65          for (WorkspaceReader workspaceReader : readers) {
66              if (workspaceReader instanceof MavenWorkspaceReader) {
67                  model = ((MavenWorkspaceReader) workspaceReader).findModel(artifact);
68                  if (model != null) {
69                      break;
70                  }
71              }
72          }
73  
74          return model;
75      }
76  
77      @Override
78      public File findArtifact(Artifact artifact) {
79          requireNonNull(artifact, "artifact cannot be null");
80          File file = null;
81  
82          for (WorkspaceReader reader : readers) {
83              file = reader.findArtifact(artifact);
84              if (file != null) {
85                  break;
86              }
87          }
88  
89          return file;
90      }
91  
92      @Override
93      public List<String> findVersions(Artifact artifact) {
94          requireNonNull(artifact, "artifact cannot be null");
95          Collection<String> versions = new LinkedHashSet<>();
96  
97          for (WorkspaceReader reader : readers) {
98              versions.addAll(reader.findVersions(artifact));
99          }
100 
101         return Collections.unmodifiableList(new ArrayList<>(versions));
102     }
103 
104     public void setReaders(Collection<WorkspaceReader> readers) {
105         this.readers = Collections.unmodifiableList(new ArrayList<>(readers));
106         Key key = new Key(this.readers);
107         this.repository = new WorkspaceRepository(key.getContentType(), key);
108     }
109 
110     public List<WorkspaceReader> getReaders() {
111         return readers;
112     }
113 
114     private static class Key {
115         private final List<Object> keys;
116         private final String type;
117 
118         Key(Collection<WorkspaceReader> readers) {
119             keys = readers.stream().map(r -> r.getRepository().getKey()).collect(Collectors.toList());
120             type = readers.stream().map(r -> r.getRepository().getContentType()).collect(Collectors.joining("+"));
121         }
122 
123         public String getContentType() {
124             return type;
125         }
126 
127         public boolean equals(Object obj) {
128             if (this == obj) {
129                 return true;
130             } else {
131                 return obj != null && this.getClass().equals(obj.getClass()) && this.keys.equals(((Key) obj).keys);
132             }
133         }
134 
135         public int hashCode() {
136             return this.keys.hashCode();
137         }
138     }
139 
140     /**
141      * chains a collection of {@link WorkspaceReader}s
142      * @param workspaceReaderCollection the collection of readers, might be empty but never <code>null</code>
143      * @return if the collection contains only one item returns the single item, otherwise creates a new
144      *         {@link MavenChainedWorkspaceReader} chaining all readers in the order of the given collection.
145      */
146     public static WorkspaceReader of(Collection<WorkspaceReader> workspaceReaderCollection) {
147         WorkspaceReader[] readers = workspaceReaderCollection.toArray(new WorkspaceReader[0]);
148         if (readers.length == 1) {
149             return readers[0];
150         }
151         return new MavenChainedWorkspaceReader(readers);
152     }
153 }